Browse Source

Documented Map.h

Thomas Buck 10 years ago
parent
commit
c278f21493
3 changed files with 249 additions and 241 deletions
  1. 3
    0
      ChangeLog
  2. 240
    239
      include/Map.h
  3. 6
    2
      src/TombRaider.cpp

+ 3
- 0
ChangeLog View File

@@ -5,6 +5,9 @@
5 5
 
6 6
  OpenRaider (0.1.2) xythobuz <xythobuz@xythobuz.de>
7 7
 
8
+	[ 20140124 ]
9
+	* Fixed some TombRaider.cpp warnings
10
+
8 11
 	[ 20140120 ]
9 12
 	* Removed HAVE_SDL & HAVE_SDL_TTF flags. SDL & TTF always required!
10 13
 	* Converted all tabs to spaces (4 spaces per tab)

+ 240
- 239
include/Map.h View File

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

+ 6
- 2
src/TombRaider.cpp View File

@@ -4147,8 +4147,12 @@ bool TombRaider::isRoomValid(int index)
4147 4147
             break;
4148 4148
         case TR_VERSION_5:
4149 4149
             if (index < _num_rooms &&
4150
-                    mRoomsTR5[index].roomX != 0xcdcdcd &&
4151
-                    mRoomsTR5[index].roomZ != 0xcdcdcd)
4150
+                    //mRoomsTR5[index].roomX != 0xcdcdcd &&
4151
+                    //mRoomsTR5[index].roomZ != 0xcdcdcd)
4152
+                    *((int *)&mRoomsTR5[index].roomX) != 0xcdcdcd &&
4153
+                    *((int *)&mRoomsTR5[index].roomZ) != 0xcdcdcd)
4154
+                    // Cast to int * as it was comparing with float 0xcdcdcd before
4155
+                    // -- xythobuz
4152 4156
             {
4153 4157
                 return true;
4154 4158
             }

Loading…
Cancel
Save