16 constexpr D_list_item() : _dli_next(nullptr), _dli_prev(nullptr) {}
18 D_list_item(D_list_item
const &) =
delete;
19 void operator = (D_list_item
const &) =
delete;
22 friend struct D_list_item_policy;
24 D_list_item *_dli_next, *_dli_prev;
27struct D_list_item_policy
29 typedef D_list_item Item;
30 static D_list_item *&prev(D_list_item *e) {
return e->_dli_prev; }
31 static D_list_item *&next(D_list_item *e) {
return e->_dli_next; }
32 static D_list_item *prev(D_list_item
const *e) {
return e->_dli_prev; }
33 static D_list_item *next(D_list_item
const *e) {
return e->_dli_next; }
37struct Sd_list_head_policy
40 static T *head(Head_type h) {
return h; }
41 static void set_head(Head_type &h, T *v) { h = v; }
46 typename C = D_list_item_policy
51 template<
typename VALUE,
typename ITEM >
55 typedef VALUE *Value_type;
56 typedef VALUE *value_type;
60 bool operator == (__Iterator
const &o)
const
61 {
return _c == o._c; }
63 bool operator != (__Iterator
const &o)
const
64 {
return _c != o._c; }
66 __Iterator &operator ++ ()
72 __Iterator &operator -- ()
78 Value_type operator * ()
const {
return static_cast<Value_type
>(_c); }
79 Value_type operator -> ()
const {
return static_cast<Value_type
>(_c); }
82 friend class D_list_cyclic;
84 explicit __Iterator(ITEM *s) : _c(s) {}
90 typedef T *Value_type;
91 typedef T *value_type;
92 typedef __Iterator<T, typename C::Item> Iterator;
93 typedef Iterator Const_iterator;
95 static void remove(T *e)
97 C::next(C::prev(e)) = C::next(e);
98 C::prev(C::next(e)) = C::prev(e);
102 static Iterator erase(Iterator
const &e)
104 typename C::Item *n = C::next(*e);
109 static Iterator iter(T
const *e) {
return Iterator(
const_cast<T*
>(e)); }
111 static bool in_list(T
const *e) {
return C::next(
const_cast<T*
>(e)); }
112 static bool has_sibling(T
const *e) {
return C::next(
const_cast<T*
>(e)) != e; }
114 static Iterator insert_after(T *e, Iterator
const &pos)
117 C::next(e) = C::next(pos._c);
118 C::prev(C::next(pos._c)) = e;
123 static Iterator insert_before(T *e, Iterator
const &pos)
126 C::prev(e) = C::prev(pos._c);
127 C::next(C::prev(pos._c)) = e;
133 static void self_insert(
typename C::Item *e)
134 { C::next(e) = C::prev(e) = e; }
136 static void remove_last(T *e)
137 { C::next(e) =
nullptr; }
145 static void splice_heads(Const_iterator pos,
typename C::Item *other_list)
147 typename C::Item *ins_next = pos._c;
148 typename C::Item *ins_prev = C::prev(pos._c);
149 typename C::Item *other_head = C::next(other_list);
150 typename C::Item *other_tail = C::prev(other_list);
152 C::next(ins_prev) = other_head;
153 C::prev(other_head) = ins_prev;
154 C::prev(ins_next) = other_tail;
155 C::next(other_tail) = ins_next;
158 static Iterator __iter(
typename C::Item *e) {
return Iterator(e); }
163 typename C = D_list_item_policy,
164 typename H = Sd_list_head_policy<T>,
167class Sd_list :
public D_list_cyclic<T, C>
170 typedef D_list_cyclic<T, C> Base;
173 class Iterator :
public Base::Iterator
176 Iterator &operator ++ ()
179 Base::Iterator::operator ++ ();
187 Iterator &operator -- () =
delete;
190 friend class Sd_list;
192 explicit Iterator(T *h) : Base::Iterator(h), _h(h) {}
193 typename C::Item *_h;
196 class R_iterator :
public Base::Iterator
199 R_iterator &operator ++ ()
202 Base::Iterator::operator -- ();
210 R_iterator &operator -- () =
delete;
213 friend class Sd_list;
215 explicit R_iterator(T *h) : Base::Iterator(h), _h(h) {}
216 typename C::Item *_h;
226 H::set_head(_f,
nullptr);
229 bool empty()
const {
return !H::head(_f); }
230 T *front()
const {
return H::head(_f); }
237 Base::remove_last(e);
238 H::set_head(_f,
nullptr);
242 if (e == H::head(_f))
243 H::set_head(_f,
static_cast<T*
>(C::next(h)));
248 Iterator erase(Iterator
const &e)
257 void push(T *e, Pos pos)
262 Base::self_insert(e);
267 Base::insert_before(e, this->iter(h));
273 void push_back(T *e) { push(e, Back); }
274 void push_front(T *e) { push(e, Front); }
275 void rotate_to(T *h) { H::set_head(_f, h); }
277 typename H::Head_type
const &head()
const {
return _f; }
278 typename H::Head_type &head() {
return _f; }
280 Iterator begin() {
return Iterator(H::head(_f)); }
281 Iterator end() {
return Iterator(
nullptr); }
286 return R_iterator(
static_cast<T*
>(C::prev(H::head(_f))));
287 return R_iterator(
nullptr);
289 R_iterator rend() {
return R_iterator(
nullptr); }
292 Sd_list(Sd_list
const &);
293 void operator = (Sd_list
const &);
295 typename H::Head_type _f;
300 typename C = D_list_item_policy,
303class D_list :
public D_list_cyclic<T, C>
306 typedef D_list_cyclic<T, C> Base;
307 typedef typename C::Item Internal_type;
313 typedef typename Base::Iterator Iterator;
314 typedef typename Base::Const_iterator Const_iterator;
315 typedef T* value_type;
316 typedef T* Value_type;
318 D_list() { this->self_insert(&_h); }
319 ~D_list() { clear(); }
325 this->self_insert(&_h);
329 Internal_type *p = C::prev(&o._h);
330 Internal_type *n = C::next(&o._h);
335 o.self_insert(&o._h);
339 D_list &operator=(D_list &&o)
348 Internal_type *p = C::prev(&o._h);
349 Internal_type *n = C::next(&o._h);
354 o.self_insert(&o._h);
358 D_list(D_list
const &) =
delete;
359 void operator = (D_list
const &) =
delete;
361 void splice(Const_iterator pos, D_list &&other)
366 Base::splice_heads(pos, &other._h);
367 other.self_insert(&other._h);
370 bool empty()
const {
return C::next(&_h) == &_h; }
372 static void remove(T *e) { Base::remove(e); }
373 Iterator erase(Iterator
const &e) {
return Base::erase(e); }
379 Internal_type *i = C::next(&_h);
382 Internal_type *d = i;
384 C::next(d) =
nullptr;
387 this->self_insert(&_h);
390 void push(T *e, Pos pos)
393 Base::insert_after(e, end());
395 Base::insert_before(e, end());
398 void push_back(T *e) { push(e, Back); }
399 void push_front(T *e) { push(e, Front); }
425 Iterator begin()
const {
return this->__iter(C::next(
const_cast<Internal_type *
>(&_h))); }
426 Iterator end()
const {
return this->__iter(
const_cast<Internal_type *
>(&_h)); }
428 bool has_sibling(T
const *e)
430 return C::next(
const_cast<T*
>(e)) != &_h
431 || C::prev(
const_cast<T*
>(e)) != &_h;