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.35 by root, Fri Jan 19 22:47:57 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>
13 19
14// use a gcc extension for auto declarations until ISO C++ sanctifies them 20// use a gcc extension for auto declarations until ISO C++ sanctifies them
15#define AUTODECL(var,expr) typeof(expr) var = (expr) 21#define AUTODECL(var,expr) typeof(expr) var = (expr)
16 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
40// rationale for using (U) not (T) is to reduce signed/unsigned issues,
41// as a is often a constant while b is the variable. it is still a bug, though.
42template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
43template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
44template<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; }
45
46template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
47
48// this is much faster than crossfires original algorithm
49// on modern cpus
50inline int
51isqrt (int n)
52{
53 return (int)sqrtf ((float)n);
54}
55
56// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
57#if 0
58// and has a max. error of 6 in the range -100..+100.
59#else
60// and has a max. error of 9 in the range -100..+100.
61#endif
62inline int
63idistance (int dx, int dy)
64{
65 unsigned int dx_ = abs (dx);
66 unsigned int dy_ = abs (dy);
67
68#if 0
69 return dx_ > dy_
70 ? (dx_ * 61685 + dy_ * 26870) >> 16
71 : (dy_ * 61685 + dx_ * 26870) >> 16;
72#else
73 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
74#endif
75}
76
77/*
78 * absdir(int): Returns a number between 1 and 8, which represent
79 * the "absolute" direction of a number (it actually takes care of
80 * "overflow" in previous calculations of a direction).
81 */
82inline int
83absdir (int d)
84{
85 return ((d - 1) & 7) + 1;
86}
87
17// makes dynamically allocated objects zero-initialised 88// makes dynamically allocated objects zero-initialised
18struct zero_initialised 89struct zero_initialised
19{ 90{
20 void *operator new (size_t s, void *p) 91 void *operator new (size_t s, void *p)
21 { 92 {
42 { 113 {
43 g_slice_free1 (s, p); 114 g_slice_free1 (s, p);
44 } 115 }
45}; 116};
46 117
118void *salloc_ (int n) throw (std::bad_alloc);
119void *salloc_ (int n, void *src) throw (std::bad_alloc);
120
47// strictly the same as g_slice_alloc, but never returns 0 121// strictly the same as g_slice_alloc, but never returns 0
48void *salloc (int size) throw (std::bad_alloc); 122template<typename T>
123inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
124
49// also copies src into the new area, like "memdup" 125// also copies src into the new area, like "memdup"
50void *salloc (int size, void *src) throw (std::bad_alloc); 126// if src is 0, clears the memory
127template<typename T>
128inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
129
130// clears the memory
131template<typename T>
132inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
133
51// for symmetry 134// for symmetry
135template<typename T>
52inline void sfree (void *ptr, int size) throw () 136inline void sfree (T *ptr, int n = 1) throw ()
53{ 137{
54 g_slice_free1 (size, ptr); 138 g_slice_free1 (n * sizeof (T), (void *)ptr);
55} 139}
56 140
57// a STL-compatible allocator that uses g_slice 141// a STL-compatible allocator that uses g_slice
58// boy, this is verbose 142// boy, this is verbose
59template<typename Tp> 143template<typename Tp>
83 pointer address (reference x) const { return &x; } 167 pointer address (reference x) const { return &x; }
84 const_pointer address (const_reference x) const { return &x; } 168 const_pointer address (const_reference x) const { return &x; }
85 169
86 pointer allocate (size_type n, const_pointer = 0) 170 pointer allocate (size_type n, const_pointer = 0)
87 { 171 {
88 return static_cast<pointer>(salloc (n * sizeof (Tp))); 172 return salloc<Tp> (n);
89 } 173 }
90 174
91 void deallocate (pointer p, size_type n) 175 void deallocate (pointer p, size_type n)
92 { 176 {
93 sfree (static_cast<void *>(p), n * sizeof (Tp)); 177 sfree<Tp> (p, n);
94 } 178 }
95 179
96 size_type max_size ()const throw () 180 size_type max_size ()const throw ()
97 { 181 {
98 return size_t (-1) / sizeof (Tp); 182 return size_t (-1) / sizeof (Tp);
107 { 191 {
108 p->~Tp (); 192 p->~Tp ();
109 } 193 }
110}; 194};
111 195
112struct refcounted 196// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
197// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
198// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
199struct tausworthe_random_generator
113{ 200{
114 refcounted () : refcnt (0) { } 201 // generator
115// virtual ~refcounted (); 202 uint32_t state [4];
116 void refcnt_inc () { ++refcnt; } 203
117 void refcnt_dec () { --refcnt; } 204 void operator =(const tausworthe_random_generator &src)
118 bool dead () { return refcnt == 0; } 205 {
119 mutable int refcnt; 206 state [0] = src.state [0];
120#if 0 207 state [1] = src.state [1];
121private: 208 state [2] = src.state [2];
122 static refcounted *rc_first; 209 state [3] = src.state [3];
123 refcounted *rc_next; 210 }
124#endif 211
212 void seed (uint32_t seed);
213 uint32_t next ();
214
215 // uniform distribution
216 uint32_t operator ()(uint32_t r_max)
217 {
218 return is_constant (r_max)
219 ? this->next () % r_max
220 : get_range (r_max);
221 }
222
223 // return a number within (min .. max)
224 int operator () (int r_min, int r_max)
225 {
226 return is_constant (r_min) && is_constant (r_max)
227 ? r_min + (*this) (max (r_max - r_min + 1, 1))
228 : get_range (r_min, r_max);
229 }
230
231 double operator ()()
232 {
233 return this->next () / (double)0xFFFFFFFFU;
234 }
235
236protected:
237 uint32_t get_range (uint32_t r_max);
238 int get_range (int r_min, int r_max);
125}; 239};
240
241typedef tausworthe_random_generator rand_gen;
242
243extern rand_gen rndm;
126 244
127template<class T> 245template<class T>
128struct refptr 246struct refptr
129{ 247{
130 T *p; 248 T *p;
152 T &operator * () const { return *p; } 270 T &operator * () const { return *p; }
153 T *operator ->() const { return p; } 271 T *operator ->() const { return p; }
154 272
155 operator T *() const { return p; } 273 operator T *() const { return p; }
156}; 274};
275
276typedef refptr<maptile> maptile_ptr;
277typedef refptr<object> object_ptr;
278typedef refptr<archetype> arch_ptr;
279typedef refptr<client> client_ptr;
280typedef refptr<player> player_ptr;
157 281
158struct str_hash 282struct str_hash
159{ 283{
160 std::size_t operator ()(const char *s) const 284 std::size_t operator ()(const char *s) const
161 { 285 {
187 { 311 {
188 return !strcmp (a, b); 312 return !strcmp (a, b);
189 } 313 }
190}; 314};
191 315
192#include <vector>
193
194template<class obj> 316template<class T>
195struct unordered_vector : std::vector<obj, slice_allocator<obj> > 317struct unordered_vector : std::vector<T, slice_allocator<T> >
196{ 318{
197 typedef typename unordered_vector::iterator iterator; 319 typedef typename unordered_vector::iterator iterator;
198 320
199 void erase (unsigned int pos) 321 void erase (unsigned int pos)
200 { 322 {
208 { 330 {
209 erase ((unsigned int )(i - this->begin ())); 331 erase ((unsigned int )(i - this->begin ()));
210 } 332 }
211}; 333};
212 334
213template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 335template<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; } 336struct 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; } 337{
338 void insert (T *obj)
339 {
340 assert (!(obj->*index));
341 push_back (obj);
342 obj->*index = this->size ();
343 }
216 344
217template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 345 void insert (T &obj)
346 {
347 insert (&obj);
348 }
349
350 void erase (T *obj)
351 {
352 assert (obj->*index);
353 int pos = obj->*index;
354 obj->*index = 0;
355
356 if (pos < this->size ())
357 {
358 (*this)[pos - 1] = (*this)[this->size () - 1];
359 (*this)[pos - 1]->*index = pos;
360 }
361
362 this->pop_back ();
363 }
364
365 void erase (T &obj)
366 {
367 errase (&obj);
368 }
369};
218 370
219// basically does what strncpy should do, but appends "..." to strings exceeding length 371// basically does what strncpy should do, but appends "..." to strings exceeding length
220void assign (char *dst, const char *src, int maxlen); 372void assign (char *dst, const char *src, int maxlen);
221 373
222// type-safe version of assign 374// type-safe version of assign
229typedef double tstamp; 381typedef double tstamp;
230 382
231// return current time as timestampe 383// return current time as timestampe
232tstamp now (); 384tstamp now ();
233 385
386int similar_direction (int a, int b);
387
234#endif 388#endif
235 389

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines