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.11 by root, Tue Sep 12 20:55:40 2006 UTC vs.
Revision 1.34 by root, Fri Jan 19 15:15:50 2007 UTC

6#else 6#else
7# define is_constant(c) 0 7# define is_constant(c) 0
8#endif 8#endif
9 9
10#include <cstddef> 10#include <cstddef>
11#include <cmath>
12#include <new>
13#include <vector>
11 14
12#include <glib.h> 15#include <glib.h>
16
17#include <shstr.h>
18#include <traits.h>
19
20// use a gcc extension for auto declarations until ISO C++ sanctifies them
21#define AUTODECL(var,expr) typeof(expr) var = (expr)
22
23// very ugly macro that basicaly declares and initialises a variable
24// that is in scope for the next statement only
25// works only for stuff that can be assigned 0 and converts to false
26// (note: works great for pointers)
27// most ugly macro I ever wrote
28#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
29
30// in range including end
31#define IN_RANGE_INC(val,beg,end) \
32 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
33
34// in range excluding end
35#define IN_RANGE_EXC(val,beg,end) \
36 ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
37
38void fork_abort (const char *msg);
39
40template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
41template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
42template<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; }
43
44template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
45
46// this is much faster than crossfires original algorithm
47// on modern cpus
48inline int
49isqrt (int n)
50{
51 return (int)sqrtf ((float)n);
52}
53
54// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
55#if 0
56// and has a max. error of 6 in the range -100..+100.
57#else
58// and has a max. error of 9 in the range -100..+100.
59#endif
60inline int
61idistance (int dx, int dy)
62{
63 unsigned int dx_ = abs (dx);
64 unsigned int dy_ = abs (dy);
65
66#if 0
67 return dx_ > dy_
68 ? (dx_ * 61685 + dy_ * 26870) >> 16
69 : (dy_ * 61685 + dx_ * 26870) >> 16;
70#else
71 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
72#endif
73}
74
75/*
76 * absdir(int): Returns a number between 1 and 8, which represent
77 * the "absolute" direction of a number (it actually takes care of
78 * "overflow" in previous calculations of a direction).
79 */
80inline int
81absdir (int d)
82{
83 return ((d - 1) & 7) + 1;
84}
13 85
14// makes dynamically allocated objects zero-initialised 86// makes dynamically allocated objects zero-initialised
15struct zero_initialised 87struct zero_initialised
16{ 88{
17 void *operator new (size_t s, void *p) 89 void *operator new (size_t s, void *p)
39 { 111 {
40 g_slice_free1 (s, p); 112 g_slice_free1 (s, p);
41 } 113 }
42}; 114};
43 115
44void throw_bad_alloc () throw (std::bad_alloc); 116void *salloc_ (int n) throw (std::bad_alloc);
45
46void *alloc (int s) throw (std::bad_alloc); 117void *salloc_ (int n, void *src) throw (std::bad_alloc);
47void dealloc (void *p, int s) throw (); 118
119// strictly the same as g_slice_alloc, but never returns 0
120template<typename T>
121inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
122
123// also copies src into the new area, like "memdup"
124// if src is 0, clears the memory
125template<typename T>
126inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
127
128// clears the memory
129template<typename T>
130inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
131
132// for symmetry
133template<typename T>
134inline void sfree (T *ptr, int n = 1) throw ()
135{
136 g_slice_free1 (n * sizeof (T), (void *)ptr);
137}
48 138
49// a STL-compatible allocator that uses g_slice 139// a STL-compatible allocator that uses g_slice
50// boy, this is verbose 140// boy, this is verbose
51template<typename Tp> 141template<typename Tp>
52struct slice_allocator 142struct slice_allocator
75 pointer address (reference x) const { return &x; } 165 pointer address (reference x) const { return &x; }
76 const_pointer address (const_reference x) const { return &x; } 166 const_pointer address (const_reference x) const { return &x; }
77 167
78 pointer allocate (size_type n, const_pointer = 0) 168 pointer allocate (size_type n, const_pointer = 0)
79 { 169 {
80 return static_cast<pointer>(alloc (n * sizeof (Tp))); 170 return salloc<Tp> (n);
81 } 171 }
82 172
83 void deallocate (pointer p, size_type n) 173 void deallocate (pointer p, size_type n)
84 { 174 {
85 dealloc (static_cast<void *>(p), n); 175 sfree<Tp> (p, n);
86 } 176 }
87 177
88 size_type max_size ()const throw () 178 size_type max_size ()const throw ()
89 { 179 {
90 return size_t (-1) / sizeof (Tp); 180 return size_t (-1) / sizeof (Tp);
99 { 189 {
100 p->~Tp (); 190 p->~Tp ();
101 } 191 }
102}; 192};
103 193
104struct refcounted 194// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
195// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
196// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
197struct tausworthe_random_generator
105{ 198{
106 mutable int refcnt; 199 // generator
107 refcounted () : refcnt (0) { } 200 uint32_t state [4];
108 void refcnt_inc () { ++refcnt; } 201
109 void refcnt_dec () { --refcnt; 202 void operator =(const tausworthe_random_generator &src)
110 if (refcnt < 0)abort();}//D 203 {
204 state [0] = src.state [0];
205 state [1] = src.state [1];
206 state [2] = src.state [2];
207 state [3] = src.state [3];
208 }
209
210 void seed (uint32_t seed);
211 uint32_t next ();
212
213 // uniform distribution
214 uint32_t operator ()(uint32_t r_max)
215 {
216 return is_constant (r_max)
217 ? this->next () % r_max
218 : get_range (r_max);
219 }
220
221 // return a number within (min .. max)
222 int operator () (int r_min, int r_max)
223 {
224 return is_constant (r_min) && is_constant (r_max)
225 ? r_min + (*this) (max (r_max - r_min + 1, 1))
226 : get_range (r_min, r_max);
227 }
228
229 double operator ()()
230 {
231 return this->next () / (double)0xFFFFFFFFU;
232 }
233
234protected:
235 uint32_t get_range (uint32_t r_max);
236 int get_range (int r_min, int r_max);
111}; 237};
238
239typedef tausworthe_random_generator rand_gen;
240
241extern rand_gen rndm;
112 242
113template<class T> 243template<class T>
114struct refptr 244struct refptr
115{ 245{
116 T *p; 246 T *p;
138 T &operator * () const { return *p; } 268 T &operator * () const { return *p; }
139 T *operator ->() const { return p; } 269 T *operator ->() const { return p; }
140 270
141 operator T *() const { return p; } 271 operator T *() const { return p; }
142}; 272};
273
274typedef refptr<maptile> maptile_ptr;
275typedef refptr<object> object_ptr;
276typedef refptr<archetype> arch_ptr;
277typedef refptr<client> client_ptr;
278typedef refptr<player> player_ptr;
143 279
144struct str_hash 280struct str_hash
145{ 281{
146 std::size_t operator ()(const char *s) const 282 std::size_t operator ()(const char *s) const
147 { 283 {
173 { 309 {
174 return !strcmp (a, b); 310 return !strcmp (a, b);
175 } 311 }
176}; 312};
177 313
178#include <vector>
179
180template<class obj> 314template<class T>
181struct unordered_vector : std::vector<obj, slice_allocator<obj> > 315struct unordered_vector : std::vector<T, slice_allocator<T> >
182{ 316{
183 typedef typename unordered_vector::iterator iterator; 317 typedef typename unordered_vector::iterator iterator;
184 318
185 void erase (unsigned int pos) 319 void erase (unsigned int pos)
186 { 320 {
194 { 328 {
195 erase ((unsigned int )(i - this->begin ())); 329 erase ((unsigned int )(i - this->begin ()));
196 } 330 }
197}; 331};
198 332
199template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 333template<class T, int T::* index>
200template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 334struct object_vector : std::vector<T *, slice_allocator<T *> >
201template<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; } 335{
336 void insert (T *obj)
337 {
338 assert (!(obj->*index));
339 push_back (obj);
340 obj->*index = this->size ();
341 }
202 342
203template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 343 void insert (T &obj)
344 {
345 insert (&obj);
346 }
347
348 void erase (T *obj)
349 {
350 assert (obj->*index);
351 int pos = obj->*index;
352 obj->*index = 0;
353
354 if (pos < this->size ())
355 {
356 (*this)[pos - 1] = (*this)[this->size () - 1];
357 (*this)[pos - 1]->*index = pos;
358 }
359
360 this->pop_back ();
361 }
362
363 void erase (T &obj)
364 {
365 errase (&obj);
366 }
367};
204 368
205// basically does what strncpy should do, but appends "..." to strings exceeding length 369// basically does what strncpy should do, but appends "..." to strings exceeding length
206void assign (char *dst, const char *src, int maxlen); 370void assign (char *dst, const char *src, int maxlen);
207 371
208// type-safe version of assign 372// type-safe version of assign
210inline void assign (char (&dst)[N], const char *src) 374inline void assign (char (&dst)[N], const char *src)
211{ 375{
212 assign ((char *)&dst, src, N); 376 assign ((char *)&dst, src, N);
213} 377}
214 378
379typedef double tstamp;
380
381// return current time as timestampe
382tstamp now ();
383
384int similar_direction (int a, int b);
385
215#endif 386#endif
216 387

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines