Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

nci/suif/suif2b/common/suif_vector.h

Go to the documentation of this file.
00001 #ifndef _VECTOR_H_
00002 #define _VECTOR_H_
00003 
00010 #if defined(USE_STL) || defined(USE_STL_VECTOR)
00011 #include <vector>
00012 #define suif_vector vector
00013 #else
00014 
00015 #include <assert.h>
00016 
00025 template <class T>
00026 class suif_vector {
00027 public:
00028         typedef T VectorType;
00029         typedef T value_type;
00030         typedef T* iterator;
00031         typedef const T* const_iterator;
00032 
00033 protected:
00034         T *buff;
00035 
00036 protected:
00037 
00038         iterator m_start;
00039         iterator m_finish;
00040         iterator m_end_of_storage;
00041 protected:
00042         void allocate_vector(unsigned n) {
00043                 if (n == 0) n++;
00044                 buff = new T[n];
00045                 m_end_of_storage = buff + n;
00046                 }       
00047         void vector_fill(iterator& start, unsigned n, const T& val) {
00048                 iterator tmp = start;
00049                 while (tmp < m_finish) {
00050                     *(tmp) = val;
00051                     tmp++;              
00052                     }                           
00053                 }       
00054         // copy from start up to, but not including, end...to dest
00055         iterator copy(const_iterator start, const_iterator end, iterator dest) {
00056                 for (const_iterator tmp = start;
00057                      tmp != end; tmp++, dest++) {
00058                   *dest = *tmp;
00059                   }
00060                 return dest;
00061                 }
00062         // What is the return value here?
00063         // It seems like it should always end up just before start.
00064         // Shouldn't it return dest2 instead?
00065         const_iterator reverse_copy(const_iterator start, const_iterator end, 
00066                                     iterator dest) {
00067                 iterator dest2 = dest + (end - start) -1;
00068                 const_iterator tmp = end -1;
00069                 while (tmp >= start) {
00070                     *dest2-- = *tmp--;
00071                     }
00072                 return tmp;
00073                 }
00074 
00075         void insert_aux(iterator pos, const T& x) {
00076                 if (m_finish != m_end_of_storage) {
00077                     reverse_copy(pos, end(), pos + 1);
00078                     *pos = x;
00079                     m_finish = end() + 1;
00080                     }
00081                 else {
00082                     int oldsize = size();
00083                     int newsize = 2 * oldsize + 1;
00084                     T *pOld = buff;
00085                     allocate_vector(newsize);
00086                     T *pPos = buff;
00087                     pPos = copy(pOld, pos, pPos);
00088                     m_finish = copy(pos, end(), pPos+1);        
00089                     *pPos = x;  
00090                     delete [] pOld;
00091                     m_start = buff;
00092                     }
00093                 }
00094 public:
00095         suif_vector() : buff(0),m_start(0),m_finish(0),m_end_of_storage(0) {
00096                 allocate_vector(1);
00097                 m_start = buff;
00098                 m_finish = buff;
00099                 }
00100 
00105         suif_vector(unsigned n, const T& val = T()) :
00106           buff(0),m_start(0),m_finish(0),m_end_of_storage(0) {
00107                 iterator tmp;
00108                 allocate_vector(n);
00109                 m_start = buff;
00110                 tmp = m_start;
00111                 m_finish = tmp + n;     
00112                 vector_fill(m_start, n, val);
00113                 }
00114 
00115         suif_vector(const suif_vector<T>& vec) :
00116           buff(0),m_start(0),m_finish(0),m_end_of_storage(0) {
00117                 const_iterator it = vec.begin(), end = vec.end();
00118                 unsigned n = vec.size();
00119                 delete [] buff;
00120                 allocate_vector(n);
00121                 copy(it, end, buff);
00122                 m_start = buff;
00123                 m_finish = buff + n;
00124                 }
00125 
00126         ~suif_vector() {
00127            delete [] buff;
00128            }
00129         suif_vector<T>& operator=(const suif_vector<T>& vec) {
00130                 const_iterator it = vec.begin(), end = vec.end();
00131                 unsigned n = vec.size();
00132                 delete [] buff;
00133                 allocate_vector(n);
00134                 copy(it, end, buff);
00135                 m_start = buff;
00136                 m_finish = buff + n;
00137                 return *this;
00138                 }
00139 
00140         iterator begin() { return m_start; }
00141         iterator end() { return m_finish; }
00142 
00143         const_iterator begin() const { return m_start; }
00144         const_iterator end() const { return m_finish; }
00146         inline unsigned length() const { return m_finish - m_start; }
00148         inline unsigned size() const { return m_finish - m_start; }
00149 
00151         inline bool empty() const { return size() == 0; }       
00152 
00154         unsigned capacity() const {
00155                 return ((unsigned)(m_end_of_storage - m_start));
00156                 }
00157 
00159         void insert(iterator pos, const T& x) {
00160                 if (m_finish != m_end_of_storage && pos == end()) {
00161                     *m_finish = x;
00162                     m_finish++;
00163                     }
00164                 else {
00165                     insert_aux(pos, x);
00166                     }   
00167                 }
00168 
00170         void insert(int pos,const T& y) {
00171                 if (pos < 0)
00172                      insert(begin(),y);
00173                 else if (pos >= (int)size())
00174                     push_back(y);
00175                 else
00176                     insert(begin() + pos,y);
00177                 }
00179         iterator erase(iterator pos) {
00180                 iterator tmp;
00181                 tmp = pos + 1;
00182                 if (tmp != end()) {
00183                     copy(tmp, end(), pos);
00184                     --m_finish;
00185                     }
00186                 else {
00187                     --m_finish;
00188                     }
00189                 return(tmp);
00190                 }
00191 
00193         void erase(unsigned pos) {
00194                 if (pos < 0)
00195                     pos = 0;
00196                 else if (pos >= size())
00197                     pos = size() - 1;
00198                 if (pos >= 0)
00199                     erase(begin() + pos);
00200                 }
00201 
00202         void push_back(const T& x) {
00203                 if (m_finish != m_end_of_storage) {
00204                     *m_finish = x;
00205                     m_finish++;
00206                     }
00207                 else {
00208                     insert_aux(end(), x);
00209                     }
00210                 }
00211 
00212         void pop_back() { m_finish--;  }
00213 
00215         T& operator[](unsigned n) {
00216                 assert ( n < size());
00217                 return buff[n];
00218                 }
00219 
00220         const T& operator[](unsigned n) const {
00221                 assert ( n < size());
00222                 return buff[n];
00223                 }
00224 
00225         T& at(unsigned n) const {
00226                 return buff[n];
00227                 }
00228 
00229         T& front() {
00230                 return *begin();
00231                 }
00232 
00233         T& back() {
00234                 return *(end()-1);
00235                 }
00236 
00237         const T& front() const {
00238                 return *begin();
00239                 }
00240 
00241         const T& back() const {
00242                 return *(end()-1);
00243                 }
00244 
00248         bool is_member(const T& x) const {
00249           for (const_iterator it = begin(); it != end(); it++) {
00250             if ((*it) == x) return true;
00251           }
00252           return false;
00253         }
00254 
00256         void clear(void) {
00257           m_finish = buff;
00258         }
00259 private:
00260 
00261 
00262 }; // class vector
00263 
00264 #endif /* USE_STL_VECTOR */
00265 
00266 #endif
00267 

Generated at Mon Jul 31 13:41:41 2000 for NCI SUIF by doxygen 1.1.2 written by Dimitri van Heesch, © 1997-2000