forked from msinilo/rdestl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intrusive_slist.h
196 lines (168 loc) · 4.93 KB
/
intrusive_slist.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
#ifndef RDESTL_INTRUSIVE_SLIST_H
#define RDESTL_INTRUSIVE_SLIST_H
#include "iterator.h"
#include "type_traits.h"
namespace rde
{
//=============================================================================
struct intrusive_slist_node
{
intrusive_slist_node()
{
next = this;
}
bool in_list() const { return this != next; }
intrusive_slist_node* next;
};
//=============================================================================
template<typename Pointer, typename Reference>
class intrusive_slist_iterator
{
public:
typedef Pointer pointer;
typedef Reference reference;
typedef forward_iterator_tag iterator_category;
intrusive_slist_iterator(): m_node(0) { }
explicit intrusive_slist_iterator(Pointer iterNode): m_node(iterNode) { }
Reference operator*() const { RDE_ASSERT(m_node); return *m_node; }
Pointer operator->() const { return m_node; }
Pointer node() const { return m_node; }
intrusive_slist_node* next() const { return m_node->next; }
intrusive_slist_iterator& operator++()
{
m_node = static_cast<Pointer>(m_node->next);
return *this;
}
intrusive_slist_iterator operator++(int)
{
intrusive_slist_iterator copy(*this);
++(*this);
return copy;
}
bool operator==(const intrusive_slist_iterator& rhs) const { return rhs.m_node == m_node; }
bool operator!=(const intrusive_slist_iterator& rhs) const { return !(rhs == *this); }
private:
Pointer m_node;
};
//=============================================================================
class intrusive_slist_base
{
public:
typedef size_t size_type;
intrusive_slist_base();
void pop_front() { unlink_after(&m_root); }
// @note: allow for constant complexity way of checking this
// (at a cost of additional variable)?
size_type size() const;
bool empty() const { return !m_root.in_list(); }
protected:
static void link_after(intrusive_slist_node* node, intrusive_slist_node* prevNode);
static void unlink_after(intrusive_slist_node* node);
intrusive_slist_node m_root;
private:
// prevent copying
intrusive_slist_base(const intrusive_slist_base&);
intrusive_slist_base& operator=(const intrusive_slist_base&);
};
//=============================================================================
// Can store pointers only!
template<class T>
class intrusive_slist: public intrusive_slist_base
{
public:
typedef T node_type;
typedef T value_type;
typedef intrusive_slist_iterator<T*, T&> iterator;
typedef intrusive_slist_iterator<const T*, const T&> const_iterator;
void push_front(value_type* v)
{
link_after(v, &m_root);
}
iterator begin() { return iterator(upcast(m_root.next)); }
const_iterator begin() const { return const_iterator(upcast(m_root.next)); }
iterator end() { return iterator(upcast(&m_root)); }
const_iterator end() const { return const_iterator(upcast(&m_root)); }
value_type* front() { return upcast(m_root.next); }
const value_type* front() const { return upcast(m_root.next); }
// @note use insert_after if possible, it's quicker.
iterator insert(iterator pos, value_type* v)
{
return insert_after(previous(pos), v);
}
iterator insert_after(iterator pos, value_type* v)
{
link_after(v, pos.node());
return iterator(v);
}
// @note use erase_after if possible, it's quicker.
iterator erase(iterator pos)
{
pos = previous(pos);
erase_after(pos);
return ++pos;
}
void erase_after(iterator pos)
{
unlink_after(pos.node());
}
// @return iterator prev such that ++prev == nextIt
static iterator previous(iterator nextIt)
{
RDE_ASSERT(nextIt.node()->in_list());
iterator prevIt = nextIt;
while (nextIt.node() != prevIt.next())
++prevIt;
return prevIt;
}
static const_iterator previous(const_iterator nextIt)
{
RDE_ASSERT(nextIt.node()->in_list());
const_iterator prevIt = nextIt;
while (nextIt.node() != prevIt.next())
++prevIt;
return prevIt;
}
// O(1)
static iterator get_iterator(value_type* v)
{
RDE_ASSERT(v->in_list());
return iterator(v);
}
// O(1)
static const_iterator get_iterator(const value_type* v)
{
RDE_ASSERT(v->in_list());
return const_iterator(v);
}
iterator erase(iterator first, iterator last)
{
if (first != last)
{
while (first.next() != last.node())
erase_after(first);
erase_after(previous(first));
}
return first;
}
void clear()
{
erase(begin(), end());
}
// O(1)
static void remove(value_type* v)
{
unlink(v);
}
private:
static RDE_FORCEINLINE node_type* upcast(intrusive_slist_node* n)
{
return static_cast<node_type*>(n);
}
static RDE_FORCEINLINE const node_type* upcast(const intrusive_slist_node* n)
{
return static_cast<const node_type*>(n);
}
};
} // namespace rde
//-----------------------------------------------------------------------------
#endif // #ifndef RDESTL_INTRUSIVE_SLIST_H