|
@@ -1,25 +1,12 @@
|
1
|
|
-/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
|
2
|
|
-/*================================================================
|
|
1
|
+/*!
|
|
2
|
+ * \file include/List.h
|
|
3
|
+ * \brief Template list
|
3
|
4
|
*
|
4
|
|
- * Project : Freyja
|
5
|
|
- * Author : Mongoose
|
6
|
|
- * Website : http://www.westga.edu/~stu7440/
|
7
|
|
- * Email : stu7440@westga.edu
|
8
|
|
- * Object : List
|
9
|
|
- * License : No use w/o permission (C) 2000 Mongoose
|
10
|
|
- * Comments: mtk Template list
|
|
5
|
+ * UINT_MAX is an error condition, used in place of -1
|
11
|
6
|
*
|
12
|
|
- * UINT_MAX is an error condition, used in place of -1
|
13
|
|
- *
|
14
|
|
- * This file was generated using Mongoose's C++
|
15
|
|
- * template generator script. <stu7440@westga.edu>
|
16
|
|
- *
|
17
|
|
- *-- History ------------------------------------------------
|
18
|
|
- *
|
19
|
|
- * 2000-10-26:
|
20
|
|
- * Mongoose - Created
|
21
|
|
- ================================================================*/
|
22
|
|
-
|
|
7
|
+ * \author Mongoose
|
|
8
|
+ * \author xythobuz
|
|
9
|
+ */
|
23
|
10
|
|
24
|
11
|
#ifndef _LIST_H_
|
25
|
12
|
#define _LIST_H_
|
|
@@ -32,418 +19,446 @@
|
32
|
19
|
#include <memory_test.h>
|
33
|
20
|
#endif
|
34
|
21
|
|
35
|
|
-
|
36
|
|
-template <class T> class ListNode
|
37
|
|
-{
|
38
|
|
- public:
|
39
|
|
-
|
40
|
|
- ListNode(T data, unsigned int id)
|
41
|
|
- {
|
42
|
|
- _data = data;
|
43
|
|
- _id = id;
|
44
|
|
- _next = NULL;
|
45
|
|
- }
|
46
|
|
-
|
47
|
|
- ~ListNode()
|
48
|
|
- {
|
49
|
|
- }
|
50
|
|
-
|
51
|
|
- void Id(unsigned int id)
|
52
|
|
- {
|
53
|
|
- _id = id;
|
54
|
|
- }
|
55
|
|
-
|
56
|
|
- unsigned int Id()
|
57
|
|
- {
|
58
|
|
- return _id;
|
59
|
|
- }
|
60
|
|
-
|
61
|
|
- void Data(T data)
|
62
|
|
- {
|
63
|
|
- _data = data;
|
64
|
|
- }
|
65
|
|
-
|
66
|
|
- T Data()
|
67
|
|
- {
|
68
|
|
- return _data;
|
69
|
|
- }
|
70
|
|
-
|
71
|
|
- ListNode<T> *Next()
|
72
|
|
- {
|
73
|
|
- return _next;
|
74
|
|
- }
|
75
|
|
-
|
76
|
|
- void Next(ListNode<T> *next)
|
77
|
|
- {
|
78
|
|
- _next = next;
|
79
|
|
- }
|
80
|
|
-
|
81
|
|
- void Print()
|
82
|
|
- {
|
83
|
|
- //printf("(%i, %p)", _id, _data);
|
84
|
|
- printf("%i", _id);
|
85
|
|
- }
|
86
|
|
-
|
87
|
|
- private:
|
88
|
|
-
|
89
|
|
- ListNode<T> *_next;
|
90
|
|
- unsigned int _id;
|
91
|
|
- T _data;
|
92
|
|
-};
|
93
|
|
-
|
94
|
|
-
|
95
|
|
-template <class T> class List
|
96
|
|
-{
|
97
|
|
- public:
|
98
|
|
-
|
99
|
|
- List()
|
100
|
|
- {
|
101
|
|
- _num_items = 0;
|
102
|
|
- _head = NULL;
|
103
|
|
- _current = NULL;
|
104
|
|
- _last = NULL;
|
105
|
|
- _cache = NULL;
|
106
|
|
- }
|
107
|
|
-
|
108
|
|
- ~List()
|
109
|
|
- {
|
110
|
|
- Clear();
|
111
|
|
- }
|
112
|
|
-
|
113
|
|
- // NOTE: this only copies data, the containers aren't the same ids
|
114
|
|
- void Copy(List<T> *list)
|
115
|
|
- {
|
116
|
|
- if (!list)
|
117
|
|
- return;
|
118
|
|
-
|
119
|
|
- for (list->Reset(); list->CurrentExists(); list->Next())
|
120
|
|
- Add(list->Current());
|
121
|
|
- }
|
122
|
|
-
|
123
|
|
- void Clear()
|
124
|
|
- {
|
125
|
|
- _num_items = 0;
|
126
|
|
- _last = _cache = NULL;
|
127
|
|
-
|
128
|
|
- while (_head)
|
129
|
|
- {
|
130
|
|
- _current = _head;
|
131
|
|
- _head = _head->Next();
|
132
|
|
- delete _current;
|
|
22
|
+/*!
|
|
23
|
+ * \brief Template class encapsulating a single list node
|
|
24
|
+ * \tparam T encapsulated data type
|
|
25
|
+ */
|
|
26
|
+template <class T> class ListNode {
|
|
27
|
+public:
|
|
28
|
+
|
|
29
|
+ /*!
|
|
30
|
+ * \brief Create a new ListNode
|
|
31
|
+ * \param data Data to be stored in node
|
|
32
|
+ * \param id node id
|
|
33
|
+ */
|
|
34
|
+ ListNode(T data, unsigned int id) {
|
|
35
|
+ _data = data;
|
|
36
|
+ _id = id;
|
|
37
|
+ _next = NULL;
|
133
|
38
|
}
|
134
|
|
- }
|
135
|
|
-
|
136
|
|
- T SearchId(unsigned int id)
|
137
|
|
- {
|
138
|
|
- ListNode<T> *current = _head;
|
139
|
|
- ListNode<T> *last = NULL;
|
140
|
39
|
|
141
|
|
- if (_cache)
|
142
|
|
- {
|
143
|
|
- if (id >= _cache->Id())
|
144
|
|
- current = _cache;
|
|
40
|
+ /*!
|
|
41
|
+ * \brief Destroy a ListNode
|
|
42
|
+ */
|
|
43
|
+ ~ListNode() {
|
145
|
44
|
}
|
146
|
45
|
|
147
|
|
- while (current)
|
148
|
|
- {
|
149
|
|
- // Found
|
150
|
|
- if (current->Id() == id)
|
151
|
|
- {
|
152
|
|
- _cache = current;
|
153
|
|
- return current->Data();
|
154
|
|
- }
|
155
|
|
-
|
156
|
|
- last = current;
|
157
|
|
- current = current->Next();
|
|
46
|
+ /*!
|
|
47
|
+ * \brief Set the id of this ListNode
|
|
48
|
+ * \param id new id
|
|
49
|
+ */
|
|
50
|
+ void Id(unsigned int id) {
|
|
51
|
+ _id = id;
|
158
|
52
|
}
|
159
|
53
|
|
160
|
|
- return 0;
|
161
|
|
- }
|
162
|
|
-
|
163
|
|
- unsigned int SearchKey(T data)
|
164
|
|
- {
|
165
|
|
- ListNode<T> *current = _head;
|
166
|
|
- ListNode<T> *last = NULL;
|
167
|
|
-
|
168
|
|
-
|
169
|
|
- if (_cache)
|
170
|
|
- {
|
171
|
|
- // Mongoose: 2001-01-31, hhmmm... fixed?
|
172
|
|
- if (data == _cache->Data())
|
173
|
|
- return _cache->Id();
|
|
54
|
+ /*!
|
|
55
|
+ * \brief Get the id of this ListNode
|
|
56
|
+ * \returns current id
|
|
57
|
+ */
|
|
58
|
+ unsigned int Id() {
|
|
59
|
+ return _id;
|
174
|
60
|
}
|
175
|
61
|
|
176
|
|
- while (current)
|
177
|
|
- {
|
178
|
|
- // Found
|
179
|
|
- if (current->Data() == data)
|
180
|
|
- {
|
181
|
|
- _cache = current;
|
182
|
|
- return current->Id();
|
183
|
|
- }
|
184
|
|
-
|
185
|
|
- last = current;
|
186
|
|
- current = current->Next();
|
|
62
|
+ /*!
|
|
63
|
+ * \brief Set the data of this ListNode
|
|
64
|
+ * \param data new data
|
|
65
|
+ */
|
|
66
|
+ void Data(T data) {
|
|
67
|
+ _data = data;
|
187
|
68
|
}
|
188
|
69
|
|
189
|
|
- return UINT_MAX;
|
190
|
|
- }
|
191
|
|
-
|
192
|
|
- T operator [] (unsigned int i)
|
193
|
|
- {
|
194
|
|
- if (_head)
|
195
|
|
- {
|
196
|
|
- return SearchId(i);
|
197
|
|
- }
|
198
|
|
- else
|
199
|
|
- {
|
200
|
|
-#ifdef DEBUG_INDEX_EMPTY_LIST
|
201
|
|
- printf("List[%i] = NULL\n", i);
|
202
|
|
-#endif
|
|
70
|
+ /*!
|
|
71
|
+ * \brief Get the data of this ListNode
|
|
72
|
+ * \returns current data
|
|
73
|
+ */
|
|
74
|
+ T Data() {
|
|
75
|
+ return _data;
|
203
|
76
|
}
|
204
|
77
|
|
205
|
|
- return 0;
|
206
|
|
- }
|
207
|
|
-
|
208
|
|
-
|
209
|
|
- void RemoveId(unsigned int id)
|
210
|
|
- {
|
211
|
|
- ListNode<T> *current = _head;
|
212
|
|
- ListNode<T> *last = NULL;
|
213
|
|
-
|
214
|
|
-
|
215
|
|
- _last = _cache = NULL;
|
216
|
|
-
|
217
|
|
- while (current)
|
218
|
|
- {
|
219
|
|
- // Remove
|
220
|
|
- if (current->Id() == id)
|
221
|
|
- {
|
222
|
|
- if (current == _head)
|
223
|
|
- _head = current->Next();
|
224
|
|
- else
|
225
|
|
- {
|
226
|
|
- last->Next(current->Next());
|
|
78
|
+ /*!
|
|
79
|
+ * \brief Get the next ListNode in the List
|
|
80
|
+ * \returns next pointer or NULL
|
|
81
|
+ */
|
|
82
|
+ ListNode<T> *Next() {
|
|
83
|
+ return _next;
|
227
|
84
|
}
|
228
|
85
|
|
229
|
|
- if (_current == current)
|
230
|
|
- _current = NULL;
|
231
|
|
-
|
232
|
|
- delete current;
|
233
|
|
- _num_items--;
|
234
|
|
-
|
235
|
|
- return;
|
236
|
|
- }
|
237
|
|
-
|
238
|
|
- last = current;
|
239
|
|
- current = current->Next();
|
240
|
|
- }
|
241
|
|
- }
|
242
|
|
-
|
243
|
|
- void Remove(T data)
|
244
|
|
- {
|
245
|
|
- ListNode<T> *current = _head;
|
246
|
|
- ListNode<T> *last = NULL;
|
247
|
|
-
|
248
|
|
-
|
249
|
|
- _last = _cache = NULL;
|
250
|
|
-
|
251
|
|
- while (current)
|
252
|
|
- {
|
253
|
|
- // Remove
|
254
|
|
- if (current->Data() == data)
|
255
|
|
- {
|
256
|
|
- if (current == _head)
|
257
|
|
- _head = current->Next();
|
258
|
|
- else
|
259
|
|
- {
|
260
|
|
- last->Next(current->Next());
|
|
86
|
+ /*!
|
|
87
|
+ * \brief Set the next ListNode in the List
|
|
88
|
+ * \param next new next pointer
|
|
89
|
+ */
|
|
90
|
+ void Next(ListNode<T> *next) {
|
|
91
|
+ _next = next;
|
261
|
92
|
}
|
262
|
93
|
|
263
|
|
- if (_current == current)
|
264
|
|
- _current = NULL;
|
|
94
|
+ /*!
|
|
95
|
+ * \brief Print this ListNode
|
|
96
|
+ */
|
|
97
|
+ void Print() {
|
|
98
|
+ printf("(%i, %p)", _id, _data);
|
|
99
|
+ }
|
265
|
100
|
|
266
|
|
- delete current;
|
267
|
|
- _num_items--;
|
|
101
|
+private:
|
268
|
102
|
|
269
|
|
- return;
|
270
|
|
- }
|
|
103
|
+ ListNode<T> *_next; //!< Next Pointer
|
|
104
|
+ unsigned int _id; //!< ListNode ID
|
|
105
|
+ T _data; //!< Encapsulated data
|
|
106
|
+};
|
271
|
107
|
|
272
|
|
- last = current;
|
273
|
|
- current = current->Next();
|
|
108
|
+/*!
|
|
109
|
+ * \brief Template class representing a linked list
|
|
110
|
+ * \tparam T encapsulated data type
|
|
111
|
+ */
|
|
112
|
+template <class T> class List {
|
|
113
|
+public:
|
|
114
|
+
|
|
115
|
+ /*!
|
|
116
|
+ * \brief Construct a new linked list
|
|
117
|
+ */
|
|
118
|
+ List() {
|
|
119
|
+ _num_items = 0;
|
|
120
|
+ _head = NULL;
|
|
121
|
+ _current = NULL;
|
|
122
|
+ _last = NULL;
|
|
123
|
+ _cache = NULL;
|
274
|
124
|
}
|
275
|
|
- }
|
276
|
125
|
|
277
|
|
- bool Empty()
|
278
|
|
- {
|
279
|
|
- return (_head == NULL);
|
280
|
|
- }
|
|
126
|
+ /*!
|
|
127
|
+ * \brief Deconstruct a linked list
|
|
128
|
+ * \sa List::Clear()
|
|
129
|
+ */
|
|
130
|
+ ~List() {
|
|
131
|
+ Clear();
|
|
132
|
+ }
|
281
|
133
|
|
282
|
|
- unsigned int NumItems()
|
283
|
|
- {
|
284
|
|
- return _num_items;
|
285
|
|
- }
|
|
134
|
+ /*!
|
|
135
|
+ * \brief Add all data from another list to this one
|
|
136
|
+ * NOTE: this only copies data, the containers aren't the same ids
|
|
137
|
+ * \param list source list where data does come from
|
|
138
|
+ */
|
|
139
|
+ void Copy(List<T> *list) {
|
|
140
|
+ if (!list)
|
|
141
|
+ return;
|
|
142
|
+ for (list->Reset(); list->CurrentExists(); list->Next())
|
|
143
|
+ Add(list->Current());
|
|
144
|
+ }
|
286
|
145
|
|
|
146
|
+ /*!
|
|
147
|
+ * \brief Delete every item in this list
|
|
148
|
+ */
|
|
149
|
+ void Clear() {
|
|
150
|
+ _num_items = 0;
|
|
151
|
+ _last = _cache = NULL;
|
|
152
|
+ while (_head) {
|
|
153
|
+ _current = _head;
|
|
154
|
+ _head = _head->Next();
|
|
155
|
+ delete _current;
|
|
156
|
+ }
|
|
157
|
+ }
|
287
|
158
|
|
288
|
|
- void Print(void (*print_func)(T))
|
289
|
|
- {
|
290
|
|
- ListNode<T> *current = _head;
|
|
159
|
+ /*!
|
|
160
|
+ * \brief Searches the list for an id
|
|
161
|
+ * \param id id to be searched
|
|
162
|
+ * \returns data with id, or 0
|
|
163
|
+ */
|
|
164
|
+ T SearchId(unsigned int id) {
|
|
165
|
+ ListNode<T> *current = _head;
|
|
166
|
+ ListNode<T> *last = NULL;
|
|
167
|
+ if (_cache) {
|
|
168
|
+ if (id >= _cache->Id())
|
|
169
|
+ current = _cache;
|
|
170
|
+ }
|
|
171
|
+ while (current) {
|
|
172
|
+ // Found
|
|
173
|
+ if (current->Id() == id) {
|
|
174
|
+ _cache = current;
|
|
175
|
+ return current->Data();
|
|
176
|
+ }
|
|
177
|
+ last = current;
|
|
178
|
+ current = current->Next();
|
|
179
|
+ }
|
|
180
|
+ return 0;
|
|
181
|
+ }
|
291
|
182
|
|
292
|
|
- printf(" [%i] {\n", _num_items);
|
|
183
|
+ /*!
|
|
184
|
+ * \brief Searches the list for specific data
|
|
185
|
+ * \param data data to be searched for
|
|
186
|
+ * \returns id of data, or UINT_MAX
|
|
187
|
+ */
|
|
188
|
+ unsigned int SearchKey(T data) {
|
|
189
|
+ ListNode<T> *current = _head;
|
|
190
|
+ ListNode<T> *last = NULL;
|
|
191
|
+ if (_cache) {
|
|
192
|
+ // Mongoose: 2001-01-31, hhmmm... fixed?
|
|
193
|
+ if (data == _cache->Data())
|
|
194
|
+ return _cache->Id();
|
|
195
|
+ }
|
|
196
|
+ while (current) {
|
|
197
|
+ // Found
|
|
198
|
+ if (current->Data() == data) {
|
|
199
|
+ _cache = current;
|
|
200
|
+ return current->Id();
|
|
201
|
+ }
|
|
202
|
+ last = current;
|
|
203
|
+ current = current->Next();
|
|
204
|
+ }
|
|
205
|
+ return UINT_MAX;
|
|
206
|
+ }
|
293
|
207
|
|
|
208
|
+ /*!
|
|
209
|
+ * \brief Search for an id
|
|
210
|
+ * \param i id to be searched
|
|
211
|
+ * \returns id or 0
|
|
212
|
+ * \sa List::SearchId()
|
|
213
|
+ */
|
|
214
|
+ T operator [] (unsigned int i) {
|
|
215
|
+ if (_head) {
|
|
216
|
+ return SearchId(i);
|
|
217
|
+ } else {
|
|
218
|
+#ifdef DEBUG_INDEX_EMPTY_LIST
|
|
219
|
+ printf("List[%i] = NULL\n", i);
|
|
220
|
+#endif
|
|
221
|
+ }
|
|
222
|
+ return 0;
|
|
223
|
+ }
|
294
|
224
|
|
295
|
|
- while (current)
|
296
|
|
- {
|
297
|
|
- printf("#%i, ", current->Id());
|
|
225
|
+ /*!
|
|
226
|
+ * \brief Search for an id and remove the associated item from the list
|
|
227
|
+ * \param id id to be deleted
|
|
228
|
+ */
|
|
229
|
+ void RemoveId(unsigned int id) {
|
|
230
|
+ ListNode<T> *current = _head;
|
|
231
|
+ ListNode<T> *last = NULL;
|
|
232
|
+ _last = _cache = NULL;
|
|
233
|
+ while (current) {
|
|
234
|
+ // Remove
|
|
235
|
+ if (current->Id() == id) {
|
|
236
|
+ if (current == _head)
|
|
237
|
+ _head = current->Next();
|
|
238
|
+ else
|
|
239
|
+ last->Next(current->Next());
|
|
240
|
+ if (_current == current)
|
|
241
|
+ _current = NULL;
|
|
242
|
+ delete current;
|
|
243
|
+ _num_items--;
|
|
244
|
+ return;
|
|
245
|
+ }
|
|
246
|
+ last = current;
|
|
247
|
+ current = current->Next();
|
|
248
|
+ }
|
|
249
|
+ }
|
298
|
250
|
|
299
|
|
- if (print_func)
|
300
|
|
- (*print_func)(current->Data());
|
|
251
|
+ /*!
|
|
252
|
+ * \brief Search for data and remove the associated item from the list
|
|
253
|
+ * \param data data to be deleted
|
|
254
|
+ */
|
|
255
|
+ void Remove(T data) {
|
|
256
|
+ ListNode<T> *current = _head;
|
|
257
|
+ ListNode<T> *last = NULL;
|
|
258
|
+ _last = _cache = NULL;
|
|
259
|
+ while (current) {
|
|
260
|
+ // Remove
|
|
261
|
+ if (current->Data() == data) {
|
|
262
|
+ if (current == _head)
|
|
263
|
+ _head = current->Next();
|
|
264
|
+ else
|
|
265
|
+ last->Next(current->Next());
|
|
266
|
+ if (_current == current)
|
|
267
|
+ _current = NULL;
|
|
268
|
+ delete current;
|
|
269
|
+ _num_items--;
|
|
270
|
+ return;
|
|
271
|
+ }
|
|
272
|
+ last = current;
|
|
273
|
+ current = current->Next();
|
|
274
|
+ }
|
|
275
|
+ }
|
301
|
276
|
|
302
|
|
- current = current->Next();
|
|
277
|
+ /*!
|
|
278
|
+ * \brief Is the list empty?
|
|
279
|
+ * \returns true if the list is empty
|
|
280
|
+ */
|
|
281
|
+ bool Empty() {
|
|
282
|
+ return (_head == NULL);
|
|
283
|
+ }
|
303
|
284
|
|
304
|
|
- fflush(stdout);
|
|
285
|
+ /*!
|
|
286
|
+ * \brief Length of the list
|
|
287
|
+ * \returns number of items in the list
|
|
288
|
+ */
|
|
289
|
+ unsigned int NumItems() {
|
|
290
|
+ return _num_items;
|
305
|
291
|
}
|
306
|
292
|
|
307
|
|
- printf(" }\n");
|
308
|
|
- }
|
|
293
|
+ /*!
|
|
294
|
+ * \brief Print the list
|
|
295
|
+ * \param print_func function that will be called for each item in the list, after its id was printed. Or NULL.
|
|
296
|
+ */
|
|
297
|
+ void Print(void (*print_func)(T)) {
|
|
298
|
+ ListNode<T> *current = _head;
|
|
299
|
+ printf(" [%i] {\n", _num_items);
|
|
300
|
+ while (current) {
|
|
301
|
+ printf("#%i, ", current->Id());
|
|
302
|
+ if (print_func)
|
|
303
|
+ (*print_func)(current->Data());
|
|
304
|
+ current = current->Next();
|
|
305
|
+ fflush(stdout);
|
|
306
|
+ }
|
|
307
|
+ printf(" }\n");
|
|
308
|
+ }
|
309
|
309
|
|
|
310
|
+ /*!
|
|
311
|
+ * \brief Print all IDs in this list
|
|
312
|
+ */
|
|
313
|
+ void Print() {
|
|
314
|
+ ListNode<T> *current = _head;
|
|
315
|
+ printf("List %i {\n", _num_items);
|
|
316
|
+ while (current) {
|
|
317
|
+ //current->Print();
|
|
318
|
+ printf("%i", current->Id());
|
|
319
|
+ current = current->Next();
|
|
320
|
+ if (current)
|
|
321
|
+ printf(", ");
|
|
322
|
+ fflush(stdout);
|
|
323
|
+ }
|
|
324
|
+ printf(" }\n");
|
|
325
|
+ }
|
310
|
326
|
|
311
|
|
- void Print()
|
312
|
|
- {
|
313
|
|
- ListNode<T> *current = _head;
|
|
327
|
+ /*!
|
|
328
|
+ * \brief Reset the iterator
|
|
329
|
+ */
|
|
330
|
+ void Reset() {
|
|
331
|
+ _current = _head;
|
|
332
|
+ _cache = _head;
|
|
333
|
+ }
|
314
|
334
|
|
315
|
|
- printf("List %i {\n", _num_items);
|
|
335
|
+ /*!
|
|
336
|
+ * \brief Traverses the list
|
|
337
|
+ * \returns true if there is a new current item, false if the list is at the end
|
|
338
|
+ * \sa List::Next()
|
|
339
|
+ */
|
|
340
|
+ bool operator ++ (int) {
|
|
341
|
+ return Next();
|
|
342
|
+ }
|
316
|
343
|
|
|
344
|
+ /*!
|
|
345
|
+ * \brief Traverses the list
|
|
346
|
+ * \returns true if there is a new current item, false if the list is at the end
|
|
347
|
+ * \sa List::Next()
|
|
348
|
+ */
|
|
349
|
+ bool operator ++ () {
|
|
350
|
+ return Next();
|
|
351
|
+ }
|
317
|
352
|
|
318
|
|
- while (current)
|
319
|
|
- {
|
320
|
|
- //current->Print();
|
321
|
|
- printf("%i", current->Id());
|
|
353
|
+ /*!
|
|
354
|
+ * \brief Traverses the list
|
|
355
|
+ * \returns true if there is a new current item, false if the list is at the end
|
|
356
|
+ */
|
|
357
|
+ bool Next() {
|
|
358
|
+ if (_current)
|
|
359
|
+ _current = _current->Next();
|
|
360
|
+ return (_current != NULL);
|
|
361
|
+ }
|
322
|
362
|
|
323
|
|
- current = current->Next();
|
|
363
|
+ /*!
|
|
364
|
+ * \brief Get the id of the current item
|
|
365
|
+ * \returns id of current item or UINT_MAX if there is no current item
|
|
366
|
+ */
|
|
367
|
+ unsigned int CurrentId() {
|
|
368
|
+ if (!_current)
|
|
369
|
+ return UINT_MAX;
|
324
|
370
|
|
325
|
|
- if (current)
|
326
|
|
- printf(", ");
|
|
371
|
+ return _current->Id();
|
|
372
|
+ }
|
327
|
373
|
|
328
|
|
- fflush(stdout);
|
|
374
|
+ /*!
|
|
375
|
+ * \brief Is there a current item?
|
|
376
|
+ * \returns true if there is a current item, false otherwise
|
|
377
|
+ */
|
|
378
|
+ bool CurrentExists() {
|
|
379
|
+ return (_current != 0);
|
329
|
380
|
}
|
330
|
381
|
|
331
|
|
- printf(" }\n");
|
332
|
|
- }
|
333
|
|
-
|
334
|
|
- void Reset()
|
335
|
|
- {
|
336
|
|
- _current = _head;
|
337
|
|
- _cache = _head;
|
338
|
|
- }
|
339
|
|
-
|
340
|
|
- bool operator ++ (int)
|
341
|
|
- {
|
342
|
|
- return Next();
|
343
|
|
- }
|
344
|
|
-
|
345
|
|
- bool operator ++ ()
|
346
|
|
- {
|
347
|
|
- return Next();
|
348
|
|
- }
|
349
|
|
-
|
350
|
|
- bool Next()
|
351
|
|
- {
|
352
|
|
- if (_current)
|
353
|
|
- _current = _current->Next();
|
354
|
|
-
|
355
|
|
- return (_current != NULL);
|
356
|
|
- }
|
357
|
|
-
|
358
|
|
- unsigned int CurrentId()
|
359
|
|
- {
|
360
|
|
- if (!_current)
|
361
|
|
- return UINT_MAX;
|
362
|
|
-
|
363
|
|
- return _current->Id();
|
364
|
|
- }
|
365
|
|
-
|
366
|
|
- bool CurrentExists()
|
367
|
|
- {
|
368
|
|
- return (_current != 0);
|
369
|
|
- }
|
370
|
|
-
|
371
|
|
- T Current()
|
372
|
|
- {
|
373
|
|
- if (_current)
|
374
|
|
- return _current->Data();
|
375
|
|
- else
|
376
|
|
- return 0;
|
377
|
|
- }
|
378
|
|
-
|
379
|
|
- unsigned int Add(T data)
|
380
|
|
- {
|
381
|
|
- ListNode<T> *node;
|
382
|
|
-
|
383
|
|
-
|
384
|
|
- node = new ListNode<T>(data, _num_items++);
|
385
|
|
- return Add(node);
|
386
|
|
- }
|
387
|
|
-
|
388
|
|
- unsigned int Add(ListNode<T> *node)
|
389
|
|
- {
|
390
|
|
- ListNode<T> *current;
|
391
|
|
- ListNode<T> *last;
|
392
|
|
- unsigned int i;
|
393
|
|
-
|
394
|
|
-
|
395
|
|
- if (_head)
|
396
|
|
- {
|
397
|
|
- current = _head;
|
398
|
|
- last = NULL;
|
399
|
|
- i = 0;
|
400
|
|
-
|
401
|
|
- //EXP
|
402
|
|
- if (_last)
|
403
|
|
- {
|
404
|
|
- i = _last->Id();
|
405
|
|
- current = _last;
|
406
|
|
- }
|
407
|
|
-
|
408
|
|
- while (current)
|
409
|
|
- {
|
410
|
|
- // Prepend
|
411
|
|
- if (current->Id() > i)
|
412
|
|
- {
|
413
|
|
- node->Id(i);
|
414
|
|
- node->Next(current);
|
415
|
|
-
|
416
|
|
- if (current == _head)
|
417
|
|
- _head = node;
|
418
|
|
- else if (last)
|
419
|
|
- last->Next(node);
|
420
|
|
-
|
421
|
|
- return node->Id();
|
|
382
|
+ /*!
|
|
383
|
+ * \brief Get the current items data
|
|
384
|
+ * \returns data of the current item or 0 if there is no current item
|
|
385
|
+ */
|
|
386
|
+ T Current() {
|
|
387
|
+ if (_current)
|
|
388
|
+ return _current->Data();
|
|
389
|
+ else
|
|
390
|
+ return 0;
|
422
|
391
|
}
|
423
|
392
|
|
424
|
|
- i++;
|
425
|
|
- last = current;
|
426
|
|
- current = current->Next();
|
427
|
|
- }
|
|
393
|
+ /*!
|
|
394
|
+ * \brief Add data to the list. Constructs a new ListNode encapsulating the data.
|
|
395
|
+ * \param data data to be stored
|
|
396
|
+ * \returns id of the new ListNode
|
|
397
|
+ */
|
|
398
|
+ unsigned int Add(T data) {
|
|
399
|
+ ListNode<T> *node;
|
428
|
400
|
|
429
|
|
- // Append
|
430
|
|
- last->Next(node);
|
|
401
|
+ node = new ListNode<T>(data, _num_items++);
|
|
402
|
+ return Add(node);
|
431
|
403
|
}
|
432
|
|
- else
|
433
|
|
- _head = node;
|
434
|
404
|
|
435
|
|
- _last = node; //EXP
|
436
|
|
-
|
437
|
|
- return node->Id();
|
438
|
|
- }
|
|
405
|
+ /*!
|
|
406
|
+ * \brief Add a ListNode to the end of this List.
|
|
407
|
+ * \param node new node
|
|
408
|
+ * \returns id of the node
|
|
409
|
+ */
|
|
410
|
+ unsigned int Add(ListNode<T> *node) {
|
|
411
|
+ ListNode<T> *current;
|
|
412
|
+ ListNode<T> *last;
|
|
413
|
+ unsigned int i;
|
|
414
|
+
|
|
415
|
+ if (_head) {
|
|
416
|
+ current = _head;
|
|
417
|
+ last = NULL;
|
|
418
|
+ i = 0;
|
|
419
|
+
|
|
420
|
+ //EXP
|
|
421
|
+ if (_last) {
|
|
422
|
+ i = _last->Id();
|
|
423
|
+ current = _last;
|
|
424
|
+ }
|
|
425
|
+
|
|
426
|
+ while (current) {
|
|
427
|
+ // Prepend
|
|
428
|
+ if (current->Id() > i) {
|
|
429
|
+ node->Id(i);
|
|
430
|
+ node->Next(current);
|
|
431
|
+
|
|
432
|
+ if (current == _head)
|
|
433
|
+ _head = node;
|
|
434
|
+ else if (last)
|
|
435
|
+ last->Next(node);
|
|
436
|
+
|
|
437
|
+ return node->Id();
|
|
438
|
+ }
|
|
439
|
+
|
|
440
|
+ i++;
|
|
441
|
+ last = current;
|
|
442
|
+ current = current->Next();
|
|
443
|
+ }
|
|
444
|
+
|
|
445
|
+ // Append
|
|
446
|
+ last->Next(node);
|
|
447
|
+ } else
|
|
448
|
+ _head = node;
|
|
449
|
+
|
|
450
|
+ _last = node; //EXP
|
|
451
|
+
|
|
452
|
+ return node->Id();
|
|
453
|
+ }
|
439
|
454
|
|
440
|
|
- private:
|
|
455
|
+private:
|
441
|
456
|
|
442
|
|
- unsigned int _num_items;
|
443
|
|
- ListNode<T> *_head;
|
444
|
|
- ListNode<T> *_current;
|
445
|
|
- ListNode<T> *_last;
|
446
|
|
- ListNode<T> *_cache;
|
|
457
|
+ unsigned int _num_items; //!< Number of items in the list
|
|
458
|
+ ListNode<T> *_head; //!< First item in the list
|
|
459
|
+ ListNode<T> *_current; //!< Current item for the list iterator
|
|
460
|
+ ListNode<T> *_last; //!< Last item in the list
|
|
461
|
+ ListNode<T> *_cache; //!< cache used by the search methods
|
447
|
462
|
};
|
448
|
463
|
|
449
|
464
|
#endif
|