w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

vector.hh
Go to the documentation of this file.
1 #ifndef LCDF_VECTOR_HH
2 #define LCDF_VECTOR_HH
3 #include <assert.h>
4 #include <stdlib.h>
5 #ifdef HAVE_NEW_HDR
6 # include <new>
7 #elif defined(HAVE_NEW_H)
8 # include <new.h>
9 #else
10 static inline void *operator new(size_t, void *v) { return v; }
11 #endif
12 #if HAVE_VALGRIND && HAVE_VALGRIND_MEMCHECK_H
13 # include <valgrind/memcheck.h>
14 #endif
15 
16 template <class T>
17 class Vector { public:
18 
19  typedef T value_type;
20  typedef T& reference;
21  typedef const T& const_reference;
22  typedef T* pointer;
23  typedef const T* const_pointer;
24  typedef const T& const_access_type;
25 
26  typedef int size_type;
27  enum { RESERVE_GROW = (size_type) -1 };
28 
29  typedef T* iterator;
30  typedef const T* const_iterator;
31 
32  explicit Vector()
33  : _l(0), _n(0), _capacity(0) {
34  }
35  explicit Vector(size_type n, const T &e)
36  : _l(0), _n(0), _capacity(0) {
37  resize(n, e);
38  }
39  // template <class In> ...
40  Vector(const Vector<T> &x);
41  ~Vector();
42 
43  Vector<T>& operator=(const Vector<T>&);
44  Vector<T>& assign(size_type n, const T& e = T());
45  // template <class In> ...
46 
47  // iterators
48  iterator begin() { return _l; }
49  const_iterator begin() const { return _l; }
50  iterator end() { return _l + _n; }
51  const_iterator end() const { return _l + _n; }
52 
53  // capacity
54  size_type size() const { return _n; }
55  void resize(size_type nn, const T& e = T());
56  size_type capacity() const { return _capacity; }
57  bool empty() const { return _n == 0; }
58  bool reserve(size_type n) { return reserve_and_push_back(n, 0); }
59 
60  // element access
62  assert((unsigned) i < (unsigned) _n);
63  return _l[i];
64  }
65  const T &operator[](size_type i) const {
66  assert((unsigned) i < (unsigned) _n);
67  return _l[i];
68  }
69  T& at(size_type i) { return operator[](i); }
70  const T& at(size_type i) const { return operator[](i); }
71  T& front() { return operator[](0); }
72  const T& front() const { return operator[](0); }
73  T& back() { return operator[](_n - 1); }
74  const T& back() const { return operator[](_n - 1); }
75  T& at_u(size_type i) { return _l[i]; }
76  const T& at_u(size_type i) const { return _l[i]; }
77 
78  // modifiers
79  inline void push_back(const T& x);
80  inline void pop_back();
81  inline void push_front(const T& x);
82  inline void pop_front();
83  iterator insert(iterator it, const T& x);
84  inline iterator erase(iterator it);
86  void swap(Vector<T>& x);
87  void clear() { erase(begin(), end()); }
88 
89  private:
90 
91  T *_l;
94 
95  void *velt(size_type i) const { return (void *)&_l[i]; }
96  static void *velt(T *l, size_type i) { return (void *)&l[i]; }
97  bool reserve_and_push_back(size_type n, const T *x);
98 
99 };
100 
101 template <class T> inline void
103 {
104  if (_n < _capacity) {
105 #ifdef VALGRIND_MAKE_MEM_UNDEFINED
106  VALGRIND_MAKE_MEM_UNDEFINED(velt(_n), sizeof(T));
107 #endif
108  new(velt(_n)) T(x);
109  ++_n;
110  } else
111  reserve_and_push_back(RESERVE_GROW, &x);
112 }
113 
114 template <class T> inline void
116 {
117  assert(_n > 0);
118  --_n;
119  _l[_n].~T();
120 #ifdef VALGRIND_MAKE_MEM_NOACCESS
121  VALGRIND_MAKE_MEM_NOACCESS(&_l[_n], sizeof(T));
122 #endif
123 }
124 
125 template <class T> inline typename Vector<T>::iterator
127 {
128  return (it < end() ? erase(it, it + 1) : it);
129 }
130 
131 template <class T> inline void
133 {
134  insert(begin(), x);
135 }
136 
137 template <class T> inline void
139 {
140  erase(begin());
141 }
142 
143 
144 template <>
145 class Vector<void*> { public:
146 
147  typedef void* value_type;
148  typedef void*& reference;
149  typedef void* const& const_reference;
150  typedef void** pointer;
151  typedef void* const* const_pointer;
152  typedef void* const_access_type;
153 
154  typedef int size_type;
155  enum { RESERVE_GROW = (size_type) -1 };
156 
157  typedef void** iterator;
158  typedef void* const* const_iterator;
159 
160  explicit Vector()
161  : _l(0), _n(0), _capacity(0) {
162  }
163  explicit Vector(size_type n, void* e)
164  : _l(0), _n(0), _capacity(0) {
165  resize(n, e);
166  }
167  Vector(const Vector<void*> &);
168  ~Vector();
169 
171  Vector<void*> &assign(size_type n, void* x = 0);
172 
173  // iterators
174  iterator begin() { return _l; }
175  const_iterator begin() const { return _l; }
176  iterator end() { return _l + _n; }
177  const_iterator end() const { return _l + _n; }
178 
179  // capacity
180  size_type size() const { return _n; }
181  void resize(size_type n, void* x = 0);
182  size_type capacity() const { return _capacity; }
183  bool empty() const { return _n == 0; }
184  bool reserve(size_type n);
185 
186  // element access
187  void*& operator[](size_type i) { assert(i>=0 && i<_n); return _l[i]; }
188  void* operator[](size_type i) const { assert(i>=0 && i<_n); return _l[i]; }
189  void*& at(size_type i) { return operator[](i); }
190  void* at(size_type i) const { return operator[](i); }
191  void*& front() { return operator[](0); }
192  void* front() const { return operator[](0); }
193  void*& back() { return operator[](_n - 1); }
194  void* back() const { return operator[](_n - 1); }
195  void*& at_u(size_type i) { return _l[i]; }
196  void* at_u(size_type i) const { return _l[i]; }
197 
198  // modifiers
199  inline void push_back(void* x);
200  inline void pop_back();
201  inline void push_front(void* x);
202  inline void pop_front();
203  iterator insert(iterator it, void* x);
204  inline iterator erase(iterator it);
206  void swap(Vector<void*> &x);
207  void clear() { _n = 0; }
208 
209  private:
210 
211  void **_l;
214 
215 };
216 
217 inline void
219 {
220  if (_n < _capacity || reserve(RESERVE_GROW)) {
221  _l[_n] = x;
222  _n++;
223  }
224 }
225 
226 inline void
228 {
229  assert(_n > 0);
230  --_n;
231 #ifdef VALGRIND_MAKE_MEM_NOACCESS
232  VALGRIND_MAKE_MEM_NOACCESS(&_l[_n], sizeof(void *));
233 #endif
234 }
235 
238 {
239  return (it < end() ? erase(it, it + 1) : it);
240 }
241 
242 inline void
244 {
245  insert(begin(), x);
246 }
247 
248 inline void
250 {
251  erase(begin());
252 }
253 
254 
255 template <class T>
256 class Vector<T*>: private Vector<void*> {
257 
259 
260  public:
261 
262  typedef T* value_type;
263  typedef T*& reference;
264  typedef T* const& const_reference;
265  typedef T** pointer;
266  typedef T* const* const_pointer;
268 
269  typedef int size_type;
270  enum { RESERVE_GROW = Base::RESERVE_GROW };
271 
272  typedef T** iterator;
273  typedef T* const* const_iterator;
274 
275  explicit Vector() : Base() { }
276  explicit Vector(size_type n, T* x) : Base(n, (void *)x) { }
277  Vector(const Vector<T*>& x) : Base(x) { }
278  ~Vector() { }
279 
281  { Base::operator=(x); return *this; }
283  { Base::assign(n, (void*)x); return *this; }
284 
285  // iterators
287  iterator begin() { return (iterator)(Base::begin()); }
288  const_iterator end() const { return (const_iterator)(Base::end()); }
289  iterator end() { return (iterator)(Base::end()); }
290 
291  // capacity
292  size_type size() const { return Base::size(); }
293  void resize(size_type n, T* x = 0) { Base::resize(n, (void*)x); }
294  size_type capacity() const { return Base::capacity(); }
295  bool empty() const { return Base::empty(); }
296  bool reserve(size_type n) { return Base::reserve(n); }
297 
298  // element access
299  T*& operator[](size_type i) { return (T*&)(Base::at(i)); }
300  T* operator[](size_type i) const { return (T*)(Base::operator[](i)); }
301  T*& at(size_type i) { return (T*&)(Base::operator[](i)); }
302  T* at(size_type i) const { return (T*)(Base::at(i)); }
303  T*& front() { return (T*&)(Base::front()); }
304  T* front() const { return (T*)(Base::front()); }
305  T*& back() { return (T*&)(Base::back()); }
306  T* back() const { return (T*)(Base::back()); }
307  T*& at_u(size_type i) { return (T*&)(Base::at_u(i)); }
308  T* at_u(size_type i) const { return (T*)(Base::at_u(i)); }
309 
310  // modifiers
311  void push_back(T* x) { Base::push_back((void*)x); }
312  void pop_back() { Base::pop_back(); }
313  void push_front(T* x) { Base::push_front((void*)x); }
314  void pop_front() { Base::pop_front(); }
315  iterator insert(iterator it, T* x) { return (iterator)Base::insert((void**)it, (void*)x); }
316  iterator erase(iterator it) { return (iterator)Base::erase((void**)it); }
317  iterator erase(iterator a, iterator b) { return (iterator)Base::erase((void**)a, (void**)b); }
318  void swap(Vector<T*>& x) { Base::swap(x); }
319  void clear() { Base::clear(); }
320 
321 };
322 
323 #include <lcdf/vector.cc>
324 #endif
#define empty
Definition: aptex-macros.h:52
#define insert
Definition: aptex-macros.h:395
T ** iterator
Definition: vector.hh:272
void push_back(T *x)
Definition: vector.hh:311
Vector< void * > Base
Definition: vector.hh:258
T *& reference
Definition: vector.hh:263
Vector(const Vector< T * > &x)
Definition: vector.hh:277
T * at(size_type i) const
Definition: vector.hh:302
bool reserve(size_type n)
Definition: vector.hh:296
Vector(size_type n, T *x)
Definition: vector.hh:276
T *const * const_iterator
Definition: vector.hh:273
iterator begin()
Definition: vector.hh:287
T * back() const
Definition: vector.hh:306
size_type size() const
Definition: vector.hh:292
T * operator[](size_type i) const
Definition: vector.hh:300
iterator end()
Definition: vector.hh:289
iterator erase(iterator a, iterator b)
Definition: vector.hh:317
void clear()
Definition: vector.hh:319
int size_type
Definition: vector.hh:269
iterator insert(iterator it, T *x)
Definition: vector.hh:315
Vector< T * > & operator=(const Vector< T * > &x)
Definition: vector.hh:280
void push_front(T *x)
Definition: vector.hh:313
bool empty() const
Definition: vector.hh:295
Vector< T * > & assign(size_type n, T *x=0)
Definition: vector.hh:282
T * at_u(size_type i) const
Definition: vector.hh:308
T * value_type
Definition: vector.hh:262
T *& at_u(size_type i)
Definition: vector.hh:307
T *& at(size_type i)
Definition: vector.hh:301
void pop_front()
Definition: vector.hh:314
T * front() const
Definition: vector.hh:304
T *const & const_reference
Definition: vector.hh:264
T ** pointer
Definition: vector.hh:265
T *& operator[](size_type i)
Definition: vector.hh:299
size_type capacity() const
Definition: vector.hh:294
const_iterator begin() const
Definition: vector.hh:286
void swap(Vector< T * > &x)
Definition: vector.hh:318
T *& front()
Definition: vector.hh:303
T *& back()
Definition: vector.hh:305
const_iterator end() const
Definition: vector.hh:288
T *const * const_pointer
Definition: vector.hh:266
iterator erase(iterator it)
Definition: vector.hh:316
T * const_access_type
Definition: vector.hh:267
void pop_back()
Definition: vector.hh:312
void resize(size_type n, T *x=0)
Definition: vector.hh:293
size_type _n
Definition: vector.hh:212
Vector(size_type n, void *e)
Definition: vector.hh:163
void ** iterator
Definition: vector.hh:157
void * back() const
Definition: vector.hh:194
iterator erase(iterator it)
void ** _l
Definition: vector.hh:211
void *const * const_iterator
Definition: vector.hh:158
size_type size() const
Definition: vector.hh:180
const_iterator end() const
Definition: vector.hh:177
void ** pointer
Definition: vector.hh:150
size_type capacity() const
Definition: vector.hh:182
void * const_access_type
Definition: vector.hh:152
void * at(size_type i) const
Definition: vector.hh:190
void * at_u(size_type i) const
Definition: vector.hh:196
void clear()
Definition: vector.hh:207
void *const & const_reference
Definition: vector.hh:149
void *const * const_pointer
Definition: vector.hh:151
void *& back()
Definition: vector.hh:193
size_type _capacity
Definition: vector.hh:213
bool empty() const
Definition: vector.hh:183
void * operator[](size_type i) const
Definition: vector.hh:188
void *& at_u(size_type i)
Definition: vector.hh:195
void *& at(size_type i)
Definition: vector.hh:189
void * value_type
Definition: vector.hh:147
void *& front()
Definition: vector.hh:191
iterator begin()
Definition: vector.hh:174
const_iterator begin() const
Definition: vector.hh:175
void *& operator[](size_type i)
Definition: vector.hh:187
void *& reference
Definition: vector.hh:148
iterator insert(iterator it, void *x)
void * front() const
Definition: vector.hh:192
iterator end()
Definition: vector.hh:176
Definition: vector.hh:17
const T & at(size_type i) const
Definition: vector.hh:70
const T & front() const
Definition: vector.hh:72
const T & at_u(size_type i) const
Definition: vector.hh:76
Vector(size_type n, const T &e)
Definition: vector.hh:35
size_type _capacity
Definition: vector.hh:93
const T * const_pointer
Definition: vector.hh:23
T & front()
Definition: vector.hh:71
void push_front(const T &x)
Definition: vector.hh:132
T & at_u(size_type i)
Definition: vector.hh:75
T * iterator
Definition: vector.hh:29
size_type capacity() const
Definition: vector.hh:56
void clear()
Definition: vector.hh:87
Vector()
Definition: vector.hh:32
iterator erase(iterator it)
Definition: vector.hh:126
void pop_front()
Definition: vector.hh:138
const_iterator end() const
Definition: vector.hh:51
const T & const_reference
Definition: vector.hh:21
iterator begin()
Definition: vector.hh:48
const_iterator begin() const
Definition: vector.hh:49
Vector< T > & assign(size_type n, const T &e=T())
Definition: vector.cc:65
void swap(Vector< T > &x)
Definition: vector.cc:187
void * velt(size_type i) const
Definition: vector.hh:95
void push_back(const T &x)
Definition: vector.hh:102
T value_type
Definition: vector.hh:19
T * pointer
Definition: vector.hh:22
T * _l
Definition: vector.hh:91
T & back()
Definition: vector.hh:73
@ RESERVE_GROW
Definition: vector.hh:27
Vector< T > & operator=(const Vector< T > &)
Definition: vector.cc:41
const T & const_access_type
Definition: vector.hh:24
void resize(size_type nn, const T &e=T())
Definition: vector.cc:166
size_type size() const
Definition: vector.hh:54
const T & back() const
Definition: vector.hh:74
T & reference
Definition: vector.hh:20
T & at(size_type i)
Definition: vector.hh:69
static void * velt(T *l, size_type i)
Definition: vector.hh:96
bool reserve_and_push_back(size_type n, const T *x)
Definition: vector.cc:132
const T & operator[](size_type i) const
Definition: vector.hh:65
const T * const_iterator
Definition: vector.hh:30
bool empty() const
Definition: vector.hh:57
void pop_back()
Definition: vector.hh:115
iterator insert(iterator it, const T &x)
Definition: vector.cc:78
iterator end()
Definition: vector.hh:50
T & operator[](size_type i)
Definition: vector.hh:61
bool reserve(size_type n)
Definition: vector.hh:58
int size_type
Definition: vector.hh:26
~Vector()
Definition: vector.cc:33
size_type _n
Definition: vector.hh:92
#define n
Definition: t4ht.c:1290
#define b
Definition: jpegint.h:372
int v
Definition: dviconv.c:10
#define T
Definition: fmt.h:20
static void
Definition: fpif.c:118
static FIELD_PTR begin
Definition: genind.c:37
#define a(n)
Definition: gpos-common.c:148
static void clear()
assert(pcxLoadImage24((char *)((void *) 0), fp, pinfo, hdr))
small capitals from c petite p scientific i
Definition: afcover.h:80
int capacity
Definition: pdfcolor.c:1335
#define size_t
Definition: glob.c:257
float x
Definition: cordic.py:15
integer nn[24]
Definition: pmxab.c:90
static int size
Definition: ppmlabel.c:24
static void resize(lua_State *L, Table *t, int nasize, int nhsize)
Definition: minilua.c:1513
#define swap(sp)
Definition: interp.c:123
ShellFileEnvironment e
Definition: sh6.c:388
back
Definition: tex4ht.c:3533
#define end(cp)
Definition: zic.c:71