1 |
/* |
2 |
* This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 |
* |
6 |
* Crossfire TRT is free software: you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation, either version 3 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 |
* |
19 |
* The authors can be reached via e-mail to <crossfire@schmorp.de> |
20 |
*/ |
21 |
|
22 |
#ifndef UTIL_H__ |
23 |
#define UTIL_H__ |
24 |
|
25 |
//#define PREFER_MALLOC |
26 |
|
27 |
#if __GNUC__ >= 3 |
28 |
# define is_constant(c) __builtin_constant_p (c) |
29 |
# define expect(expr,value) __builtin_expect ((expr),(value)) |
30 |
# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
31 |
#else |
32 |
# define is_constant(c) 0 |
33 |
# define expect(expr,value) (expr) |
34 |
# define prefetch(addr,rw,locality) |
35 |
#endif |
36 |
|
37 |
#if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) |
38 |
# define decltype(x) typeof(x) |
39 |
#endif |
40 |
|
41 |
// put into ifs if you are very sure that the expression |
42 |
// is mostly true or mosty false. note that these return |
43 |
// booleans, not the expression. |
44 |
#define expect_false(expr) expect ((expr) != 0, 0) |
45 |
#define expect_true(expr) expect ((expr) != 0, 1) |
46 |
|
47 |
#include <cstddef> |
48 |
#include <cmath> |
49 |
#include <new> |
50 |
#include <vector> |
51 |
|
52 |
#include <glib.h> |
53 |
|
54 |
#include <shstr.h> |
55 |
#include <traits.h> |
56 |
|
57 |
// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
58 |
#define auto(var,expr) decltype(expr) var = (expr) |
59 |
|
60 |
// very ugly macro that basicaly declares and initialises a variable |
61 |
// that is in scope for the next statement only |
62 |
// works only for stuff that can be assigned 0 and converts to false |
63 |
// (note: works great for pointers) |
64 |
// most ugly macro I ever wrote |
65 |
#define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
66 |
|
67 |
// in range including end |
68 |
#define IN_RANGE_INC(val,beg,end) \ |
69 |
((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
70 |
|
71 |
// in range excluding end |
72 |
#define IN_RANGE_EXC(val,beg,end) \ |
73 |
((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
74 |
|
75 |
void fork_abort (const char *msg); |
76 |
|
77 |
// rationale for using (U) not (T) is to reduce signed/unsigned issues, |
78 |
// as a is often a constant while b is the variable. it is still a bug, though. |
79 |
template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
80 |
template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
81 |
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; } |
82 |
|
83 |
template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
84 |
|
85 |
template<typename T> |
86 |
static inline T |
87 |
lerp (T val, T min_in, T max_in, T min_out, T max_out) |
88 |
{ |
89 |
return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
90 |
} |
91 |
|
92 |
// lots of stuff taken from FXT |
93 |
|
94 |
/* Rotate right. This is used in various places for checksumming */ |
95 |
//TODO: that sucks, use a better checksum algo |
96 |
static inline uint32_t |
97 |
rotate_right (uint32_t c, uint32_t count = 1) |
98 |
{ |
99 |
return (c << (32 - count)) | (c >> count); |
100 |
} |
101 |
|
102 |
static inline uint32_t |
103 |
rotate_left (uint32_t c, uint32_t count = 1) |
104 |
{ |
105 |
return (c >> (32 - count)) | (c << count); |
106 |
} |
107 |
|
108 |
// Return abs(a-b) |
109 |
// Both a and b must not have the most significant bit set |
110 |
static inline uint32_t |
111 |
upos_abs_diff (uint32_t a, uint32_t b) |
112 |
{ |
113 |
long d1 = b - a; |
114 |
long d2 = (d1 & (d1 >> 31)) << 1; |
115 |
|
116 |
return d1 - d2; // == (b - d) - (a + d); |
117 |
} |
118 |
|
119 |
// Both a and b must not have the most significant bit set |
120 |
static inline uint32_t |
121 |
upos_min (uint32_t a, uint32_t b) |
122 |
{ |
123 |
int32_t d = b - a; |
124 |
d &= d >> 31; |
125 |
return a + d; |
126 |
} |
127 |
|
128 |
// Both a and b must not have the most significant bit set |
129 |
static inline uint32_t |
130 |
upos_max (uint32_t a, uint32_t b) |
131 |
{ |
132 |
int32_t d = b - a; |
133 |
d &= d >> 31; |
134 |
return b - d; |
135 |
} |
136 |
|
137 |
// this is much faster than crossfires original algorithm |
138 |
// on modern cpus |
139 |
inline int |
140 |
isqrt (int n) |
141 |
{ |
142 |
return (int)sqrtf ((float)n); |
143 |
} |
144 |
|
145 |
// this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
146 |
#if 0 |
147 |
// and has a max. error of 6 in the range -100..+100. |
148 |
#else |
149 |
// and has a max. error of 9 in the range -100..+100. |
150 |
#endif |
151 |
inline int |
152 |
idistance (int dx, int dy) |
153 |
{ |
154 |
unsigned int dx_ = abs (dx); |
155 |
unsigned int dy_ = abs (dy); |
156 |
|
157 |
#if 0 |
158 |
return dx_ > dy_ |
159 |
? (dx_ * 61685 + dy_ * 26870) >> 16 |
160 |
: (dy_ * 61685 + dx_ * 26870) >> 16; |
161 |
#else |
162 |
return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
163 |
#endif |
164 |
} |
165 |
|
166 |
/* |
167 |
* absdir(int): Returns a number between 1 and 8, which represent |
168 |
* the "absolute" direction of a number (it actually takes care of |
169 |
* "overflow" in previous calculations of a direction). |
170 |
*/ |
171 |
inline int |
172 |
absdir (int d) |
173 |
{ |
174 |
return ((d - 1) & 7) + 1; |
175 |
} |
176 |
|
177 |
extern size_t slice_alloc; // statistics |
178 |
|
179 |
// makes dynamically allocated objects zero-initialised |
180 |
struct zero_initialised |
181 |
{ |
182 |
void *operator new (size_t s, void *p) |
183 |
{ |
184 |
memset (p, 0, s); |
185 |
return p; |
186 |
} |
187 |
|
188 |
void *operator new (size_t s) |
189 |
{ |
190 |
slice_alloc += s; |
191 |
return g_slice_alloc0 (s); |
192 |
} |
193 |
|
194 |
void *operator new[] (size_t s) |
195 |
{ |
196 |
slice_alloc += s; |
197 |
return g_slice_alloc0 (s); |
198 |
} |
199 |
|
200 |
void operator delete (void *p, size_t s) |
201 |
{ |
202 |
slice_alloc -= s; |
203 |
g_slice_free1 (s, p); |
204 |
} |
205 |
|
206 |
void operator delete[] (void *p, size_t s) |
207 |
{ |
208 |
slice_alloc -= s; |
209 |
g_slice_free1 (s, p); |
210 |
} |
211 |
}; |
212 |
|
213 |
void *salloc_ (int n) throw (std::bad_alloc); |
214 |
void *salloc_ (int n, void *src) throw (std::bad_alloc); |
215 |
|
216 |
// strictly the same as g_slice_alloc, but never returns 0 |
217 |
template<typename T> |
218 |
inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
219 |
|
220 |
// also copies src into the new area, like "memdup" |
221 |
// if src is 0, clears the memory |
222 |
template<typename T> |
223 |
inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
224 |
|
225 |
// clears the memory |
226 |
template<typename T> |
227 |
inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
228 |
|
229 |
// for symmetry |
230 |
template<typename T> |
231 |
inline void sfree (T *ptr, int n = 1) throw () |
232 |
{ |
233 |
#ifdef PREFER_MALLOC |
234 |
free (ptr); |
235 |
#else |
236 |
slice_alloc -= n * sizeof (T); |
237 |
g_slice_free1 (n * sizeof (T), (void *)ptr); |
238 |
#endif |
239 |
} |
240 |
|
241 |
// a STL-compatible allocator that uses g_slice |
242 |
// boy, this is verbose |
243 |
template<typename Tp> |
244 |
struct slice_allocator |
245 |
{ |
246 |
typedef size_t size_type; |
247 |
typedef ptrdiff_t difference_type; |
248 |
typedef Tp *pointer; |
249 |
typedef const Tp *const_pointer; |
250 |
typedef Tp &reference; |
251 |
typedef const Tp &const_reference; |
252 |
typedef Tp value_type; |
253 |
|
254 |
template <class U> |
255 |
struct rebind |
256 |
{ |
257 |
typedef slice_allocator<U> other; |
258 |
}; |
259 |
|
260 |
slice_allocator () throw () { } |
261 |
slice_allocator (const slice_allocator &o) throw () { } |
262 |
template<typename Tp2> |
263 |
slice_allocator (const slice_allocator<Tp2> &) throw () { } |
264 |
|
265 |
~slice_allocator () { } |
266 |
|
267 |
pointer address (reference x) const { return &x; } |
268 |
const_pointer address (const_reference x) const { return &x; } |
269 |
|
270 |
pointer allocate (size_type n, const_pointer = 0) |
271 |
{ |
272 |
return salloc<Tp> (n); |
273 |
} |
274 |
|
275 |
void deallocate (pointer p, size_type n) |
276 |
{ |
277 |
sfree<Tp> (p, n); |
278 |
} |
279 |
|
280 |
size_type max_size ()const throw () |
281 |
{ |
282 |
return size_t (-1) / sizeof (Tp); |
283 |
} |
284 |
|
285 |
void construct (pointer p, const Tp &val) |
286 |
{ |
287 |
::new (p) Tp (val); |
288 |
} |
289 |
|
290 |
void destroy (pointer p) |
291 |
{ |
292 |
p->~Tp (); |
293 |
} |
294 |
}; |
295 |
|
296 |
// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
297 |
// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
298 |
// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
299 |
struct tausworthe_random_generator |
300 |
{ |
301 |
// generator |
302 |
uint32_t state [4]; |
303 |
|
304 |
void operator =(const tausworthe_random_generator &src) |
305 |
{ |
306 |
state [0] = src.state [0]; |
307 |
state [1] = src.state [1]; |
308 |
state [2] = src.state [2]; |
309 |
state [3] = src.state [3]; |
310 |
} |
311 |
|
312 |
void seed (uint32_t seed); |
313 |
uint32_t next (); |
314 |
|
315 |
// uniform distribution |
316 |
uint32_t operator ()(uint32_t num) |
317 |
{ |
318 |
return is_constant (num) |
319 |
? (next () * (uint64_t)num) >> 32U |
320 |
: get_range (num); |
321 |
} |
322 |
|
323 |
// return a number within (min .. max) |
324 |
int operator () (int r_min, int r_max) |
325 |
{ |
326 |
return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
327 |
? r_min + operator ()(r_max - r_min + 1) |
328 |
: get_range (r_min, r_max); |
329 |
} |
330 |
|
331 |
double operator ()() |
332 |
{ |
333 |
return this->next () / (double)0xFFFFFFFFU; |
334 |
} |
335 |
|
336 |
protected: |
337 |
uint32_t get_range (uint32_t r_max); |
338 |
int get_range (int r_min, int r_max); |
339 |
}; |
340 |
|
341 |
typedef tausworthe_random_generator rand_gen; |
342 |
|
343 |
extern rand_gen rndm; |
344 |
|
345 |
INTERFACE_CLASS (attachable) |
346 |
struct refcnt_base |
347 |
{ |
348 |
typedef int refcnt_t; |
349 |
mutable refcnt_t ACC (RW, refcnt); |
350 |
|
351 |
MTH void refcnt_inc () const { ++refcnt; } |
352 |
MTH void refcnt_dec () const { --refcnt; } |
353 |
|
354 |
refcnt_base () : refcnt (0) { } |
355 |
}; |
356 |
|
357 |
// to avoid branches with more advanced compilers |
358 |
extern refcnt_base::refcnt_t refcnt_dummy; |
359 |
|
360 |
template<class T> |
361 |
struct refptr |
362 |
{ |
363 |
// p if not null |
364 |
refcnt_base::refcnt_t *refcnt_ref () { return p ? &p->refcnt : &refcnt_dummy; } |
365 |
|
366 |
void refcnt_dec () |
367 |
{ |
368 |
if (!is_constant (p)) |
369 |
--*refcnt_ref (); |
370 |
else if (p) |
371 |
--p->refcnt; |
372 |
} |
373 |
|
374 |
void refcnt_inc () |
375 |
{ |
376 |
if (!is_constant (p)) |
377 |
++*refcnt_ref (); |
378 |
else if (p) |
379 |
++p->refcnt; |
380 |
} |
381 |
|
382 |
T *p; |
383 |
|
384 |
refptr () : p(0) { } |
385 |
refptr (const refptr<T> &p) : p(p.p) { refcnt_inc (); } |
386 |
refptr (T *p) : p(p) { refcnt_inc (); } |
387 |
~refptr () { refcnt_dec (); } |
388 |
|
389 |
const refptr<T> &operator =(T *o) |
390 |
{ |
391 |
// if decrementing ever destroys we need to reverse the order here |
392 |
refcnt_dec (); |
393 |
p = o; |
394 |
refcnt_inc (); |
395 |
return *this; |
396 |
} |
397 |
|
398 |
const refptr<T> &operator =(const refptr<T> &o) |
399 |
{ |
400 |
*this = o.p; |
401 |
return *this; |
402 |
} |
403 |
|
404 |
T &operator * () const { return *p; } |
405 |
T *operator ->() const { return p; } |
406 |
|
407 |
operator T *() const { return p; } |
408 |
}; |
409 |
|
410 |
typedef refptr<maptile> maptile_ptr; |
411 |
typedef refptr<object> object_ptr; |
412 |
typedef refptr<archetype> arch_ptr; |
413 |
typedef refptr<client> client_ptr; |
414 |
typedef refptr<player> player_ptr; |
415 |
|
416 |
struct str_hash |
417 |
{ |
418 |
std::size_t operator ()(const char *s) const |
419 |
{ |
420 |
unsigned long hash = 0; |
421 |
|
422 |
/* use the one-at-a-time hash function, which supposedly is |
423 |
* better than the djb2-like one used by perl5.005, but |
424 |
* certainly is better then the bug used here before. |
425 |
* see http://burtleburtle.net/bob/hash/doobs.html |
426 |
*/ |
427 |
while (*s) |
428 |
{ |
429 |
hash += *s++; |
430 |
hash += hash << 10; |
431 |
hash ^= hash >> 6; |
432 |
} |
433 |
|
434 |
hash += hash << 3; |
435 |
hash ^= hash >> 11; |
436 |
hash += hash << 15; |
437 |
|
438 |
return hash; |
439 |
} |
440 |
}; |
441 |
|
442 |
struct str_equal |
443 |
{ |
444 |
bool operator ()(const char *a, const char *b) const |
445 |
{ |
446 |
return !strcmp (a, b); |
447 |
} |
448 |
}; |
449 |
|
450 |
// Mostly the same as std::vector, but insert/erase can reorder |
451 |
// the elements, making append(=insert)/remove O(1) instead of O(n). |
452 |
// |
453 |
// NOTE: only some forms of erase are available |
454 |
template<class T> |
455 |
struct unordered_vector : std::vector<T, slice_allocator<T> > |
456 |
{ |
457 |
typedef typename unordered_vector::iterator iterator; |
458 |
|
459 |
void erase (unsigned int pos) |
460 |
{ |
461 |
if (pos < this->size () - 1) |
462 |
(*this)[pos] = (*this)[this->size () - 1]; |
463 |
|
464 |
this->pop_back (); |
465 |
} |
466 |
|
467 |
void erase (iterator i) |
468 |
{ |
469 |
erase ((unsigned int )(i - this->begin ())); |
470 |
} |
471 |
}; |
472 |
|
473 |
// This container blends advantages of linked lists |
474 |
// (efficiency) with vectors (random access) by |
475 |
// by using an unordered vector and storing the vector |
476 |
// index inside the object. |
477 |
// |
478 |
// + memory-efficient on most 64 bit archs |
479 |
// + O(1) insert/remove |
480 |
// + free unique (but varying) id for inserted objects |
481 |
// + cache-friendly iteration |
482 |
// - only works for pointers to structs |
483 |
// |
484 |
// NOTE: only some forms of erase/insert are available |
485 |
typedef int object_vector_index; |
486 |
|
487 |
template<class T, object_vector_index T::*indexmember> |
488 |
struct object_vector : std::vector<T *, slice_allocator<T *> > |
489 |
{ |
490 |
typedef typename object_vector::iterator iterator; |
491 |
|
492 |
bool contains (const T *obj) const |
493 |
{ |
494 |
return obj->*indexmember; |
495 |
} |
496 |
|
497 |
iterator find (const T *obj) |
498 |
{ |
499 |
return obj->*indexmember |
500 |
? this->begin () + obj->*indexmember - 1 |
501 |
: this->end (); |
502 |
} |
503 |
|
504 |
void push_back (T *obj) |
505 |
{ |
506 |
std::vector<T *, slice_allocator<T *> >::push_back (obj); |
507 |
obj->*indexmember = this->size (); |
508 |
} |
509 |
|
510 |
void insert (T *obj) |
511 |
{ |
512 |
push_back (obj); |
513 |
} |
514 |
|
515 |
void insert (T &obj) |
516 |
{ |
517 |
insert (&obj); |
518 |
} |
519 |
|
520 |
void erase (T *obj) |
521 |
{ |
522 |
unsigned int pos = obj->*indexmember; |
523 |
obj->*indexmember = 0; |
524 |
|
525 |
if (pos < this->size ()) |
526 |
{ |
527 |
(*this)[pos - 1] = (*this)[this->size () - 1]; |
528 |
(*this)[pos - 1]->*indexmember = pos; |
529 |
} |
530 |
|
531 |
this->pop_back (); |
532 |
} |
533 |
|
534 |
void erase (T &obj) |
535 |
{ |
536 |
erase (&obj); |
537 |
} |
538 |
}; |
539 |
|
540 |
// basically does what strncpy should do, but appends "..." to strings exceeding length |
541 |
void assign (char *dst, const char *src, int maxlen); |
542 |
|
543 |
// type-safe version of assign |
544 |
template<int N> |
545 |
inline void assign (char (&dst)[N], const char *src) |
546 |
{ |
547 |
assign ((char *)&dst, src, N); |
548 |
} |
549 |
|
550 |
typedef double tstamp; |
551 |
|
552 |
// return current time as timestampe |
553 |
tstamp now (); |
554 |
|
555 |
int similar_direction (int a, int b); |
556 |
|
557 |
// like sprintf, but returns a "static" buffer |
558 |
const char *format (const char *format, ...); |
559 |
|
560 |
#endif |
561 |
|