|
|
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 | |
|
|
26 | //#define PREFER_MALLOC |
|
|
27 | |
4 | #if __GNUC__ >= 3 |
28 | #if __GNUC__ >= 3 |
5 | # define is_constant(c) __builtin_constant_p (c) |
29 | # define is_constant(c) __builtin_constant_p (c) |
|
|
30 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
31 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
6 | #else |
32 | #else |
7 | # define is_constant(c) 0 |
33 | # define is_constant(c) 0 |
|
|
34 | # define expect(expr,value) (expr) |
|
|
35 | # define prefetch(addr,rw,locality) |
8 | #endif |
36 | #endif |
|
|
37 | |
|
|
38 | #if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) |
|
|
39 | # define decltype(x) typeof(x) |
|
|
40 | #endif |
|
|
41 | |
|
|
42 | // put into ifs if you are very sure that the expression |
|
|
43 | // is mostly true or mosty false. note that these return |
|
|
44 | // booleans, not the expression. |
|
|
45 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
46 | #define expect_true(expr) expect ((expr) != 0, 1) |
9 | |
47 | |
10 | #include <cstddef> |
48 | #include <cstddef> |
11 | #include <cmath> |
49 | #include <cmath> |
12 | #include <new> |
50 | #include <new> |
13 | #include <vector> |
51 | #include <vector> |
… | |
… | |
15 | #include <glib.h> |
53 | #include <glib.h> |
16 | |
54 | |
17 | #include <shstr.h> |
55 | #include <shstr.h> |
18 | #include <traits.h> |
56 | #include <traits.h> |
19 | |
57 | |
20 | // 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) |
21 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
59 | #define auto(var,expr) decltype(expr) var = (expr) |
22 | |
60 | |
23 | // very ugly macro that basicaly declares and initialises a variable |
61 | // very ugly macro that basicaly declares and initialises a variable |
24 | // that is in scope for the next statement only |
62 | // that is in scope for the next statement only |
25 | // 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 |
26 | // (note: works great for pointers) |
64 | // (note: works great for pointers) |
27 | // most ugly macro I ever wrote |
65 | // most ugly macro I ever wrote |
28 | #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) |
29 | |
67 | |
30 | // in range including end |
68 | // in range including end |
31 | #define IN_RANGE_INC(val,beg,end) \ |
69 | #define IN_RANGE_INC(val,beg,end) \ |
32 | ((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)) |
33 | |
71 | |
… | |
… | |
35 | #define IN_RANGE_EXC(val,beg,end) \ |
73 | #define IN_RANGE_EXC(val,beg,end) \ |
36 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
74 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
37 | |
75 | |
38 | void fork_abort (const char *msg); |
76 | void fork_abort (const char *msg); |
39 | |
77 | |
|
|
78 | // rationale for using (U) not (T) is to reduce signed/unsigned issues, |
|
|
79 | // as a is often a constant while b is the variable. it is still a bug, though. |
40 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
80 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
41 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
81 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
42 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
82 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } |
43 | |
83 | |
44 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
84 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
85 | |
|
|
86 | template<typename T> |
|
|
87 | static inline T |
|
|
88 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
|
|
89 | { |
|
|
90 | return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
|
|
91 | } |
|
|
92 | |
|
|
93 | // lots of stuff taken from FXT |
|
|
94 | |
|
|
95 | /* Rotate right. This is used in various places for checksumming */ |
|
|
96 | //TODO: that sucks, use a better checksum algo |
|
|
97 | static inline uint32_t |
|
|
98 | rotate_right (uint32_t c, uint32_t count = 1) |
|
|
99 | { |
|
|
100 | return (c << (32 - count)) | (c >> count); |
|
|
101 | } |
|
|
102 | |
|
|
103 | static inline uint32_t |
|
|
104 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
105 | { |
|
|
106 | return (c >> (32 - count)) | (c << count); |
|
|
107 | } |
|
|
108 | |
|
|
109 | // Return abs(a-b) |
|
|
110 | // Both a and b must not have the most significant bit set |
|
|
111 | static inline uint32_t |
|
|
112 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
113 | { |
|
|
114 | long d1 = b - a; |
|
|
115 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
116 | |
|
|
117 | return d1 - d2; // == (b - d) - (a + d); |
|
|
118 | } |
|
|
119 | |
|
|
120 | // Both a and b must not have the most significant bit set |
|
|
121 | static inline uint32_t |
|
|
122 | upos_min (uint32_t a, uint32_t b) |
|
|
123 | { |
|
|
124 | int32_t d = b - a; |
|
|
125 | d &= d >> 31; |
|
|
126 | return a + d; |
|
|
127 | } |
|
|
128 | |
|
|
129 | // Both a and b must not have the most significant bit set |
|
|
130 | static inline uint32_t |
|
|
131 | upos_max (uint32_t a, uint32_t b) |
|
|
132 | { |
|
|
133 | int32_t d = b - a; |
|
|
134 | d &= d >> 31; |
|
|
135 | return b - d; |
|
|
136 | } |
45 | |
137 | |
46 | // this is much faster than crossfires original algorithm |
138 | // this is much faster than crossfires original algorithm |
47 | // on modern cpus |
139 | // on modern cpus |
48 | inline int |
140 | inline int |
49 | isqrt (int n) |
141 | isqrt (int n) |
… | |
… | |
131 | |
223 | |
132 | // for symmetry |
224 | // for symmetry |
133 | template<typename T> |
225 | template<typename T> |
134 | inline void sfree (T *ptr, int n = 1) throw () |
226 | inline void sfree (T *ptr, int n = 1) throw () |
135 | { |
227 | { |
|
|
228 | #ifdef PREFER_MALLOC |
|
|
229 | free (ptr); |
|
|
230 | #else |
136 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
231 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
232 | #endif |
137 | } |
233 | } |
138 | |
234 | |
139 | // a STL-compatible allocator that uses g_slice |
235 | // a STL-compatible allocator that uses g_slice |
140 | // boy, this is verbose |
236 | // boy, this is verbose |
141 | template<typename Tp> |
237 | template<typename Tp> |
… | |
… | |
209 | |
305 | |
210 | void seed (uint32_t seed); |
306 | void seed (uint32_t seed); |
211 | uint32_t next (); |
307 | uint32_t next (); |
212 | |
308 | |
213 | // uniform distribution |
309 | // uniform distribution |
214 | uint32_t operator ()(uint32_t r_max) |
310 | uint32_t operator ()(uint32_t num) |
215 | { |
311 | { |
216 | return is_constant (r_max) |
312 | return is_constant (num) |
217 | ? this->next () % r_max |
313 | ? (next () * (uint64_t)num) >> 32U |
218 | : get_range (r_max); |
314 | : get_range (num); |
219 | } |
315 | } |
220 | |
316 | |
221 | // return a number within (min .. max) |
317 | // return a number within (min .. max) |
222 | int operator () (int r_min, int r_max) |
318 | int operator () (int r_min, int r_max) |
223 | { |
319 | { |
224 | return is_constant (r_min) && is_constant (r_max) |
320 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
225 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
321 | ? r_min + operator ()(r_max - r_min + 1) |
226 | : get_range (r_min, r_max); |
322 | : get_range (r_min, r_max); |
227 | } |
323 | } |
228 | |
324 | |
229 | double operator ()() |
325 | double operator ()() |
230 | { |
326 | { |
… | |
… | |
309 | { |
405 | { |
310 | return !strcmp (a, b); |
406 | return !strcmp (a, b); |
311 | } |
407 | } |
312 | }; |
408 | }; |
313 | |
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 |
314 | template<class T> |
414 | template<class T> |
315 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
415 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
316 | { |
416 | { |
317 | typedef typename unordered_vector::iterator iterator; |
417 | typedef typename unordered_vector::iterator iterator; |
318 | |
418 | |
… | |
… | |
328 | { |
428 | { |
329 | erase ((unsigned int )(i - this->begin ())); |
429 | erase ((unsigned int )(i - this->begin ())); |
330 | } |
430 | } |
331 | }; |
431 | }; |
332 | |
432 | |
333 | template<class T, int T::* index> |
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 |
|
|
445 | typedef int object_vector_index; |
|
|
446 | |
|
|
447 | template<class T, object_vector_index T::*indexmember> |
334 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
448 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
335 | { |
449 | { |
|
|
450 | typedef typename object_vector::iterator iterator; |
|
|
451 | |
|
|
452 | bool contains (const T *obj) const |
|
|
453 | { |
|
|
454 | return obj->*indexmember; |
|
|
455 | } |
|
|
456 | |
|
|
457 | iterator find (const T *obj) |
|
|
458 | { |
|
|
459 | return obj->*indexmember |
|
|
460 | ? this->begin () + obj->*indexmember - 1 |
|
|
461 | : this->end (); |
|
|
462 | } |
|
|
463 | |
336 | void insert (T *obj) |
464 | void insert (T *obj) |
337 | { |
465 | { |
338 | assert (!(obj->*index)); |
|
|
339 | push_back (obj); |
466 | push_back (obj); |
340 | obj->*index = this->size (); |
467 | obj->*indexmember = this->size (); |
341 | } |
468 | } |
342 | |
469 | |
343 | void insert (T &obj) |
470 | void insert (T &obj) |
344 | { |
471 | { |
345 | insert (&obj); |
472 | insert (&obj); |
346 | } |
473 | } |
347 | |
474 | |
348 | void erase (T *obj) |
475 | void erase (T *obj) |
349 | { |
476 | { |
350 | assert (obj->*index); |
|
|
351 | int pos = obj->*index; |
477 | unsigned int pos = obj->*indexmember; |
352 | obj->*index = 0; |
478 | obj->*indexmember = 0; |
353 | |
479 | |
354 | if (pos < this->size ()) |
480 | if (pos < this->size ()) |
355 | { |
481 | { |
356 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
482 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
357 | (*this)[pos - 1]->*index = pos; |
483 | (*this)[pos - 1]->*indexmember = pos; |
358 | } |
484 | } |
359 | |
485 | |
360 | this->pop_back (); |
486 | this->pop_back (); |
361 | } |
487 | } |
362 | |
488 | |
363 | void erase (T &obj) |
489 | void erase (T &obj) |
364 | { |
490 | { |
365 | errase (&obj); |
491 | erase (&obj); |
366 | } |
492 | } |
367 | }; |
493 | }; |
368 | |
494 | |
369 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
495 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
370 | void assign (char *dst, const char *src, int maxlen); |
496 | void assign (char *dst, const char *src, int maxlen); |
… | |
… | |
381 | // return current time as timestampe |
507 | // return current time as timestampe |
382 | tstamp now (); |
508 | tstamp now (); |
383 | |
509 | |
384 | int similar_direction (int a, int b); |
510 | int similar_direction (int a, int b); |
385 | |
511 | |
|
|
512 | // like printf, but returns a std::string |
|
|
513 | const std::string format (const char *format, ...); |
|
|
514 | |
386 | #endif |
515 | #endif |
387 | |
516 | |