UniqueSortedArray.h

Go to the documentation of this file.
00001 
00007 #ifndef USRTARRAY_H
00008 #define USRTARRAY_H
00009 
00010 #include "SortedArray.h"
00011 
00012 template <class Elem> class UniqueSortedArray : public SortedArray<Elem> {
00013 
00014   public:
00015 
00016     UniqueSortedArray(int s=0) : SortedArray<Elem>(s) { }
00017 
00018     UniqueSortedArray(UniqueSortedArray<Elem> &ua) : 
00019       SortedArray<Elem>(ua) { }
00020 
00021     UniqueSortedArray(SortedArray<Elem> &sa) : SortedArray<Elem>(sa) { 
00022       this->uniq(); 
00023     }
00024 
00025 /*
00026     UniqueSortedArray(ResizeArray<Elem> &ra) : SortedArray<Elem>(ra) {
00027       this->uniq();
00028     }
00029 */
00030   
00031     UniqueSortedArray<Elem>& operator =(UniqueSortedArray<Elem> & ua) {
00032       SortedArray<Elem>::operator=(ua);
00033       return(*this);
00034     }
00035   
00036     UniqueSortedArray<Elem>& operator =(SortedArray<Elem> &sa) {
00037       SortedArray<Elem>::operator=(sa);
00038       this->uniq();
00039       return(*this);
00040     }
00041 
00042 /*
00043     UniqueSortedArray<Elem>& operator =(ResizeArray<Elem> &ra) {
00044       SortedArray<Elem>::operator=(ra);
00045       this->uniq();
00046       return(*this);
00047     }
00048 */
00049   
00050     int add(const Elem& elem) { return(insert(elem)); }
00051 
00052     inline int insert(const Elem& elem);
00053 
00054 };
00055 
00056 template <class Elem>
00057 inline int 
00058 UniqueSortedArray<Elem>::insert(const Elem& elem) {
00059   int found = this->bsearch(elem);
00060   if (found == -1) {
00061     return ResizeArray<Elem>::insert(elem, 0);
00062   }
00063   if (found < this->size() && this->rep[found] == elem) {
00064     return -2;
00065   }
00066   if (found == (this->size()-1) && this->rep[found] < elem) {
00067     return ResizeArray<Elem>::insert(elem, this->size());
00068   } else {
00069     return ResizeArray<Elem>::insert(elem, found);
00070   }
00071 }
00072 #endif

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