SortedArray.h

Go to the documentation of this file.
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 isSorted;
00017 
00018   public:
00019 
00020     SortedArray(void) : SortableResizeArray<Elem>() { 
00021       isSorted = 1;
00022     }
00023 
00024     SortedArray(int s) : SortableResizeArray<Elem>(s) { 
00025       isSorted = 1;
00026     }
00027 
00028     SortedArray(SortedArray<Elem> &sa) : SortableResizeArray<Elem>(sa) { 
00029       if(!(isSorted = sa.isSorted)) sort();
00030       isSorted = 1;
00031     }
00032 
00033     SortedArray(SortableResizeArray<Elem> &ra) : 
00034         SortableResizeArray<Elem>(ra) {
00035       sort(); isSorted = 1;
00036     }
00037 
00038     SortedArray<Elem>& operator =(SortedArray<Elem> & sa) {
00039       SortableResizeArray<Elem>::operator=(sa);
00040       isSorted = sa.isSorted;
00041       return(*this);
00042     }
00043 
00044     SortedArray<Elem>& operator =(SortableResizeArray<Elem> &ra) {
00045       SortableResizeArray<Elem>::operator=(ra);
00046       sort(); isSorted = 1;
00047       return(*this);
00048     }
00049 
00050     int load(const Elem& elem) {
00051       isSorted = 0;
00052       return(ResizeArray<Elem>::add(elem));
00053     }
00054 
00055     int add(const Elem& elem) {
00056       return(insert(elem));
00057     }
00058 
00059     int del(const Elem & elem) {
00060       int found = bsearch(elem);
00061       if (this->size() != 0 && this->rep[found] == elem) {
00062         return(SortableResizeArray<Elem>::del(found,1));
00063       } else {
00064         return(-1);
00065       }
00066     }
00067 
00068     void sort(void) { SortableResizeArray<Elem>::sort(); isSorted = 1; }
00069 
00070     int bsearch(const Elem& elem) { 
00071       if (!isSorted) sort();
00072       return (SortableResizeArray<Elem>::bsearch(elem));
00073     }
00074 
00075     inline int insert(const Elem& elem);
00076 
00077     int index(const Elem& elem) { return bsearch(elem); }
00078 
00079     inline Elem *find(const Elem& elem);
00080 };
00081 
00082 template <class Elem>
00083 inline int SortedArray<Elem>::insert(const Elem& elem) {
00084   int found = bsearch(elem);
00085   if (found == -1) {
00086     return (ResizeArray<Elem>::insert(elem, 0));
00087   }
00088   if (found == (this->size()-1) && this->rep[found] < elem) {
00089     return (ResizeArray<Elem>::insert(elem, this->size()));
00090   } else {
00091     return (ResizeArray<Elem>::insert(elem, found));
00092   }
00093 }
00094 
00095 template <class Elem>
00096 inline Elem * SortedArray<Elem>::find(const Elem& elem) {
00097   int found = bsearch(elem);
00098   if ( found < 0 || found == this->size() ) 
00099     return ((Elem *)NULL);
00100   if (this->rep[found] == elem) {
00101     return (&(this->rep[found]));
00102   } else {
00103     return ((Elem *)NULL);
00104   }
00105 }
00106 
00107 #endif

Generated on Mon Nov 20 01:17:14 2017 for NAMD by  doxygen 1.4.7