ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
Revision: 1.37
Committed: Thu Feb 15 15:43:36 2007 UTC (17 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.36: +39 -0 lines
Log Message:
- useless µ-opts
- use maximum norm in get_rangevector (a bit, should use more)

File Contents

# User Rev Content
1 root 1.1 #ifndef UTIL_H__
2     #define UTIL_H__
3    
4 root 1.36 //#define PREFER_MALLOC
5    
6 root 1.2 #if __GNUC__ >= 3
7     # define is_constant(c) __builtin_constant_p (c)
8     #else
9     # define is_constant(c) 0
10     #endif
11    
12 root 1.11 #include <cstddef>
13 root 1.28 #include <cmath>
14 root 1.25 #include <new>
15     #include <vector>
16 root 1.11
17     #include <glib.h>
18    
19 root 1.25 #include <shstr.h>
20     #include <traits.h>
21    
22 root 1.14 // use a gcc extension for auto declarations until ISO C++ sanctifies them
23     #define AUTODECL(var,expr) typeof(expr) var = (expr)
24    
25 root 1.26 // 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 root 1.27 // 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    
40 root 1.31 void fork_abort (const char *msg);
41    
42 root 1.35 // 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.
44     template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
45     template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
46     template<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 root 1.32
48     template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
49    
50 root 1.37 // lots of stuff taken from FXT
51    
52     /* Rotate right. This is used in various places for checksumming */
53     //TODO: this sucks, use a better checksum algo
54     static inline uint32_t
55     rotate_right (uint32_t c)
56     {
57     return (c << 31) | (c >> 1);
58     }
59    
60     // Return abs(a-b)
61     // Both a and b must not have the most significant bit set
62     static inline uint32_t
63     upos_abs_diff (uint32_t a, uint32_t b)
64     {
65     long d1 = b - a;
66     long d2 = (d1 & (d1 >> 31)) << 1;
67    
68     return d1 - d2; // == (b - d) - (a + d);
69     }
70    
71     // Both a and b must not have the most significant bit set
72     static inline uint32_t
73     upos_min (uint32_t a, uint32_t b)
74     {
75     int32_t d = b - a;
76     d &= d >> 31;
77     return a + d;
78     }
79    
80     // Both a and b must not have the most significant bit set
81     static inline uint32_t
82     upos_max (uint32_t a, uint32_t b)
83     {
84     int32_t d = b - a;
85     d &= d >> 31;
86     return b - d;
87     }
88    
89 root 1.28 // this is much faster than crossfires original algorithm
90     // on modern cpus
91     inline int
92     isqrt (int n)
93     {
94     return (int)sqrtf ((float)n);
95     }
96    
97     // this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
98     #if 0
99     // and has a max. error of 6 in the range -100..+100.
100     #else
101     // and has a max. error of 9 in the range -100..+100.
102     #endif
103     inline int
104     idistance (int dx, int dy)
105     {
106     unsigned int dx_ = abs (dx);
107     unsigned int dy_ = abs (dy);
108    
109     #if 0
110     return dx_ > dy_
111     ? (dx_ * 61685 + dy_ * 26870) >> 16
112     : (dy_ * 61685 + dx_ * 26870) >> 16;
113     #else
114 root 1.30 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
115 root 1.28 #endif
116     }
117    
118 root 1.29 /*
119     * absdir(int): Returns a number between 1 and 8, which represent
120     * the "absolute" direction of a number (it actually takes care of
121     * "overflow" in previous calculations of a direction).
122     */
123     inline int
124     absdir (int d)
125     {
126     return ((d - 1) & 7) + 1;
127     }
128 root 1.28
129 root 1.1 // makes dynamically allocated objects zero-initialised
130     struct zero_initialised
131     {
132 root 1.11 void *operator new (size_t s, void *p)
133     {
134     memset (p, 0, s);
135     return p;
136     }
137    
138     void *operator new (size_t s)
139     {
140     return g_slice_alloc0 (s);
141     }
142    
143     void *operator new[] (size_t s)
144     {
145     return g_slice_alloc0 (s);
146     }
147    
148     void operator delete (void *p, size_t s)
149     {
150     g_slice_free1 (s, p);
151     }
152    
153     void operator delete[] (void *p, size_t s)
154     {
155     g_slice_free1 (s, p);
156     }
157     };
158    
159 root 1.20 void *salloc_ (int n) throw (std::bad_alloc);
160     void *salloc_ (int n, void *src) throw (std::bad_alloc);
161    
162 root 1.12 // strictly the same as g_slice_alloc, but never returns 0
163 root 1.20 template<typename T>
164     inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
165    
166 root 1.17 // also copies src into the new area, like "memdup"
167 root 1.18 // if src is 0, clears the memory
168     template<typename T>
169 root 1.20 inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
170 root 1.18
171 root 1.21 // clears the memory
172     template<typename T>
173     inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
174    
175 root 1.12 // for symmetry
176 root 1.18 template<typename T>
177 root 1.20 inline void sfree (T *ptr, int n = 1) throw ()
178 root 1.12 {
179 root 1.36 #ifdef PREFER_MALLOC
180     free (ptr);
181     #else
182 root 1.20 g_slice_free1 (n * sizeof (T), (void *)ptr);
183 root 1.36 #endif
184 root 1.12 }
185 root 1.11
186     // a STL-compatible allocator that uses g_slice
187     // boy, this is verbose
188     template<typename Tp>
189     struct slice_allocator
190     {
191     typedef size_t size_type;
192     typedef ptrdiff_t difference_type;
193     typedef Tp *pointer;
194     typedef const Tp *const_pointer;
195     typedef Tp &reference;
196     typedef const Tp &const_reference;
197     typedef Tp value_type;
198    
199     template <class U>
200     struct rebind
201     {
202     typedef slice_allocator<U> other;
203     };
204    
205     slice_allocator () throw () { }
206     slice_allocator (const slice_allocator &o) throw () { }
207     template<typename Tp2>
208     slice_allocator (const slice_allocator<Tp2> &) throw () { }
209    
210     ~slice_allocator () { }
211    
212     pointer address (reference x) const { return &x; }
213     const_pointer address (const_reference x) const { return &x; }
214    
215     pointer allocate (size_type n, const_pointer = 0)
216     {
217 root 1.18 return salloc<Tp> (n);
218 root 1.11 }
219    
220     void deallocate (pointer p, size_type n)
221     {
222 root 1.19 sfree<Tp> (p, n);
223 root 1.11 }
224    
225     size_type max_size ()const throw ()
226     {
227     return size_t (-1) / sizeof (Tp);
228     }
229    
230     void construct (pointer p, const Tp &val)
231     {
232     ::new (p) Tp (val);
233     }
234    
235     void destroy (pointer p)
236     {
237     p->~Tp ();
238     }
239 root 1.1 };
240    
241 root 1.32 // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
242     // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
243     // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
244     struct tausworthe_random_generator
245     {
246 root 1.34 // generator
247 root 1.32 uint32_t state [4];
248    
249 root 1.34 void operator =(const tausworthe_random_generator &src)
250     {
251     state [0] = src.state [0];
252     state [1] = src.state [1];
253     state [2] = src.state [2];
254     state [3] = src.state [3];
255     }
256    
257     void seed (uint32_t seed);
258 root 1.32 uint32_t next ();
259    
260 root 1.34 // uniform distribution
261 root 1.32 uint32_t operator ()(uint32_t r_max)
262     {
263 root 1.34 return is_constant (r_max)
264     ? this->next () % r_max
265     : get_range (r_max);
266 root 1.32 }
267    
268     // return a number within (min .. max)
269     int operator () (int r_min, int r_max)
270     {
271 root 1.34 return is_constant (r_min) && is_constant (r_max)
272     ? r_min + (*this) (max (r_max - r_min + 1, 1))
273     : get_range (r_min, r_max);
274 root 1.32 }
275    
276     double operator ()()
277     {
278 root 1.34 return this->next () / (double)0xFFFFFFFFU;
279 root 1.32 }
280 root 1.34
281     protected:
282     uint32_t get_range (uint32_t r_max);
283     int get_range (int r_min, int r_max);
284 root 1.32 };
285    
286     typedef tausworthe_random_generator rand_gen;
287    
288     extern rand_gen rndm;
289    
290 root 1.7 template<class T>
291     struct refptr
292     {
293     T *p;
294    
295     refptr () : p(0) { }
296     refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); }
297     refptr (T *p) : p(p) { if (p) p->refcnt_inc (); }
298     ~refptr () { if (p) p->refcnt_dec (); }
299    
300     const refptr<T> &operator =(T *o)
301     {
302     if (p) p->refcnt_dec ();
303     p = o;
304     if (p) p->refcnt_inc ();
305    
306     return *this;
307     }
308    
309     const refptr<T> &operator =(const refptr<T> o)
310     {
311     *this = o.p;
312     return *this;
313     }
314    
315     T &operator * () const { return *p; }
316     T *operator ->() const { return p; }
317    
318     operator T *() const { return p; }
319     };
320    
321 root 1.24 typedef refptr<maptile> maptile_ptr;
322 root 1.22 typedef refptr<object> object_ptr;
323     typedef refptr<archetype> arch_ptr;
324 root 1.24 typedef refptr<client> client_ptr;
325     typedef refptr<player> player_ptr;
326 root 1.22
327 root 1.4 struct str_hash
328     {
329     std::size_t operator ()(const char *s) const
330     {
331     unsigned long hash = 0;
332    
333     /* use the one-at-a-time hash function, which supposedly is
334     * better than the djb2-like one used by perl5.005, but
335     * certainly is better then the bug used here before.
336     * see http://burtleburtle.net/bob/hash/doobs.html
337     */
338     while (*s)
339     {
340     hash += *s++;
341     hash += hash << 10;
342     hash ^= hash >> 6;
343     }
344    
345     hash += hash << 3;
346     hash ^= hash >> 11;
347     hash += hash << 15;
348    
349     return hash;
350     }
351     };
352    
353     struct str_equal
354     {
355     bool operator ()(const char *a, const char *b) const
356     {
357     return !strcmp (a, b);
358     }
359     };
360    
361 root 1.26 template<class T>
362     struct unordered_vector : std::vector<T, slice_allocator<T> >
363 root 1.6 {
364 root 1.11 typedef typename unordered_vector::iterator iterator;
365 root 1.6
366     void erase (unsigned int pos)
367     {
368     if (pos < this->size () - 1)
369     (*this)[pos] = (*this)[this->size () - 1];
370    
371     this->pop_back ();
372     }
373    
374     void erase (iterator i)
375     {
376     erase ((unsigned int )(i - this->begin ()));
377     }
378     };
379    
380 root 1.26 template<class T, int T::* index>
381     struct object_vector : std::vector<T *, slice_allocator<T *> >
382     {
383     void insert (T *obj)
384     {
385     assert (!(obj->*index));
386     push_back (obj);
387     obj->*index = this->size ();
388     }
389    
390     void insert (T &obj)
391     {
392     insert (&obj);
393     }
394    
395     void erase (T *obj)
396     {
397     assert (obj->*index);
398     int pos = obj->*index;
399     obj->*index = 0;
400    
401     if (pos < this->size ())
402     {
403     (*this)[pos - 1] = (*this)[this->size () - 1];
404     (*this)[pos - 1]->*index = pos;
405     }
406    
407     this->pop_back ();
408     }
409    
410     void erase (T &obj)
411     {
412     errase (&obj);
413     }
414     };
415    
416 root 1.10 // basically does what strncpy should do, but appends "..." to strings exceeding length
417     void assign (char *dst, const char *src, int maxlen);
418    
419     // type-safe version of assign
420 root 1.9 template<int N>
421     inline void assign (char (&dst)[N], const char *src)
422     {
423 root 1.10 assign ((char *)&dst, src, N);
424 root 1.9 }
425    
426 root 1.17 typedef double tstamp;
427    
428     // return current time as timestampe
429     tstamp now ();
430    
431 root 1.25 int similar_direction (int a, int b);
432    
433 root 1.1 #endif
434