|
|
1 | /* |
|
|
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
|
|
5 | * |
|
|
6 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
|
|
7 | * under the terms of the GNU General Public License as published by the Free |
|
|
8 | * Software Foundation; either version 2 of the License, or (at your option) |
|
|
9 | * any later version. |
|
|
10 | * |
|
|
11 | * This program is distributed in the hope that it will be useful, but |
|
|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|
|
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
|
14 | * for more details. |
|
|
15 | * |
|
|
16 | * You should have received a copy of the GNU General Public License along |
|
|
17 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
|
|
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
19 | * |
|
|
20 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
|
|
21 | */ |
|
|
22 | |
1 | #ifndef UTIL_H__ |
23 | #ifndef UTIL_H__ |
2 | #define UTIL_H__ |
24 | #define UTIL_H__ |
3 | |
25 | |
4 | //#define PREFER_MALLOC |
26 | //#define PREFER_MALLOC |
5 | |
27 | |
… | |
… | |
11 | # define is_constant(c) 0 |
33 | # define is_constant(c) 0 |
12 | # define expect(expr,value) (expr) |
34 | # define expect(expr,value) (expr) |
13 | # define prefetch(addr,rw,locality) |
35 | # define prefetch(addr,rw,locality) |
14 | #endif |
36 | #endif |
15 | |
37 | |
|
|
38 | #if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) |
|
|
39 | # define decltype(x) typeof(x) |
|
|
40 | #endif |
|
|
41 | |
16 | // put into ifs if you are very sure that the expression |
42 | // put into ifs if you are very sure that the expression |
17 | // is mostly true or mosty false. note that these return |
43 | // is mostly true or mosty false. note that these return |
18 | // booleans, not the expression. |
44 | // booleans, not the expression. |
19 | #define expect_false(expr) expect ((expr) != 0, 0) |
45 | #define expect_false(expr) expect ((expr) != 0, 0) |
20 | #define expect_true(expr) expect ((expr) != 0, 1) |
46 | #define expect_true(expr) expect ((expr) != 0, 1) |
… | |
… | |
27 | #include <glib.h> |
53 | #include <glib.h> |
28 | |
54 | |
29 | #include <shstr.h> |
55 | #include <shstr.h> |
30 | #include <traits.h> |
56 | #include <traits.h> |
31 | |
57 | |
32 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
58 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
33 | #define auto(var,expr) typeof(expr) var = (expr) |
59 | #define auto(var,expr) decltype(expr) var = (expr) |
34 | |
60 | |
35 | // very ugly macro that basicaly declares and initialises a variable |
61 | // very ugly macro that basicaly declares and initialises a variable |
36 | // that is in scope for the next statement only |
62 | // that is in scope for the next statement only |
37 | // works only for stuff that can be assigned 0 and converts to false |
63 | // works only for stuff that can be assigned 0 and converts to false |
38 | // (note: works great for pointers) |
64 | // (note: works great for pointers) |
39 | // most ugly macro I ever wrote |
65 | // most ugly macro I ever wrote |
40 | #define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
66 | #define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
41 | |
67 | |
42 | // in range including end |
68 | // in range including end |
43 | #define IN_RANGE_INC(val,beg,end) \ |
69 | #define IN_RANGE_INC(val,beg,end) \ |
44 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
70 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
45 | |
71 | |
… | |
… | |
379 | { |
405 | { |
380 | return !strcmp (a, b); |
406 | return !strcmp (a, b); |
381 | } |
407 | } |
382 | }; |
408 | }; |
383 | |
409 | |
|
|
410 | // Mostly the same as std::vector, but insert/erase can reorder |
|
|
411 | // the elements, making insret/remove O(1) instead of O(n). |
|
|
412 | // |
|
|
413 | // NOTE: only some forms of erase/insert are available |
384 | template<class T> |
414 | template<class T> |
385 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
415 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
386 | { |
416 | { |
387 | typedef typename unordered_vector::iterator iterator; |
417 | typedef typename unordered_vector::iterator iterator; |
388 | |
418 | |
… | |
… | |
398 | { |
428 | { |
399 | erase ((unsigned int )(i - this->begin ())); |
429 | erase ((unsigned int )(i - this->begin ())); |
400 | } |
430 | } |
401 | }; |
431 | }; |
402 | |
432 | |
|
|
433 | // This container blends advantages of linked lists |
|
|
434 | // (efficiency) with vectors (random access) by |
|
|
435 | // by using an unordered vector and storing the vector |
|
|
436 | // index inside the object. |
|
|
437 | // |
|
|
438 | // + memory-efficient on most 64 bit archs |
|
|
439 | // + O(1) insert/remove |
|
|
440 | // + free unique (but varying) id for inserted objects |
|
|
441 | // + cache-friendly iteration |
|
|
442 | // - only works for pointers to structs |
|
|
443 | // |
|
|
444 | // NOTE: only some forms of erase/insert are available |
403 | template<class T, int T::* index> |
445 | template<class T, int T::* index> |
404 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
446 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
405 | { |
447 | { |
|
|
448 | typedef typename object_vector::iterator iterator; |
|
|
449 | |
|
|
450 | bool contains (const T *obj) const |
|
|
451 | { |
|
|
452 | return obj->*index; |
|
|
453 | } |
|
|
454 | |
|
|
455 | iterator find (const T *obj) |
|
|
456 | { |
|
|
457 | return obj->*index |
|
|
458 | ? this->begin () + obj->*index - 1 |
|
|
459 | : this->end (); |
|
|
460 | } |
|
|
461 | |
406 | void insert (T *obj) |
462 | void insert (T *obj) |
407 | { |
463 | { |
408 | assert (!(obj->*index)); |
|
|
409 | push_back (obj); |
464 | push_back (obj); |
410 | obj->*index = this->size (); |
465 | obj->*index = this->size (); |
411 | } |
466 | } |
412 | |
467 | |
413 | void insert (T &obj) |
468 | void insert (T &obj) |
… | |
… | |
415 | insert (&obj); |
470 | insert (&obj); |
416 | } |
471 | } |
417 | |
472 | |
418 | void erase (T *obj) |
473 | void erase (T *obj) |
419 | { |
474 | { |
420 | assert (obj->*index); |
|
|
421 | unsigned int pos = obj->*index; |
475 | unsigned int pos = obj->*index; |
422 | obj->*index = 0; |
476 | obj->*index = 0; |
423 | |
477 | |
424 | if (pos < this->size ()) |
478 | if (pos < this->size ()) |
425 | { |
479 | { |