ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.13 by root, Thu Sep 14 00:07:15 2006 UTC vs.
Revision 1.44 by root, Fri May 11 08:00:00 2007 UTC

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 auto(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
40void 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.
44template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
45template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
46template<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
48template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
49
50template<typename T>
51static inline T
52lerp (T val, T min_in, T max_in, T min_out, T max_out)
53{
54 return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out;
55}
56
57// lots of stuff taken from FXT
58
59/* Rotate right. This is used in various places for checksumming */
60//TODO: that sucks, use a better checksum algo
61static inline uint32_t
62rotate_right (uint32_t c, uint32_t count = 1)
63{
64 return (c << (32 - count)) | (c >> count);
65}
66
67static inline uint32_t
68rotate_left (uint32_t c, uint32_t count = 1)
69{
70 return (c >> (32 - count)) | (c << count);
71}
72
73// Return abs(a-b)
74// Both a and b must not have the most significant bit set
75static inline uint32_t
76upos_abs_diff (uint32_t a, uint32_t b)
77{
78 long d1 = b - a;
79 long d2 = (d1 & (d1 >> 31)) << 1;
80
81 return d1 - d2; // == (b - d) - (a + d);
82}
83
84// Both a and b must not have the most significant bit set
85static inline uint32_t
86upos_min (uint32_t a, uint32_t b)
87{
88 int32_t d = b - a;
89 d &= d >> 31;
90 return a + d;
91}
92
93// Both a and b must not have the most significant bit set
94static inline uint32_t
95upos_max (uint32_t a, uint32_t b)
96{
97 int32_t d = b - a;
98 d &= d >> 31;
99 return b - d;
100}
101
102// this is much faster than crossfires original algorithm
103// on modern cpus
104inline int
105isqrt (int n)
106{
107 return (int)sqrtf ((float)n);
108}
109
110// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
111#if 0
112// and has a max. error of 6 in the range -100..+100.
113#else
114// and has a max. error of 9 in the range -100..+100.
115#endif
116inline int
117idistance (int dx, int dy)
118{
119 unsigned int dx_ = abs (dx);
120 unsigned int dy_ = abs (dy);
121
122#if 0
123 return dx_ > dy_
124 ? (dx_ * 61685 + dy_ * 26870) >> 16
125 : (dy_ * 61685 + dx_ * 26870) >> 16;
126#else
127 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
128#endif
129}
130
131/*
132 * absdir(int): Returns a number between 1 and 8, which represent
133 * the "absolute" direction of a number (it actually takes care of
134 * "overflow" in previous calculations of a direction).
135 */
136inline int
137absdir (int d)
138{
139 return ((d - 1) & 7) + 1;
140}
13 141
14// makes dynamically allocated objects zero-initialised 142// makes dynamically allocated objects zero-initialised
15struct zero_initialised 143struct zero_initialised
16{ 144{
17 void *operator new (size_t s, void *p) 145 void *operator new (size_t s, void *p)
39 { 167 {
40 g_slice_free1 (s, p); 168 g_slice_free1 (s, p);
41 } 169 }
42}; 170};
43 171
172void *salloc_ (int n) throw (std::bad_alloc);
173void *salloc_ (int n, void *src) throw (std::bad_alloc);
174
44// strictly the same as g_slice_alloc, but never returns 0 175// strictly the same as g_slice_alloc, but never returns 0
45void *alloc (int s) throw (std::bad_alloc); 176template<typename T>
177inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
178
179// also copies src into the new area, like "memdup"
180// if src is 0, clears the memory
181template<typename T>
182inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
183
184// clears the memory
185template<typename T>
186inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
187
46// for symmetry 188// for symmetry
47inline void dealloc (void *p, int s) throw () 189template<typename T>
190inline void sfree (T *ptr, int n = 1) throw ()
48{ 191{
49 g_slice_free1 (s, p); 192#ifdef PREFER_MALLOC
193 free (ptr);
194#else
195 g_slice_free1 (n * sizeof (T), (void *)ptr);
196#endif
50} 197}
51 198
52// a STL-compatible allocator that uses g_slice 199// a STL-compatible allocator that uses g_slice
53// boy, this is verbose 200// boy, this is verbose
54template<typename Tp> 201template<typename Tp>
78 pointer address (reference x) const { return &x; } 225 pointer address (reference x) const { return &x; }
79 const_pointer address (const_reference x) const { return &x; } 226 const_pointer address (const_reference x) const { return &x; }
80 227
81 pointer allocate (size_type n, const_pointer = 0) 228 pointer allocate (size_type n, const_pointer = 0)
82 { 229 {
83 return static_cast<pointer>(alloc (n * sizeof (Tp))); 230 return salloc<Tp> (n);
84 } 231 }
85 232
86 void deallocate (pointer p, size_type n) 233 void deallocate (pointer p, size_type n)
87 { 234 {
88 dealloc (static_cast<void *>(p), n * sizeof (Tp)); 235 sfree<Tp> (p, n);
89 } 236 }
90 237
91 size_type max_size ()const throw () 238 size_type max_size ()const throw ()
92 { 239 {
93 return size_t (-1) / sizeof (Tp); 240 return size_t (-1) / sizeof (Tp);
102 { 249 {
103 p->~Tp (); 250 p->~Tp ();
104 } 251 }
105}; 252};
106 253
107struct refcounted 254// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
255// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
256// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
257struct tausworthe_random_generator
108{ 258{
109 mutable int refcnt; 259 // generator
110 refcounted () : refcnt (0) { } 260 uint32_t state [4];
111 void refcnt_inc () { ++refcnt; } 261
112 void refcnt_dec () { --refcnt; 262 void operator =(const tausworthe_random_generator &src)
113 if (refcnt < 0)abort();}//D 263 {
264 state [0] = src.state [0];
265 state [1] = src.state [1];
266 state [2] = src.state [2];
267 state [3] = src.state [3];
268 }
269
270 void seed (uint32_t seed);
271 uint32_t next ();
272
273 // uniform distribution
274 uint32_t operator ()(uint32_t num)
275 {
276 return is_constant (num)
277 ? (next () * (uint64_t)num) >> 32U
278 : get_range (num);
279 }
280
281 // return a number within (min .. max)
282 int operator () (int r_min, int r_max)
283 {
284 return is_constant (r_min) && is_constant (r_max) && r_min <= r_max
285 ? r_min + operator ()(r_max - r_min + 1)
286 : get_range (r_min, r_max);
287 }
288
289 double operator ()()
290 {
291 return this->next () / (double)0xFFFFFFFFU;
292 }
293
294protected:
295 uint32_t get_range (uint32_t r_max);
296 int get_range (int r_min, int r_max);
114}; 297};
298
299typedef tausworthe_random_generator rand_gen;
300
301extern rand_gen rndm;
115 302
116template<class T> 303template<class T>
117struct refptr 304struct refptr
118{ 305{
119 T *p; 306 T *p;
141 T &operator * () const { return *p; } 328 T &operator * () const { return *p; }
142 T *operator ->() const { return p; } 329 T *operator ->() const { return p; }
143 330
144 operator T *() const { return p; } 331 operator T *() const { return p; }
145}; 332};
333
334typedef refptr<maptile> maptile_ptr;
335typedef refptr<object> object_ptr;
336typedef refptr<archetype> arch_ptr;
337typedef refptr<client> client_ptr;
338typedef refptr<player> player_ptr;
146 339
147struct str_hash 340struct str_hash
148{ 341{
149 std::size_t operator ()(const char *s) const 342 std::size_t operator ()(const char *s) const
150 { 343 {
176 { 369 {
177 return !strcmp (a, b); 370 return !strcmp (a, b);
178 } 371 }
179}; 372};
180 373
181#include <vector>
182
183template<class obj> 374template<class T>
184struct unordered_vector : std::vector<obj, slice_allocator<obj> > 375struct unordered_vector : std::vector<T, slice_allocator<T> >
185{ 376{
186 typedef typename unordered_vector::iterator iterator; 377 typedef typename unordered_vector::iterator iterator;
187 378
188 void erase (unsigned int pos) 379 void erase (unsigned int pos)
189 { 380 {
197 { 388 {
198 erase ((unsigned int )(i - this->begin ())); 389 erase ((unsigned int )(i - this->begin ()));
199 } 390 }
200}; 391};
201 392
202template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 393template<class T, int T::* index>
203template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 394struct object_vector : std::vector<T *, slice_allocator<T *> >
204template<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; } 395{
396 void insert (T *obj)
397 {
398 assert (!(obj->*index));
399 push_back (obj);
400 obj->*index = this->size ();
401 }
205 402
206template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 403 void insert (T &obj)
404 {
405 insert (&obj);
406 }
407
408 void erase (T *obj)
409 {
410 assert (obj->*index);
411 unsigned int pos = obj->*index;
412 obj->*index = 0;
413
414 if (pos < this->size ())
415 {
416 (*this)[pos - 1] = (*this)[this->size () - 1];
417 (*this)[pos - 1]->*index = pos;
418 }
419
420 this->pop_back ();
421 }
422
423 void erase (T &obj)
424 {
425 errase (&obj);
426 }
427};
207 428
208// basically does what strncpy should do, but appends "..." to strings exceeding length 429// basically does what strncpy should do, but appends "..." to strings exceeding length
209void assign (char *dst, const char *src, int maxlen); 430void assign (char *dst, const char *src, int maxlen);
210 431
211// type-safe version of assign 432// type-safe version of assign
213inline void assign (char (&dst)[N], const char *src) 434inline void assign (char (&dst)[N], const char *src)
214{ 435{
215 assign ((char *)&dst, src, N); 436 assign ((char *)&dst, src, N);
216} 437}
217 438
439typedef double tstamp;
440
441// return current time as timestampe
442tstamp now ();
443
444int similar_direction (int a, int b);
445
446// like printf, but returns a std::string
447const std::string format (const char *format, ...);
448
218#endif 449#endif
219 450

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines