1 | #ifndef UTIL_H__ |
1 | #ifndef UTIL_H__ |
2 | #define UTIL_H__ |
2 | #define UTIL_H__ |
|
|
3 | |
|
|
4 | //#define PREFER_MALLOC |
3 | |
5 | |
4 | #if __GNUC__ >= 3 |
6 | #if __GNUC__ >= 3 |
5 | # define is_constant(c) __builtin_constant_p (c) |
7 | # define is_constant(c) __builtin_constant_p (c) |
6 | #else |
8 | #else |
7 | # define is_constant(c) 0 |
9 | # define is_constant(c) 0 |
… | |
… | |
34 | // in range excluding end |
36 | // in range excluding end |
35 | #define IN_RANGE_EXC(val,beg,end) \ |
37 | #define IN_RANGE_EXC(val,beg,end) \ |
36 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
38 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
37 | |
39 | |
38 | void fork_abort (const char *msg); |
40 | void fork_abort (const char *msg); |
|
|
41 | |
|
|
42 | // rationale for using (U) not (T) is to reduce signed/unsigned issues, |
|
|
43 | // as a is often a constant while b is the variable. it is still a bug, though. |
|
|
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; } |
|
|
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 | |
|
|
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
39 | |
49 | |
40 | // this is much faster than crossfires original algorithm |
50 | // this is much faster than crossfires original algorithm |
41 | // on modern cpus |
51 | // on modern cpus |
42 | inline int |
52 | inline int |
43 | isqrt (int n) |
53 | isqrt (int n) |
… | |
… | |
125 | |
135 | |
126 | // for symmetry |
136 | // for symmetry |
127 | template<typename T> |
137 | template<typename T> |
128 | inline void sfree (T *ptr, int n = 1) throw () |
138 | inline void sfree (T *ptr, int n = 1) throw () |
129 | { |
139 | { |
|
|
140 | #ifdef PREFER_MALLOC |
|
|
141 | free (ptr); |
|
|
142 | #else |
130 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
143 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
144 | #endif |
131 | } |
145 | } |
132 | |
146 | |
133 | // a STL-compatible allocator that uses g_slice |
147 | // a STL-compatible allocator that uses g_slice |
134 | // boy, this is verbose |
148 | // boy, this is verbose |
135 | template<typename Tp> |
149 | template<typename Tp> |
… | |
… | |
182 | void destroy (pointer p) |
196 | void destroy (pointer p) |
183 | { |
197 | { |
184 | p->~Tp (); |
198 | p->~Tp (); |
185 | } |
199 | } |
186 | }; |
200 | }; |
|
|
201 | |
|
|
202 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
203 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
204 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
205 | struct tausworthe_random_generator |
|
|
206 | { |
|
|
207 | // generator |
|
|
208 | uint32_t state [4]; |
|
|
209 | |
|
|
210 | void operator =(const tausworthe_random_generator &src) |
|
|
211 | { |
|
|
212 | state [0] = src.state [0]; |
|
|
213 | state [1] = src.state [1]; |
|
|
214 | state [2] = src.state [2]; |
|
|
215 | state [3] = src.state [3]; |
|
|
216 | } |
|
|
217 | |
|
|
218 | void seed (uint32_t seed); |
|
|
219 | uint32_t next (); |
|
|
220 | |
|
|
221 | // uniform distribution |
|
|
222 | uint32_t operator ()(uint32_t r_max) |
|
|
223 | { |
|
|
224 | return is_constant (r_max) |
|
|
225 | ? this->next () % r_max |
|
|
226 | : get_range (r_max); |
|
|
227 | } |
|
|
228 | |
|
|
229 | // return a number within (min .. max) |
|
|
230 | int operator () (int r_min, int r_max) |
|
|
231 | { |
|
|
232 | return is_constant (r_min) && is_constant (r_max) |
|
|
233 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
|
|
234 | : get_range (r_min, r_max); |
|
|
235 | } |
|
|
236 | |
|
|
237 | double operator ()() |
|
|
238 | { |
|
|
239 | return this->next () / (double)0xFFFFFFFFU; |
|
|
240 | } |
|
|
241 | |
|
|
242 | protected: |
|
|
243 | uint32_t get_range (uint32_t r_max); |
|
|
244 | int get_range (int r_min, int r_max); |
|
|
245 | }; |
|
|
246 | |
|
|
247 | typedef tausworthe_random_generator rand_gen; |
|
|
248 | |
|
|
249 | extern rand_gen rndm; |
187 | |
250 | |
188 | template<class T> |
251 | template<class T> |
189 | struct refptr |
252 | struct refptr |
190 | { |
253 | { |
191 | T *p; |
254 | T *p; |
… | |
… | |
309 | { |
372 | { |
310 | errase (&obj); |
373 | errase (&obj); |
311 | } |
374 | } |
312 | }; |
375 | }; |
313 | |
376 | |
314 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
315 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
316 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
|
|
317 | |
|
|
318 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
319 | |
|
|
320 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
377 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
321 | void assign (char *dst, const char *src, int maxlen); |
378 | void assign (char *dst, const char *src, int maxlen); |
322 | |
379 | |
323 | // type-safe version of assign |
380 | // type-safe version of assign |
324 | template<int N> |
381 | template<int N> |