Tulip  4.10.0
Better Visualization Through Research
SimpleVector.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef _SIMPLE_VECTOR_H_
22 #define _SIMPLE_VECTOR_H_
23 #include <cstdlib>
24 
25 namespace tlp {
26 
27 // SimpleVector
28 // simple copy constructor
29 // deallocate with hand
30 
31 template<typename T>
32 class SimpleVector {
33 protected:
34  T *beginP;
35  T *middleP;
36  T *endP;
37 
38 public:
39  typedef T *iterator;
40  typedef const T *const_iterator;
41 
42  SimpleVector():beginP(NULL),middleP(NULL),endP(NULL) { }
43  SimpleVector(const SimpleVector &v):
44  beginP(v.beginP),middleP(v.middleP),endP(v.endP) { }
45  explicit SimpleVector(size_t s):beginP(NULL),middleP(NULL),endP(NULL) {
46  resize(s);
47  }
48  void resize(size_t s) {
49  middleP=beginP+s;
50 
51  if (middleP>endP || size()<capacity()/2)
52  doRealloc(size());
53  }
54  iterator begin() {
55  return beginP;
56  }
57  iterator end() {
58  return middleP;
59  }
60  const_iterator begin() const {
61  return beginP;
62  }
63  const_iterator end() const {
64  return middleP;
65  }
66  T &front() {
67  assert(!empty());
68  return *beginP;
69  }
70  const T &front() const {
71  assert(!empty());
72  return *beginP;
73  }
74  T &back() {
75  assert(!empty());
76  return *(middleP-1);
77  }
78  const T &back() const {
79  assert(!empty());
80  return *(middleP-1);
81  }
82  size_t size() const {
83  return size_t(middleP-beginP);
84  }
85  size_t capacity() const {
86  return size_t(endP-beginP);
87  }
88  bool empty() const {
89  return beginP == endP;
90  }
91  T &operator[](size_t i) {
92  assert(i<size());
93  return *(beginP+i);
94  }
95  T operator[](size_t i) const {
96  assert(i<size());
97  return *(beginP+i);
98  }
99  void push_back(const T &v) {
100  if (middleP==endP) {
101  size_t s=size();
102  doRealloc(s==0?1:2*s);
103  }
104 
105  *middleP=v;
106  ++middleP;
107  }
108  void pop_back() {
109  assert(!empty());
110  --middleP;
111 
112  if (size()<capacity()/2)
113  doRealloc(size());
114  }
115  void clear() {
116  deallocateAll();
117  beginP=middleP=endP=NULL;
118  }
119  void deallocateAll() {
120  free(beginP);
121  }
122 protected:
123  void doRealloc(size_t s) {
124  size_t i=middleP-beginP;
125  beginP=(T *)realloc(beginP,s*sizeof(T));
126  middleP=beginP+i;
127  endP=beginP+s;
128  assert(middleP<=endP);
129  }
130 };
131 
132 }
133 #endif
134 
135 ///@endcond