Click
Loading...
Searching...
No Matches
vector.hh
Go to the documentation of this file.
1#ifndef CLICK_VECTOR_HH
2#define CLICK_VECTOR_HH
3#include <click/algorithm.hh>
4#include <click/array_memory.hh>
5CLICK_DECLS
6
11template <typename AM,size_t ALIGNMENTM> class vector_memory { public:
12 typedef int size_type;
13 typedef typename AM::type type;
14 typedef type *iterator;
15 inline bool need_argument_copy(const type *argp) const {
17 && (uintptr_t) argp - (uintptr_t) l_ < (size_t) (n_ * sizeof(type));
18 }
19
20 vector_memory()
21 : l_(0), n_(0), capacity_(0) {
22 }
23 ~vector_memory();
24
25 void assign(const vector_memory<AM,ALIGNMENTM> &x);
26 void assign(size_type n, const type *vp);
27 void resize(size_type n, const type *vp);
28 iterator begin() {
29 return l_;
30 }
31 iterator end() {
32 return l_ + n_;
33 }
34 iterator insert(iterator it, const type *vp);
35 iterator erase(iterator a, iterator b);
36 inline void push_back(const type *vp) {
37 if (n_ < capacity_) {
38 AM::mark_undefined(l_ + n_, 1);
39 AM::fill(l_ + n_, 1, vp);
40 ++n_;
41 } else
42 reserve_and_push_back(-1, vp);
43 }
44#if HAVE_CXX_RVALUE_REFERENCES
45 inline void move_construct_back(type* vp) {
46 if (n_ < capacity_) {
47 AM::mark_undefined(l_ + n_, 1);
48 AM::move_construct(l_ + n_, vp);
49 ++n_;
50 } else
51 reserve_and_push_back(-1, vp);
52 }
53#endif
54 inline void pop_back() {
55 assert(n_ > 0);
56 --n_;
57 AM::destroy(l_ + n_, 1);
58 AM::mark_noaccess(l_ + n_, 1);
59 }
60 inline void clear() {
61 AM::destroy(l_, n_);
62 AM::mark_noaccess(l_, n_);
63 n_ = 0;
64 }
65 bool reserve_and_push_back(size_type n, const type *vp);
66 void swap(vector_memory<AM,ALIGNMENTM> &x);
67
68 type *l_;
69 size_type n_;
70 size_type capacity_;
71};
110template <typename T, size_t ALIGNMENT=CLICK_DEFAULT_ALIGNMENT>
111class Vector {
112
113 typedef typename array_memory<T>::type array_memory_type;
114 mutable vector_memory<array_memory_type,ALIGNMENT> vm_;
115
116 public:
117
118 typedef T value_type;
119 typedef T &reference;
120 typedef const T &const_reference;
121 typedef T *pointer;
122 typedef const T *const_pointer;
123
126 typedef const T &const_access_type;
127
128 typedef int size_type;
129
130 typedef T *iterator;
131 typedef const T *const_iterator;
132
134 enum { RESERVE_GROW = (size_type) -1 };
135
136
137 explicit inline Vector();
138 explicit inline Vector(size_type n, value_argument_type v);
139 inline Vector(const Vector<T,ALIGNMENT> &x);
140#if HAVE_CXX_RVALUE_REFERENCES
141 inline Vector(Vector<T,ALIGNMENT> &&x);
142#endif
143
145#if HAVE_CXX_RVALUE_REFERENCES
147#endif
149
150 inline iterator begin();
151 inline iterator end();
152 inline const_iterator begin() const;
153 inline const_iterator end() const;
154 inline const_iterator cbegin() const;
155 inline const_iterator cend() const;
156
157 inline size_type size() const;
158 inline size_type capacity() const;
159 inline bool empty() const;
160 inline void resize(size_type n, value_argument_type v = T());
161 inline bool reserve(size_type n);
162
163 inline T &operator[](size_type i);
164 inline const T &operator[](size_type i) const;
165 inline T &at(size_type i);
166 inline const T &at(size_type i) const;
167 inline T &front();
168 inline const T &front() const;
169 inline T &back();
170 inline const T &back() const;
171
173 inline const T &unchecked_at(size_type i) const;
174 inline T &at_u(size_type i) CLICK_DEPRECATED;
175 inline const T &at_u(size_type i) const CLICK_DEPRECATED;
176
177 inline T *data();
178 inline const T *data() const;
179
181#if HAVE_CXX_RVALUE_REFERENCES
182 template <typename A = fast_argument<T> >
183 inline typename A::enable_rvalue_reference push_back(T &&v);
184#endif
185 inline void pop_back();
187 inline void pop_front();
188
192 inline void fill(T value);
193
194 inline void clear();
195
197
198 inline void swap(Vector<T,ALIGNMENT> &x);
199
200 inline void shuffle(int n);
201
202};
203
205template <typename T, size_t ALIGNMENT>
208
210template <typename T, size_t ALIGNMENT>
212 vm_.resize(n, array_memory_type::cast(&v));
213}
214
216template <typename T, size_t ALIGNMENT>
218 vm_.assign(x.vm_);
219}
220
221#if HAVE_CXX_RVALUE_REFERENCES
223template <typename T, size_t ALIGNMENT>
225 vm_.swap(x.vm_);
226}
227#endif
228
230template <typename T, size_t ALIGNMENT>
232 return vm_.n_;
233}
234
236template <typename T, size_t ALIGNMENT>
237inline bool Vector<T,ALIGNMENT>::empty() const {
238 return vm_.n_ == 0;
239}
240
246template <typename T, size_t ALIGNMENT>
248 return vm_.capacity_;
249}
250
252template <typename T, size_t ALIGNMENT>
254 return (iterator) vm_.l_;
255}
256
258template <typename T, size_t ALIGNMENT>
260 return (const_iterator) vm_.l_;
261}
262
265template <typename T, size_t ALIGNMENT>
267 return (iterator) vm_.l_ + vm_.n_;
268}
269
271template <typename T, size_t ALIGNMENT>
273 return (const_iterator) vm_.l_ + vm_.n_;
274}
275
277template <typename T, size_t ALIGNMENT>
279 return (const_iterator) vm_.l_;
280}
281
284template <typename T, size_t ALIGNMENT>
286 return (iterator) vm_.l_ + vm_.n_;
287}
288
291template <typename T, size_t ALIGNMENT>
293 assert((unsigned) i < (unsigned) vm_.n_);
294 return *(T *)&vm_.l_[i];
295}
296
298template <typename T, size_t ALIGNMENT>
300 assert((unsigned) i < (unsigned) vm_.n_);
301 return *(T *)&vm_.l_[i];
302}
303
307template <typename T, size_t ALIGNMENT>
309 return operator[](i);
310}
311
313template <typename T, size_t ALIGNMENT>
314inline const T &Vector<T,ALIGNMENT>::at(size_type i) const {
315 return operator[](i);
316}
317
320template <typename T, size_t ALIGNMENT>
322 return operator[](0);
323}
324
326template <typename T, size_t ALIGNMENT>
327inline const T &Vector<T,ALIGNMENT>::front() const {
328 return operator[](0);
329}
330
333template <typename T, size_t ALIGNMENT>
335 return operator[](vm_.n_ - 1);
336}
337
339template <typename T, size_t ALIGNMENT>
340inline const T &Vector<T,ALIGNMENT>::back() const {
341 return operator[](vm_.n_ - 1);
342}
343
349template <typename T, size_t ALIGNMENT>
351 return *(T *)&vm_.l_[i];
352}
353
355template <typename T, size_t ALIGNMENT>
357 return *(T *)&vm_.l_[i];
358}
359
361template <typename T, size_t ALIGNMENT>
362inline T &Vector<T,ALIGNMENT>::at_u(size_type i) {
363 return unchecked_at(i);
364}
365
366template <typename T, size_t ALIGNMENT>
367inline const T &Vector<T,ALIGNMENT>::at_u(size_type i) const {
368 return unchecked_at(i);
369}
375template <typename T, size_t ALIGNMENT>
377 return (T *) vm_.l_;
378}
379
381template <typename T, size_t ALIGNMENT>
382inline const T *Vector<T,ALIGNMENT>::data() const {
383 return (const T *) vm_.l_;
384}
385
389template <typename T, size_t ALIGNMENT>
391 vm_.resize(n, array_memory_type::cast(&v));
392}
393
398template <typename T, size_t ALIGNMENT>
400 vm_.push_back(array_memory_type::cast(&v));
401}
402
403#if HAVE_CXX_RVALUE_REFERENCES
405template <typename T, size_t ALIGNMENT> template <typename A>
406inline typename A::enable_rvalue_reference Vector<T,ALIGNMENT>::push_back(T&& v)
407{
408 vm_.move_construct_back(array_memory_type::cast(&v));
409}
410#endif
411
415template <typename T, size_t ALIGNMENT>
417 vm_.pop_back();
418}
419
424template <typename T, size_t ALIGNMENT>
426 vm_.insert(vm_.l_, array_memory_type::cast(&v));
427}
428
433template <typename T, size_t ALIGNMENT>
435 vm_.erase(vm_.l_, vm_.l_ + 1);
436}
437
440template <typename T, size_t ALIGNMENT>
441inline typename Vector<T,ALIGNMENT>::iterator
443 return (iterator) vm_.insert(array_memory_type::cast(it),
444 array_memory_type::cast(&v));
445}
446
449template <typename T, size_t ALIGNMENT>
450inline typename Vector<T,ALIGNMENT>::iterator
452 return (it < end() ? erase(it, it + 1) : it);
453}
454
457template <typename T, size_t ALIGNMENT>
458inline typename Vector<T,ALIGNMENT>::iterator
460 return (iterator) vm_.erase(array_memory_type::cast(a),
461 array_memory_type::cast(b));
462}
463
464template <typename T, size_t ALIGNMENT>
465inline void
467 Vector<T,ALIGNMENT>::iterator __first = begin();
468 Vector<T,ALIGNMENT>::iterator __last = end();
469 for (; __first != __last; ++__first)
470 *__first = value;
471}
472
475template <typename T, size_t ALIGNMENT>
477 vm_.clear();
478}
479
486template <typename T, size_t ALIGNMENT>
488 return vm_.reserve_and_push_back(n, 0);
489}
490
492template <typename T, size_t ALIGNMENT>
494 vm_.swap(x.vm_);
495}
496
498template <typename T, size_t ALIGNMENT>
500 Vector<T,ALIGNMENT> n(sz,0);
501 for (int i = 0; i < sz; i++) {
502 n[i] = *(T *)&vm_.l_[i + begin];
503 }
504 return n;
505}
506
508template <typename T, size_t ALIGNMENT>
510 vm_.assign(x.vm_);
511 return *this;
512}
513
514#if HAVE_CXX_RVALUE_REFERENCES
515template <typename T, size_t ALIGNMENT>
517 vm_.swap(x.vm_);
518 return *this;
519}
520#endif
521
524template <typename T, size_t ALIGNMENT>
526 vm_.assign(n, array_memory_type::cast(&v));
527 return *this;
528}
529
530template <typename T, size_t ALIGNMENT>
531inline void click_swap(Vector<T,ALIGNMENT> &a, Vector<T,ALIGNMENT> &b) {
532 a.swap(b);
533}
534
535template <typename T, size_t ALIGNMENT>
536inline void assign_consume(Vector<T,ALIGNMENT> &a, Vector<T,ALIGNMENT> &b) {
537 a.swap(b);
538}
539
540template <typename T, size_t ALIGNMENT>
541inline void Vector<T,ALIGNMENT>::shuffle(int n) {
542 if (size() > 1)
543 {
544 size_t i;
545 for (i = 0; i < (unsigned)n; i++)
546 {
547 int n_i = i % size();
548 size_t j = click_random(0, size() -1);
549 if (n_i == j)
550 continue;
551 assert(j >= 0 && j < size());
552 T t = *(T*)&vm_.l_[n_i];
553 *(T*)&vm_.l_[j] = *(T*)&vm_.l_[n_i];
554 *(T*)&vm_.l_[n_i] = t;
555 }
556 }
557}
558
559CLICK_ENDDECLS
560#include <click/vector.cc>
561#endif
Base class for Click elements.
Definition element.hh:42
Vector template.
Definition vector.hh:111
T * data()
Return a pointer to the vector's data.
Definition vector.hh:376
T & front()
Return a reference to the first element.
Definition vector.hh:321
T & at(size_type i)
Return a reference to the ith element.
Definition vector.hh:308
bool empty() const
Test if the vector is empty (size() == 0).
Definition vector.hh:237
Vector< T, ALIGNMENT > & operator=(const Vector< T, ALIGNMENT > &x)
Replace this vector's contents with a copy of x.
Definition vector.hh:509
iterator erase(iterator a, iterator b)
Remove the elements in [a, b).
Definition vector.hh:459
T & reference
Reference to value type.
Definition vector.hh:119
Vector()
Construct an empty vector.
Definition vector.hh:206
T * iterator
Iterator type.
Definition vector.hh:130
const T * const_pointer
Pointer to const value type.
Definition vector.hh:122
iterator erase(iterator it)
Remove the element at position it.
Definition vector.hh:451
bool reserve(size_type n)
Reserve space for at least n more elements.
Definition vector.hh:487
void pop_front()
Remove the first element.
Definition vector.hh:434
void clear()
Remove all elements.
Definition vector.hh:476
T * pointer
Pointer to value type.
Definition vector.hh:121
fast_argument< T >::type value_argument_type
Type used for value arguments (either T or const T &).
Definition vector.hh:125
Vector< T, ALIGNMENT > & assign(size_type n, value_argument_type v=T())
Replace this vector's contents with n copies of v.
Definition vector.hh:525
const T & back() const
Definition vector.hh:340
int size_type
Type of sizes (size()).
Definition vector.hh:128
const_iterator end() const
Definition vector.hh:272
T & back()
Return a reference to the last element (number size()-1).
Definition vector.hh:334
T & operator[](size_type i)
Return a reference to the ith element.
Definition vector.hh:292
iterator end()
Return an iterator for the end of the vector.
Definition vector.hh:266
void push_front(value_argument_type v)
Prepend element v.
Definition vector.hh:425
const T & const_reference
Const reference to value type.
Definition vector.hh:120
const_iterator begin() const
Definition vector.hh:259
size_type capacity() const
Return the vector's capacity.
Definition vector.hh:247
Vector(const Vector< T, ALIGNMENT > &x)
Construct a vector as a copy of x.
Definition vector.hh:217
iterator begin()
Return an iterator for the first element in the vector.
Definition vector.hh:253
void pop_back()
Remove the last element.
Definition vector.hh:416
const T * data() const
Definition vector.hh:382
T value_type
Value type.
Definition vector.hh:118
Vector< T, ALIGNMENT > sub(size_type begin, size_type sz)
Swap the contents of this vector and x.
Definition vector.hh:499
Vector(size_type n, value_argument_type v)
Construct a vector containing n copies of v.
Definition vector.hh:211
void resize(size_type n, value_argument_type v=T())
Resize the vector to contain n elements.
Definition vector.hh:390
iterator insert(iterator it, value_argument_type v)
Insert v before position it.
Definition vector.hh:442
void swap(Vector< T, ALIGNMENT > &x)
Swap the contents of this vector and x.
Definition vector.hh:493
const T & front() const
Definition vector.hh:327
const T & unchecked_at(size_type i) const
Definition vector.hh:356
T & unchecked_at(size_type i)
Return a reference to the ith element.
Definition vector.hh:350
const T & at(size_type i) const
Definition vector.hh:314
const_iterator cend() const
Return a const_iterator for the end of the vector.
Definition vector.hh:285
const T * const_iterator
Const iterator type.
Definition vector.hh:131
size_type size() const
Return the number of elements.
Definition vector.hh:231
void push_back(value_argument_type v)
Append element v.
Definition vector.hh:399
const_iterator cbegin() const
Return a const_iterator for the beginning of the vector.
Definition vector.hh:278
const T & operator[](size_type i) const
Definition vector.hh:299
Template defining a fast argument type for objects of type T.
Definition type_traits.hh:148