ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
Revision: 1.33
Committed: Thu Jan 18 22:20:00 2007 UTC (17 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.32: +1 -1 lines
Log Message:
RANDOM was broken due to signedness issues, also rewrote random_roll, no longer uses luck, but should

File Contents

# User Rev Content
1 root 1.1 #ifndef UTIL_H__
2     #define UTIL_H__
3    
4 root 1.2 #if __GNUC__ >= 3
5     # define is_constant(c) __builtin_constant_p (c)
6     #else
7     # define is_constant(c) 0
8     #endif
9    
10 root 1.11 #include <cstddef>
11 root 1.28 #include <cmath>
12 root 1.25 #include <new>
13     #include <vector>
14 root 1.11
15     #include <glib.h>
16    
17 root 1.25 #include <shstr.h>
18     #include <traits.h>
19    
20 root 1.14 // use a gcc extension for auto declarations until ISO C++ sanctifies them
21     #define AUTODECL(var,expr) typeof(expr) var = (expr)
22    
23 root 1.26 // 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 root 1.27 // 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 root 1.31 void fork_abort (const char *msg);
39    
40 root 1.32 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
41     template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
42     template<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    
44     template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
45    
46 root 1.28 // this is much faster than crossfires original algorithm
47     // on modern cpus
48     inline int
49     isqrt (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
60     inline int
61     idistance (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 root 1.30 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
72 root 1.28 #endif
73     }
74    
75 root 1.29 /*
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     */
80     inline int
81     absdir (int d)
82     {
83     return ((d - 1) & 7) + 1;
84     }
85 root 1.28
86 root 1.1 // makes dynamically allocated objects zero-initialised
87     struct zero_initialised
88     {
89 root 1.11 void *operator new (size_t s, void *p)
90     {
91     memset (p, 0, s);
92     return p;
93     }
94    
95     void *operator new (size_t s)
96     {
97     return g_slice_alloc0 (s);
98     }
99    
100     void *operator new[] (size_t s)
101     {
102     return g_slice_alloc0 (s);
103     }
104    
105     void operator delete (void *p, size_t s)
106     {
107     g_slice_free1 (s, p);
108     }
109    
110     void operator delete[] (void *p, size_t s)
111     {
112     g_slice_free1 (s, p);
113     }
114     };
115    
116 root 1.20 void *salloc_ (int n) throw (std::bad_alloc);
117     void *salloc_ (int n, void *src) throw (std::bad_alloc);
118    
119 root 1.12 // strictly the same as g_slice_alloc, but never returns 0
120 root 1.20 template<typename T>
121     inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
122    
123 root 1.17 // also copies src into the new area, like "memdup"
124 root 1.18 // if src is 0, clears the memory
125     template<typename T>
126 root 1.20 inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
127 root 1.18
128 root 1.21 // clears the memory
129     template<typename T>
130     inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
131    
132 root 1.12 // for symmetry
133 root 1.18 template<typename T>
134 root 1.20 inline void sfree (T *ptr, int n = 1) throw ()
135 root 1.12 {
136 root 1.20 g_slice_free1 (n * sizeof (T), (void *)ptr);
137 root 1.12 }
138 root 1.11
139     // a STL-compatible allocator that uses g_slice
140     // boy, this is verbose
141     template<typename Tp>
142     struct slice_allocator
143     {
144     typedef size_t size_type;
145     typedef ptrdiff_t difference_type;
146     typedef Tp *pointer;
147     typedef const Tp *const_pointer;
148     typedef Tp &reference;
149     typedef const Tp &const_reference;
150     typedef Tp value_type;
151    
152     template <class U>
153     struct rebind
154     {
155     typedef slice_allocator<U> other;
156     };
157    
158     slice_allocator () throw () { }
159     slice_allocator (const slice_allocator &o) throw () { }
160     template<typename Tp2>
161     slice_allocator (const slice_allocator<Tp2> &) throw () { }
162    
163     ~slice_allocator () { }
164    
165     pointer address (reference x) const { return &x; }
166     const_pointer address (const_reference x) const { return &x; }
167    
168     pointer allocate (size_type n, const_pointer = 0)
169     {
170 root 1.18 return salloc<Tp> (n);
171 root 1.11 }
172    
173     void deallocate (pointer p, size_type n)
174     {
175 root 1.19 sfree<Tp> (p, n);
176 root 1.11 }
177    
178     size_type max_size ()const throw ()
179     {
180     return size_t (-1) / sizeof (Tp);
181     }
182    
183     void construct (pointer p, const Tp &val)
184     {
185     ::new (p) Tp (val);
186     }
187    
188     void destroy (pointer p)
189     {
190     p->~Tp ();
191     }
192 root 1.1 };
193    
194 root 1.32 // 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
197     struct tausworthe_random_generator
198     {
199     uint32_t state [4];
200    
201     tausworthe_random_generator (uint32_t seed);
202     uint32_t next ();
203    
204     uint32_t operator ()(uint32_t r_max)
205     {
206     return next () % r_max;
207     }
208    
209     // return a number within (min .. max)
210     int operator () (int r_min, int r_max)
211     {
212 root 1.33 return r_min + (*this) (max (r_max - r_min + 1, 1));
213 root 1.32 }
214    
215     double operator ()()
216     {
217     return next () / (double)0xFFFFFFFFU;
218     }
219     };
220    
221     typedef tausworthe_random_generator rand_gen;
222    
223     extern rand_gen rndm;
224    
225 root 1.7 template<class T>
226     struct refptr
227     {
228     T *p;
229    
230     refptr () : p(0) { }
231     refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); }
232     refptr (T *p) : p(p) { if (p) p->refcnt_inc (); }
233     ~refptr () { if (p) p->refcnt_dec (); }
234    
235     const refptr<T> &operator =(T *o)
236     {
237     if (p) p->refcnt_dec ();
238     p = o;
239     if (p) p->refcnt_inc ();
240    
241     return *this;
242     }
243    
244     const refptr<T> &operator =(const refptr<T> o)
245     {
246     *this = o.p;
247     return *this;
248     }
249    
250     T &operator * () const { return *p; }
251     T *operator ->() const { return p; }
252    
253     operator T *() const { return p; }
254     };
255    
256 root 1.24 typedef refptr<maptile> maptile_ptr;
257 root 1.22 typedef refptr<object> object_ptr;
258     typedef refptr<archetype> arch_ptr;
259 root 1.24 typedef refptr<client> client_ptr;
260     typedef refptr<player> player_ptr;
261 root 1.22
262 root 1.4 struct str_hash
263     {
264     std::size_t operator ()(const char *s) const
265     {
266     unsigned long hash = 0;
267    
268     /* use the one-at-a-time hash function, which supposedly is
269     * better than the djb2-like one used by perl5.005, but
270     * certainly is better then the bug used here before.
271     * see http://burtleburtle.net/bob/hash/doobs.html
272     */
273     while (*s)
274     {
275     hash += *s++;
276     hash += hash << 10;
277     hash ^= hash >> 6;
278     }
279    
280     hash += hash << 3;
281     hash ^= hash >> 11;
282     hash += hash << 15;
283    
284     return hash;
285     }
286     };
287    
288     struct str_equal
289     {
290     bool operator ()(const char *a, const char *b) const
291     {
292     return !strcmp (a, b);
293     }
294     };
295    
296 root 1.26 template<class T>
297     struct unordered_vector : std::vector<T, slice_allocator<T> >
298 root 1.6 {
299 root 1.11 typedef typename unordered_vector::iterator iterator;
300 root 1.6
301     void erase (unsigned int pos)
302     {
303     if (pos < this->size () - 1)
304     (*this)[pos] = (*this)[this->size () - 1];
305    
306     this->pop_back ();
307     }
308    
309     void erase (iterator i)
310     {
311     erase ((unsigned int )(i - this->begin ()));
312     }
313     };
314    
315 root 1.26 template<class T, int T::* index>
316     struct object_vector : std::vector<T *, slice_allocator<T *> >
317     {
318     void insert (T *obj)
319     {
320     assert (!(obj->*index));
321     push_back (obj);
322     obj->*index = this->size ();
323     }
324    
325     void insert (T &obj)
326     {
327     insert (&obj);
328     }
329    
330     void erase (T *obj)
331     {
332     assert (obj->*index);
333     int pos = obj->*index;
334     obj->*index = 0;
335    
336     if (pos < this->size ())
337     {
338     (*this)[pos - 1] = (*this)[this->size () - 1];
339     (*this)[pos - 1]->*index = pos;
340     }
341    
342     this->pop_back ();
343     }
344    
345     void erase (T &obj)
346     {
347     errase (&obj);
348     }
349     };
350    
351 root 1.10 // basically does what strncpy should do, but appends "..." to strings exceeding length
352     void assign (char *dst, const char *src, int maxlen);
353    
354     // type-safe version of assign
355 root 1.9 template<int N>
356     inline void assign (char (&dst)[N], const char *src)
357     {
358 root 1.10 assign ((char *)&dst, src, N);
359 root 1.9 }
360    
361 root 1.17 typedef double tstamp;
362    
363     // return current time as timestampe
364     tstamp now ();
365    
366 root 1.25 int similar_direction (int a, int b);
367    
368 root 1.1 #endif
369