forked from msinilo/rdestl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_list.h
353 lines (323 loc) · 8.06 KB
/
fixed_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#ifndef RDESTL_FIXED_LIST_H
#define RDESTL_FIXED_LIST_H
#include "alignment.h"
#include "int_to_type.h"
#include "iterator.h"
#include "type_traits.h"
namespace rde
{
template<typename T, size_t TCapacity>
class fixed_list
{
// Selects index_type that's "big" enough
// to hold TCapacity of objects.
template<int i>
struct type_selector
{
typedef unsigned char index_type;
};
template<>
struct type_selector<1>
{
typedef unsigned short index_type;
};
template<>
struct type_selector<2>
{
typedef unsigned long index_type;
};
enum { select = TCapacity + 1 > 255 ? (TCapacity + 1 > 65535 ? 2 : 1) : 0 };
typedef type_selector<select> index_type_selector;
public:
typedef typename index_type_selector::index_type index_type;
private:
struct node
{
node() {}
explicit node(const T& v, int i): value(v), index((index_type)i) {}
void reset()
{
index = index_next = index_prev = 0;
}
bool in_list() const { return index != index_next; }
T value;
index_type index;
index_type index_next;
index_type index_prev;
};
template<typename TNodePtr, typename TPtr, typename TRef>
class node_iterator
{
public:
typedef bidirectional_iterator_tag iterator_category;
explicit node_iterator(TNodePtr node, const fixed_list* list)
: m_node(node), m_list(list) { /**/ }
template<typename UNodePtr, typename UPtr, typename URef>
node_iterator(const node_iterator<UNodePtr, UPtr, URef>& rhs)
: m_node(rhs.node()),
m_list(rhs.list())
{
/**/
}
TRef operator*() const
{
RDE_ASSERT(m_node != 0);
return m_node->value;
}
TPtr operator->() const
{
return &m_node->value;
}
TNodePtr node() const
{
return m_node;
}
const fixed_list* list() const
{
return m_list;
}
node_iterator& operator++()
{
m_node = m_list->get_next(m_node);
return *this;
}
node_iterator& operator--()
{
m_node = m_list->get_prev(m_node);
return *this;
}
node_iterator operator++(int)
{
node_iterator copy(*this);
++(*this);
return copy;
}
node_iterator operator--(int)
{
node_iterator copy(*this);
--(*this);
return copy;
}
bool operator==(const node_iterator& rhs) const
{
return rhs.m_node == m_node;
}
bool operator!=(const node_iterator& rhs) const
{
return !(rhs == *this);
}
private:
TNodePtr m_node;
const fixed_list* m_list;
};
public:
typedef T value_type;
typedef size_t size_type;
typedef node_iterator<node*, T*, T&> iterator;
typedef node_iterator<const node*, const T*, const T&> const_iterator;
fixed_list()
: m_num_nodes(0)
{
init_free_indices();
get_root()->reset();
}
template<class InputIterator>
fixed_list(InputIterator first, InputIterator last)
{
get_root()->reset();
assign(first, last);
}
fixed_list(const fixed_list& rhs)
{
get_root()->reset();
copy(rhs,
int_to_type<has_trivial_copy<T>::value>());
}
explicit fixed_list(e_noinitialize)
{
/**/
}
~fixed_list()
{
clear();
}
fixed_list& operator=(const fixed_list& rhs)
{
if (this != &rhs)
{
copy(rhs,
int_to_type<has_trivial_copy<T>::value>());
}
return *this;
}
iterator begin() { return iterator(get_next(get_root()), this); }
const_iterator begin() const { return const_iterator(get_next(get_root()), this); }
iterator end() { return iterator(get_root(), this); }
const_iterator end() const { return const_iterator(get_root(), this); }
T& front() { RDE_ASSERT(!empty()); return get_next(get_root())->value; }
const T& front() const { RDE_ASSERT(!empty()); return get_next(get_root())->value; }
T& back() { RDE_ASSERT(!empty()); return get_prev(get_root())->value; }
const T& back() const { RDE_ASSERT(!empty()); return get_prev(get_root())->value; }
void push_front(const T& value)
{
RDE_ASSERT(m_free_index_top > 0); // 0 is root
const int index = m_free_indices[m_free_index_top--];
node* new_node = construct_node(value, index);
++m_num_nodes;
RDE_ASSERT(m_num_nodes <= TCapacity);
link_before(new_node, get_next(get_root()));
}
// @pre: !empty()
inline void pop_front()
{
RDE_ASSERT(!empty());
node* front_node = get_next(get_root());
m_free_indices[++m_free_index_top] = front_node->index;
unlink(front_node);
destruct_node(front_node);
--m_num_nodes;
}
void push_back(const T& value)
{
RDE_ASSERT(m_free_index_top > 0); // 0 is root
const int index = m_free_indices[m_free_index_top--];
node* new_node = construct_node(value, index);
++m_num_nodes;
RDE_ASSERT(m_num_nodes <= TCapacity + 1);
link_before(new_node, get_root());
}
// @pre: !empty()
inline void pop_back()
{
RDE_ASSERT(!empty());
node* back_node = get_prev(get_root());
m_free_indices[++m_free_index_top] = back_node->index;
unlink(back_node);
destruct_node(back_node);
--m_num_nodes;
}
iterator insert(iterator pos, const T& value)
{
RDE_ASSERT(m_free_index_top > 0);
const int index = m_free_indices[m_free_index_top--];
node* new_node = construct_node(value, index);
link_before(new_node, pos.node());
++m_num_nodes;
return iterator(new_node, this);
}
// @pre: !empty()
iterator erase(iterator it)
{
RDE_ASSERT(!empty());
RDE_ASSERT(it.node()->in_list());
RDE_ASSERT(it.list() == this);
iterator it_erase(it);
++it;
node* erase_node = it_erase.node();
m_free_indices[++m_free_index_top] = erase_node->index;
unlink(erase_node);
destruct_node(erase_node);
--m_num_nodes;
return it;
}
iterator erase(iterator first, iterator last)
{
while (first != last)
first = erase(first);
return first;
}
template<class InputIterator>
void assign(InputIterator first, InputIterator last)
{
clear();
while (first != last)
{
push_back(*first);
++first;
}
}
void clear()
{
// Quicker than erase(begin(), end()), doesnt have
// to unlink all the nodes.
node* root = get_root();
node* iter = get_next(root);
while (iter != root)
{
node* next = get_next(iter);
destruct_node(iter);
iter = next;
}
root->reset();
m_num_nodes = 0;
init_free_indices();
}
bool empty() const { return m_num_nodes == 0; }
size_type size() const
{
return m_num_nodes;
}
private:
void link_before(node* n, node* next)
{
n->index_prev = next->index_prev;
get_prev(n)->index_next = n->index;
next->index_prev = n->index;
n->index_next = next->index;
}
void unlink(node* n)
{
get_prev(n)->index_next = n->index_next;
get_next(n)->index_prev = n->index_prev;
n->index_next = n->index_prev = n->index;
}
RDE_FORCEINLINE node* get_prev(const node* n) const
{
return get_nodes() + n->index_prev;
}
RDE_FORCEINLINE node* get_next(const node* n) const
{
return get_nodes() + n->index_next;
}
node* construct_node(const T& value, int index)
{
node* mem = get_nodes() + index;
return new (mem) node(value, index);
}
void destruct_node(node* n)
{
(void)n;
n->~node();
}
void init_free_indices()
{
for (int i = 0; i < TCapacity + 1; ++i)
m_free_indices[i] = (index_type)i;
m_free_index_top = TCapacity;
}
RDE_FORCEINLINE node* get_nodes() const
{
return (node*)(&m_node_mem[0]);
}
void copy(const fixed_list& rhs, int_to_type<true>)
{
Sys::MemCpy(this, &rhs, sizeof(rhs));
}
void copy(const fixed_list& rhs, int_to_type<false>)
{
assign(rhs.begin(), rhs.end());
}
RDE_FORCEINLINE node* get_root() const
{
// I'll be nice and inline it for the compiler (=get_nodes()).
return (node*)(&m_node_mem[0]);
}
typedef typename aligned_as<node>::res etype_t;
etype_t m_node_mem[sizeof(node) * (TCapacity + 1) / sizeof(etype_t)];
index_type m_free_indices[TCapacity + 1];
int m_free_index_top;
size_type m_num_nodes; // Excluding root!
};
} // namespace rde
//-----------------------------------------------------------------------------
#endif // #ifndef RDESTL_FIXED_LIST_H