00001 00007 #ifndef SRTARRAY_H 00008 #define SRTARRAY_H 00009 00010 #include "SortableResizeArray.h" 00011 00012 template <class Elem> class SortedArray: public SortableResizeArray<Elem> { 00013 00014 protected: 00015 00016 int found; 00017 int isSorted; 00018 00019 public: 00020 00021 SortedArray(void) : SortableResizeArray<Elem>() { 00022 found = -1; isSorted = 1; 00023 } 00024 00025 SortedArray(int s) : SortableResizeArray<Elem>(s) { 00026 found = -1; isSorted = 1; 00027 } 00028 00029 SortedArray(SortedArray<Elem> &sa) : SortableResizeArray<Elem>(sa) { 00030 found = -1; if(!(isSorted = sa.isSorted)) sort(); 00031 isSorted = 1; 00032 } 00033 00034 SortedArray(SortableResizeArray<Elem> &ra) : 00035 SortableResizeArray<Elem>(ra) { 00036 found = -1; sort(); isSorted = 1; 00037 } 00038 00039 SortedArray<Elem>& operator =(SortedArray<Elem> & sa) { 00040 SortableResizeArray<Elem>::operator=(sa); 00041 found = -1; isSorted = sa.isSorted; 00042 return(*this); 00043 } 00044 00045 SortedArray<Elem>& operator =(SortableResizeArray<Elem> &ra) { 00046 SortableResizeArray<Elem>::operator=(ra); 00047 found = -1; sort(); isSorted = 1; 00048 return(*this); 00049 } 00050 00051 int load(const Elem& elem) { 00052 isSorted = 0; 00053 return(ResizeArray<Elem>::add(elem)); 00054 } 00055 00056 int add(const Elem& elem) { 00057 return(insert(elem)); 00058 } 00059 00060 int del(const Elem & elem) { 00061 found = bsearch(elem); 00062 if (this->size() != 0 && (*(this->rep))[found] == elem) { 00063 return(SortableResizeArray<Elem>::del(found,1)); 00064 } else { 00065 found = -1; 00066 return(-1); 00067 } 00068 } 00069 00070 void sort(void) { SortableResizeArray<Elem>::sort(); isSorted = 1; } 00071 00072 int bsearch(const Elem& elem) { 00073 if (!isSorted) sort(); 00074 return (SortableResizeArray<Elem>::bsearch(elem)); 00075 } 00076 00077 inline int insert(const Elem& elem); 00078 00079 int index(const Elem& elem) { return(found = bsearch(elem)); } 00080 00081 inline Elem *find(const Elem& elem); 00082 00083 Elem * find(void) { 00084 if (found < 0 || ++found >= this->size()) { 00085 return((Elem *)NULL); 00086 } 00087 else if ((*(this->rep))[found] == (*(this->rep))[found-1]) { 00088 return (&(*(this->rep))[found]); 00089 } else { 00090 return ((Elem *)NULL); 00091 } 00092 } 00093 }; 00094 00095 template <class Elem> 00096 inline int SortedArray<Elem>::insert(const Elem& elem) { 00097 found = bsearch(elem); 00098 if (found == -1) { 00099 return (ResizeArray<Elem>::insert(elem, 0)); 00100 } 00101 if (found == (this->size()-1) && (*(this->rep))[found] < elem) { 00102 return (ResizeArray<Elem>::insert(elem, this->size())); 00103 } else { 00104 return (ResizeArray<Elem>::insert(elem, found)); 00105 } 00106 } 00107 00108 template <class Elem> 00109 inline Elem * SortedArray<Elem>::find(const Elem& elem) { 00110 if ( (found = bsearch(elem)) < 0) 00111 return ((Elem *)NULL); 00112 if ((*(this->rep))[found] == elem) { 00113 return (&(*(this->rep))[found]); 00114 } else { 00115 found = -1; 00116 return ((Elem *)NULL); 00117 } 00118 } 00119 00120 #endif
1.3.9.1