… | |
… | |
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) |
… | |
… | |
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 | |
49 | |
|
|
50 | template<typename T> |
|
|
51 | static inline T |
|
|
52 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
|
|
53 | { |
|
|
54 | return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
|
|
55 | } |
|
|
56 | |
50 | // lots of stuff taken from FXT |
57 | // lots of stuff taken from FXT |
51 | |
58 | |
52 | /* Rotate right. This is used in various places for checksumming */ |
59 | /* Rotate right. This is used in various places for checksumming */ |
53 | //TODO: this sucks, use a better checksum algo |
60 | //TODO: that sucks, use a better checksum algo |
54 | static inline uint32_t |
61 | static inline uint32_t |
55 | rotate_right (uint32_t c) |
62 | rotate_right (uint32_t c, uint32_t count = 1) |
56 | { |
63 | { |
57 | return (c << 31) | (c >> 1); |
64 | return (c << (32 - count)) | (c >> count); |
|
|
65 | } |
|
|
66 | |
|
|
67 | static inline uint32_t |
|
|
68 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
69 | { |
|
|
70 | return (c >> (32 - count)) | (c << count); |
58 | } |
71 | } |
59 | |
72 | |
60 | // Return abs(a-b) |
73 | // Return abs(a-b) |
61 | // Both a and b must not have the most significant bit set |
74 | // Both a and b must not have the most significant bit set |
62 | static inline uint32_t |
75 | static inline uint32_t |
… | |
… | |
256 | |
269 | |
257 | void seed (uint32_t seed); |
270 | void seed (uint32_t seed); |
258 | uint32_t next (); |
271 | uint32_t next (); |
259 | |
272 | |
260 | // uniform distribution |
273 | // uniform distribution |
261 | uint32_t operator ()(uint32_t r_max) |
274 | uint32_t operator ()(uint32_t num) |
262 | { |
275 | { |
263 | return is_constant (r_max) |
276 | return is_constant (num) |
264 | ? this->next () % r_max |
277 | ? (next () * (uint64_t)num) >> 32U |
265 | : get_range (r_max); |
278 | : get_range (num); |
266 | } |
279 | } |
267 | |
280 | |
268 | // return a number within (min .. max) |
281 | // return a number within (min .. max) |
269 | int operator () (int r_min, int r_max) |
282 | int operator () (int r_min, int r_max) |
270 | { |
283 | { |
271 | return is_constant (r_min) && is_constant (r_max) |
284 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
272 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
285 | ? r_min + operator ()(r_max - r_min + 1) |
273 | : get_range (r_min, r_max); |
286 | : get_range (r_min, r_max); |
274 | } |
287 | } |
275 | |
288 | |
276 | double operator ()() |
289 | double operator ()() |
277 | { |
290 | { |
… | |
… | |
393 | } |
406 | } |
394 | |
407 | |
395 | void erase (T *obj) |
408 | void erase (T *obj) |
396 | { |
409 | { |
397 | assert (obj->*index); |
410 | assert (obj->*index); |
398 | int pos = obj->*index; |
411 | unsigned int pos = obj->*index; |
399 | obj->*index = 0; |
412 | obj->*index = 0; |
400 | |
413 | |
401 | if (pos < this->size ()) |
414 | if (pos < this->size ()) |
402 | { |
415 | { |
403 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
416 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
… | |
… | |
428 | // return current time as timestampe |
441 | // return current time as timestampe |
429 | tstamp now (); |
442 | tstamp now (); |
430 | |
443 | |
431 | int similar_direction (int a, int b); |
444 | int similar_direction (int a, int b); |
432 | |
445 | |
|
|
446 | // like printf, but returns a std::string |
|
|
447 | const std::string format (const char *format, ...); |
|
|
448 | |
433 | #endif |
449 | #endif |
434 | |
450 | |