… | |
… | |
18 | |
18 | |
19 | #include <shstr.h> |
19 | #include <shstr.h> |
20 | #include <traits.h> |
20 | #include <traits.h> |
21 | |
21 | |
22 | // 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 |
23 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
23 | #define auto(var,expr) typeof(expr) var = (expr) |
24 | |
24 | |
25 | // very ugly macro that basicaly declares and initialises a variable |
25 | // very ugly macro that basicaly declares and initialises a variable |
26 | // that is in scope for the next statement only |
26 | // that is in scope for the next statement only |
27 | // 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 |
28 | // (note: works great for pointers) |
28 | // (note: works great for pointers) |
… | |
… | |
44 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
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; } |
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; } |
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 | |
47 | |
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
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 | } |
49 | |
94 | |
50 | // this is much faster than crossfires original algorithm |
95 | // this is much faster than crossfires original algorithm |
51 | // on modern cpus |
96 | // on modern cpus |
52 | inline int |
97 | inline int |
53 | isqrt (int n) |
98 | isqrt (int n) |
… | |
… | |
220 | |
265 | |
221 | // uniform distribution |
266 | // uniform distribution |
222 | uint32_t operator ()(uint32_t r_max) |
267 | uint32_t operator ()(uint32_t r_max) |
223 | { |
268 | { |
224 | return is_constant (r_max) |
269 | return is_constant (r_max) |
225 | ? this->next () % r_max |
270 | ? (next () * (uint64_t)r_max) >> 32U |
226 | : get_range (r_max); |
271 | : get_range (r_max); |
227 | } |
272 | } |
228 | |
273 | |
229 | // return a number within (min .. max) |
274 | // return a number within (min .. max) |
230 | int operator () (int r_min, int r_max) |
275 | int operator () (int r_min, int r_max) |
231 | { |
276 | { |
232 | return is_constant (r_min) && is_constant (r_max) |
277 | return is_constant (r_min) && is_constant (r_max) |
233 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
278 | ? r_min + operator ()(max (r_max - r_min + 1, 1)) |
234 | : get_range (r_min, r_max); |
279 | : get_range (r_min, r_max); |
235 | } |
280 | } |
236 | |
281 | |
237 | double operator ()() |
282 | double operator ()() |
238 | { |
283 | { |
… | |
… | |
354 | } |
399 | } |
355 | |
400 | |
356 | void erase (T *obj) |
401 | void erase (T *obj) |
357 | { |
402 | { |
358 | assert (obj->*index); |
403 | assert (obj->*index); |
359 | int pos = obj->*index; |
404 | unsigned int pos = obj->*index; |
360 | obj->*index = 0; |
405 | obj->*index = 0; |
361 | |
406 | |
362 | if (pos < this->size ()) |
407 | if (pos < this->size ()) |
363 | { |
408 | { |
364 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
409 | (*this)[pos - 1] = (*this)[this->size () - 1]; |