2#define CLICK_DEQUE_HH 1
3#include <click/algorithm.hh>
4#include <click/array_memory.hh>
8template <
typename AM>
class deque_memory {
public:
10 typedef typename AM::type type;
11 inline bool need_argument_copy(
const type *argp)
const {
14 inline size_type canonp(size_type p)
const {
15 return p < capacity_ ? p : p - capacity_;
17 inline size_type i2p(size_type i)
const {
18 return canonp(head_ + i);
20 inline size_type prevp(size_type p)
const {
21 return p ? p - 1 : capacity_ - 1;
23 inline size_type nextp(size_type p)
const {
24 return p + 1 != capacity_ ? p + 1 : 0;
26 inline size_type naccess(size_type n)
const {
27 return head_ + n <= capacity_ ? n : capacity_ - head_;
31 : l_(0), head_(0), n_(0), capacity_(0) {
34 void assign(
const deque_memory<AM> &x);
35 void assign(size_type n,
const type *vp);
36 void resize(size_type n,
const type *vp);
37 bool insert(
int i,
const type *vp);
38 int erase(
int ai,
int bi);
39 inline void push_back(
const type *vp) {
41 size_type p = i2p(n_);
42 AM::mark_undefined(l_ + p, 1);
43 AM::fill(l_ + p, 1, vp);
46 reserve_and_push(-1,
false, vp);
48#if HAVE_CXX_RVALUE_REFERENCES
49 inline void move_construct_back(type* vp) {
51 size_type p = i2p(n_);
52 AM::mark_undefined(l_ + p, 1);
53 AM::move_construct(l_ + p, vp);
56 reserve_and_push(-1,
false, vp);
59 inline void pop_back() {
62 size_type p = i2p(n_);
63 AM::destroy(l_ + p, 1);
64 AM::mark_noaccess(l_ + p, 1);
66 inline void push_front(
const type *vp) {
69 AM::mark_undefined(l_ + head_, 1);
70 AM::fill(l_ + head_, 1, vp);
73 reserve_and_push(-1,
true, vp);
75#if HAVE_CXX_RVALUE_REFERENCES
76 inline void move_construct_front(type* vp) {
79 AM::mark_undefined(l_ + head_, 1);
80 AM::move_construct(l_ + head_, vp);
83 reserve_and_push(-1,
true, vp);
86 inline void pop_front() {
89 AM::destroy(l_ + head_, 1);
90 AM::mark_noaccess(l_ + head_, 1);
94 size_type f = naccess(n_);
95 AM::destroy(l_ + head_, f);
96 AM::destroy(l_, n_ - f);
97 AM::mark_noaccess(l_, capacity_);
100 bool reserve_and_push(size_type n,
bool isfront,
const type *vp);
101 void swap(deque_memory<AM> &x);
110template <
typename T>
class Deque_const_iterator;
150 typedef typename array_memory<T>::type array_memory_type;
151 mutable deque_memory<array_memory_type> vm_;
163 typedef const T &const_access_type;
174 explicit inline Deque();
177#if HAVE_CXX_RVALUE_REFERENCES
182#if HAVE_CXX_RVALUE_REFERENCES
196 inline bool empty()
const;
205 inline const T &
front()
const;
207 inline const T &
back()
const;
211 inline T &at_u(
size_type i) CLICK_DEPRECATED;
212 inline const T &at_u(
size_type i)
const CLICK_DEPRECATED;
215#if HAVE_CXX_RVALUE_REFERENCES
216 template <
typename A = fast_argument<T> >
217 inline typename A::enable_rvalue_reference
push_back(T &&v);
221#if HAVE_CXX_RVALUE_REFERENCES
222 template <
typename A = fast_argument<T> >
223 inline typename A::enable_rvalue_reference
push_front(T &&v);
238class Deque_const_iterator {
241 friend class Deque<T>;
244 typedef Deque_const_iterator<T> const_iterator;
247 Deque_const_iterator() {
249 Deque_const_iterator(
const Deque<T> *q, size_type p)
252 const T *operator->()
const {
258 const T &operator[](size_type i)
const {
259 return (*q_)[p_ + i];
261 bool operator==(
const const_iterator &x)
const {
262 return p_ == x.p_ && q_ == x.q_;
264 bool operator!=(
const const_iterator &x)
const {
265 return p_ != x.p_ || q_ != x.q_;
267 bool operator<(
const const_iterator &x)
const {
271 bool operator<=(
const const_iterator &x)
const {
275 bool operator>=(
const const_iterator &x)
const {
279 bool operator>(
const const_iterator &x)
const {
283 size_type diff(
const const_iterator &x)
const {
287 void operator++(
int) {
290 const_iterator &operator++() {
294 void operator--(
int) {
297 const_iterator &operator--() {
301 const_iterator &operator+=(size_type n) {
305 const_iterator &operator-=(size_type n) {
314 typedef Deque_const_iterator<T> const_iterator;
320 : const_iterator(q, p) {
322 T *operator->()
const {
323 return const_cast<T *
>(const_iterator::operator->());
325 T &operator*()
const {
326 return const_cast<T &
>(const_iterator::operator*());
328 T &operator[](
int n)
const {
329 return const_cast<T &
>(const_iterator::operator[](n));
331 iterator &operator+=(size_type n) {
332 const_iterator::operator+=(n);
335 iterator &operator-=(size_type n) {
336 const_iterator::operator-=(n);
349 vm_.resize(n, array_memory_type::cast(&v));
358#if HAVE_CXX_RVALUE_REFERENCES
385 return vm_.capacity_;
430 assert((
unsigned) i < (
unsigned) vm_.n_);
431 return *(T *)&vm_.l_[vm_.i2p(i)];
437 assert((
unsigned) i < (
unsigned) vm_.n_);
438 return *(T *)&vm_.l_[vm_.i2p(i)];
446 return operator[](i);
452 return operator[](i);
459 return operator[](0);
465 return operator[](0);
472 return operator[](vm_.n_ - 1);
478 return operator[](vm_.n_ - 1);
488 return *(T *)&vm_.l_[vm_.i2p(i)];
494 return *(T *)&vm_.l_[vm_.i2p(i)];
500 return unchecked_at(i);
505 return unchecked_at(i);
514 vm_.resize(n, array_memory_type::cast(&v));
523 vm_.push_back(array_memory_type::cast(&v));
526#if HAVE_CXX_RVALUE_REFERENCES
528template <
typename T>
template <
typename A>
530 vm_.move_construct_back(array_memory_type::cast(&v));
548 vm_.push_front(array_memory_type::cast(&v));
551#if HAVE_CXX_RVALUE_REFERENCES
553template <
typename T>
template <
typename A>
555 vm_.move_construct_front(array_memory_type::cast(&v));
572 assert(it.q_ ==
this);
573 if (likely(vm_.insert(it.p_, array_memory_type::cast(&v))))
584 assert(it.q_ ==
this);
585 return (it < end() ? erase(it, it + 1) : it);
593 assert(a.q_ ==
this && b.q_ ==
this);
594 return iterator(
this, vm_.erase(a.p_, b.p_));
612 return vm_.reserve_and_push(n,
false, 0);
628#if HAVE_CXX_RVALUE_REFERENCES
641 vm_.assign(n, array_memory_type::cast(&v));
646Deque_const_iterator<T> operator+(Deque_const_iterator<T> it,
652Deque_const_iterator<T> operator-(Deque_const_iterator<T> it,
670typename Deque_const_iterator<T>::size_type
671operator-(
const Deque_const_iterator<T> &a,
const Deque_const_iterator<T> &b) {
686#include <click/deque.cc>
Deque template.
Definition deque.hh:148
Deque_const_iterator< T > const_iterator
Const iterator type.
Definition deque.hh:168
Deque< T > & operator=(const Deque< T > &x)
Replace this deque's contents with a copy of x.
Definition deque.hh:623
void resize(size_type n, value_argument_type v=T())
Resize the deque to contain n elements.
Definition deque.hh:513
void pop_front()
Remove the first element.
Definition deque.hh:563
iterator erase(iterator it)
Remove the element at position it.
Definition deque.hh:583
const T & const_reference
Const reference to value type.
Definition deque.hh:157
void push_front(value_argument_type v)
Prepend element v.
Definition deque.hh:547
void clear()
Remove all elements.
Definition deque.hh:600
const_iterator cend() const
Return a const_iterator for the end of the deque.
Definition deque.hh:422
Deque_iterator< T > iterator
Iterator type.
Definition deque.hh:167
bool reserve(size_type n)
Reserve space for at least n more elements.
Definition deque.hh:611
int size_type
Type of sizes (size()).
Definition deque.hh:165
iterator end()
Return an iterator for the end of the deque.
Definition deque.hh:403
T & at(size_type i)
Return a reference to the ith element.
Definition deque.hh:445
T & front()
Return a reference to the first element.
Definition deque.hh:458
T & operator[](size_type i)
Return a reference to the ith element.
Definition deque.hh:429
Deque< T > & assign(size_type n, value_argument_type v=T())
Replace this deque's contents with n copies of v.
Definition deque.hh:640
fast_argument< T >::type value_argument_type
Type used for value arguments (either T or const T &).
Definition deque.hh:162
size_type size() const
Return the number of elements.
Definition deque.hh:368
T & reference
Reference to value type.
Definition deque.hh:156
T & back()
Return a reference to the last element (number size()-1).
Definition deque.hh:471
bool empty() const
Test if the deque is empty (size() == 0).
Definition deque.hh:374
void push_back(value_argument_type v)
Append v to the end of the deque.
Definition deque.hh:522
void pop_back()
Remove the last element.
Definition deque.hh:538
iterator insert(iterator it, value_argument_type v)
Insert v before position it.
Definition deque.hh:571
const T * const_pointer
Pointer to const value type.
Definition deque.hh:159
const_iterator cbegin() const
Return a const_iterator for the beginning of the deque.
Definition deque.hh:415
Deque()
Construct an empty deque.
Definition deque.hh:343
T * pointer
Pointer to value type.
Definition deque.hh:158
T value_type
Value type.
Definition deque.hh:155
void swap(Deque< T > &x)
Swap the contents of this deque and x.
Definition deque.hh:617
iterator begin()
Return an iterator for the first element in the deque.
Definition deque.hh:390
size_type capacity() const
Return the deque's capacity.
Definition deque.hh:384
T & unchecked_at(size_type i)
Return a reference to the ith element.
Definition deque.hh:487
Template defining a fast argument type for objects of type T.
Definition type_traits.hh:148
Timestamp operator*(const Timestamp &a, double b)
Scale a by a factor of b and return the result.
Definition timestamp.hh:1447