1 | #ifndef UTIL_H__ |
1 | #ifndef UTIL_H__ |
2 | #define UTIL_H__ |
2 | #define UTIL_H__ |
3 | |
3 | |
|
|
4 | //#define PREFER_MALLOC |
|
|
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) |
|
|
8 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
9 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
6 | #else |
10 | #else |
7 | # define is_constant(c) 0 |
11 | # define is_constant(c) 0 |
|
|
12 | # define expect(expr,value) (expr) |
|
|
13 | # define prefetch(addr,rw,locality) |
8 | #endif |
14 | #endif |
9 | |
15 | |
|
|
16 | // put into ifs if you are very sure that the expression |
|
|
17 | // is mostly true or mosty false. note that these return |
|
|
18 | // booleans, not the expression. |
|
|
19 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
20 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
21 | |
10 | #include <cstddef> |
22 | #include <cstddef> |
|
|
23 | #include <cmath> |
|
|
24 | #include <new> |
|
|
25 | #include <vector> |
11 | |
26 | |
12 | #include <glib.h> |
27 | #include <glib.h> |
13 | |
28 | |
|
|
29 | #include <shstr.h> |
|
|
30 | #include <traits.h> |
|
|
31 | |
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
32 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
33 | #define auto(var,expr) typeof(expr) var = (expr) |
|
|
34 | |
|
|
35 | // very ugly macro that basicaly declares and initialises a variable |
|
|
36 | // that is in scope for the next statement only |
|
|
37 | // works only for stuff that can be assigned 0 and converts to false |
|
|
38 | // (note: works great for pointers) |
|
|
39 | // most ugly macro I ever wrote |
|
|
40 | #define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
|
|
41 | |
|
|
42 | // in range including end |
|
|
43 | #define IN_RANGE_INC(val,beg,end) \ |
|
|
44 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
|
|
45 | |
|
|
46 | // in range excluding end |
|
|
47 | #define IN_RANGE_EXC(val,beg,end) \ |
|
|
48 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
49 | |
|
|
50 | void fork_abort (const char *msg); |
|
|
51 | |
|
|
52 | // rationale for using (U) not (T) is to reduce signed/unsigned issues, |
|
|
53 | // as a is often a constant while b is the variable. it is still a bug, though. |
|
|
54 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
|
|
55 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
|
|
56 | 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; } |
|
|
57 | |
|
|
58 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
59 | |
|
|
60 | template<typename T> |
|
|
61 | static inline T |
|
|
62 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
|
|
63 | { |
|
|
64 | return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
|
|
65 | } |
|
|
66 | |
|
|
67 | // lots of stuff taken from FXT |
|
|
68 | |
|
|
69 | /* Rotate right. This is used in various places for checksumming */ |
|
|
70 | //TODO: that sucks, use a better checksum algo |
|
|
71 | static inline uint32_t |
|
|
72 | rotate_right (uint32_t c, uint32_t count = 1) |
|
|
73 | { |
|
|
74 | return (c << (32 - count)) | (c >> count); |
|
|
75 | } |
|
|
76 | |
|
|
77 | static inline uint32_t |
|
|
78 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
79 | { |
|
|
80 | return (c >> (32 - count)) | (c << count); |
|
|
81 | } |
|
|
82 | |
|
|
83 | // Return abs(a-b) |
|
|
84 | // Both a and b must not have the most significant bit set |
|
|
85 | static inline uint32_t |
|
|
86 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
87 | { |
|
|
88 | long d1 = b - a; |
|
|
89 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
90 | |
|
|
91 | return d1 - d2; // == (b - d) - (a + d); |
|
|
92 | } |
|
|
93 | |
|
|
94 | // Both a and b must not have the most significant bit set |
|
|
95 | static inline uint32_t |
|
|
96 | upos_min (uint32_t a, uint32_t b) |
|
|
97 | { |
|
|
98 | int32_t d = b - a; |
|
|
99 | d &= d >> 31; |
|
|
100 | return a + d; |
|
|
101 | } |
|
|
102 | |
|
|
103 | // Both a and b must not have the most significant bit set |
|
|
104 | static inline uint32_t |
|
|
105 | upos_max (uint32_t a, uint32_t b) |
|
|
106 | { |
|
|
107 | int32_t d = b - a; |
|
|
108 | d &= d >> 31; |
|
|
109 | return b - d; |
|
|
110 | } |
|
|
111 | |
|
|
112 | // this is much faster than crossfires original algorithm |
|
|
113 | // on modern cpus |
|
|
114 | inline int |
|
|
115 | isqrt (int n) |
|
|
116 | { |
|
|
117 | return (int)sqrtf ((float)n); |
|
|
118 | } |
|
|
119 | |
|
|
120 | // this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
|
|
121 | #if 0 |
|
|
122 | // and has a max. error of 6 in the range -100..+100. |
|
|
123 | #else |
|
|
124 | // and has a max. error of 9 in the range -100..+100. |
|
|
125 | #endif |
|
|
126 | inline int |
|
|
127 | idistance (int dx, int dy) |
|
|
128 | { |
|
|
129 | unsigned int dx_ = abs (dx); |
|
|
130 | unsigned int dy_ = abs (dy); |
|
|
131 | |
|
|
132 | #if 0 |
|
|
133 | return dx_ > dy_ |
|
|
134 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
|
|
135 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
|
|
136 | #else |
|
|
137 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
|
|
138 | #endif |
|
|
139 | } |
|
|
140 | |
|
|
141 | /* |
|
|
142 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
143 | * the "absolute" direction of a number (it actually takes care of |
|
|
144 | * "overflow" in previous calculations of a direction). |
|
|
145 | */ |
|
|
146 | inline int |
|
|
147 | absdir (int d) |
|
|
148 | { |
|
|
149 | return ((d - 1) & 7) + 1; |
|
|
150 | } |
16 | |
151 | |
17 | // makes dynamically allocated objects zero-initialised |
152 | // makes dynamically allocated objects zero-initialised |
18 | struct zero_initialised |
153 | struct zero_initialised |
19 | { |
154 | { |
20 | void *operator new (size_t s, void *p) |
155 | void *operator new (size_t s, void *p) |
… | |
… | |
62 | |
197 | |
63 | // for symmetry |
198 | // for symmetry |
64 | template<typename T> |
199 | template<typename T> |
65 | inline void sfree (T *ptr, int n = 1) throw () |
200 | inline void sfree (T *ptr, int n = 1) throw () |
66 | { |
201 | { |
|
|
202 | #ifdef PREFER_MALLOC |
|
|
203 | free (ptr); |
|
|
204 | #else |
67 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
205 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
206 | #endif |
68 | } |
207 | } |
69 | |
208 | |
70 | // a STL-compatible allocator that uses g_slice |
209 | // a STL-compatible allocator that uses g_slice |
71 | // boy, this is verbose |
210 | // boy, this is verbose |
72 | template<typename Tp> |
211 | template<typename Tp> |
… | |
… | |
120 | { |
259 | { |
121 | p->~Tp (); |
260 | p->~Tp (); |
122 | } |
261 | } |
123 | }; |
262 | }; |
124 | |
263 | |
125 | struct refcounted |
264 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
265 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
266 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
267 | struct tausworthe_random_generator |
126 | { |
268 | { |
127 | refcounted () : refcnt (0) { } |
269 | // generator |
128 | virtual ~refcounted (); |
270 | uint32_t state [4]; |
129 | void refcnt_inc () { ++refcnt; } |
271 | |
130 | void refcnt_dec () { --refcnt; } |
272 | void operator =(const tausworthe_random_generator &src) |
131 | bool dead () { return refcnt == 0; } |
273 | { |
132 | mutable int refcnt; |
274 | state [0] = src.state [0]; |
133 | #if 0 |
275 | state [1] = src.state [1]; |
134 | private: |
276 | state [2] = src.state [2]; |
135 | static refcounted *rc_first; |
277 | state [3] = src.state [3]; |
136 | refcounted *rc_next; |
278 | } |
137 | #endif |
279 | |
|
|
280 | void seed (uint32_t seed); |
|
|
281 | uint32_t next (); |
|
|
282 | |
|
|
283 | // uniform distribution |
|
|
284 | uint32_t operator ()(uint32_t num) |
|
|
285 | { |
|
|
286 | return is_constant (num) |
|
|
287 | ? (next () * (uint64_t)num) >> 32U |
|
|
288 | : get_range (num); |
|
|
289 | } |
|
|
290 | |
|
|
291 | // return a number within (min .. max) |
|
|
292 | int operator () (int r_min, int r_max) |
|
|
293 | { |
|
|
294 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
|
|
295 | ? r_min + operator ()(r_max - r_min + 1) |
|
|
296 | : get_range (r_min, r_max); |
|
|
297 | } |
|
|
298 | |
|
|
299 | double operator ()() |
|
|
300 | { |
|
|
301 | return this->next () / (double)0xFFFFFFFFU; |
|
|
302 | } |
|
|
303 | |
|
|
304 | protected: |
|
|
305 | uint32_t get_range (uint32_t r_max); |
|
|
306 | int get_range (int r_min, int r_max); |
138 | }; |
307 | }; |
|
|
308 | |
|
|
309 | typedef tausworthe_random_generator rand_gen; |
|
|
310 | |
|
|
311 | extern rand_gen rndm; |
139 | |
312 | |
140 | template<class T> |
313 | template<class T> |
141 | struct refptr |
314 | struct refptr |
142 | { |
315 | { |
143 | T *p; |
316 | T *p; |
… | |
… | |
166 | T *operator ->() const { return p; } |
339 | T *operator ->() const { return p; } |
167 | |
340 | |
168 | operator T *() const { return p; } |
341 | operator T *() const { return p; } |
169 | }; |
342 | }; |
170 | |
343 | |
171 | typedef refptr<player> player_ptr; |
344 | typedef refptr<maptile> maptile_ptr; |
172 | typedef refptr<object> object_ptr; |
345 | typedef refptr<object> object_ptr; |
173 | typedef refptr<archetype> arch_ptr; |
346 | typedef refptr<archetype> arch_ptr; |
|
|
347 | typedef refptr<client> client_ptr; |
|
|
348 | typedef refptr<player> player_ptr; |
174 | |
349 | |
175 | struct str_hash |
350 | struct str_hash |
176 | { |
351 | { |
177 | std::size_t operator ()(const char *s) const |
352 | std::size_t operator ()(const char *s) const |
178 | { |
353 | { |
… | |
… | |
204 | { |
379 | { |
205 | return !strcmp (a, b); |
380 | return !strcmp (a, b); |
206 | } |
381 | } |
207 | }; |
382 | }; |
208 | |
383 | |
209 | #include <vector> |
|
|
210 | |
|
|
211 | template<class obj> |
384 | template<class T> |
212 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
385 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
213 | { |
386 | { |
214 | typedef typename unordered_vector::iterator iterator; |
387 | typedef typename unordered_vector::iterator iterator; |
215 | |
388 | |
216 | void erase (unsigned int pos) |
389 | void erase (unsigned int pos) |
217 | { |
390 | { |
… | |
… | |
225 | { |
398 | { |
226 | erase ((unsigned int )(i - this->begin ())); |
399 | erase ((unsigned int )(i - this->begin ())); |
227 | } |
400 | } |
228 | }; |
401 | }; |
229 | |
402 | |
230 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
403 | template<class T, int T::* index> |
231 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
404 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
232 | 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; } |
405 | { |
|
|
406 | void insert (T *obj) |
|
|
407 | { |
|
|
408 | assert (!(obj->*index)); |
|
|
409 | push_back (obj); |
|
|
410 | obj->*index = this->size (); |
|
|
411 | } |
233 | |
412 | |
234 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
413 | void insert (T &obj) |
|
|
414 | { |
|
|
415 | insert (&obj); |
|
|
416 | } |
|
|
417 | |
|
|
418 | void erase (T *obj) |
|
|
419 | { |
|
|
420 | assert (obj->*index); |
|
|
421 | unsigned int pos = obj->*index; |
|
|
422 | obj->*index = 0; |
|
|
423 | |
|
|
424 | if (pos < this->size ()) |
|
|
425 | { |
|
|
426 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
427 | (*this)[pos - 1]->*index = pos; |
|
|
428 | } |
|
|
429 | |
|
|
430 | this->pop_back (); |
|
|
431 | } |
|
|
432 | |
|
|
433 | void erase (T &obj) |
|
|
434 | { |
|
|
435 | errase (&obj); |
|
|
436 | } |
|
|
437 | }; |
235 | |
438 | |
236 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
439 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
237 | void assign (char *dst, const char *src, int maxlen); |
440 | void assign (char *dst, const char *src, int maxlen); |
238 | |
441 | |
239 | // type-safe version of assign |
442 | // type-safe version of assign |
… | |
… | |
246 | typedef double tstamp; |
449 | typedef double tstamp; |
247 | |
450 | |
248 | // return current time as timestampe |
451 | // return current time as timestampe |
249 | tstamp now (); |
452 | tstamp now (); |
250 | |
453 | |
|
|
454 | int similar_direction (int a, int b); |
|
|
455 | |
|
|
456 | // like printf, but returns a std::string |
|
|
457 | const std::string format (const char *format, ...); |
|
|
458 | |
251 | #endif |
459 | #endif |
252 | |
460 | |