22#include <l4/cxx/type_traits>
23#include <l4/cxx/std_alloc>
24#include <l4/cxx/std_ops>
51 List_item *operator * ()
const noexcept {
return _c; }
52 List_item *operator -> ()
const noexcept {
return _c; }
53 Iter &operator ++ ()
noexcept
66 Iter operator ++ (
int)
noexcept
67 {
Iter o = *
this; operator ++ ();
return o; }
69 Iter &operator -- ()
noexcept
82 Iter operator -- (
int)
noexcept
83 {
Iter o = *
this; operator -- ();
return o; }
118 template<
typename T,
bool Poly = false>
122 static bool const P = !Conversion<const T*, const List_item *>::exists
125 static List_item *cast_to_li(T *i, Int_to_type<true>)
noexcept
128 static List_item *cast_to_li(T *i, Int_to_type<false>)
noexcept
131 static T *cast_to_type(
List_item *i, Int_to_type<true>)
noexcept
132 {
return dynamic_cast<T*
>(i); }
134 static T *cast_to_type(
List_item *i, Int_to_type<false>)
noexcept
135 {
return static_cast<T*
>(i); }
139 template<
typename O >
141 :
Iter(o) {
dynamic_cast<T*
>(*o); }
144 T_iter(T *f = 0) noexcept :
Iter(cast_to_li(f, Int_to_type<P>())) {}
145 T_iter(T *c, T *f) noexcept
146 :
Iter(cast_to_li(c, Int_to_type<P>()),
147 cast_to_li(f, Int_to_type<P>()))
150 inline T *operator * ()
const noexcept
151 {
return cast_to_type(Iter::operator * (),Int_to_type<P>()); }
152 inline T *operator -> ()
const noexcept
153 {
return operator * (); }
160 { Iter::operator ++ ();
return *
this; }
162 { Iter::operator -- ();
return *
this; }
163 inline T *remove_me()
noexcept;
166 List_item() noexcept : _n(this), _p(this) {}
216 template<
typename C,
typename N >
217 static inline C *
push_back(C *head, N *p)
noexcept;
227 template<
typename C,
typename N >
228 static inline C *
push_front(C *head, N *p)
noexcept;
238 template<
typename C,
typename N >
239 static inline C *
remove(C *head, N *p)
noexcept;
247template<
typename C,
typename N >
258template<
typename C,
typename N >
264 h->insert_prev_item(p);
268template<
typename C,
typename N >
280 h =
static_cast<C*
>(p->_n);
287template<
typename T,
bool Poly >
290{
return cast_to_type(Iter::remove_me(), Int_to_type<P>()); }
293template<
typename T >
294class T_list_item :
public List_item
302template<
typename LI >
312 void push_front(LI *e) { _h = LI::push_front(_h, e); }
313 void push_back(LI *e) { _h = LI::push_back(_h, e); }
314 void insert_before(LI *e, LI *p)
316 p->insert_prev_item(e);
320 void insert_after(LI *e, LI *p) { p->insert_next_item(e); }
323 { _h = LI::remove(_h, e); }
325 LI *head()
const {
return _h; }
333template<
typename D,
template<
typename A>
class Alloc = New_allocator >
340 E(D
const &d) noexcept : data(d) {}
345 class Node :
private E
348 typedef Alloc<Node> Node_alloc;
360 Iter(E *e) noexcept : _i(e) {}
362 D &operator * ()
const noexcept {
return (*_i)->data; }
363 D &operator -> ()
const noexcept {
return (*_i)->data; }
365 Iter operator ++ (
int)
noexcept
366 {
Iter o = *
this; operator ++ ();
return o; }
367 Iter operator -- (
int)
noexcept
368 {
Iter o = *
this; operator -- ();
return o; }
369 Iter &operator ++ ()
noexcept { ++_i;
return *
this; }
370 Iter &operator -- ()
noexcept { --_i;
return *
this; }
373 operator E* ()
const noexcept {
return *_i; }
376 List(Alloc<Node>
const &a = Alloc<Node>()) noexcept : _h(0), _l(0), _a(a) {}
381 void *n = _a.alloc();
390 void *n = _a.alloc();
398 { E *e = i; _h =
E::remove(_h, e); --_l; _a.free(e); }
401 unsigned long size() const noexcept {
return _l; }
405 {
Iter i = _h;
for (; idx && *i; ++i, --idx) { }
return *i; }
409 {
Iter i = _h;
for (; idx && *i; ++i, --idx) { }
return *i; }
Iterator for a list of ListItem-s.
List_item * remove_me() noexcept
Remove item pointed to by iterator, and return pointer to element.
Iterator for derived classes from ListItem.
void remove_me() noexcept
Remove this item from the list.
static C * remove(C *head, N *p) noexcept
Remove item from a list.
static C * push_front(C *head, N *p) noexcept
Prepend item to a list.
void insert_next_item(List_item *p) noexcept
Insert item p after this item.
void insert_prev_item(List_item *p) noexcept
Insert item p before this item.
List_item * get_prev_item() const noexcept
Get previous item.
List_item * get_next_item() const noexcept
Get next item.
static C * push_back(C *head, N *p) noexcept
Append item to a list.
Doubly linked list, with internal allocation.
void remove(Iter const &i) noexcept
Remove element pointed to by the iterator.
unsigned long size() const noexcept
Get the length of the list.
void push_front(D const &d) noexcept
Add element at the beginning of the list.
D const & operator[](unsigned long idx) const noexcept
Random access.
Iter items() noexcept
Get iterator for the list elements.
void push_back(D const &d) noexcept
Add element at the end of the list.