3#include <click/algorithm.hh>
4#include <click/array_memory.hh>
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));
21 : l_(0), n_(0), capacity_(0) {
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);
34 iterator insert(iterator it,
const type *vp);
35 iterator erase(iterator a, iterator b);
36 inline void push_back(
const type *vp) {
38 AM::mark_undefined(l_ + n_, 1);
39 AM::fill(l_ + n_, 1, vp);
42 reserve_and_push_back(-1, vp);
44#if HAVE_CXX_RVALUE_REFERENCES
45 inline void move_construct_back(type* vp) {
47 AM::mark_undefined(l_ + n_, 1);
48 AM::move_construct(l_ + n_, vp);
51 reserve_and_push_back(-1, vp);
54 inline void pop_back() {
57 AM::destroy(l_ + n_, 1);
58 AM::mark_noaccess(l_ + n_, 1);
62 AM::mark_noaccess(l_, n_);
65 bool reserve_and_push_back(size_type n,
const type *vp);
66 void swap(vector_memory<AM,ALIGNMENTM> &x);
110template <
typename T,
size_t ALIGNMENT=CLICK_DEFAULT_ALIGNMENT>
113 typedef typename array_memory<T>::type array_memory_type;
114 mutable vector_memory<array_memory_type,ALIGNMENT> vm_;
126 typedef const T &const_access_type;
140#if HAVE_CXX_RVALUE_REFERENCES
145#if HAVE_CXX_RVALUE_REFERENCES
174 inline T &at_u(
size_type i) CLICK_DEPRECATED;
175 inline const T &at_u(
size_type i)
const CLICK_DEPRECATED;
181#if HAVE_CXX_RVALUE_REFERENCES
182 template <
typename A = fast_argument<T> >
183 inline typename A::enable_rvalue_reference
push_back(T &&v);
192 inline void fill(T value);
200 inline void shuffle(
int n);
205template <
typename T,
size_t ALIGNMENT>
210template <
typename T,
size_t ALIGNMENT>
212 vm_.resize(n, array_memory_type::cast(&v));
216template <
typename T,
size_t ALIGNMENT>
221#if HAVE_CXX_RVALUE_REFERENCES
223template <
typename T,
size_t ALIGNMENT>
230template <
typename T,
size_t ALIGNMENT>
236template <
typename T,
size_t ALIGNMENT>
246template <
typename T,
size_t ALIGNMENT>
248 return vm_.capacity_;
252template <
typename T,
size_t ALIGNMENT>
258template <
typename T,
size_t ALIGNMENT>
265template <
typename T,
size_t ALIGNMENT>
271template <
typename T,
size_t ALIGNMENT>
277template <
typename T,
size_t ALIGNMENT>
284template <
typename T,
size_t ALIGNMENT>
291template <
typename T,
size_t ALIGNMENT>
293 assert((
unsigned) i < (
unsigned) vm_.n_);
294 return *(T *)&vm_.l_[i];
298template <
typename T,
size_t ALIGNMENT>
300 assert((
unsigned) i < (
unsigned) vm_.n_);
301 return *(T *)&vm_.l_[i];
307template <
typename T,
size_t ALIGNMENT>
309 return operator[](i);
313template <
typename T,
size_t ALIGNMENT>
315 return operator[](i);
320template <
typename T,
size_t ALIGNMENT>
322 return operator[](0);
326template <
typename T,
size_t ALIGNMENT>
328 return operator[](0);
333template <
typename T,
size_t ALIGNMENT>
335 return operator[](vm_.n_ - 1);
339template <
typename T,
size_t ALIGNMENT>
341 return operator[](vm_.n_ - 1);
349template <
typename T,
size_t ALIGNMENT>
351 return *(T *)&vm_.l_[i];
355template <
typename T,
size_t ALIGNMENT>
357 return *(T *)&vm_.l_[i];
361template <
typename T,
size_t ALIGNMENT>
363 return unchecked_at(i);
366template <
typename T,
size_t ALIGNMENT>
368 return unchecked_at(i);
375template <
typename T,
size_t ALIGNMENT>
381template <
typename T,
size_t ALIGNMENT>
383 return (
const T *) vm_.l_;
389template <
typename T,
size_t ALIGNMENT>
391 vm_.resize(n, array_memory_type::cast(&v));
398template <
typename T,
size_t ALIGNMENT>
400 vm_.push_back(array_memory_type::cast(&v));
403#if HAVE_CXX_RVALUE_REFERENCES
405template <
typename T,
size_t ALIGNMENT>
template <
typename A>
408 vm_.move_construct_back(array_memory_type::cast(&v));
415template <
typename T,
size_t ALIGNMENT>
424template <
typename T,
size_t ALIGNMENT>
426 vm_.insert(vm_.l_, array_memory_type::cast(&v));
433template <
typename T,
size_t ALIGNMENT>
435 vm_.erase(vm_.l_, vm_.l_ + 1);
440template <
typename T,
size_t ALIGNMENT>
443 return (
iterator) vm_.insert(array_memory_type::cast(it),
444 array_memory_type::cast(&v));
449template <
typename T,
size_t ALIGNMENT>
452 return (it < end() ? erase(it, it + 1) : it);
457template <
typename T,
size_t ALIGNMENT>
460 return (
iterator) vm_.erase(array_memory_type::cast(a),
461 array_memory_type::cast(b));
464template <
typename T,
size_t ALIGNMENT>
469 for (; __first != __last; ++__first)
475template <
typename T,
size_t ALIGNMENT>
486template <
typename T,
size_t ALIGNMENT>
488 return vm_.reserve_and_push_back(n, 0);
492template <
typename T,
size_t ALIGNMENT>
498template <
typename T,
size_t ALIGNMENT>
501 for (
int i = 0; i < sz; i++) {
502 n[i] = *(T *)&vm_.l_[i + begin];
508template <
typename T,
size_t ALIGNMENT>
514#if HAVE_CXX_RVALUE_REFERENCES
515template <
typename T,
size_t ALIGNMENT>
524template <
typename T,
size_t ALIGNMENT>
526 vm_.assign(n, array_memory_type::cast(&v));
530template <
typename T,
size_t ALIGNMENT>
535template <
typename T,
size_t ALIGNMENT>
540template <
typename T,
size_t ALIGNMENT>
545 for (i = 0; i < (unsigned)n; i++)
547 int n_i = i % size();
548 size_t j = click_random(0, size() -1);
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;
560#include <click/vector.cc>
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