Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/SimpleVector.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 
00022 #ifndef DOXYGEN_NOTFOR_DEVEL
00023 #ifndef _SIMPLE_VECTOR_H_
00024 #define _SIMPLE_VECTOR_H_
00025 #include <cstdlib>
00026 
00027 namespace tlp {
00028 
00029 // SimpleVector
00030 //   simple copy constructor
00031 //   deallocate with hand
00032 
00033 template<typename T>
00034 class SimpleVector {
00035 protected:
00036   T *beginP;
00037   T *middleP;
00038   T *endP;
00039 
00040 public:
00041   typedef T *iterator;
00042   typedef const T *const_iterator;
00043 
00044   SimpleVector():beginP(NULL),middleP(NULL),endP(NULL) {  }
00045   SimpleVector(const SimpleVector &v):
00046     beginP(v.beginP),middleP(v.middleP),endP(v.endP) { }
00047   explicit SimpleVector(size_t s):beginP(NULL),middleP(NULL),endP(NULL) {
00048     resize(s);
00049   }
00050   void resize(size_t s) {
00051     middleP=beginP+s;
00052 
00053     if (middleP>endP || size()<capacity()/2)
00054       doRealloc(size());
00055   }
00056   iterator begin() {
00057     return beginP;
00058   }
00059   iterator end() {
00060     return middleP;
00061   }
00062   const_iterator begin() const {
00063     return beginP;
00064   }
00065   const_iterator end() const {
00066     return middleP;
00067   }
00068   T &front() {
00069     assert(!empty());
00070     return *beginP;
00071   }
00072   const T &front() const {
00073     assert(!empty());
00074     return *beginP;
00075   }
00076   T &back() {
00077     assert(!empty());
00078     return *(middleP-1);
00079   }
00080   const T &back() const {
00081     assert(!empty());
00082     return *(middleP-1);
00083   }
00084   size_t size() const {
00085     return size_t(middleP-beginP);
00086   }
00087   size_t capacity() const {
00088     return size_t(endP-beginP);
00089   }
00090   bool empty() const {
00091     return beginP == endP;
00092   }
00093   T &operator[](size_t i) {
00094     assert(i<size());
00095     return *(beginP+i);
00096   }
00097   T operator[](size_t i) const {
00098     assert(i<size());
00099     return *(beginP+i);
00100   }
00101   void push_back(const T &v) {
00102     if (middleP==endP) {
00103       size_t s=size();
00104       doRealloc(s==0?1:2*s);
00105     }
00106 
00107     *middleP=v;
00108     ++middleP;
00109   }
00110   void pop_back() {
00111     assert(!empty());
00112     --middleP;
00113 
00114     if (size()<capacity()/2)
00115       doRealloc(size());
00116   }
00117   void clear() {
00118     deallocateAll();
00119     beginP=middleP=endP=NULL;
00120   }
00121   void deallocateAll() {
00122     free(beginP);
00123   }
00124 protected:
00125   void doRealloc(size_t s) {
00126     size_t i=middleP-beginP;
00127     beginP=(T *)realloc(beginP,s*sizeof(T));
00128     middleP=beginP+i;
00129     endP=beginP+s;
00130     assert(middleP<=endP);
00131   }
00132 };
00133 
00134 }
00135 #endif
00136 #endif //DOXYGEN_NOTFOR_DEVEL
00137 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties