28
28
#include " utilities/debug.hpp"
29
29
30
30
template <typename T>
31
- inline ZListNode<T>::ZListNode(ZListNode* next, ZListNode* prev ) :
32
- _next (next ),
33
- _prev(prev ) {}
31
+ inline ZListNode<T>::ZListNode() :
32
+ _next (this ),
33
+ _prev(this ) {}
34
34
35
35
template <typename T>
36
- inline void ZListNode<T>::set_unused() {
37
- _next = NULL ;
38
- _prev = NULL ;
36
+ inline ZListNode<T>::~ZListNode () {
37
+ verify_links_unlinked ();
39
38
}
40
39
41
40
template <typename T>
42
- inline ZListNode<T>::ZListNode() {
43
- set_unused ();
41
+ inline void ZListNode<T>::verify_links() const {
42
+ assert (_next->_prev == this , " Corrupt list node" );
43
+ assert (_prev->_next == this , " Corrupt list node" );
44
44
}
45
45
46
46
template <typename T>
47
- inline ZListNode<T>::~ZListNode () {
48
- set_unused ();
47
+ inline void ZListNode<T>::verify_links_linked() const {
48
+ assert (_next != this , " Should be in a list" );
49
+ assert (_prev != this , " Should be in a list" );
50
+ verify_links ();
49
51
}
50
52
51
53
template <typename T>
52
- inline bool ZListNode<T>::is_unused() const {
53
- return _next == NULL && _prev == NULL ;
54
+ inline void ZListNode<T>::verify_links_unlinked() const {
55
+ assert (_next == this , " Should not be in a list" );
56
+ assert (_prev == this , " Should not be in a list" );
54
57
}
55
58
56
59
template <typename T>
57
- inline void ZList<T>::verify() const {
58
- assert (_head._next ->_prev == &_head, " List corrupt" );
59
- assert (_head._prev ->_next == &_head, " List corrupt" );
60
+ inline void ZList<T>::verify_head() const {
61
+ _head.verify_links ();
60
62
}
61
63
62
64
template <typename T>
63
65
inline void ZList<T>::insert(ZListNode<T>* before, ZListNode<T>* node) {
64
- verify ();
66
+ verify_head ();
67
+
68
+ before->verify_links ();
69
+ node->verify_links_unlinked ();
65
70
66
- assert (node->is_unused (), " Already in a list" );
67
71
node->_prev = before;
68
72
node->_next = before->_next ;
69
73
before->_next = node;
70
74
node->_next ->_prev = node;
71
75
76
+ before->verify_links_linked ();
77
+ node->verify_links_linked ();
78
+
72
79
_size++;
73
80
}
74
81
@@ -84,20 +91,20 @@ inline T* ZList<T>::cast_to_outer(ZListNode<T>* node) const {
84
91
85
92
template <typename T>
86
93
inline ZList<T>::ZList() :
87
- _head (&_head, &_head ),
94
+ _head (),
88
95
_size(0 ) {
89
- verify ();
96
+ verify_head ();
90
97
}
91
98
92
99
template <typename T>
93
100
inline size_t ZList<T>::size() const {
94
- verify ();
101
+ verify_head ();
95
102
return _size;
96
103
}
97
104
98
105
template <typename T>
99
106
inline bool ZList<T>::is_empty() const {
100
- return _size == 0 ;
107
+ return size () == 0 ;
101
108
}
102
109
103
110
template <typename T>
@@ -112,15 +119,27 @@ inline T* ZList<T>::last() const {
112
119
113
120
template <typename T>
114
121
inline T* ZList<T>::next(T* elem) const {
115
- verify ();
116
- ZListNode<T>* next = cast_to_inner (elem)->_next ;
122
+ verify_head ();
123
+
124
+ ZListNode<T>* const node = cast_to_inner (elem);
125
+ node->verify_links_linked ();
126
+
127
+ ZListNode<T>* const next = node->_next ;
128
+ next->verify_links_linked ();
129
+
117
130
return (next == &_head) ? NULL : cast_to_outer (next);
118
131
}
119
132
120
133
template <typename T>
121
134
inline T* ZList<T>::prev(T* elem) const {
122
- verify ();
123
- ZListNode<T>* prev = cast_to_inner (elem)->_prev ;
135
+ verify_head ();
136
+
137
+ ZListNode<T>* const node = cast_to_inner (elem);
138
+ node->verify_links_linked ();
139
+
140
+ ZListNode<T>* const prev = node->_prev ;
141
+ prev->verify_links_linked ();
142
+
124
143
return (prev == &_head) ? NULL : cast_to_outer (prev);
125
144
}
126
145
@@ -146,19 +165,24 @@ inline void ZList<T>::insert_after(T* after, T* elem) {
146
165
147
166
template <typename T>
148
167
inline void ZList<T>::remove(T* elem) {
149
- verify ();
168
+ verify_head ();
150
169
151
170
ZListNode<T>* const node = cast_to_inner (elem);
152
- assert (! node->is_unused (), " Not in a list " );
171
+ node->verify_links_linked ( );
153
172
154
173
ZListNode<T>* const next = node->_next ;
155
174
ZListNode<T>* const prev = node->_prev ;
156
- assert (next->_prev == node, " List corrupt" );
157
- assert (prev->_next == node, " List corrupt" );
175
+ next->verify_links_linked ();
176
+ prev->verify_links_linked ();
177
+
178
+ node->_next = prev->_next ;
179
+ node->_prev = next->_prev ;
180
+ node->verify_links_unlinked ();
158
181
159
- prev->_next = next;
160
182
next->_prev = prev;
161
- node->set_unused ();
183
+ prev->_next = next;
184
+ next->verify_links ();
185
+ prev->verify_links ();
162
186
163
187
_size--;
164
188
}
@@ -183,28 +207,6 @@ inline T* ZList<T>::remove_last() {
183
207
return elem;
184
208
}
185
209
186
- template <typename T>
187
- inline void ZList<T>::transfer(ZList<T>* list) {
188
- verify ();
189
-
190
- if (!list->is_empty ()) {
191
- list->_head ._next ->_prev = _head._prev ;
192
- list->_head ._prev ->_next = _head._prev ->_next ;
193
-
194
- _head._prev ->_next = list->_head ._next ;
195
- _head._prev = list->_head ._prev ;
196
-
197
- list->_head ._next = &list->_head ;
198
- list->_head ._prev = &list->_head ;
199
-
200
- _size += list->_size ;
201
- list->_size = 0 ;
202
-
203
- list->verify ();
204
- verify ();
205
- }
206
- }
207
-
208
210
template <typename T, bool Forward>
209
211
inline ZListIteratorImpl<T, Forward>::ZListIteratorImpl(const ZList<T>* list) :
210
212
_list (list),
0 commit comments