ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.79 by root, Fri Dec 19 22:47:29 2008 UTC vs.
Revision 1.82 by root, Sat Dec 27 02:33:32 2008 UTC

72#endif 72#endif
73 73
74// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) 74// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever)
75#define auto(var,expr) decltype(expr) var = (expr) 75#define auto(var,expr) decltype(expr) var = (expr)
76 76
77// very ugly macro that basicaly declares and initialises a variable 77// very ugly macro that basically declares and initialises a variable
78// that is in scope for the next statement only 78// that is in scope for the next statement only
79// works only for stuff that can be assigned 0 and converts to false 79// works only for stuff that can be assigned 0 and converts to false
80// (note: works great for pointers) 80// (note: works great for pointers)
81// most ugly macro I ever wrote 81// most ugly macro I ever wrote
82#define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) 82#define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
96// as a is often a constant while b is the variable. it is still a bug, though. 96// as a is often a constant while b is the variable. it is still a bug, though.
97template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } 97template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
98template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } 98template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
99template<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; } 99template<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; }
100 100
101template<typename T> static inline void min_it (T &v, T m) { v = min (v, m); } 101template<typename T, typename U> static inline void min_it (T &v, U m) { v = min (v, (T)m); }
102template<typename T> static inline void max_it (T &v, T m) { v = max (v, m); } 102template<typename T, typename U> static inline void max_it (T &v, U m) { v = max (v, (T)m); }
103template<typename T> static inline void clamp_it (T &v, T a, T b) { v = clamp (v, a, b); } 103template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = clamp (v, (T)a, (T)b); }
104 104
105template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 105template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
106 106
107template<typename T, typename U, typename V> static inline T min (T a, U b, V c) { return min (a, min (b, c)); } 107template<typename T, typename U, typename V> static inline T min (T a, U b, V c) { return min (a, min (b, c)); }
108template<typename T, typename U, typename V> static inline T max (T a, U b, V c) { return max (a, max (b, c)); } 108template<typename T, typename U, typename V> static inline T max (T a, U b, V c) { return max (a, max (b, c)); }
406 uint32_t next (); 406 uint32_t next ();
407 407
408 // uniform distribution, 0 .. max (0, num - 1) 408 // uniform distribution, 0 .. max (0, num - 1)
409 uint32_t operator ()(uint32_t num) 409 uint32_t operator ()(uint32_t num)
410 { 410 {
411 return is_constant (num) 411 return !is_constant (num) ? get_range (num) // non-constant
412 ? (next () * (uint64_t)num) >> 32U 412 : num & (num - 1) ? (next () * (uint64_t)num) >> 32U // constant, non-power-of-two
413 : get_range (num); 413 : next () & (num - 1); // constant, power-of-two
414 } 414 }
415 415
416 // return a number within (min .. max) 416 // return a number within (min .. max)
417 int operator () (int r_min, int r_max) 417 int operator () (int r_min, int r_max)
418 { 418 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines