… | |
… | |
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) |
… | |
… | |
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 | |
49 | |
50 | // lots of stuff taken from FXT |
50 | // lots of stuff taken from FXT |
51 | |
51 | |
52 | /* Rotate right. This is used in various places for checksumming */ |
52 | /* Rotate right. This is used in various places for checksumming */ |
53 | //TODO: this sucks, use a better checksum algo |
53 | //TODO: that sucks, use a better checksum algo |
54 | static inline uint32_t |
54 | static inline uint32_t |
55 | rotate_right (uint32_t c) |
55 | rotate_right (uint32_t c, uint32_t count = 1) |
56 | { |
56 | { |
57 | return (c << 31) | (c >> 1); |
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); |
58 | } |
64 | } |
59 | |
65 | |
60 | // Return abs(a-b) |
66 | // Return abs(a-b) |
61 | // Both a and b must not have the most significant bit set |
67 | // Both a and b must not have the most significant bit set |
62 | static inline uint32_t |
68 | static inline uint32_t |
… | |
… | |
259 | |
265 | |
260 | // uniform distribution |
266 | // uniform distribution |
261 | uint32_t operator ()(uint32_t r_max) |
267 | uint32_t operator ()(uint32_t r_max) |
262 | { |
268 | { |
263 | return is_constant (r_max) |
269 | return is_constant (r_max) |
264 | ? this->next () % r_max |
270 | ? (next () * (uint64_t)r_max) >> 32U |
265 | : get_range (r_max); |
271 | : get_range (r_max); |
266 | } |
272 | } |
267 | |
273 | |
268 | // return a number within (min .. max) |
274 | // return a number within (min .. max) |
269 | int operator () (int r_min, int r_max) |
275 | int operator () (int r_min, int r_max) |
270 | { |
276 | { |
271 | return is_constant (r_min) && is_constant (r_max) |
277 | return is_constant (r_min) && is_constant (r_max) |
272 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
278 | ? r_min + operator ()(max (r_max - r_min + 1, 1)) |
273 | : get_range (r_min, r_max); |
279 | : get_range (r_min, r_max); |
274 | } |
280 | } |
275 | |
281 | |
276 | double operator ()() |
282 | double operator ()() |
277 | { |
283 | { |
… | |
… | |
393 | } |
399 | } |
394 | |
400 | |
395 | void erase (T *obj) |
401 | void erase (T *obj) |
396 | { |
402 | { |
397 | assert (obj->*index); |
403 | assert (obj->*index); |
398 | int pos = obj->*index; |
404 | unsigned int pos = obj->*index; |
399 | obj->*index = 0; |
405 | obj->*index = 0; |
400 | |
406 | |
401 | if (pos < this->size ()) |
407 | if (pos < this->size ()) |
402 | { |
408 | { |
403 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
409 | (*this)[pos - 1] = (*this)[this->size () - 1]; |