L4Re Operating System Framework
Interface and Usage Documentation
•All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dlist
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
5 *
6 * License: see LICENSE.spdx (in this directory or the directories above)
7 */
8
9#pragma once
10
11namespace cxx {
12
13class D_list_item
14{
15public:
16 D_list_item() : _dli_next(0) {}
17private:
18 friend class D_list_item_policy;
19
20 D_list_item(D_list_item const &);
21 void operator = (D_list_item const &);
22
23 D_list_item *_dli_next, *_dli_prev;
24};
25
26class D_list_item_policy
27{
28public:
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; }
34};
35
36template< typename T >
37struct Sd_list_head_policy
38{
39 typedef T *Head_type;
40 static T *head(Head_type h) { return h; }
41 static void set_head(Head_type &h, T *v) { h = v; }
42};
43
44template<
45 typename T,
46 typename C = D_list_item_policy
47>
48class D_list_cyclic
49{
50protected:
51 template< typename VALUE, typename ITEM >
52 class __Iterator
53 {
54 public:
55 typedef VALUE *Value_type;
56 typedef VALUE *value_type;
57
58 __Iterator() {}
59
60 bool operator == (__Iterator const &o) const
61 { return _c == o._c; }
62
63 bool operator != (__Iterator const &o) const
64 { return _c != o._c; }
65
66 __Iterator &operator ++ ()
67 {
68 _c = C::next(_c);
69 return *this;
70 }
71
72 __Iterator &operator -- ()
73 {
74 _c = C::prev(_c);
75 return *this;
76 }
77
78 Value_type operator * () const { return static_cast<Value_type>(_c); }
79 Value_type operator -> () const { return static_cast<Value_type>(_c); }
80
81 private:
82 friend class D_list_cyclic;
83
84 explicit __Iterator(ITEM *s) : _c(s) {}
85
86 ITEM *_c;
87 };
88
89public:
90 typedef T *Value_type;
91 typedef T *value_type;
92 typedef __Iterator<T, typename C::Item> Iterator;
93 typedef Iterator Const_iterator;
94
95 static void remove(T *e)
96 {
97 C::next(C::prev(e)) = C::next(e);
98 C::prev(C::next(e)) = C::prev(e);
99 C::next(e) = 0;
100 }
101
102 static Iterator erase(Iterator const &e)
103 {
104 typename C::Item *n = C::next(*e);
105 remove(*e);
106 return __iter(n);
107 }
108
109 static Iterator iter(T const *e) { return Iterator(const_cast<T*>(e)); }
110
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; }
113
114 static Iterator insert_after(T *e, Iterator const &pos)
115 {
116 C::prev(e) = *pos;
117 C::next(e) = C::next(*pos);
118 C::prev(C::next(*pos)) = e;
119 C::next(*pos) = e;
120 return pos;
121 }
122
123 static Iterator insert_before(T *e, Iterator const &pos)
124 {
125 C::next(e) = *pos;
126 C::prev(e) = C::prev(*pos);
127 C::next(C::prev(*pos)) = e;
128 C::prev(*pos) = e;
129 return pos;
130 }
131
132 static T *self_insert(T *e)
133 { C::next(e) = C::prev(e) = e; return e; }
134
135 static void remove_last(T *e)
136 { C::next(e) = 0; }
137
138protected:
139 static Iterator __iter(typename C::Item *e) { return Iterator(e); }
140};
141
142template<
143 typename T,
144 typename C = D_list_item_policy,
145 typename H = Sd_list_head_policy<T>,
146 bool BSS = false
147>
148class Sd_list : public D_list_cyclic<T, C>
149{
150private:
151 typedef D_list_cyclic<T, C> Base;
152
153public:
154 typedef typename Base::Iterator Iterator;
155 enum Pos
156 { Back, Front };
157
158 Sd_list() { if (!BSS) H::set_head(_f, 0); }
159
160 bool empty() const { return !H::head(_f); }
161 T *front() const { return H::head(_f); }
162
163 void remove(T *e)
164 {
165 T *h = H::head(_f);
166 if (e == C::next(e)) // must be the last
167 {
168 Base::remove_last(e);
169 H::set_head(_f, 0);
170 return;
171 }
172
173 if (e == H::head(_f))
174 H::set_head(_f, static_cast<T*>(C::next(h)));
175
176 Base::remove(e);
177 }
178
179 Iterator erase(Iterator const &e)
180 {
181 typename C::Item *n = C::next(*e);
182 remove(*e);
183 return __iter(n);
184 }
185
186 void push(T *e, Pos pos)
187 {
188 T *h = H::head(_f);
189 if (!h)
190 H::set_head(_f, Base::self_insert(e));
191 else
192 {
193 Base::insert_before(e, this->iter(h));
194 if (pos == Front)
195 H::set_head(_f, e);
196 }
197 }
198
199 void push_back(T *e) { push(e, Back); }
200 void push_front(T *e) { push(e, Front); }
201 void rotate_to(T *h) { H::set_head(_f, h); }
202
203 typename H::Head_type const &head() const { return _f; }
204 typename H::Head_type &head() { return _f; }
205
206private:
207 Sd_list(Sd_list const &);
208 void operator = (Sd_list const &);
209
210 typename H::Head_type _f;
211};
212
213template<
214 typename T,
215 typename C = D_list_item_policy,
216 bool BSS = false
217>
218class D_list : public D_list_cyclic<T, C>
219{
220private:
221 typedef D_list_cyclic<T, C> Base;
222 typedef typename C::Item Internal_type;
223
224public:
225 enum Pos
226 { Back, Front };
227
228 typedef typename Base::Iterator Iterator;
229 typedef typename Base::Const_iterator Const_iterator;
230 typedef T* value_type;
231 typedef T* Value_type;
232
233 D_list() { this->self_insert(static_cast<T*>(&_h)); }
234
235 bool empty() const { return C::next(static_cast<T const *>(&_h)) == &_h; }
236
237 static void remove(T *e) { Base::remove(e); }
238 Iterator erase(Iterator const &e) { return Base::erase(e); }
239
240 void push(T *e, Pos pos)
241 {
242 if (pos == Front)
243 Base::insert_after(e, end());
244 else
245 Base::insert_before(e, end());
246 }
247
248 void push_back(T *e) { push(e, Back); }
249 void push_front(T *e) { push(e, Front); }
250
251 Iterator begin() const { return this->__iter(C::next(const_cast<Internal_type *>(&_h))); }
252 Iterator end() const { return this->__iter(const_cast<Internal_type *>(&_h)); }
253
254private:
255 D_list(D_list const &);
256 void operator = (D_list const &);
257
258 Internal_type _h;
259};
260
261}
262
Our C++ library.
Definition arith:11