-
Notifications
You must be signed in to change notification settings - Fork 6
/
List.h
489 lines (417 loc) · 13.8 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/******************************************************************************
*
* List.h
*
*
* By Patrick Wyatt - 5/16/2010
*
***/
/******************************************************************************
*
* WHAT IT IS
*
* This module defines a linked-list implementation that uses "embedded"
* links rather than separately allocated link-nodes as does STL and
* more or less all other linked-list implementations.
*
* Why is this cool:
* 1. No additional memory allocations (malloc) required to link
* an object into a linked list.
* 2. Not necessary to traverse an additional pointer references
* to get to the object being dereferenced.
* 3. Probably most importantly, when objects get deleted, they
* automatically unlink themselves from the lists they're
* linked to, eliminating many common types of bugs.
*
* HOW TO USE IT
*
* Declare a structure that will be contained in one or more linked lists:
* class CFoo {
* LIST_LINK(CFoo) m_linkByData;
* LIST_LINK(CFoo) m_linkByType;
* int m_data;
* int m_type;
* ...
* };
*
* Declare list variables:
* LIST_DECLARE(CFoo, m_linkByData) listByData;
* LIST_DECLARE(CFoo, m_linkByType) listByType;
* LIST_PTR(CFoo) m_listPtr = foo ? &listByData : &listByType;
*
* Operations on links:
* T * Prev ();
* T * Next ();
* void Unlink ();
* bool IsLinked () const;
*
* Operations on lists:
* bool Empty () const;
* void UnlinkAll ();
* void DeleteAll ();
*
* T * Head ();
* T * Tail ();
* T * Prev (T * node);
* T * Next (T * node);
*
* void InsertHead (T * node);
* void InsertTail (T * node);
* void InsertBefore (T * node, T * before);
* void InsertAfter (T * node, T * after);
*
* NOTES
*
* Limitations:
* All nodes must be allocated on (at least) two-byte boundaries
* because the low bit is used as a sentinel to signal end-of-list.
*
* Thanks to:
* Something like this code was originally implemented by Mike
* O'Brien in storm.dll for Diablo in 1995, and again at ArenaNet
* for Guild Wars.
*
***/
#ifdef LIST_H
#error "Header included more than once"
#endif
#define LIST_H
/******************************************************************************
*
* List definition macros
*
***/
// Define a linked list:
// T = type of object being linked
// link = member within object which is the link field
#define LIST_DECLARE(T, link) TListDeclare<T, offsetof(T, link)>
// Define a field within a structure that will be used to link it into a list
#define LIST_LINK(T) TLink<T>
// Define a pointer to a list
#define LIST_PTR(T) TList<T> *
/******************************************************************************
*
* TLink
*
***/
//=============================================================================
template<class T>
class TLink {
public:
~TLink ();
TLink ();
bool IsLinked () const;
void Unlink ();
T * Prev ();
T * Next ();
const T * Prev () const;
const T * Next () const;
// For use by list-type classes, not user code;
// the alternative is to friend TList<T>, THash<T>,
// and (eventually) many other structures.
TLink (size_t offset);
void SetOffset (size_t offset);
TLink<T> * NextLink ();
TLink<T> * PrevLink ();
void InsertBefore (T * node, TLink<T> * nextLink);
void InsertAfter (T * node, TLink<T> * prevLink);
private:
T * m_nextNode; // pointer to the next >object<
TLink<T> * m_prevLink; // pointer to the previous >link field<
void RemoveFromList ();
// Hide copy-constructor and assignment operator
TLink (const TLink &);
TLink & operator= (const TLink &);
};
//=============================================================================
template<class T>
TLink<T>::~TLink () {
RemoveFromList();
}
//=============================================================================
template<class T>
TLink<T>::TLink () {
// Mark this node as the end of the list, with no link offset
m_nextNode = (T *) ((size_t) this + 1 - 0);
m_prevLink = this;
}
//=============================================================================
template<class T>
TLink<T>::TLink (size_t offset) {
// Mark this node as the end of the list, with the link offset set
m_nextNode = (T *) ((size_t) this + 1 - offset);
m_prevLink = this;
}
//=============================================================================
template<class T>
void TLink<T>::SetOffset (size_t offset) {
// Mark this node as the end of the list, with the link offset set
m_nextNode = (T *) ((size_t) this + 1 - offset);
m_prevLink = this;
}
//=============================================================================
template<class T>
TLink<T> * TLink<T>::NextLink () {
// Calculate the offset from a node pointer to a link structure
size_t offset = (size_t) this - ((size_t) m_prevLink->m_nextNode & ~1);
// Get the link field for the next node
return (TLink<T> *) (((size_t) m_nextNode & ~1) + offset);
}
//=============================================================================
template<class T>
void TLink<T>::RemoveFromList () {
NextLink()->m_prevLink = m_prevLink;
m_prevLink->m_nextNode = m_nextNode;
}
//=============================================================================
template<class T>
void TLink<T>::InsertBefore (T * node, TLink<T> * nextLink) {
RemoveFromList();
m_prevLink = nextLink->m_prevLink;
m_nextNode = m_prevLink->m_nextNode;
nextLink->m_prevLink->m_nextNode = node;
nextLink->m_prevLink = this;
}
//=============================================================================
template<class T>
void TLink<T>::InsertAfter (T * node, TLink<T> * prevLink) {
RemoveFromList();
m_prevLink = prevLink;
m_nextNode = prevLink->m_nextNode;
prevLink->NextLink()->m_prevLink = this;
prevLink->m_nextNode = node;
}
//=============================================================================
template<class T>
bool TLink<T>::IsLinked () const {
return m_prevLink != this;
}
//=============================================================================
template<class T>
void TLink<T>::Unlink () {
RemoveFromList();
// Mark this node as the end of the list with no link offset
m_nextNode = (T *) ((size_t) this + 1);
m_prevLink = this;
}
//=============================================================================
template<class T>
TLink<T> * TLink<T>::PrevLink () {
return m_prevLink;
}
//=============================================================================
template<class T>
T * TLink<T>::Prev () {
T * prevNode = m_prevLink->m_prevLink->m_nextNode;
if ((size_t) prevNode & 1)
return NULL;
return prevNode;
}
//=============================================================================
template<class T>
const T * TLink<T>::Prev () const {
const T * prevNode = m_prevLink->m_prevLink->m_nextNode;
if ((size_t) prevNode & 1)
return NULL;
return prevNode;
}
//=============================================================================
template<class T>
T * TLink<T>::Next () {
if ((size_t) m_nextNode & 1)
return NULL;
return m_nextNode;
}
//=============================================================================
template<class T>
const T * TLink<T>::Next () const {
if ((size_t) m_nextNode & 1)
return NULL;
return m_nextNode;
}
/******************************************************************************
*
* TList
*
***/
//=============================================================================
template<class T>
class TList {
public:
~TList ();
TList ();
bool Empty () const;
void UnlinkAll ();
void DeleteAll ();
T * Head ();
T * Tail ();
const T * Head () const;
const T * Tail () const;
T * Prev (T * node);
T * Next (T * node);
const T * Prev (const T * node) const;
const T * Next (const T * node) const;
void InsertHead (T * node);
void InsertTail (T * node);
void InsertBefore (T * node, T * before);
void InsertAfter (T * node, T * after);
private:
TLink<T> m_link;
size_t m_offset;
TList (size_t offset);
TLink<T> * GetLinkFromNode (const T * node) const;
template<class T, size_t offset> friend class TListDeclare;
// Hide copy-constructor and assignment operator
TList (const TList &);
TList & operator= (const TList &);
};
//=============================================================================
template<class T>
TList<T>::~TList () {
UnlinkAll();
}
//=============================================================================
template<class T>
TList<T>::TList () :
m_link(),
m_offset((size_t) -1)
{}
//=============================================================================
template<class T>
TList<T>::TList (size_t offset) :
m_link(offset),
m_offset(offset)
{}
//=============================================================================
template<class T>
bool TList<T>::Empty () const {
return m_link.Next() == NULL;
}
//=============================================================================
template<class T>
void TList<T>::UnlinkAll () {
for (;;) {
TLink<T> * link = m_link.PrevLink();
if (link == &m_link)
break;
link->Unlink();
}
}
//=============================================================================
template<class T>
void TList<T>::DeleteAll () {
while (T * node = m_link.Next())
delete node;
}
//=============================================================================
template<class T>
T * TList<T>::Head () {
return m_link.Next();
}
//=============================================================================
template<class T>
T * TList<T>::Tail () {
return m_link.Prev();
}
//=============================================================================
template<class T>
const T * TList<T>::Head () const {
return m_link.Next();
}
//=============================================================================
template<class T>
const T * TList<T>::Tail () const {
return m_link.Prev();
}
//=============================================================================
template<class T>
T * TList<T>::Prev (T * node) {
return GetLinkFromNode(node)->Prev();
}
//=============================================================================
template<class T>
const T * TList<T>::Prev (const T * node) const {
return GetLinkFromNode(node)->Prev();
}
//=============================================================================
template<class T>
T * TList<T>::Next (T * node) {
return GetLinkFromNode(node)->Next();
}
//=============================================================================
template<class T>
const T * TList<T>::Next (const T * node) const {
return GetLinkFromNode(node)->Next();
}
//=============================================================================
template<class T>
void TList<T>::InsertHead (T * node) {
InsertAfter(node, NULL);
}
//=============================================================================
template<class T>
void TList<T>::InsertTail (T * node) {
InsertBefore(node, NULL);
}
//=============================================================================
template<class T>
void TList<T>::InsertBefore (T * node, T * before) {
ASSERT(!((size_t) node & 1));
GetLinkFromNode(node)->InsertBefore(
node,
before ? GetLinkFromNode(before) : &m_link
);
}
//=============================================================================
template<class T>
void TList<T>::InsertAfter (T * node, T * after) {
ASSERT(!((size_t) node & 1));
GetLinkFromNode(node)->InsertAfter(
node,
after ? GetLinkFromNode(after) : &m_link
);
}
//=============================================================================
template<class T>
TLink<T> * TList<T>::GetLinkFromNode (const T * node) const {
ASSERT(m_offset != (size_t) -1);
return (TLink<T> *) ((size_t) node + m_offset);
}
/******************************************************************************
*
* TListDeclare - declare a list with a known link offset
*
***/
//=============================================================================
template<class T, size_t offset>
class TListDeclare : public TList<T> {
public:
TListDeclare ();
};
//=============================================================================
template<class T, size_t offset>
TListDeclare<T, offset>::TListDeclare () : TList<T>(offset)
{}
//===================================
// MIT License
//
// Copyright (c) 2010 by Patrick Wyatt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//===================================