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.22 by root, Sat Dec 23 06:21:02 2006 UTC vs.
Revision 1.28 by root, Mon Jan 15 01:25:41 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)
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
16 67
17// makes dynamically allocated objects zero-initialised 68// makes dynamically allocated objects zero-initialised
18struct zero_initialised 69struct zero_initialised
19{ 70{
20 void *operator new (size_t s, void *p) 71 void *operator new (size_t s, void *p)
120 { 171 {
121 p->~Tp (); 172 p->~Tp ();
122 } 173 }
123}; 174};
124 175
125struct refcounted
126{
127 refcounted () : refcnt (0) { }
128// virtual ~refcounted ();
129 void refcnt_inc () { ++refcnt; }
130 void refcnt_dec () { --refcnt; }
131 bool dead () { return refcnt == 0; }
132 mutable int refcnt;
133#if 0
134private:
135 static refcounted *rc_first;
136 refcounted *rc_next;
137#endif
138};
139
140template<class T> 176template<class T>
141struct refptr 177struct refptr
142{ 178{
143 T *p; 179 T *p;
144 180
166 T *operator ->() const { return p; } 202 T *operator ->() const { return p; }
167 203
168 operator T *() const { return p; } 204 operator T *() const { return p; }
169}; 205};
170 206
171typedef refptr<player> player_ptr; 207typedef refptr<maptile> maptile_ptr;
172typedef refptr<object> object_ptr; 208typedef refptr<object> object_ptr;
173typedef refptr<archetype> arch_ptr; 209typedef refptr<archetype> arch_ptr;
210typedef refptr<client> client_ptr;
211typedef refptr<player> player_ptr;
174 212
175struct str_hash 213struct str_hash
176{ 214{
177 std::size_t operator ()(const char *s) const 215 std::size_t operator ()(const char *s) const
178 { 216 {
204 { 242 {
205 return !strcmp (a, b); 243 return !strcmp (a, b);
206 } 244 }
207}; 245};
208 246
209#include <vector>
210
211template<class obj> 247template<class T>
212struct unordered_vector : std::vector<obj, slice_allocator<obj> > 248struct unordered_vector : std::vector<T, slice_allocator<T> >
213{ 249{
214 typedef typename unordered_vector::iterator iterator; 250 typedef typename unordered_vector::iterator iterator;
215 251
216 void erase (unsigned int pos) 252 void erase (unsigned int pos)
217 { 253 {
222 } 258 }
223 259
224 void erase (iterator i) 260 void erase (iterator i)
225 { 261 {
226 erase ((unsigned int )(i - this->begin ())); 262 erase ((unsigned int )(i - this->begin ()));
263 }
264};
265
266template<class T, int T::* index>
267struct object_vector : std::vector<T *, slice_allocator<T *> >
268{
269 void insert (T *obj)
270 {
271 assert (!(obj->*index));
272 push_back (obj);
273 obj->*index = this->size ();
274 }
275
276 void insert (T &obj)
277 {
278 insert (&obj);
279 }
280
281 void erase (T *obj)
282 {
283 assert (obj->*index);
284 int pos = obj->*index;
285 obj->*index = 0;
286
287 if (pos < this->size ())
288 {
289 (*this)[pos - 1] = (*this)[this->size () - 1];
290 (*this)[pos - 1]->*index = pos;
291 }
292
293 this->pop_back ();
294 }
295
296 void erase (T &obj)
297 {
298 errase (&obj);
227 } 299 }
228}; 300};
229 301
230template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } 302template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
231template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 303template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
246typedef double tstamp; 318typedef double tstamp;
247 319
248// return current time as timestampe 320// return current time as timestampe
249tstamp now (); 321tstamp now ();
250 322
323int similar_direction (int a, int b);
324
251#endif 325#endif
252 326

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines