-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlist.h
260 lines (228 loc) · 5.92 KB
/
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
#ifndef RDESTL_LIST_H
#define RDESTL_LIST_H
#include "allocator.h"
#include "iterator.h"
namespace rde
{
namespace internal
{
struct list_base_node
{
list_base_node()
{
#if RDE_DEBUG
reset();
#endif
}
void reset() { next = prev = this; }
bool in_list() const { return this != next; }
void link_before(list_base_node* nextNode);
void unlink();
list_base_node* prev;
list_base_node* next;
};
} // namespace internal
//=============================================================================
template<typename T, class TAllocator = rde::allocator>
class list
{
private:
struct node: public internal::list_base_node
{
node(): list_base_node() {}
explicit node(const T& v): list_base_node(), value(v) {}
T value;
};
static RDE_FORCEINLINE node* upcast(internal::list_base_node* n)
{
return (node*)n;
}
template<typename TNodePtr, typename TPtr, typename TRef>
class node_iterator
{
public:
typedef bidirectional_iterator_tag iterator_category;
explicit node_iterator(): m_node(NULL) { }
explicit node_iterator(TNodePtr node): m_node(node) { }
template<typename UNodePtr, typename UPtr, typename URef>
node_iterator(const node_iterator<UNodePtr, UPtr, URef>& rhs)
: m_node(rhs.node()) { }
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; }
node_iterator& operator++()
{
m_node = upcast(m_node->next);
return *this;
}
node_iterator& operator--()
{
m_node = upcast(m_node->prev);
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;
};
public:
typedef T value_type;
typedef TAllocator allocator_type;
typedef size_t size_type;
typedef node_iterator<node*, T*, T&> iterator;
typedef node_iterator<const node*, const T*, const T&> const_iterator;
static const std::size_t kNodeSize = sizeof(node);
explicit list(const allocator_type& allocator = allocator_type())
: m_allocator(allocator)
{
m_root.reset();
}
template<class InputIterator>
list(InputIterator first, InputIterator last,
const allocator_type& allocator = allocator_type())
: m_allocator(allocator)
{
m_root.reset();
assign(first, last);
}
list(const list& rhs, const allocator_type& allocator = allocator_type())
: m_allocator(allocator)
{
m_root.reset();
assign(rhs.begin(), rhs.end());
}
~list()
{
clear();
}
list& operator=(const list& rhs)
{
if (this != &rhs)
assign(rhs.begin(), rhs.end());
return *this;
}
iterator begin() { return iterator(upcast(m_root.next)); }
const_iterator begin() const { return const_iterator(upcast(m_root.next)); }
iterator end() { return iterator(&m_root); }
const_iterator end() const { return const_iterator(&m_root); }
T& front() { RDE_ASSERT(!empty()); return upcast(m_root.next)->value; }
const T& front() const { RDE_ASSERT(!empty()); return upcast(m_root.next)->value; }
T& back() { RDE_ASSERT(!empty()); return upcast(m_root.prev)->value; }
const T& back() const { RDE_ASSERT(!empty()); return upcast(m_root.prev)->value; }
void push_front(const T& value)
{
node* newNode = construct_node(value);
newNode->link_before(m_root.next);
}
// @pre: !empty()
inline void pop_front()
{
RDE_ASSERT(!empty());
node* frontNode = upcast(m_root.next);
frontNode->unlink();
destruct_node(frontNode);
}
void push_back(const T& value)
{
node* newNode = construct_node(value);
newNode->link_before(&m_root);
}
// @pre: !empty()
inline void pop_back()
{
RDE_ASSERT(!empty());
node* backNode = upcast(m_root.prev);
backNode->unlink();
destruct_node(backNode);
}
iterator insert(iterator pos, const T& value)
{
node* newNode = construct_node(value);
newNode->link_before(pos.node());
return iterator(newNode);
}
iterator erase(iterator it)
{
RDE_ASSERT(it.node()->in_list());
iterator itErase(it);
++it;
itErase.node()->unlink();
destruct_node(itErase.node());
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;
}
}
bool empty() const { return !m_root.in_list(); }
void clear()
{
// quicker then erase(begin(), end())
node* it = upcast(m_root.next);
while (it != &m_root)
{
node* nextIt = upcast(it->next);
destruct_node(it);
it = nextIt;
}
m_root.reset();
}
// @todo: consider keeping size member, would make this O(1)
// as a policy? via preprocessor macro? TBD
size_type size() const
{
const node* it = upcast(m_root.next);
size_type size(0);
while (it != &m_root)
{
++size;
it = upcast(it->next);
}
return size;
}
const allocator_type& get_allocator() const { return m_allocator; }
void set_allocator(const allocator_type& allocator)
{
m_allocator = allocator;
}
private:
node* construct_node(const T& value)
{
void* mem = m_allocator.allocate(sizeof(node));
return new (mem) node(value);
}
void destruct_node(node* n)
{
n->~node();
m_allocator.deallocate(n, sizeof(node));
}
allocator_type m_allocator;
node m_root;
};
} // namespace rde
//-----------------------------------------------------------------------------
#endif // RDESTL_LIST_H