-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdlist.h
169 lines (138 loc) · 5.13 KB
/
dlist.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
/**
*
* Copyright (c) 2010-2015 Voidware Ltd. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code as
* defined in and that are subject to the Voidware Public Source Licence version
* 1.0 (the 'Licence'). You may not use this file except in compliance with the
* Licence or with expressly written permission from Voidware. Please obtain a
* copy of the Licence at http://www.voidware.com/legal/vpsl1.txt and read it
* before using this file.
*
* The Original Code and all software distributed under the Licence are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
* OR IMPLIED, AND VOIDWARE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING
* WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Please see the Licence for the specific language governing rights and
* limitations under the Licence.
*
*/
#ifndef __dlist_h__
#define __dlist_h__
namespace Utils
{
/* Forward Decls */
struct DList;
struct DListRec
{
/* Link element class.
* This mixes in the necessary pointers for the dlist when
* inherited.
*/
friend struct DList;
// Constructors
DListRec() { init(); }
// Accessors
DListRec* next() const { return next_; }
DListRec* previous() const { return previous_; }
bool orphan() const { return next_ == this; }
protected:
void init() { reset(); }
void reset() { next_ = this; previous_ = this; }
void remove()
{
previous_->next_ = next_;
next_->previous_ = previous_;
/* Tidy up our next and previous pointers
* to ourself. This protects against remove
* when not already in a list.
*/
reset();
}
void insert(DListRec* rec)
{
next_ = rec;
previous_ = rec->previous_;
previous_->next_ = this;
next_->previous_ = this;
}
void removeAndInsert(DListRec* rec)
{
previous_->next_ = next_;
next_->previous_ = previous_;
insert(rec);
}
private:
DListRec* next_;
DListRec* previous_;
};
struct DList
{
// Constructors
DList() { init(); }
// Destructor
~DList() { empty(); }
// Accessors
DListRec* first() const { return list_; }
DListRec* last() const
{ return list_ ? list_->previous() : 0; }
// Features
void append(DListRec* rec);
void insert(DListRec* rec);
void insertAt(DListRec* rec, DListRec* atRec)
{
rec->removeAndInsert(atRec);
if (list_ == atRec) list_ = rec;
}
void add(DListRec* rec) { append(rec); }
unsigned int size() const;
void remove(DListRec* rec) { extract(rec); }
void extract(DListRec *rec);
void empty() { list_ = 0; }
bool member(DListRec*) const;
bool isEmpty() const { return list_ == 0; }
DListRec* extractFirst();
protected:
void init() { list_ = 0; }
private:
DListRec* list_;
};
struct DListIterator: public DListRec
{
// Constructors
DListIterator() { init(); }
DListIterator(const DList& list);
DListIterator(const DListIterator& i)
{
init();
*this = i;
}
// Destructor
~DListIterator() {}
// Features
DListIterator& operator=(const DListIterator&);
DListIterator& operator++();
DListIterator& operator--();
DListRec* operator*() const { return pos_; }
void remove();
void extract();
void insert(DListRec*);
DListIterator& find(const DListRec*);
void invalidate() { pos_ = 0; }
void reset() { pos_ = 0; operator++(); }
int operator==(DListIterator& i) const
{ return pos_ == i.pos_; }
protected:
void init()
{
pos_ = 0;
list_ = 0;
}
DList* list_;
DListRec* pos_;
};
};
#endif /* __dlist_h__ */