--- deliantra/server/include/util.h 2010/03/26 01:04:44 1.97 +++ deliantra/server/include/util.h 2010/04/29 15:49:04 1.103 @@ -57,6 +57,15 @@ // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) #define auto(var,expr) decltype(expr) var = (expr) +// could use the sizeof (arr) /( sizeof (arr [0]) here, but C++ is +// much more obfuscated... :) + +template +inline int array_length (const T (&arr)[N]) +{ + return N; +} + // very ugly macro that basically declares and initialises a variable // that is in scope for the next statement only // works only for stuff that can be assigned 0 and converts to false @@ -95,12 +104,19 @@ static inline T sign (T v) { return v < 0 ? -1 : +1; } // relies on 2c representation template<> -inline sint8 sign (sint8 v) { return 1 - (sint8 (uint8 (v) >> 7) * 2); } +inline sint8 sign (sint8 v) { return 1 - (sint8 (uint8 (v) >> 7) * 2); } +template<> +inline sint16 sign (sint16 v) { return 1 - (sint16 (uint16 (v) >> 15) * 2); } +template<> +inline sint32 sign (sint32 v) { return 1 - (sint32 (uint32 (v) >> 31) * 2); } // sign0 returns -1, 0 or +1 template static inline T sign0 (T v) { return v ? sign (v) : 0; } +template +static inline T copysign (T a, U b) { return a > 0 ? b : -b; } + // div* only work correctly for div > 0 // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) template static inline T div (T val, T div) @@ -469,7 +485,7 @@ : this->next () & (num - 1); // constant, power-of-two } - // return a number within (min .. max) + // return a number within the closed interval [min .. max] int operator () (int r_min, int r_max) { return is_constant (r_min) && is_constant (r_max) && r_min <= r_max @@ -477,6 +493,7 @@ : get_range (r_min, r_max); } + // return a number within the closed interval [0..1] double operator ()() { return this->next () / (double)0xFFFFFFFFU; @@ -561,6 +578,7 @@ typedef refptr arch_ptr; typedef refptr client_ptr; typedef refptr player_ptr; +typedef refptr region_ptr; #define STRHSH_NULL 2166136261 @@ -575,7 +593,7 @@ uint32_t hash = STRHSH_NULL; while (*s) - hash = (hash ^ *s++) * 16777619; + hash = (hash ^ *s++) * 16777619U; return hash; } @@ -586,7 +604,7 @@ uint32_t hash = STRHSH_NULL; while (len--) - hash = (hash ^ *s++) * 16777619; + hash = (hash ^ *s++) * 16777619U; return hash; }