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 |
8 | #endif |
10 | #endif |
9 | |
11 | |
10 | #include <cstddef> |
12 | #include <cstddef> |
|
|
13 | #include <cmath> |
|
|
14 | #include <new> |
|
|
15 | #include <vector> |
11 | |
16 | |
12 | #include <glib.h> |
17 | #include <glib.h> |
|
|
18 | |
|
|
19 | #include <shstr.h> |
|
|
20 | #include <traits.h> |
13 | |
21 | |
14 | // 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 |
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
23 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
16 | |
24 | |
|
|
25 | // very ugly macro that basicaly declares and initialises a variable |
|
|
26 | // that is in scope for the next statement only |
|
|
27 | // works only for stuff that can be assigned 0 and converts to false |
|
|
28 | // (note: works great for pointers) |
|
|
29 | // most ugly macro I ever wrote |
|
|
30 | #define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
|
|
31 | |
|
|
32 | // in range including end |
|
|
33 | #define IN_RANGE_INC(val,beg,end) \ |
|
|
34 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
|
|
35 | |
|
|
36 | // in range excluding end |
|
|
37 | #define IN_RANGE_EXC(val,beg,end) \ |
|
|
38 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
39 | |
|
|
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; } |
|
|
49 | |
|
|
50 | // lots of stuff taken from FXT |
|
|
51 | |
|
|
52 | /* Rotate right. This is used in various places for checksumming */ |
|
|
53 | //TODO: this sucks, use a better checksum algo |
|
|
54 | static inline uint32_t |
|
|
55 | rotate_right (uint32_t c) |
|
|
56 | { |
|
|
57 | return (c << 31) | (c >> 1); |
|
|
58 | } |
|
|
59 | |
|
|
60 | // Return abs(a-b) |
|
|
61 | // Both a and b must not have the most significant bit set |
|
|
62 | static inline uint32_t |
|
|
63 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
64 | { |
|
|
65 | long d1 = b - a; |
|
|
66 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
67 | |
|
|
68 | return d1 - d2; // == (b - d) - (a + d); |
|
|
69 | } |
|
|
70 | |
|
|
71 | // Both a and b must not have the most significant bit set |
|
|
72 | static inline uint32_t |
|
|
73 | upos_min (uint32_t a, uint32_t b) |
|
|
74 | { |
|
|
75 | int32_t d = b - a; |
|
|
76 | d &= d >> 31; |
|
|
77 | return a + d; |
|
|
78 | } |
|
|
79 | |
|
|
80 | // Both a and b must not have the most significant bit set |
|
|
81 | static inline uint32_t |
|
|
82 | upos_max (uint32_t a, uint32_t b) |
|
|
83 | { |
|
|
84 | int32_t d = b - a; |
|
|
85 | d &= d >> 31; |
|
|
86 | return b - d; |
|
|
87 | } |
|
|
88 | |
|
|
89 | // this is much faster than crossfires original algorithm |
|
|
90 | // on modern cpus |
|
|
91 | inline int |
|
|
92 | isqrt (int n) |
|
|
93 | { |
|
|
94 | return (int)sqrtf ((float)n); |
|
|
95 | } |
|
|
96 | |
|
|
97 | // this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
|
|
98 | #if 0 |
|
|
99 | // and has a max. error of 6 in the range -100..+100. |
|
|
100 | #else |
|
|
101 | // and has a max. error of 9 in the range -100..+100. |
|
|
102 | #endif |
|
|
103 | inline int |
|
|
104 | idistance (int dx, int dy) |
|
|
105 | { |
|
|
106 | unsigned int dx_ = abs (dx); |
|
|
107 | unsigned int dy_ = abs (dy); |
|
|
108 | |
|
|
109 | #if 0 |
|
|
110 | return dx_ > dy_ |
|
|
111 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
|
|
112 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
|
|
113 | #else |
|
|
114 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
|
|
115 | #endif |
|
|
116 | } |
|
|
117 | |
|
|
118 | /* |
|
|
119 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
120 | * the "absolute" direction of a number (it actually takes care of |
|
|
121 | * "overflow" in previous calculations of a direction). |
|
|
122 | */ |
|
|
123 | inline int |
|
|
124 | absdir (int d) |
|
|
125 | { |
|
|
126 | return ((d - 1) & 7) + 1; |
|
|
127 | } |
|
|
128 | |
17 | // makes dynamically allocated objects zero-initialised |
129 | // makes dynamically allocated objects zero-initialised |
18 | struct zero_initialised |
130 | struct zero_initialised |
19 | { |
131 | { |
20 | void *operator new (size_t s, void *p) |
132 | void *operator new (size_t s, void *p) |
21 | { |
133 | { |
… | |
… | |
42 | { |
154 | { |
43 | g_slice_free1 (s, p); |
155 | g_slice_free1 (s, p); |
44 | } |
156 | } |
45 | }; |
157 | }; |
46 | |
158 | |
|
|
159 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
160 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
161 | |
47 | // strictly the same as g_slice_alloc, but never returns 0 |
162 | // strictly the same as g_slice_alloc, but never returns 0 |
48 | void *salloc (int size) throw (std::bad_alloc); |
163 | template<typename T> |
|
|
164 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
165 | |
49 | // also copies src into the new area, like "memdup" |
166 | // also copies src into the new area, like "memdup" |
50 | // if src is 0, clears the memory |
167 | // if src is 0, clears the memory |
51 | void *salloc (int size, void *src) throw (std::bad_alloc); |
|
|
52 | |
|
|
53 | // and as a template |
|
|
54 | template<typename T> |
168 | template<typename T> |
55 | inline T *salloc (int size) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T)); } |
169 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
170 | |
|
|
171 | // clears the memory |
56 | template<typename T> |
172 | template<typename T> |
57 | inline T *salloc (int size, T *src) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T), (void *)src); } |
173 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
58 | |
174 | |
59 | // for symmetry |
175 | // for symmetry |
60 | template<typename T> |
176 | template<typename T> |
61 | inline void sfree (T *ptr, int size) throw () |
177 | inline void sfree (T *ptr, int n = 1) throw () |
62 | { |
178 | { |
|
|
179 | #ifdef PREFER_MALLOC |
|
|
180 | free (ptr); |
|
|
181 | #else |
63 | g_slice_free1 (size * sizeof (T), (void *)ptr); |
182 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
183 | #endif |
64 | } |
184 | } |
65 | |
185 | |
66 | // a STL-compatible allocator that uses g_slice |
186 | // a STL-compatible allocator that uses g_slice |
67 | // boy, this is verbose |
187 | // boy, this is verbose |
68 | template<typename Tp> |
188 | template<typename Tp> |
… | |
… | |
116 | { |
236 | { |
117 | p->~Tp (); |
237 | p->~Tp (); |
118 | } |
238 | } |
119 | }; |
239 | }; |
120 | |
240 | |
121 | struct refcounted |
241 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
242 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
243 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
244 | struct tausworthe_random_generator |
122 | { |
245 | { |
123 | refcounted () : refcnt (0) { } |
246 | // generator |
124 | // virtual ~refcounted (); |
247 | uint32_t state [4]; |
125 | void refcnt_inc () { ++refcnt; } |
248 | |
126 | void refcnt_dec () { --refcnt; } |
249 | void operator =(const tausworthe_random_generator &src) |
127 | bool dead () { return refcnt == 0; } |
250 | { |
128 | mutable int refcnt; |
251 | state [0] = src.state [0]; |
129 | #if 0 |
252 | state [1] = src.state [1]; |
130 | private: |
253 | state [2] = src.state [2]; |
131 | static refcounted *rc_first; |
254 | state [3] = src.state [3]; |
132 | refcounted *rc_next; |
255 | } |
133 | #endif |
256 | |
|
|
257 | void seed (uint32_t seed); |
|
|
258 | uint32_t next (); |
|
|
259 | |
|
|
260 | // uniform distribution |
|
|
261 | uint32_t operator ()(uint32_t r_max) |
|
|
262 | { |
|
|
263 | return is_constant (r_max) |
|
|
264 | ? this->next () % r_max |
|
|
265 | : get_range (r_max); |
|
|
266 | } |
|
|
267 | |
|
|
268 | // return a number within (min .. max) |
|
|
269 | int operator () (int r_min, int r_max) |
|
|
270 | { |
|
|
271 | return is_constant (r_min) && is_constant (r_max) |
|
|
272 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
|
|
273 | : get_range (r_min, r_max); |
|
|
274 | } |
|
|
275 | |
|
|
276 | double operator ()() |
|
|
277 | { |
|
|
278 | return this->next () / (double)0xFFFFFFFFU; |
|
|
279 | } |
|
|
280 | |
|
|
281 | protected: |
|
|
282 | uint32_t get_range (uint32_t r_max); |
|
|
283 | int get_range (int r_min, int r_max); |
134 | }; |
284 | }; |
|
|
285 | |
|
|
286 | typedef tausworthe_random_generator rand_gen; |
|
|
287 | |
|
|
288 | extern rand_gen rndm; |
135 | |
289 | |
136 | template<class T> |
290 | template<class T> |
137 | struct refptr |
291 | struct refptr |
138 | { |
292 | { |
139 | T *p; |
293 | T *p; |
… | |
… | |
161 | T &operator * () const { return *p; } |
315 | T &operator * () const { return *p; } |
162 | T *operator ->() const { return p; } |
316 | T *operator ->() const { return p; } |
163 | |
317 | |
164 | operator T *() const { return p; } |
318 | operator T *() const { return p; } |
165 | }; |
319 | }; |
|
|
320 | |
|
|
321 | typedef refptr<maptile> maptile_ptr; |
|
|
322 | typedef refptr<object> object_ptr; |
|
|
323 | typedef refptr<archetype> arch_ptr; |
|
|
324 | typedef refptr<client> client_ptr; |
|
|
325 | typedef refptr<player> player_ptr; |
166 | |
326 | |
167 | struct str_hash |
327 | struct str_hash |
168 | { |
328 | { |
169 | std::size_t operator ()(const char *s) const |
329 | std::size_t operator ()(const char *s) const |
170 | { |
330 | { |
… | |
… | |
196 | { |
356 | { |
197 | return !strcmp (a, b); |
357 | return !strcmp (a, b); |
198 | } |
358 | } |
199 | }; |
359 | }; |
200 | |
360 | |
201 | #include <vector> |
|
|
202 | |
|
|
203 | template<class obj> |
361 | template<class T> |
204 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
362 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
205 | { |
363 | { |
206 | typedef typename unordered_vector::iterator iterator; |
364 | typedef typename unordered_vector::iterator iterator; |
207 | |
365 | |
208 | void erase (unsigned int pos) |
366 | void erase (unsigned int pos) |
209 | { |
367 | { |
… | |
… | |
217 | { |
375 | { |
218 | erase ((unsigned int )(i - this->begin ())); |
376 | erase ((unsigned int )(i - this->begin ())); |
219 | } |
377 | } |
220 | }; |
378 | }; |
221 | |
379 | |
222 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
380 | template<class T, int T::* index> |
223 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
381 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
224 | 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; } |
382 | { |
|
|
383 | void insert (T *obj) |
|
|
384 | { |
|
|
385 | assert (!(obj->*index)); |
|
|
386 | push_back (obj); |
|
|
387 | obj->*index = this->size (); |
|
|
388 | } |
225 | |
389 | |
226 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
390 | void insert (T &obj) |
|
|
391 | { |
|
|
392 | insert (&obj); |
|
|
393 | } |
|
|
394 | |
|
|
395 | void erase (T *obj) |
|
|
396 | { |
|
|
397 | assert (obj->*index); |
|
|
398 | int pos = obj->*index; |
|
|
399 | obj->*index = 0; |
|
|
400 | |
|
|
401 | if (pos < this->size ()) |
|
|
402 | { |
|
|
403 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
404 | (*this)[pos - 1]->*index = pos; |
|
|
405 | } |
|
|
406 | |
|
|
407 | this->pop_back (); |
|
|
408 | } |
|
|
409 | |
|
|
410 | void erase (T &obj) |
|
|
411 | { |
|
|
412 | errase (&obj); |
|
|
413 | } |
|
|
414 | }; |
227 | |
415 | |
228 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
416 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
229 | void assign (char *dst, const char *src, int maxlen); |
417 | void assign (char *dst, const char *src, int maxlen); |
230 | |
418 | |
231 | // type-safe version of assign |
419 | // type-safe version of assign |
… | |
… | |
238 | typedef double tstamp; |
426 | typedef double tstamp; |
239 | |
427 | |
240 | // return current time as timestampe |
428 | // return current time as timestampe |
241 | tstamp now (); |
429 | tstamp now (); |
242 | |
430 | |
|
|
431 | int similar_direction (int a, int b); |
|
|
432 | |
243 | #endif |
433 | #endif |
244 | |
434 | |