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> |
|
|
21 | |
|
|
22 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
|
|
23 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
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 | // this is much faster than crossfires original algorithm |
|
|
51 | // on modern cpus |
|
|
52 | inline int |
|
|
53 | isqrt (int n) |
|
|
54 | { |
|
|
55 | return (int)sqrtf ((float)n); |
|
|
56 | } |
|
|
57 | |
|
|
58 | // this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
|
|
59 | #if 0 |
|
|
60 | // and has a max. error of 6 in the range -100..+100. |
|
|
61 | #else |
|
|
62 | // and has a max. error of 9 in the range -100..+100. |
|
|
63 | #endif |
|
|
64 | inline int |
|
|
65 | idistance (int dx, int dy) |
|
|
66 | { |
|
|
67 | unsigned int dx_ = abs (dx); |
|
|
68 | unsigned int dy_ = abs (dy); |
|
|
69 | |
|
|
70 | #if 0 |
|
|
71 | return dx_ > dy_ |
|
|
72 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
|
|
73 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
|
|
74 | #else |
|
|
75 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
|
|
76 | #endif |
|
|
77 | } |
|
|
78 | |
|
|
79 | /* |
|
|
80 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
81 | * the "absolute" direction of a number (it actually takes care of |
|
|
82 | * "overflow" in previous calculations of a direction). |
|
|
83 | */ |
|
|
84 | inline int |
|
|
85 | absdir (int d) |
|
|
86 | { |
|
|
87 | return ((d - 1) & 7) + 1; |
|
|
88 | } |
13 | |
89 | |
14 | // makes dynamically allocated objects zero-initialised |
90 | // makes dynamically allocated objects zero-initialised |
15 | struct zero_initialised |
91 | struct zero_initialised |
16 | { |
92 | { |
17 | void *operator new (size_t s, void *p) |
93 | void *operator new (size_t s, void *p) |
… | |
… | |
39 | { |
115 | { |
40 | g_slice_free1 (s, p); |
116 | g_slice_free1 (s, p); |
41 | } |
117 | } |
42 | }; |
118 | }; |
43 | |
119 | |
44 | void throw_bad_alloc () throw (std::bad_alloc); |
120 | void *salloc_ (int n) throw (std::bad_alloc); |
45 | |
|
|
46 | void *alloc (int s) throw (std::bad_alloc); |
121 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
47 | void dealloc (void *p, int s) throw (); |
122 | |
|
|
123 | // strictly the same as g_slice_alloc, but never returns 0 |
|
|
124 | template<typename T> |
|
|
125 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
126 | |
|
|
127 | // also copies src into the new area, like "memdup" |
|
|
128 | // if src is 0, clears the memory |
|
|
129 | template<typename T> |
|
|
130 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
131 | |
|
|
132 | // clears the memory |
|
|
133 | template<typename T> |
|
|
134 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
|
|
135 | |
|
|
136 | // for symmetry |
|
|
137 | template<typename T> |
|
|
138 | inline void sfree (T *ptr, int n = 1) throw () |
|
|
139 | { |
|
|
140 | #ifdef PREFER_MALLOC |
|
|
141 | free (ptr); |
|
|
142 | #else |
|
|
143 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
144 | #endif |
|
|
145 | } |
48 | |
146 | |
49 | // a STL-compatible allocator that uses g_slice |
147 | // a STL-compatible allocator that uses g_slice |
50 | // boy, this is verbose |
148 | // boy, this is verbose |
51 | template<typename Tp> |
149 | template<typename Tp> |
52 | struct slice_allocator |
150 | struct slice_allocator |
… | |
… | |
75 | pointer address (reference x) const { return &x; } |
173 | pointer address (reference x) const { return &x; } |
76 | const_pointer address (const_reference x) const { return &x; } |
174 | const_pointer address (const_reference x) const { return &x; } |
77 | |
175 | |
78 | pointer allocate (size_type n, const_pointer = 0) |
176 | pointer allocate (size_type n, const_pointer = 0) |
79 | { |
177 | { |
80 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
178 | return salloc<Tp> (n); |
81 | } |
179 | } |
82 | |
180 | |
83 | void deallocate (pointer p, size_type n) |
181 | void deallocate (pointer p, size_type n) |
84 | { |
182 | { |
85 | dealloc (static_cast<void *>(p), n); |
183 | sfree<Tp> (p, n); |
86 | } |
184 | } |
87 | |
185 | |
88 | size_type max_size ()const throw () |
186 | size_type max_size ()const throw () |
89 | { |
187 | { |
90 | return size_t (-1) / sizeof (Tp); |
188 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
99 | { |
197 | { |
100 | p->~Tp (); |
198 | p->~Tp (); |
101 | } |
199 | } |
102 | }; |
200 | }; |
103 | |
201 | |
104 | struct refcounted |
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 |
105 | { |
206 | { |
106 | mutable int refcnt; |
207 | // generator |
107 | refcounted () : refcnt (0) { } |
208 | uint32_t state [4]; |
108 | void refcnt_inc () { ++refcnt; } |
209 | |
109 | void refcnt_dec () { --refcnt; |
210 | void operator =(const tausworthe_random_generator &src) |
110 | if (refcnt < 0)abort();}//D |
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); |
111 | }; |
245 | }; |
|
|
246 | |
|
|
247 | typedef tausworthe_random_generator rand_gen; |
|
|
248 | |
|
|
249 | extern rand_gen rndm; |
112 | |
250 | |
113 | template<class T> |
251 | template<class T> |
114 | struct refptr |
252 | struct refptr |
115 | { |
253 | { |
116 | T *p; |
254 | T *p; |
… | |
… | |
138 | T &operator * () const { return *p; } |
276 | T &operator * () const { return *p; } |
139 | T *operator ->() const { return p; } |
277 | T *operator ->() const { return p; } |
140 | |
278 | |
141 | operator T *() const { return p; } |
279 | operator T *() const { return p; } |
142 | }; |
280 | }; |
|
|
281 | |
|
|
282 | typedef refptr<maptile> maptile_ptr; |
|
|
283 | typedef refptr<object> object_ptr; |
|
|
284 | typedef refptr<archetype> arch_ptr; |
|
|
285 | typedef refptr<client> client_ptr; |
|
|
286 | typedef refptr<player> player_ptr; |
143 | |
287 | |
144 | struct str_hash |
288 | struct str_hash |
145 | { |
289 | { |
146 | std::size_t operator ()(const char *s) const |
290 | std::size_t operator ()(const char *s) const |
147 | { |
291 | { |
… | |
… | |
173 | { |
317 | { |
174 | return !strcmp (a, b); |
318 | return !strcmp (a, b); |
175 | } |
319 | } |
176 | }; |
320 | }; |
177 | |
321 | |
178 | #include <vector> |
|
|
179 | |
|
|
180 | template<class obj> |
322 | template<class T> |
181 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
323 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
182 | { |
324 | { |
183 | typedef typename unordered_vector::iterator iterator; |
325 | typedef typename unordered_vector::iterator iterator; |
184 | |
326 | |
185 | void erase (unsigned int pos) |
327 | void erase (unsigned int pos) |
186 | { |
328 | { |
… | |
… | |
194 | { |
336 | { |
195 | erase ((unsigned int )(i - this->begin ())); |
337 | erase ((unsigned int )(i - this->begin ())); |
196 | } |
338 | } |
197 | }; |
339 | }; |
198 | |
340 | |
199 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
341 | template<class T, int T::* index> |
200 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
342 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
201 | 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; } |
343 | { |
|
|
344 | void insert (T *obj) |
|
|
345 | { |
|
|
346 | assert (!(obj->*index)); |
|
|
347 | push_back (obj); |
|
|
348 | obj->*index = this->size (); |
|
|
349 | } |
202 | |
350 | |
203 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
351 | void insert (T &obj) |
|
|
352 | { |
|
|
353 | insert (&obj); |
|
|
354 | } |
|
|
355 | |
|
|
356 | void erase (T *obj) |
|
|
357 | { |
|
|
358 | assert (obj->*index); |
|
|
359 | int pos = obj->*index; |
|
|
360 | obj->*index = 0; |
|
|
361 | |
|
|
362 | if (pos < this->size ()) |
|
|
363 | { |
|
|
364 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
365 | (*this)[pos - 1]->*index = pos; |
|
|
366 | } |
|
|
367 | |
|
|
368 | this->pop_back (); |
|
|
369 | } |
|
|
370 | |
|
|
371 | void erase (T &obj) |
|
|
372 | { |
|
|
373 | errase (&obj); |
|
|
374 | } |
|
|
375 | }; |
204 | |
376 | |
205 | // 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 |
206 | void assign (char *dst, const char *src, int maxlen); |
378 | void assign (char *dst, const char *src, int maxlen); |
207 | |
379 | |
208 | // type-safe version of assign |
380 | // type-safe version of assign |
… | |
… | |
210 | inline void assign (char (&dst)[N], const char *src) |
382 | inline void assign (char (&dst)[N], const char *src) |
211 | { |
383 | { |
212 | assign ((char *)&dst, src, N); |
384 | assign ((char *)&dst, src, N); |
213 | } |
385 | } |
214 | |
386 | |
215 | #endif |
387 | typedef double tstamp; |
216 | |
388 | |
|
|
389 | // return current time as timestampe |
|
|
390 | tstamp now (); |
|
|
391 | |
|
|
392 | int similar_direction (int a, int b); |
|
|
393 | |
|
|
394 | #endif |
|
|
395 | |