1 | #ifndef UTIL_H__ |
1 | #ifndef UTIL_H__ |
2 | #define UTIL_H__ |
2 | #define UTIL_H__ |
|
|
3 | |
|
|
4 | //#define PREFER_MALLOC |
3 | |
5 | |
4 | #if __GNUC__ >= 3 |
6 | #if __GNUC__ >= 3 |
5 | # define is_constant(c) __builtin_constant_p (c) |
7 | # define is_constant(c) __builtin_constant_p (c) |
6 | #else |
8 | #else |
7 | # define is_constant(c) 0 |
9 | # define is_constant(c) 0 |
… | |
… | |
16 | |
18 | |
17 | #include <shstr.h> |
19 | #include <shstr.h> |
18 | #include <traits.h> |
20 | #include <traits.h> |
19 | |
21 | |
20 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
22 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
21 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
23 | #define auto(var,expr) typeof(expr) var = (expr) |
22 | |
24 | |
23 | // very ugly macro that basicaly declares and initialises a variable |
25 | // very ugly macro that basicaly declares and initialises a variable |
24 | // that is in scope for the next statement only |
26 | // that is in scope for the next statement only |
25 | // works only for stuff that can be assigned 0 and converts to false |
27 | // works only for stuff that can be assigned 0 and converts to false |
26 | // (note: works great for pointers) |
28 | // (note: works great for pointers) |
… | |
… | |
32 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
34 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
33 | |
35 | |
34 | // in range excluding end |
36 | // in range excluding end |
35 | #define IN_RANGE_EXC(val,beg,end) \ |
37 | #define IN_RANGE_EXC(val,beg,end) \ |
36 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
38 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
39 | |
|
|
40 | void fork_abort (const char *msg); |
|
|
41 | |
|
|
42 | // rationale for using (U) not (T) is to reduce signed/unsigned issues, |
|
|
43 | // as a is often a constant while b is the variable. it is still a bug, though. |
|
|
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
|
|
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
|
|
46 | 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; } |
|
|
47 | |
|
|
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
49 | |
|
|
50 | // lots of stuff taken from FXT |
|
|
51 | |
|
|
52 | /* Rotate right. This is used in various places for checksumming */ |
|
|
53 | //TODO: that sucks, use a better checksum algo |
|
|
54 | static inline uint32_t |
|
|
55 | rotate_right (uint32_t c, uint32_t count = 1) |
|
|
56 | { |
|
|
57 | return (c << (32 - count)) | (c >> count); |
|
|
58 | } |
|
|
59 | |
|
|
60 | static inline uint32_t |
|
|
61 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
62 | { |
|
|
63 | return (c >> (32 - count)) | (c << count); |
|
|
64 | } |
|
|
65 | |
|
|
66 | // Return abs(a-b) |
|
|
67 | // Both a and b must not have the most significant bit set |
|
|
68 | static inline uint32_t |
|
|
69 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
70 | { |
|
|
71 | long d1 = b - a; |
|
|
72 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
73 | |
|
|
74 | return d1 - d2; // == (b - d) - (a + d); |
|
|
75 | } |
|
|
76 | |
|
|
77 | // Both a and b must not have the most significant bit set |
|
|
78 | static inline uint32_t |
|
|
79 | upos_min (uint32_t a, uint32_t b) |
|
|
80 | { |
|
|
81 | int32_t d = b - a; |
|
|
82 | d &= d >> 31; |
|
|
83 | return a + d; |
|
|
84 | } |
|
|
85 | |
|
|
86 | // Both a and b must not have the most significant bit set |
|
|
87 | static inline uint32_t |
|
|
88 | upos_max (uint32_t a, uint32_t b) |
|
|
89 | { |
|
|
90 | int32_t d = b - a; |
|
|
91 | d &= d >> 31; |
|
|
92 | return b - d; |
|
|
93 | } |
37 | |
94 | |
38 | // this is much faster than crossfires original algorithm |
95 | // this is much faster than crossfires original algorithm |
39 | // on modern cpus |
96 | // on modern cpus |
40 | inline int |
97 | inline int |
41 | isqrt (int n) |
98 | isqrt (int n) |
… | |
… | |
58 | #if 0 |
115 | #if 0 |
59 | return dx_ > dy_ |
116 | return dx_ > dy_ |
60 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
117 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
61 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
118 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
62 | #else |
119 | #else |
63 | return dx + dy - min (dx, dy) * 5 / 8; |
120 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
64 | #endif |
121 | #endif |
65 | } |
122 | } |
66 | |
123 | |
|
|
124 | /* |
|
|
125 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
126 | * the "absolute" direction of a number (it actually takes care of |
|
|
127 | * "overflow" in previous calculations of a direction). |
|
|
128 | */ |
|
|
129 | inline int |
|
|
130 | absdir (int d) |
|
|
131 | { |
|
|
132 | return ((d - 1) & 7) + 1; |
|
|
133 | } |
67 | |
134 | |
68 | // makes dynamically allocated objects zero-initialised |
135 | // makes dynamically allocated objects zero-initialised |
69 | struct zero_initialised |
136 | struct zero_initialised |
70 | { |
137 | { |
71 | void *operator new (size_t s, void *p) |
138 | void *operator new (size_t s, void *p) |
… | |
… | |
113 | |
180 | |
114 | // for symmetry |
181 | // for symmetry |
115 | template<typename T> |
182 | template<typename T> |
116 | inline void sfree (T *ptr, int n = 1) throw () |
183 | inline void sfree (T *ptr, int n = 1) throw () |
117 | { |
184 | { |
|
|
185 | #ifdef PREFER_MALLOC |
|
|
186 | free (ptr); |
|
|
187 | #else |
118 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
188 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
189 | #endif |
119 | } |
190 | } |
120 | |
191 | |
121 | // a STL-compatible allocator that uses g_slice |
192 | // a STL-compatible allocator that uses g_slice |
122 | // boy, this is verbose |
193 | // boy, this is verbose |
123 | template<typename Tp> |
194 | template<typename Tp> |
… | |
… | |
170 | void destroy (pointer p) |
241 | void destroy (pointer p) |
171 | { |
242 | { |
172 | p->~Tp (); |
243 | p->~Tp (); |
173 | } |
244 | } |
174 | }; |
245 | }; |
|
|
246 | |
|
|
247 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
248 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
249 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
250 | struct tausworthe_random_generator |
|
|
251 | { |
|
|
252 | // generator |
|
|
253 | uint32_t state [4]; |
|
|
254 | |
|
|
255 | void operator =(const tausworthe_random_generator &src) |
|
|
256 | { |
|
|
257 | state [0] = src.state [0]; |
|
|
258 | state [1] = src.state [1]; |
|
|
259 | state [2] = src.state [2]; |
|
|
260 | state [3] = src.state [3]; |
|
|
261 | } |
|
|
262 | |
|
|
263 | void seed (uint32_t seed); |
|
|
264 | uint32_t next (); |
|
|
265 | |
|
|
266 | // uniform distribution |
|
|
267 | uint32_t operator ()(uint32_t r_max) |
|
|
268 | { |
|
|
269 | return is_constant (r_max) |
|
|
270 | ? (next () * (uint64_t)r_max) >> 32U |
|
|
271 | : get_range (r_max); |
|
|
272 | } |
|
|
273 | |
|
|
274 | // return a number within (min .. max) |
|
|
275 | int operator () (int r_min, int r_max) |
|
|
276 | { |
|
|
277 | return is_constant (r_min) && is_constant (r_max) |
|
|
278 | ? r_min + operator ()(max (r_max - r_min + 1, 1)) |
|
|
279 | : get_range (r_min, r_max); |
|
|
280 | } |
|
|
281 | |
|
|
282 | double operator ()() |
|
|
283 | { |
|
|
284 | return this->next () / (double)0xFFFFFFFFU; |
|
|
285 | } |
|
|
286 | |
|
|
287 | protected: |
|
|
288 | uint32_t get_range (uint32_t r_max); |
|
|
289 | int get_range (int r_min, int r_max); |
|
|
290 | }; |
|
|
291 | |
|
|
292 | typedef tausworthe_random_generator rand_gen; |
|
|
293 | |
|
|
294 | extern rand_gen rndm; |
175 | |
295 | |
176 | template<class T> |
296 | template<class T> |
177 | struct refptr |
297 | struct refptr |
178 | { |
298 | { |
179 | T *p; |
299 | T *p; |
… | |
… | |
279 | } |
399 | } |
280 | |
400 | |
281 | void erase (T *obj) |
401 | void erase (T *obj) |
282 | { |
402 | { |
283 | assert (obj->*index); |
403 | assert (obj->*index); |
284 | int pos = obj->*index; |
404 | unsigned int pos = obj->*index; |
285 | obj->*index = 0; |
405 | obj->*index = 0; |
286 | |
406 | |
287 | if (pos < this->size ()) |
407 | if (pos < this->size ()) |
288 | { |
408 | { |
289 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
409 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
… | |
… | |
297 | { |
417 | { |
298 | errase (&obj); |
418 | errase (&obj); |
299 | } |
419 | } |
300 | }; |
420 | }; |
301 | |
421 | |
302 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
303 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
304 | 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; } |
|
|
305 | |
|
|
306 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
307 | |
|
|
308 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
422 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
309 | void assign (char *dst, const char *src, int maxlen); |
423 | void assign (char *dst, const char *src, int maxlen); |
310 | |
424 | |
311 | // type-safe version of assign |
425 | // type-safe version of assign |
312 | template<int N> |
426 | template<int N> |