|
@@ -1,571 +0,0 @@
|
1
|
|
-/*!
|
2
|
|
- * \file include/Map.h
|
3
|
|
- * \brief Template Map
|
4
|
|
- *
|
5
|
|
- * Using RBTree and list overlay for faster access
|
6
|
|
- *
|
7
|
|
- * Access: O(1)/O(n)/O(logn) ?
|
8
|
|
- * Insert: O(nlogn) ?
|
9
|
|
- * Remove: O(nlogn) ?
|
10
|
|
- *
|
11
|
|
- * \author Mongoose
|
12
|
|
- * \author xythobuz
|
13
|
|
- */
|
14
|
|
-
|
15
|
|
-#ifndef _MAP_H_
|
16
|
|
-#define _MAP_H_
|
17
|
|
-
|
18
|
|
-#include <stdlib.h>
|
19
|
|
-#include <stdio.h>
|
20
|
|
-
|
21
|
|
-#include <Tree.h>
|
22
|
|
-
|
23
|
|
-#ifdef DEBUG_MEMORY
|
24
|
|
-#include <memory_test.h>
|
25
|
|
-#endif
|
26
|
|
-
|
27
|
|
-/*!
|
28
|
|
- * \brief Template class encapsulating a single map node
|
29
|
|
- * \tparam K key type
|
30
|
|
- * \tparam D data type
|
31
|
|
- */
|
32
|
|
-template <class K, class D> class MapNode {
|
33
|
|
-public:
|
34
|
|
-
|
35
|
|
- /*!
|
36
|
|
- * \brief Construct a MapNode
|
37
|
|
- * \param key key of this Node
|
38
|
|
- * \param data data of this Node
|
39
|
|
- */
|
40
|
|
- MapNode(K key, D data) {
|
41
|
|
- _data = data;
|
42
|
|
- _key = key;
|
43
|
|
- _next = NULL;
|
44
|
|
- }
|
45
|
|
-
|
46
|
|
- /*!
|
47
|
|
- * \brief Deconstruct a MapNode
|
48
|
|
- */
|
49
|
|
- ~MapNode() {
|
50
|
|
- }
|
51
|
|
-
|
52
|
|
- /*!
|
53
|
|
- * \brief Set the key of this MapNode
|
54
|
|
- * \param key new key
|
55
|
|
- */
|
56
|
|
- void Key(K key) {
|
57
|
|
- _key = key;
|
58
|
|
- }
|
59
|
|
-
|
60
|
|
- /*!
|
61
|
|
- * \brief Get the key of this MapNode
|
62
|
|
- * \returns key
|
63
|
|
- */
|
64
|
|
- K Key() {
|
65
|
|
- return _key;
|
66
|
|
- }
|
67
|
|
-
|
68
|
|
- /*!
|
69
|
|
- * \brief Set the data of this MapNode
|
70
|
|
- * \param data new data
|
71
|
|
- */
|
72
|
|
- void Data(D data) {
|
73
|
|
- _data = data;
|
74
|
|
- }
|
75
|
|
-
|
76
|
|
- /*!
|
77
|
|
- * \brief Get the data of this MapNode
|
78
|
|
- * \returns data
|
79
|
|
- */
|
80
|
|
- D Data() {
|
81
|
|
- return _data;
|
82
|
|
- }
|
83
|
|
-
|
84
|
|
- /*!
|
85
|
|
- * \brief Get the next MapNode in the Map
|
86
|
|
- * \returns next pointer
|
87
|
|
- */
|
88
|
|
- MapNode<K, D> *Next() {
|
89
|
|
- return _next;
|
90
|
|
- }
|
91
|
|
-
|
92
|
|
- /*!
|
93
|
|
- * \brief Set the next MapNode in the Map
|
94
|
|
- * \param next new next pointer
|
95
|
|
- */
|
96
|
|
- void Next(MapNode<K, D> *next) {
|
97
|
|
- _next = next;
|
98
|
|
- }
|
99
|
|
-
|
100
|
|
-private:
|
101
|
|
-
|
102
|
|
- MapNode<K, D> *_next; //!< Next pointer
|
103
|
|
- K _key; //!< Key of the MapNode
|
104
|
|
- D _data; //!< Data of the MapNode
|
105
|
|
-};
|
106
|
|
-
|
107
|
|
-/*!
|
108
|
|
- * \brief Template class representing a Map, mapping data to a key.
|
109
|
|
- * \tparam K key type
|
110
|
|
- * \tparam D data type
|
111
|
|
- */
|
112
|
|
-template <class K, class D> class Map {
|
113
|
|
-public:
|
114
|
|
-
|
115
|
|
- /*!
|
116
|
|
- * \brief Construct a Map
|
117
|
|
- */
|
118
|
|
- Map() {
|
119
|
|
- UnSetError();
|
120
|
|
- _num_items = 0;
|
121
|
|
- _head = NULL;
|
122
|
|
- _current = NULL;
|
123
|
|
- _cache = NULL;
|
124
|
|
- }
|
125
|
|
-
|
126
|
|
- /*!
|
127
|
|
- * \brief Deconstruct a Map
|
128
|
|
- * \sa Map::Clear()
|
129
|
|
- */
|
130
|
|
- ~Map() {
|
131
|
|
- Clear();
|
132
|
|
- }
|
133
|
|
-
|
134
|
|
- /*!
|
135
|
|
- * \brief Deletes every item in the Map. Clears error flag.
|
136
|
|
- */
|
137
|
|
- void Clear() {
|
138
|
|
- UnSetError();
|
139
|
|
- _num_items = 0;
|
140
|
|
- _cache = NULL;
|
141
|
|
- while (_head) {
|
142
|
|
- _current = _head;
|
143
|
|
- _head = _head->Next();
|
144
|
|
- delete _current;
|
145
|
|
- }
|
146
|
|
- _tree.Clear();
|
147
|
|
- }
|
148
|
|
-
|
149
|
|
- /*!
|
150
|
|
- * \brief Set the error flag
|
151
|
|
- */
|
152
|
|
- void SetError() {
|
153
|
|
- _error = true;
|
154
|
|
- }
|
155
|
|
-
|
156
|
|
- /*!
|
157
|
|
- * \brief Unset the error flag
|
158
|
|
- */
|
159
|
|
- void UnSetError() {
|
160
|
|
- _error = false;
|
161
|
|
- }
|
162
|
|
-
|
163
|
|
- /*!
|
164
|
|
- * \brief Get the error flag
|
165
|
|
- * \returns error flag
|
166
|
|
- */
|
167
|
|
- bool GetError() {
|
168
|
|
- return _error;
|
169
|
|
- }
|
170
|
|
-
|
171
|
|
- /*!
|
172
|
|
- * \brief Search for data with a key. Sets error flag if nothing is found.
|
173
|
|
- * \param key key to search for
|
174
|
|
- * \returns data matching key or 0
|
175
|
|
- */
|
176
|
|
- D FindDataByKey(K key) {
|
177
|
|
- MapNode<K, D> *current = NULL;
|
178
|
|
- MapNode<K, D> *next = NULL;
|
179
|
|
-
|
180
|
|
-
|
181
|
|
- if (_head) {
|
182
|
|
- UnSetError();
|
183
|
|
-
|
184
|
|
- if (_cache) {
|
185
|
|
- next = _cache->Next();
|
186
|
|
- }
|
187
|
|
-
|
188
|
|
- // Mongoose 2002.02.19, Optimize for sequential searches
|
189
|
|
- if (next && key == next->Key()) {
|
190
|
|
- current = next;
|
191
|
|
- } else { // Mongoose 2002.02.19, Use search algorithm otherwise
|
192
|
|
- current = _tree.SearchByKey(key, &_error);
|
193
|
|
-
|
194
|
|
- if (_error)
|
195
|
|
- return false;
|
196
|
|
- }
|
197
|
|
-
|
198
|
|
- if (current) {
|
199
|
|
- _cache = _current = current;
|
200
|
|
- return current->Data();
|
201
|
|
- }
|
202
|
|
- }
|
203
|
|
-
|
204
|
|
- SetError();
|
205
|
|
- return 0;
|
206
|
|
- }
|
207
|
|
-
|
208
|
|
- /*!
|
209
|
|
- * \brief Search for data with a key
|
210
|
|
- * \param key key to search for
|
211
|
|
- * \returns data matching key or 0
|
212
|
|
- * \sa Map::FindDataByKey()
|
213
|
|
- */
|
214
|
|
- D operator [] (K key) {
|
215
|
|
- return FindDataByKey(key);
|
216
|
|
- }
|
217
|
|
-
|
218
|
|
- /*!
|
219
|
|
- * \brief Search for a key with specific data. Sets error flag if nothing is found.
|
220
|
|
- * \param data data to search for
|
221
|
|
- * \returns key matching data or 0
|
222
|
|
- */
|
223
|
|
- K FindKeyByData(D data) {
|
224
|
|
- MapNode<K, D> *current = _head;
|
225
|
|
- MapNode<K, D> *last = NULL;
|
226
|
|
-
|
227
|
|
-
|
228
|
|
- UnSetError();
|
229
|
|
-
|
230
|
|
- while (current) {
|
231
|
|
- // Found
|
232
|
|
- if (current->Data() == data) {
|
233
|
|
- _cache = current;
|
234
|
|
- return current->Key();
|
235
|
|
- }
|
236
|
|
-
|
237
|
|
- last = current;
|
238
|
|
- current = current->Next();
|
239
|
|
- }
|
240
|
|
-
|
241
|
|
- SetError();
|
242
|
|
-
|
243
|
|
- return 0;
|
244
|
|
- }
|
245
|
|
-
|
246
|
|
- /*!
|
247
|
|
- * \brief Returns data of the cached MapNode
|
248
|
|
- * \returns cached data
|
249
|
|
- */
|
250
|
|
- D getCache() {
|
251
|
|
- if (_cache == 0x0) {
|
252
|
|
- printf("Map::getCache> Bad request - should segfault\n");
|
253
|
|
- }
|
254
|
|
-
|
255
|
|
- return _cache->Data();
|
256
|
|
- }
|
257
|
|
-
|
258
|
|
- /*!
|
259
|
|
- * \brief Check if a key is in this map. Sets error flag if nothing is found.
|
260
|
|
- * \returns true if the key is in the map, false otherwise
|
261
|
|
- * \sa Tree:SearchByKey()
|
262
|
|
- */
|
263
|
|
- bool findKey(K key) {
|
264
|
|
- MapNode<K, D> *current = NULL;
|
265
|
|
- MapNode<K, D> *next = NULL;
|
266
|
|
-
|
267
|
|
- if (_head) {
|
268
|
|
- UnSetError();
|
269
|
|
-
|
270
|
|
- if (_cache) {
|
271
|
|
- if (_cache->Key() == key) {
|
272
|
|
- return true;
|
273
|
|
- }
|
274
|
|
-
|
275
|
|
- next = _cache->Next();
|
276
|
|
- }
|
277
|
|
-
|
278
|
|
- // Mongoose 2002.02.19, Optimize for sequential searches
|
279
|
|
- if (next && key == next->Key()) {
|
280
|
|
- current = next;
|
281
|
|
- } else { // Mongoose 2002.02.19, Use search algorithm otherwise
|
282
|
|
- current = _tree.SearchByKey(key, &_error);
|
283
|
|
- }
|
284
|
|
-
|
285
|
|
- if (current) {
|
286
|
|
- _cache = _current = current;
|
287
|
|
- //curData = current->Data();
|
288
|
|
- return true;
|
289
|
|
- }
|
290
|
|
- }
|
291
|
|
-
|
292
|
|
- SetError();
|
293
|
|
- return false;
|
294
|
|
- }
|
295
|
|
-
|
296
|
|
- /*!
|
297
|
|
- * \brief Add a Key-Data pair to the Map, creating a new MapNode. Clears error flag.
|
298
|
|
- * \param key key to add
|
299
|
|
- * \param data to add
|
300
|
|
- * \returns true on success, false if there is not enough memory
|
301
|
|
- */
|
302
|
|
- bool Add(K key, D data) {
|
303
|
|
- MapNode<K, D> *node;
|
304
|
|
-
|
305
|
|
- UnSetError();
|
306
|
|
- node = new MapNode<K, D>(key, data);
|
307
|
|
- _num_items++;
|
308
|
|
- return Add(node);
|
309
|
|
- }
|
310
|
|
-
|
311
|
|
- /*!
|
312
|
|
- * \brief Add a MapNode to the Map. Clears error flag on success.
|
313
|
|
- * \param node node to add
|
314
|
|
- * \returns true on success, false if node is NULL
|
315
|
|
- * \sa Tree:Insert()
|
316
|
|
- */
|
317
|
|
- bool Add(MapNode<K, D> *node) {
|
318
|
|
- MapNode<K, D> *current = _head;
|
319
|
|
- MapNode<K, D> *last = NULL;
|
320
|
|
-
|
321
|
|
- if (!node)
|
322
|
|
- return false;
|
323
|
|
-
|
324
|
|
- UnSetError();
|
325
|
|
-
|
326
|
|
- if (_head) {
|
327
|
|
- current = _head;
|
328
|
|
- last = NULL;
|
329
|
|
-
|
330
|
|
- while (current) {
|
331
|
|
- // Prepend
|
332
|
|
- if (current->Key() > node->Key()) {
|
333
|
|
- node->Next(current);
|
334
|
|
-
|
335
|
|
- if (current == _head) {
|
336
|
|
- _head = node;
|
337
|
|
- } else if (last) {
|
338
|
|
- last->Next(node);
|
339
|
|
- }
|
340
|
|
-
|
341
|
|
- _tree.Insert(node->Key(), node);
|
342
|
|
- return true;
|
343
|
|
- }
|
344
|
|
-
|
345
|
|
- last = current;
|
346
|
|
- current = current->Next();
|
347
|
|
- }
|
348
|
|
-
|
349
|
|
- // Append
|
350
|
|
- last->Next(node);
|
351
|
|
- } else {
|
352
|
|
- _head = node;
|
353
|
|
- }
|
354
|
|
-
|
355
|
|
- _tree.Insert(node->Key(), node);
|
356
|
|
- return true;
|
357
|
|
- }
|
358
|
|
-
|
359
|
|
- /*!
|
360
|
|
- * \brief Remove item with the specified key. Sets error flag if nothing is removed.
|
361
|
|
- * \param key key to remove
|
362
|
|
- */
|
363
|
|
- void RemoveByKey(K key) {
|
364
|
|
- MapNode<K, D> *current = _head;
|
365
|
|
- MapNode<K, D> *last = NULL;
|
366
|
|
-
|
367
|
|
- UnSetError();
|
368
|
|
-
|
369
|
|
- _cache = NULL;
|
370
|
|
-
|
371
|
|
- while (current) {
|
372
|
|
- // Remove
|
373
|
|
- if (current->Key() == key) {
|
374
|
|
- if (current == _head) {
|
375
|
|
- _head = current->Next();
|
376
|
|
- } else {
|
377
|
|
- last->Next(current->Next());
|
378
|
|
- }
|
379
|
|
-
|
380
|
|
- if (_current == current) {
|
381
|
|
- _current = NULL;
|
382
|
|
- }
|
383
|
|
-
|
384
|
|
- _tree.RemoveByKey(current->Key());
|
385
|
|
- delete current;
|
386
|
|
- _num_items--;
|
387
|
|
-
|
388
|
|
- return;
|
389
|
|
- }
|
390
|
|
-
|
391
|
|
- last = current;
|
392
|
|
- current = current->Next();
|
393
|
|
- }
|
394
|
|
-
|
395
|
|
- SetError();
|
396
|
|
- }
|
397
|
|
-
|
398
|
|
- /*!
|
399
|
|
- * \brief Remove item with the specified data. Sets error flag if nothing is removed.
|
400
|
|
- * \param data data to remove
|
401
|
|
- */
|
402
|
|
- void RemoveByData(D data) {
|
403
|
|
- MapNode<K, D> *current = _head;
|
404
|
|
- MapNode<K, D> *last = NULL;
|
405
|
|
-
|
406
|
|
- UnSetError();
|
407
|
|
-
|
408
|
|
- _cache = NULL;
|
409
|
|
-
|
410
|
|
- while (current)
|
411
|
|
- {
|
412
|
|
- // Remove
|
413
|
|
- if (current->Data() == data) {
|
414
|
|
- if (current == _head) {
|
415
|
|
- _head = current->Next();
|
416
|
|
- } else {
|
417
|
|
- last->Next(current->Next());
|
418
|
|
- }
|
419
|
|
-
|
420
|
|
- if (_current == current) {
|
421
|
|
- _current = NULL;
|
422
|
|
- }
|
423
|
|
-
|
424
|
|
- _tree.RemoveByKey(current->Key());
|
425
|
|
- delete current;
|
426
|
|
- _num_items--;
|
427
|
|
-
|
428
|
|
- return;
|
429
|
|
- }
|
430
|
|
-
|
431
|
|
- last = current;
|
432
|
|
- current = current->Next();
|
433
|
|
- }
|
434
|
|
-
|
435
|
|
- SetError();
|
436
|
|
- }
|
437
|
|
-
|
438
|
|
- /*!
|
439
|
|
- * \brief Check if the Map is empty
|
440
|
|
- * \returns true if Map is empty, false otherwise
|
441
|
|
- */
|
442
|
|
- bool Empty() {
|
443
|
|
- return (_head == NULL);
|
444
|
|
- }
|
445
|
|
-
|
446
|
|
- /*!
|
447
|
|
- * \brief Returns number of items in the Map
|
448
|
|
- * \returns number of items
|
449
|
|
- */
|
450
|
|
- unsigned int NumItems() {
|
451
|
|
- return _num_items;
|
452
|
|
- }
|
453
|
|
-
|
454
|
|
- /*!
|
455
|
|
- * \brief Print the Map. Sets error flag if one or both functions aren't provided.
|
456
|
|
- * \param print_key_func function that prints a key
|
457
|
|
- * \param print_data_func function that prints the data
|
458
|
|
- */
|
459
|
|
- void Print(void (*print_key_func)(K), void (*print_data_func)(D)) {
|
460
|
|
- MapNode<K, D> *current = _head;
|
461
|
|
-
|
462
|
|
- UnSetError();
|
463
|
|
-
|
464
|
|
- if (!print_key_func || !print_data_func) {
|
465
|
|
- SetError();
|
466
|
|
- return;
|
467
|
|
- }
|
468
|
|
-
|
469
|
|
- printf(" [%u] {\n", _num_items);
|
470
|
|
-
|
471
|
|
- while (current) {
|
472
|
|
- printf("(");
|
473
|
|
- (*print_key_func)(current->Key());
|
474
|
|
- printf(", ");
|
475
|
|
- (*print_data_func)(current->Data());
|
476
|
|
- printf("), ");
|
477
|
|
-
|
478
|
|
- current = current->Next();
|
479
|
|
- fflush(stdout);
|
480
|
|
- }
|
481
|
|
-
|
482
|
|
- printf(" }\n");
|
483
|
|
- }
|
484
|
|
-
|
485
|
|
- /*!
|
486
|
|
- * \brief Reset the Map iterator
|
487
|
|
- */
|
488
|
|
- void Reset() {
|
489
|
|
- _current = _head;
|
490
|
|
- _cache = _head;
|
491
|
|
- }
|
492
|
|
-
|
493
|
|
- /*!
|
494
|
|
- * \brief Iterate over the Map
|
495
|
|
- * \returns true if there is a new current element
|
496
|
|
- * \sa Map::Next()
|
497
|
|
- */
|
498
|
|
- bool operator ++ (int) {
|
499
|
|
- return Next();
|
500
|
|
- }
|
501
|
|
-
|
502
|
|
- /*!
|
503
|
|
- * \brief Iterate over the Map
|
504
|
|
- * \returns true if there is a new current element
|
505
|
|
- * \sa Map::Next()
|
506
|
|
- */
|
507
|
|
- bool operator ++ () {
|
508
|
|
- return Next();
|
509
|
|
- }
|
510
|
|
-
|
511
|
|
- /*!
|
512
|
|
- * \brief Iterate over the Map
|
513
|
|
- * \returns true if there is a new current element
|
514
|
|
- * \sa Map::Next()
|
515
|
|
- */
|
516
|
|
- bool Next() {
|
517
|
|
- if (_current) {
|
518
|
|
- _current = _current->Next();
|
519
|
|
- }
|
520
|
|
-
|
521
|
|
- return (_current != NULL);
|
522
|
|
- }
|
523
|
|
-
|
524
|
|
- /*!
|
525
|
|
- * \brief Check if there is a current item in the iterator
|
526
|
|
- * \returns true if the current item exists
|
527
|
|
- */
|
528
|
|
- bool CurrentExists() {
|
529
|
|
- return (_current != 0);
|
530
|
|
- }
|
531
|
|
-
|
532
|
|
- /*!
|
533
|
|
- * \brief Returns the key of the current item. Sets error flag if there is no current item.
|
534
|
|
- * \returns key or 0 if there is no current item
|
535
|
|
- */
|
536
|
|
- K CurrentKey() {
|
537
|
|
- UnSetError();
|
538
|
|
-
|
539
|
|
- if (!_current) {
|
540
|
|
- SetError();
|
541
|
|
- return 0;
|
542
|
|
- }
|
543
|
|
-
|
544
|
|
- return _current->Key();
|
545
|
|
- }
|
546
|
|
-
|
547
|
|
- /*!
|
548
|
|
- * \brief Returns the data of the current item. Sets error flag if there is no current item.
|
549
|
|
- * \returns data or 0 if there is no current item
|
550
|
|
- */
|
551
|
|
- D Current() {
|
552
|
|
- UnSetError();
|
553
|
|
-
|
554
|
|
- if (!_current) {
|
555
|
|
- SetError();
|
556
|
|
- return 0;
|
557
|
|
- }
|
558
|
|
-
|
559
|
|
- return _current->Data();
|
560
|
|
- }
|
561
|
|
-
|
562
|
|
-private:
|
563
|
|
-
|
564
|
|
- unsigned int _num_items; //!< Number of items in the Map
|
565
|
|
- bool _error; //!< Error flag
|
566
|
|
- Tree<K, MapNode<K, D> *> _tree; //!< Tree containing the MapNodes
|
567
|
|
- MapNode<K, D> *_head; //!< First item of the Map
|
568
|
|
- MapNode<K, D> *_current; //!< Current item for the iterator
|
569
|
|
- MapNode<K, D> *_cache; //!< Cached item used by search & remove methods
|
570
|
|
-};
|
571
|
|
-#endif
|