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.17 by root, Sat Dec 16 03:08:26 2006 UTC vs.
Revision 1.39 by pippijn, Thu Mar 1 12:28:16 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>
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
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
50// lots of stuff taken from FXT
51
52/* Rotate right. This is used in various places for checksumming */
53//TODO: that sucks, use a better checksum algo
54static inline uint32_t
55rotate_right (uint32_t c, uint32_t count = 1)
56{
57 return (c << (32 - count)) | (c >> count);
58}
59
60static inline uint32_t
61rotate_left (uint32_t c, uint32_t count = 1)
62{
63 return (c >> (32 - count)) | (c << count);
64}
65
66// Return abs(a-b)
67// Both a and b must not have the most significant bit set
68static inline uint32_t
69upos_abs_diff (uint32_t a, uint32_t b)
70{
71 long d1 = b - a;
72 long d2 = (d1 & (d1 >> 31)) << 1;
73
74 return d1 - d2; // == (b - d) - (a + d);
75}
76
77// Both a and b must not have the most significant bit set
78static inline uint32_t
79upos_min (uint32_t a, uint32_t b)
80{
81 int32_t d = b - a;
82 d &= d >> 31;
83 return a + d;
84}
85
86// Both a and b must not have the most significant bit set
87static inline uint32_t
88upos_max (uint32_t a, uint32_t b)
89{
90 int32_t d = b - a;
91 d &= d >> 31;
92 return b - d;
93}
94
95// this is much faster than crossfires original algorithm
96// on modern cpus
97inline int
98isqrt (int n)
99{
100 return (int)sqrtf ((float)n);
101}
102
103// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
104#if 0
105// and has a max. error of 6 in the range -100..+100.
106#else
107// and has a max. error of 9 in the range -100..+100.
108#endif
109inline int
110idistance (int dx, int dy)
111{
112 unsigned int dx_ = abs (dx);
113 unsigned int dy_ = abs (dy);
114
115#if 0
116 return dx_ > dy_
117 ? (dx_ * 61685 + dy_ * 26870) >> 16
118 : (dy_ * 61685 + dx_ * 26870) >> 16;
119#else
120 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
121#endif
122}
123
124/*
125 * absdir(int): Returns a number between 1 and 8, which represent
126 * the "absolute" direction of a number (it actually takes care of
127 * "overflow" in previous calculations of a direction).
128 */
129inline int
130absdir (int d)
131{
132 return ((d - 1) & 7) + 1;
133}
134
17// makes dynamically allocated objects zero-initialised 135// makes dynamically allocated objects zero-initialised
18struct zero_initialised 136struct zero_initialised
19{ 137{
20 void *operator new (size_t s, void *p) 138 void *operator new (size_t s, void *p)
21 { 139 {
42 { 160 {
43 g_slice_free1 (s, p); 161 g_slice_free1 (s, p);
44 } 162 }
45}; 163};
46 164
165void *salloc_ (int n) throw (std::bad_alloc);
166void *salloc_ (int n, void *src) throw (std::bad_alloc);
167
47// strictly the same as g_slice_alloc, but never returns 0 168// strictly the same as g_slice_alloc, but never returns 0
48void *salloc (int size) throw (std::bad_alloc); 169template<typename T>
170inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
171
49// also copies src into the new area, like "memdup" 172// also copies src into the new area, like "memdup"
50void *salloc (int size, void *src) throw (std::bad_alloc); 173// if src is 0, clears the memory
174template<typename T>
175inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
176
177// clears the memory
178template<typename T>
179inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
180
51// for symmetry 181// for symmetry
182template<typename T>
52inline void sfree (void *ptr, int size) throw () 183inline void sfree (T *ptr, int n = 1) throw ()
53{ 184{
185#ifdef PREFER_MALLOC
186 free (ptr);
187#else
54 g_slice_free1 (size, ptr); 188 g_slice_free1 (n * sizeof (T), (void *)ptr);
189#endif
55} 190}
56 191
57// a STL-compatible allocator that uses g_slice 192// a STL-compatible allocator that uses g_slice
58// boy, this is verbose 193// boy, this is verbose
59template<typename Tp> 194template<typename Tp>
83 pointer address (reference x) const { return &x; } 218 pointer address (reference x) const { return &x; }
84 const_pointer address (const_reference x) const { return &x; } 219 const_pointer address (const_reference x) const { return &x; }
85 220
86 pointer allocate (size_type n, const_pointer = 0) 221 pointer allocate (size_type n, const_pointer = 0)
87 { 222 {
88 return static_cast<pointer>(salloc (n * sizeof (Tp))); 223 return salloc<Tp> (n);
89 } 224 }
90 225
91 void deallocate (pointer p, size_type n) 226 void deallocate (pointer p, size_type n)
92 { 227 {
93 sfree (static_cast<void *>(p), n * sizeof (Tp)); 228 sfree<Tp> (p, n);
94 } 229 }
95 230
96 size_type max_size ()const throw () 231 size_type max_size ()const throw ()
97 { 232 {
98 return size_t (-1) / sizeof (Tp); 233 return size_t (-1) / sizeof (Tp);
107 { 242 {
108 p->~Tp (); 243 p->~Tp ();
109 } 244 }
110}; 245};
111 246
112struct refcounted 247// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
248// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
249// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
250struct tausworthe_random_generator
113{ 251{
114 refcounted () : refcnt (0) { } 252 // generator
115// virtual ~refcounted (); 253 uint32_t state [4];
116 void refcnt_inc () { ++refcnt; } 254
117 void refcnt_dec () { --refcnt; } 255 void operator =(const tausworthe_random_generator &src)
118 bool dead () { return refcnt == 0; } 256 {
119 mutable int refcnt; 257 state [0] = src.state [0];
120#if 0 258 state [1] = src.state [1];
121private: 259 state [2] = src.state [2];
122 static refcounted *rc_first; 260 state [3] = src.state [3];
123 refcounted *rc_next; 261 }
124#endif 262
263 void seed (uint32_t seed);
264 uint32_t next ();
265
266 // uniform distribution
267 uint32_t operator ()(uint32_t r_max)
268 {
269 return is_constant (r_max)
270 ? this->next () % r_max
271 : get_range (r_max);
272 }
273
274 // return a number within (min .. max)
275 int operator () (int r_min, int r_max)
276 {
277 return is_constant (r_min) && is_constant (r_max)
278 ? r_min + (*this) (max (r_max - r_min + 1, 1))
279 : get_range (r_min, r_max);
280 }
281
282 double operator ()()
283 {
284 return this->next () / (double)0xFFFFFFFFU;
285 }
286
287protected:
288 uint32_t get_range (uint32_t r_max);
289 int get_range (int r_min, int r_max);
125}; 290};
291
292typedef tausworthe_random_generator rand_gen;
293
294extern rand_gen rndm;
126 295
127template<class T> 296template<class T>
128struct refptr 297struct refptr
129{ 298{
130 T *p; 299 T *p;
152 T &operator * () const { return *p; } 321 T &operator * () const { return *p; }
153 T *operator ->() const { return p; } 322 T *operator ->() const { return p; }
154 323
155 operator T *() const { return p; } 324 operator T *() const { return p; }
156}; 325};
326
327typedef refptr<maptile> maptile_ptr;
328typedef refptr<object> object_ptr;
329typedef refptr<archetype> arch_ptr;
330typedef refptr<client> client_ptr;
331typedef refptr<player> player_ptr;
157 332
158struct str_hash 333struct str_hash
159{ 334{
160 std::size_t operator ()(const char *s) const 335 std::size_t operator ()(const char *s) const
161 { 336 {
187 { 362 {
188 return !strcmp (a, b); 363 return !strcmp (a, b);
189 } 364 }
190}; 365};
191 366
192#include <vector>
193
194template<class obj> 367template<class T>
195struct unordered_vector : std::vector<obj, slice_allocator<obj> > 368struct unordered_vector : std::vector<T, slice_allocator<T> >
196{ 369{
197 typedef typename unordered_vector::iterator iterator; 370 typedef typename unordered_vector::iterator iterator;
198 371
199 void erase (unsigned int pos) 372 void erase (unsigned int pos)
200 { 373 {
208 { 381 {
209 erase ((unsigned int )(i - this->begin ())); 382 erase ((unsigned int )(i - this->begin ()));
210 } 383 }
211}; 384};
212 385
213template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 386template<class T, int T::* index>
214template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 387struct object_vector : std::vector<T *, slice_allocator<T *> >
215template<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; } 388{
389 void insert (T *obj)
390 {
391 assert (!(obj->*index));
392 push_back (obj);
393 obj->*index = this->size ();
394 }
216 395
217template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 396 void insert (T &obj)
397 {
398 insert (&obj);
399 }
400
401 void erase (T *obj)
402 {
403 assert (obj->*index);
404 unsigned int pos = obj->*index;
405 obj->*index = 0;
406
407 if (pos < this->size ())
408 {
409 (*this)[pos - 1] = (*this)[this->size () - 1];
410 (*this)[pos - 1]->*index = pos;
411 }
412
413 this->pop_back ();
414 }
415
416 void erase (T &obj)
417 {
418 errase (&obj);
419 }
420};
218 421
219// basically does what strncpy should do, but appends "..." to strings exceeding length 422// basically does what strncpy should do, but appends "..." to strings exceeding length
220void assign (char *dst, const char *src, int maxlen); 423void assign (char *dst, const char *src, int maxlen);
221 424
222// type-safe version of assign 425// type-safe version of assign
229typedef double tstamp; 432typedef double tstamp;
230 433
231// return current time as timestampe 434// return current time as timestampe
232tstamp now (); 435tstamp now ();
233 436
437int similar_direction (int a, int b);
438
234#endif 439#endif
235 440

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines