Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
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 1 and Inria Bordeaux - Sud Ouest
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 
22 #ifndef DOXYGEN_NOTFOR_DEVEL
23 #ifndef _SIMPLE_VECTOR_H_
24 #define _SIMPLE_VECTOR_H_
25 #include <cstdlib>
26 
27 namespace tlp {
28 
29 // SimpleVector
30 // simple copy constructor
31 // deallocate with hand
32 
33 template<typename T>
34 class SimpleVector {
35 protected:
36  T *beginP;
37  T *middleP;
38  T *endP;
39 
40 public:
41  typedef T *iterator;
42  typedef const T *const_iterator;
43 
44  SimpleVector():beginP(NULL),middleP(NULL),endP(NULL) { }
45  SimpleVector(const SimpleVector &v):
46  beginP(v.beginP),middleP(v.middleP),endP(v.endP) { }
47  explicit SimpleVector(size_t s):beginP(NULL),middleP(NULL),endP(NULL) {
48  resize(s);
49  }
50  void resize(size_t s) {
51  middleP=beginP+s;
52 
53  if (middleP>endP || size()<capacity()/2)
54  doRealloc(size());
55  }
56  iterator begin() {
57  return beginP;
58  }
59  iterator end() {
60  return middleP;
61  }
62  const_iterator begin() const {
63  return beginP;
64  }
65  const_iterator end() const {
66  return middleP;
67  }
68  T &front() {
69  assert(!empty());
70  return *beginP;
71  }
72  const T &front() const {
73  assert(!empty());
74  return *beginP;
75  }
76  T &back() {
77  assert(!empty());
78  return *(middleP-1);
79  }
80  const T &back() const {
81  assert(!empty());
82  return *(middleP-1);
83  }
84  size_t size() const {
85  return size_t(middleP-beginP);
86  }
87  size_t capacity() const {
88  return size_t(endP-beginP);
89  }
90  bool empty() const {
91  return beginP == endP;
92  }
93  T &operator[](size_t i) {
94  assert(i<size());
95  return *(beginP+i);
96  }
97  T operator[](size_t i) const {
98  assert(i<size());
99  return *(beginP+i);
100  }
101  void push_back(const T &v) {
102  if (middleP==endP) {
103  size_t s=size();
104  doRealloc(s==0?1:2*s);
105  }
106 
107  *middleP=v;
108  ++middleP;
109  }
110  void pop_back() {
111  assert(!empty());
112  --middleP;
113 
114  if (size()<capacity()/2)
115  doRealloc(size());
116  }
117  void clear() {
118  deallocateAll();
119  beginP=middleP=endP=NULL;
120  }
121  void deallocateAll() {
122  free(beginP);
123  }
124 protected:
125  void doRealloc(size_t s) {
126  size_t i=middleP-beginP;
127  beginP=(T *)realloc(beginP,s*sizeof(T));
128  middleP=beginP+i;
129  endP=beginP+s;
130  assert(middleP<=endP);
131  }
132 };
133 
134 }
135 #endif
136 #endif //DOXYGEN_NOTFOR_DEVEL
137 ///@endcond