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
|
/************************************************************************************
PublicHeader: OVR
Filename : OVR_List.h
Content : Template implementation for doubly-connected linked List
Created : September 19, 2012
Notes :
Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
Licensed under the Oculus VR SDK License Version 2.0 (the "License");
you may not use the Oculus VR SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#ifndef OVR_List_h
#define OVR_List_h
#include "OVR_Types.h"
namespace OVR {
//-----------------------------------------------------------------------------------
// ***** ListNode
//
// Base class for the elements of the intrusive linked list.
// To store elements in the List do:
//
// struct MyData : ListNode<MyData>
// {
// . . .
// };
template<class T>
struct ListNode
{
union {
T* pPrev;
void* pVoidPrev;
};
union {
T* pNext;
void* pVoidNext;
};
void RemoveNode()
{
pPrev->pNext = pNext;
pNext->pPrev = pPrev;
}
// Removes us from the list and inserts pnew there instead.
void ReplaceNodeWith(T* pnew)
{
pPrev->pNext = pnew;
pNext->pPrev = pnew;
pnew->pPrev = pPrev;
pnew->pNext = pNext;
}
// Inserts the argument linked list node after us in the list.
void InsertNodeAfter(T* p)
{
p->pPrev = pNext->pPrev; // this
p->pNext = pNext;
pNext->pPrev = p;
pNext = p;
}
// Inserts the argument linked list node before us in the list.
void InsertNodeBefore(T* p)
{
p->pNext = pNext->pPrev; // this
p->pPrev = pPrev;
pPrev->pNext = p;
pPrev = p;
}
void Alloc_MoveTo(ListNode<T>* pdest)
{
pdest->pNext = pNext;
pdest->pPrev = pPrev;
pPrev->pNext = (T*)pdest;
pNext->pPrev = (T*)pdest;
}
};
//------------------------------------------------------------------------
// ***** List
//
// Doubly linked intrusive list.
// The data type must be derived from ListNode.
//
// Adding: PushFront(), PushBack().
// Removing: Remove() - the element must be in the list!
// Moving: BringToFront(), SendToBack() - the element must be in the list!
//
// Iterating:
// MyData* data = MyList.GetFirst();
// while (!MyList.IsNull(data))
// {
// . . .
// data = MyList.GetNext(data);
// }
//
// Removing:
// MyData* data = MyList.GetFirst();
// while (!MyList.IsNull(data))
// {
// MyData* next = MyList.GetNext(data);
// if (ToBeRemoved(data))
// MyList.Remove(data);
// data = next;
// }
//
// List<> represents a doubly-linked list of T, where each T must derive
// from ListNode<B>. B specifies the base class that was directly
// derived from ListNode, and is only necessary if there is an intermediate
// inheritance chain.
template<class T, class B = T> class List
{
public:
typedef T ValueType;
List()
{
Root.pNext = Root.pPrev = (ValueType*)&Root;
}
void Clear()
{
Root.pNext = Root.pPrev = (ValueType*)&Root;
}
const ValueType* GetFirst() const { return (const ValueType*)Root.pNext; }
const ValueType* GetLast () const { return (const ValueType*)Root.pPrev; }
ValueType* GetFirst() { return (ValueType*)Root.pNext; }
ValueType* GetLast () { return (ValueType*)Root.pPrev; }
// Determine if list is empty (i.e.) points to itself.
// Go through void* access to avoid issues with strict-aliasing optimizing out the
// access after RemoveNode(), etc.
bool IsEmpty() const { return Root.pVoidNext == (const T*)(const B*)&Root; }
bool IsFirst(const ValueType* p) const { return p == Root.pNext; }
bool IsLast (const ValueType* p) const { return p == Root.pPrev; }
bool IsNull (const ValueType* p) const { return p == (const T*)(const B*)&Root; }
inline static const ValueType* GetPrev(const ValueType* p) { return (const ValueType*)p->pPrev; }
inline static const ValueType* GetNext(const ValueType* p) { return (const ValueType*)p->pNext; }
inline static ValueType* GetPrev( ValueType* p) { return (ValueType*)p->pPrev; }
inline static ValueType* GetNext( ValueType* p) { return (ValueType*)p->pNext; }
void PushFront(ValueType* p)
{
p->pNext = Root.pNext;
p->pPrev = (ValueType*)&Root;
Root.pNext->pPrev = p;
Root.pNext = p;
}
void PushBack(ValueType* p)
{
p->pPrev = Root.pPrev;
p->pNext = (ValueType*)&Root;
Root.pPrev->pNext = p;
Root.pPrev = p;
}
static void Remove(ValueType* p)
{
p->pPrev->pNext = p->pNext;
p->pNext->pPrev = p->pPrev;
}
void BringToFront(ValueType* p)
{
Remove(p);
PushFront(p);
}
void SendToBack(ValueType* p)
{
Remove(p);
PushBack(p);
}
// Appends the contents of the argument list to the front of this list;
// items are removed from the argument list.
void PushListToFront(List<T>& src)
{
if (!src.IsEmpty())
{
ValueType* pfirst = src.GetFirst();
ValueType* plast = src.GetLast();
src.Clear();
plast->pNext = Root.pNext;
pfirst->pPrev = (ValueType*)&Root;
Root.pNext->pPrev = plast;
Root.pNext = pfirst;
}
}
void PushListToBack(List<T>& src)
{
if (!src.IsEmpty())
{
ValueType* pfirst = src.GetFirst();
ValueType* plast = src.GetLast();
src.Clear();
plast->pNext = (ValueType*)&Root;
pfirst->pPrev = Root.pPrev;
Root.pPrev->pNext = pfirst;
Root.pPrev = plast;
}
}
// Removes all source list items after (and including) the 'pfirst' node from the
// source list and adds them to out list.
void PushFollowingListItemsToFront(List<T>& src, ValueType *pfirst)
{
if (pfirst != &src.Root)
{
ValueType *plast = src.Root.pPrev;
// Remove list remainder from source.
pfirst->pPrev->pNext = (ValueType*)&src.Root;
src.Root.pPrev = pfirst->pPrev;
// Add the rest of the items to list.
plast->pNext = Root.pNext;
pfirst->pPrev = (ValueType*)&Root;
Root.pNext->pPrev = plast;
Root.pNext = pfirst;
}
}
// Removes all source list items up to but NOT including the 'pend' node from the
// source list and adds them to out list.
void PushPrecedingListItemsToFront(List<T>& src, ValueType *ptail)
{
if (src.GetFirst() != ptail)
{
ValueType *pfirst = src.Root.pNext;
ValueType *plast = ptail->pPrev;
// Remove list remainder from source.
ptail->pPrev = (ValueType*)&src.Root;
src.Root.pNext = ptail;
// Add the rest of the items to list.
plast->pNext = Root.pNext;
pfirst->pPrev = (ValueType*)&Root;
Root.pNext->pPrev = plast;
Root.pNext = pfirst;
}
}
// Removes a range of source list items starting at 'pfirst' and up to, but not including 'pend',
// and adds them to out list. Note that source items MUST already be in the list.
void PushListItemsToFront(ValueType *pfirst, ValueType *pend)
{
if (pfirst != pend)
{
ValueType *plast = pend->pPrev;
// Remove list remainder from source.
pfirst->pPrev->pNext = pend;
pend->pPrev = pfirst->pPrev;
// Add the rest of the items to list.
plast->pNext = Root.pNext;
pfirst->pPrev = (ValueType*)&Root;
Root.pNext->pPrev = plast;
Root.pNext = pfirst;
}
}
void Alloc_MoveTo(List<T>* pdest)
{
if (IsEmpty())
pdest->Clear();
else
{
pdest->Root.pNext = Root.pNext;
pdest->Root.pPrev = Root.pPrev;
Root.pNext->pPrev = (ValueType*)&pdest->Root;
Root.pPrev->pNext = (ValueType*)&pdest->Root;
}
}
private:
// Copying is prohibited
List(const List<T>&);
const List<T>& operator = (const List<T>&);
ListNode<B> Root;
};
//------------------------------------------------------------------------
// ***** FreeListElements
//
// Remove all elements in the list and free them in the allocator
template<class List, class Allocator>
void FreeListElements(List& list, Allocator& allocator)
{
typename List::ValueType* self = list.GetFirst();
while(!list.IsNull(self))
{
typename List::ValueType* next = list.GetNext(self);
allocator.Free(self);
self = next;
}
list.Clear();
}
} // OVR
#endif
|