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.25 by root, Sat Dec 30 10:16:10 2006 UTC vs.
Revision 1.29 by root, Mon Jan 15 01:39:42 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>
11#include <new> 12#include <new>
12#include <vector> 13#include <vector>
13 14
14#include <glib.h> 15#include <glib.h>
15 16
16#include <shstr.h> 17#include <shstr.h>
17#include <traits.h> 18#include <traits.h>
18 19
19// 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
20#define AUTODECL(var,expr) typeof(expr) var = (expr) 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
38// this is much faster than crossfires original algorithm
39// on modern cpus
40inline int
41isqrt (int n)
42{
43 return (int)sqrtf ((float)n);
44}
45
46// this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
47#if 0
48// and has a max. error of 6 in the range -100..+100.
49#else
50// and has a max. error of 9 in the range -100..+100.
51#endif
52inline int
53idistance (int dx, int dy)
54{
55 unsigned int dx_ = abs (dx);
56 unsigned int dy_ = abs (dy);
57
58#if 0
59 return dx_ > dy_
60 ? (dx_ * 61685 + dy_ * 26870) >> 16
61 : (dy_ * 61685 + dx_ * 26870) >> 16;
62#else
63 return dx + dy - min (dx, dy) * 5 / 8;
64#endif
65}
66
67/*
68 * absdir(int): Returns a number between 1 and 8, which represent
69 * the "absolute" direction of a number (it actually takes care of
70 * "overflow" in previous calculations of a direction).
71 */
72inline int
73absdir (int d)
74{
75 return ((d - 1) & 7) + 1;
76}
21 77
22// makes dynamically allocated objects zero-initialised 78// makes dynamically allocated objects zero-initialised
23struct zero_initialised 79struct zero_initialised
24{ 80{
25 void *operator new (size_t s, void *p) 81 void *operator new (size_t s, void *p)
196 { 252 {
197 return !strcmp (a, b); 253 return !strcmp (a, b);
198 } 254 }
199}; 255};
200 256
201template<class obj> 257template<class T>
202struct unordered_vector : std::vector<obj, slice_allocator<obj> > 258struct unordered_vector : std::vector<T, slice_allocator<T> >
203{ 259{
204 typedef typename unordered_vector::iterator iterator; 260 typedef typename unordered_vector::iterator iterator;
205 261
206 void erase (unsigned int pos) 262 void erase (unsigned int pos)
207 { 263 {
212 } 268 }
213 269
214 void erase (iterator i) 270 void erase (iterator i)
215 { 271 {
216 erase ((unsigned int )(i - this->begin ())); 272 erase ((unsigned int )(i - this->begin ()));
273 }
274};
275
276template<class T, int T::* index>
277struct object_vector : std::vector<T *, slice_allocator<T *> >
278{
279 void insert (T *obj)
280 {
281 assert (!(obj->*index));
282 push_back (obj);
283 obj->*index = this->size ();
284 }
285
286 void insert (T &obj)
287 {
288 insert (&obj);
289 }
290
291 void erase (T *obj)
292 {
293 assert (obj->*index);
294 int pos = obj->*index;
295 obj->*index = 0;
296
297 if (pos < this->size ())
298 {
299 (*this)[pos - 1] = (*this)[this->size () - 1];
300 (*this)[pos - 1]->*index = pos;
301 }
302
303 this->pop_back ();
304 }
305
306 void erase (T &obj)
307 {
308 errase (&obj);
217 } 309 }
218}; 310};
219 311
220template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 312template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
221template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 313template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines