22#include <click/deque.hh>
27deque_memory<AM>::~deque_memory()
29 size_type f = naccess(n_);
30 AM::destroy(l_ + head_, f);
31 AM::destroy(l_, n_ - f);
32 CLICK_LFREE(l_, capacity_ *
sizeof(type));
36void deque_memory<AM>::assign(
const deque_memory<AM> &x)
39 size_type f = naccess(n_);
40 AM::destroy(l_ + head_, f);
41 AM::destroy(l_, n_ - f);
43 if (reserve_and_push(x.n_,
false, 0)) {
45 AM::mark_undefined(l_, n_);
47 AM::copy(l_, x.l_ + x.head_, f);
48 AM::copy(l_ + f, x.l_, n_ - f);
54void deque_memory<AM>::assign(size_type n,
const type *vp)
56 if (unlikely(need_argument_copy(vp))) {
58 return assign(n, &v_copy);
66bool deque_memory<AM>::insert(size_type i,
const type *vp)
68 assert((
unsigned) i <= (
unsigned) n_);
69 if (unlikely(need_argument_copy(vp))) {
71 return insert(i, &v_copy);
74 if (n_ == capacity_ && !reserve_and_push(-1,
false, 0))
76 size_type srcpp, dstpp, size;
85 srcpp = head_ + n_ - 1;
91 AM::mark_undefined(l_ + canonp(dstpp), 1);
92 AM::move(l_ + canonp(dstpp), l_ + canonp(srcpp), 1);
93 dstpp += delta, srcpp += delta, --size;
95 AM::mark_undefined(l_ + canonp(dstpp), 1);
96 AM::fill(l_ + canonp(dstpp), 1, vp);
101template <
typename AM>
102int deque_memory<AM>::erase(size_type ai, size_type bi)
104 assert(ai >= bi || (ai >= 0 && (
unsigned) bi <= (
unsigned) n_));
106 size_type srcpp, dstpp, size;
109 srcpp = head_ + ai - 1;
110 dstpp = head_ + bi - 1;
113 head_ = canonp(head_ + bi - ai);
121 AM::destroy(l_ + canonp(dstpp), 1);
122 AM::copy(l_ + canonp(dstpp), l_ + canonp(srcpp), 1);
123 dstpp += delta, srcpp += delta, --size;
125 for (size = bi - ai; size; --size, dstpp += delta) {
126 AM::destroy(l_ + canonp(dstpp), 1);
127 AM::mark_noaccess(l_ + canonp(dstpp), 1);
135template <
typename AM>
136bool deque_memory<AM>::reserve_and_push(size_type want,
bool isfront,
const type *push_vp)
138 if (unlikely(push_vp && need_argument_copy(push_vp))) {
139 type push_v_copy(*push_vp);
140 return reserve_and_push(want, isfront, &push_v_copy);
144 want = (capacity_ > 0 ? capacity_ * 2 : 4);
146 if (want > capacity_) {
147 type *new_l = (type *) CLICK_LALLOC(want *
sizeof(type));
150 AM::mark_noaccess(new_l + n_, want - n_);
151 size_type f = naccess(n_);
152 AM::move(new_l, l_ + head_, f);
153 AM::move(new_l + f, l_, n_ - f);
154 CLICK_LFREE(l_, capacity_ *
sizeof(type));
160 if (unlikely(push_vp))
161 (isfront ? push_front(push_vp) : push_back(push_vp));
165template <
typename AM>
166void deque_memory<AM>::resize(size_type n,
const type *vp)
168 if (unlikely(need_argument_copy(vp))) {
170 return resize(n, &v_copy);
173 if (n <= capacity_ || reserve_and_push(n,
false, 0)) {
176 for (size_type p = i2p(n), x = i2p(n_); p != x; p = nextp(p)) {
177 AM::destroy(l_ + p, 1);
178 AM::mark_noaccess(l_ + p, 1);
181 for (size_type p = i2p(n_), x = i2p(n); p != x; p = nextp(p)) {
182 AM::mark_undefined(l_ + p, 1);
183 AM::fill(l_ + p, 1, vp);
189template <
typename AM>
190void deque_memory<AM>::swap(deque_memory<AM> &x)
196 size_type head = head_;
204 size_type capacity = capacity_;
205 capacity_ = x.capacity_;
206 x.capacity_ = capacity;