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.41 by root, Sat Apr 21 22:57:16 2007 UTC vs.
Revision 1.49 by root, Mon Jun 4 13:04:00 2007 UTC

1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 *
6 * Crossfire TRT is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * The authors can be reached via e-mail to <crossfire@schmorp.de>
21 */
22
1#ifndef UTIL_H__ 23#ifndef UTIL_H__
2#define UTIL_H__ 24#define UTIL_H__
3 25
4//#define PREFER_MALLOC 26//#define PREFER_MALLOC
5 27
6#if __GNUC__ >= 3 28#if __GNUC__ >= 3
7# define is_constant(c) __builtin_constant_p (c) 29# define is_constant(c) __builtin_constant_p (c)
30# define expect(expr,value) __builtin_expect ((expr),(value))
31# define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
8#else 32#else
9# define is_constant(c) 0 33# define is_constant(c) 0
34# define expect(expr,value) (expr)
35# define prefetch(addr,rw,locality)
10#endif 36#endif
37
38#if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4)
39# define decltype(x) typeof(x)
40#endif
41
42// put into ifs if you are very sure that the expression
43// is mostly true or mosty false. note that these return
44// booleans, not the expression.
45#define expect_false(expr) expect ((expr) != 0, 0)
46#define expect_true(expr) expect ((expr) != 0, 1)
11 47
12#include <cstddef> 48#include <cstddef>
13#include <cmath> 49#include <cmath>
14#include <new> 50#include <new>
15#include <vector> 51#include <vector>
17#include <glib.h> 53#include <glib.h>
18 54
19#include <shstr.h> 55#include <shstr.h>
20#include <traits.h> 56#include <traits.h>
21 57
22// use a gcc extension for auto declarations until ISO C++ sanctifies them 58// use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever)
23#define auto(var,expr) typeof(expr) var = (expr) 59#define auto(var,expr) decltype(expr) var = (expr)
24 60
25// very ugly macro that basicaly declares and initialises a variable 61// very ugly macro that basicaly declares and initialises a variable
26// that is in scope for the next statement only 62// that is in scope for the next statement only
27// works only for stuff that can be assigned 0 and converts to false 63// works only for stuff that can be assigned 0 and converts to false
28// (note: works great for pointers) 64// (note: works great for pointers)
29// most ugly macro I ever wrote 65// most ugly macro I ever wrote
30#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) 66#define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
31 67
32// in range including end 68// in range including end
33#define IN_RANGE_INC(val,beg,end) \ 69#define IN_RANGE_INC(val,beg,end) \
34 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 70 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
35 71
44template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } 80template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
45template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } 81template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
46template<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; } 82template<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 83
48template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 84template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
85
86template<typename T>
87static inline T
88lerp (T val, T min_in, T max_in, T min_out, T max_out)
89{
90 return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out;
91}
49 92
50// lots of stuff taken from FXT 93// lots of stuff taken from FXT
51 94
52/* Rotate right. This is used in various places for checksumming */ 95/* Rotate right. This is used in various places for checksumming */
53//TODO: that sucks, use a better checksum algo 96//TODO: that sucks, use a better checksum algo
262 305
263 void seed (uint32_t seed); 306 void seed (uint32_t seed);
264 uint32_t next (); 307 uint32_t next ();
265 308
266 // uniform distribution 309 // uniform distribution
267 uint32_t operator ()(uint32_t r_max) 310 uint32_t operator ()(uint32_t num)
268 { 311 {
269 return is_constant (r_max) 312 return is_constant (num)
270 ? (next () * (uint64_t)r_max) >> 32U 313 ? (next () * (uint64_t)num) >> 32U
271 : get_range (r_max); 314 : get_range (num);
272 } 315 }
273 316
274 // return a number within (min .. max) 317 // return a number within (min .. max)
275 int operator () (int r_min, int r_max) 318 int operator () (int r_min, int r_max)
276 { 319 {
277 return is_constant (r_min) && is_constant (r_max) 320 return is_constant (r_min) && is_constant (r_max) && r_min <= r_max
278 ? r_min + operator ()(max (r_max - r_min + 1, 1)) 321 ? r_min + operator ()(r_max - r_min + 1)
279 : get_range (r_min, r_max); 322 : get_range (r_min, r_max);
280 } 323 }
281 324
282 double operator ()() 325 double operator ()()
283 { 326 {
362 { 405 {
363 return !strcmp (a, b); 406 return !strcmp (a, b);
364 } 407 }
365}; 408};
366 409
410// Mostly the same as std::vector, but insert/erase can reorder
411// the elements, making insret/remove O(1) instead of O(n).
412//
413// NOTE: only some forms of erase/insert are available
367template<class T> 414template<class T>
368struct unordered_vector : std::vector<T, slice_allocator<T> > 415struct unordered_vector : std::vector<T, slice_allocator<T> >
369{ 416{
370 typedef typename unordered_vector::iterator iterator; 417 typedef typename unordered_vector::iterator iterator;
371 418
381 { 428 {
382 erase ((unsigned int )(i - this->begin ())); 429 erase ((unsigned int )(i - this->begin ()));
383 } 430 }
384}; 431};
385 432
433// This container blends advantages of linked lists
434// (efficiency) with vectors (random access) by
435// by using an unordered vector and storing the vector
436// index inside the object.
437//
438// + memory-efficient on most 64 bit archs
439// + O(1) insert/remove
440// + free unique (but varying) id for inserted objects
441// + cache-friendly iteration
442// - only works for pointers to structs
443//
444// NOTE: only some forms of erase/insert are available
386template<class T, int T::* index> 445template<class T, int T::* index>
387struct object_vector : std::vector<T *, slice_allocator<T *> > 446struct object_vector : std::vector<T *, slice_allocator<T *> >
388{ 447{
448 typedef typename object_vector::iterator iterator;
449
450 bool contains (const T *obj) const
451 {
452 return obj->*index;
453 }
454
455 iterator find (const T *obj)
456 {
457 return obj->*index
458 ? this->begin () + obj->*index - 1
459 : this->end ();
460 }
461
389 void insert (T *obj) 462 void insert (T *obj)
390 { 463 {
391 assert (!(obj->*index));
392 push_back (obj); 464 push_back (obj);
393 obj->*index = this->size (); 465 obj->*index = this->size ();
394 } 466 }
395 467
396 void insert (T &obj) 468 void insert (T &obj)
398 insert (&obj); 470 insert (&obj);
399 } 471 }
400 472
401 void erase (T *obj) 473 void erase (T *obj)
402 { 474 {
403 assert (obj->*index);
404 unsigned int pos = obj->*index; 475 unsigned int pos = obj->*index;
405 obj->*index = 0; 476 obj->*index = 0;
406 477
407 if (pos < this->size ()) 478 if (pos < this->size ())
408 { 479 {
434// return current time as timestampe 505// return current time as timestampe
435tstamp now (); 506tstamp now ();
436 507
437int similar_direction (int a, int b); 508int similar_direction (int a, int b);
438 509
510// like printf, but returns a std::string
511const std::string format (const char *format, ...);
512
439#endif 513#endif
440 514

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines