1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the Affero GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * and the GNU General Public License along with this program. If not, see |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
19 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
21 | */ |
22 | |
22 | |
23 | #ifndef UTIL_H__ |
23 | #ifndef UTIL_H__ |
24 | #define UTIL_H__ |
24 | #define UTIL_H__ |
… | |
… | |
86 | void cleanup (const char *cause, bool make_core = false); |
86 | void cleanup (const char *cause, bool make_core = false); |
87 | void fork_abort (const char *msg); |
87 | void fork_abort (const char *msg); |
88 | |
88 | |
89 | // rationale for using (U) not (T) is to reduce signed/unsigned issues, |
89 | // rationale for using (U) not (T) is to reduce signed/unsigned issues, |
90 | // as a is often a constant while b is the variable. it is still a bug, though. |
90 | // as a is often a constant while b is the variable. it is still a bug, though. |
91 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
91 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
92 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
92 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
93 | 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; } |
93 | 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; } |
94 | |
94 | |
95 | template<typename T, typename U> static inline void min_it (T &v, U m) { v = min (v, (T)m); } |
95 | template<typename T, typename U> static inline void min_it (T &v, U m) { v = min (v, (T)m); } |
96 | template<typename T, typename U> static inline void max_it (T &v, U m) { v = max (v, (T)m); } |
96 | template<typename T, typename U> static inline void max_it (T &v, U m) { v = max (v, (T)m); } |
97 | template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = clamp (v, (T)a, (T)b); } |
97 | template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = clamp (v, (T)a, (T)b); } |
… | |
… | |
114 | |
114 | |
115 | // sign0 returns -1, 0 or +1 |
115 | // sign0 returns -1, 0 or +1 |
116 | template<typename T> |
116 | template<typename T> |
117 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
117 | static inline T sign0 (T v) { return v ? sign (v) : 0; } |
118 | |
118 | |
|
|
119 | //clashes with C++0x |
119 | template<typename T, typename U> |
120 | template<typename T, typename U> |
120 | static inline T copysign (T a, U b) { return a > 0 ? b : -b; } |
121 | static inline T copysign (T a, U b) { return a > 0 ? b : -b; } |
121 | |
122 | |
122 | // div* only work correctly for div > 0 |
123 | // div* only work correctly for div > 0 |
123 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
124 | // div, with correct rounding (< 0.5 downwards, >=0.5 upwards) |
… | |
… | |
251 | #else |
252 | #else |
252 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
253 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
253 | #endif |
254 | #endif |
254 | } |
255 | } |
255 | |
256 | |
|
|
257 | // can be substantially faster than floor, if your value range allows for it |
|
|
258 | template<typename T> |
|
|
259 | inline T |
|
|
260 | fastfloor (T x) |
|
|
261 | { |
|
|
262 | return std::floor (x); |
|
|
263 | } |
|
|
264 | |
|
|
265 | inline float |
|
|
266 | fastfloor (float x) |
|
|
267 | { |
|
|
268 | return sint32(x) - (x < 0); |
|
|
269 | } |
|
|
270 | |
|
|
271 | inline double |
|
|
272 | fastfloor (double x) |
|
|
273 | { |
|
|
274 | return sint64(x) - (x < 0); |
|
|
275 | } |
|
|
276 | |
256 | /* |
277 | /* |
257 | * absdir(int): Returns a number between 1 and 8, which represent |
278 | * absdir(int): Returns a number between 1 and 8, which represent |
258 | * the "absolute" direction of a number (it actually takes care of |
279 | * the "absolute" direction of a number (it actually takes care of |
259 | * "overflow" in previous calculations of a direction). |
280 | * "overflow" in previous calculations of a direction). |
260 | */ |
281 | */ |
… | |
… | |
303 | if (expect_true (ptr)) |
324 | if (expect_true (ptr)) |
304 | { |
325 | { |
305 | slice_alloc -= n * sizeof (T); |
326 | slice_alloc -= n * sizeof (T); |
306 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
327 | if (DEBUG_POISON) memset (ptr, DEBUG_POISON, n * sizeof (T)); |
307 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
328 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
308 | assert (slice_alloc >= 0);//D |
|
|
309 | } |
329 | } |
310 | } |
330 | } |
311 | |
331 | |
312 | // nulls the pointer |
332 | // nulls the pointer |
313 | template<typename T> |
333 | template<typename T> |
… | |
… | |
429 | { |
449 | { |
430 | p->~Tp (); |
450 | p->~Tp (); |
431 | } |
451 | } |
432 | }; |
452 | }; |
433 | |
453 | |
434 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
454 | // basically a memory area, but refcounted |
435 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
455 | struct refcnt_buf |
436 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
437 | struct tausworthe_random_generator |
|
|
438 | { |
456 | { |
439 | uint32_t state [4]; |
457 | char *data; |
440 | |
458 | |
441 | void operator =(const tausworthe_random_generator &src) |
459 | refcnt_buf (size_t size = 0); |
442 | { |
460 | refcnt_buf (void *data, size_t size); |
443 | state [0] = src.state [0]; |
|
|
444 | state [1] = src.state [1]; |
|
|
445 | state [2] = src.state [2]; |
|
|
446 | state [3] = src.state [3]; |
|
|
447 | } |
|
|
448 | |
461 | |
449 | void seed (uint32_t seed); |
462 | refcnt_buf (const refcnt_buf &src) |
450 | uint32_t next (); |
|
|
451 | }; |
|
|
452 | |
|
|
453 | // Xorshift RNGs, George Marsaglia |
|
|
454 | // http://www.jstatsoft.org/v08/i14/paper |
|
|
455 | // this one is about 40% faster than the tausworthe one above (i.e. not much), |
|
|
456 | // despite the inlining, and has the issue of only creating 2**32-1 numbers. |
|
|
457 | // see also http://www.iro.umontreal.ca/~lecuyer/myftp/papers/xorshift.pdf |
|
|
458 | struct xorshift_random_generator |
|
|
459 | { |
|
|
460 | uint32_t x, y; |
|
|
461 | |
|
|
462 | void operator =(const xorshift_random_generator &src) |
|
|
463 | { |
463 | { |
464 | x = src.x; |
464 | data = src.data; |
465 | y = src.y; |
465 | inc (); |
466 | } |
466 | } |
467 | |
467 | |
468 | void seed (uint32_t seed) |
468 | ~refcnt_buf (); |
469 | { |
|
|
470 | x = seed; |
|
|
471 | y = seed * 69069U; |
|
|
472 | } |
|
|
473 | |
469 | |
474 | uint32_t next () |
470 | refcnt_buf &operator =(const refcnt_buf &src); |
|
|
471 | |
|
|
472 | operator char *() |
475 | { |
473 | { |
476 | uint32_t t = x ^ (x << 10); |
|
|
477 | x = y; |
|
|
478 | y = y ^ (y >> 13) ^ t ^ (t >> 10); |
|
|
479 | return y; |
474 | return data; |
480 | } |
475 | } |
481 | }; |
|
|
482 | |
476 | |
483 | template<class generator> |
477 | size_t size () const |
484 | struct random_number_generator : generator |
|
|
485 | { |
|
|
486 | // uniform distribution, [0 .. num - 1] |
|
|
487 | uint32_t operator ()(uint32_t num) |
|
|
488 | { |
478 | { |
489 | return !is_constant (num) ? get_range (num) // non-constant |
479 | return _size (); |
490 | : num & (num - 1) ? (this->next () * (uint64_t)num) >> 32U // constant, non-power-of-two |
|
|
491 | : this->next () & (num - 1); // constant, power-of-two |
|
|
492 | } |
|
|
493 | |
|
|
494 | // return a number within the closed interval [min .. max] |
|
|
495 | int operator () (int r_min, int r_max) |
|
|
496 | { |
|
|
497 | return is_constant (r_min <= r_max) && r_min <= r_max |
|
|
498 | ? r_min + operator ()(r_max - r_min + 1) |
|
|
499 | : get_range (r_min, r_max); |
|
|
500 | } |
|
|
501 | |
|
|
502 | // return a number within the half-open interval [0..1[ |
|
|
503 | double operator ()() |
|
|
504 | { |
|
|
505 | return this->next () / (double)0x100000000ULL; |
|
|
506 | } |
480 | } |
507 | |
481 | |
508 | protected: |
482 | protected: |
509 | uint32_t get_range (uint32_t r_max); |
483 | enum { |
510 | int get_range (int r_min, int r_max); |
484 | overhead = sizeof (uint32_t) * 2 |
511 | }; |
485 | }; |
512 | |
486 | |
513 | typedef random_number_generator<tausworthe_random_generator> rand_gen; |
487 | uint32_t &_size () const |
|
|
488 | { |
|
|
489 | return ((unsigned int *)data)[-2]; |
|
|
490 | } |
514 | |
491 | |
515 | extern rand_gen rndm, rmg_rndm; |
492 | uint32_t &_refcnt () const |
|
|
493 | { |
|
|
494 | return ((unsigned int *)data)[-1]; |
|
|
495 | } |
|
|
496 | |
|
|
497 | void _alloc (uint32_t size) |
|
|
498 | { |
|
|
499 | data = ((char *)salloc<char> (size + overhead)) + overhead; |
|
|
500 | _size () = size; |
|
|
501 | _refcnt () = 1; |
|
|
502 | } |
|
|
503 | |
|
|
504 | void _dealloc (); |
|
|
505 | |
|
|
506 | void inc () |
|
|
507 | { |
|
|
508 | ++_refcnt (); |
|
|
509 | } |
|
|
510 | |
|
|
511 | void dec () |
|
|
512 | { |
|
|
513 | if (!--_refcnt ()) |
|
|
514 | _dealloc (); |
|
|
515 | } |
|
|
516 | }; |
516 | |
517 | |
517 | INTERFACE_CLASS (attachable) |
518 | INTERFACE_CLASS (attachable) |
518 | struct refcnt_base |
519 | struct refcnt_base |
519 | { |
520 | { |
520 | typedef int refcnt_t; |
521 | typedef int refcnt_t; |
… | |
… | |
594 | // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/) |
595 | // use FNV-1a hash (http://isthe.com/chongo/tech/comp/fnv/) |
595 | // it is about twice as fast as the one-at-a-time one, |
596 | // it is about twice as fast as the one-at-a-time one, |
596 | // with good distribution. |
597 | // with good distribution. |
597 | // FNV-1a is faster on many cpus because the multiplication |
598 | // FNV-1a is faster on many cpus because the multiplication |
598 | // runs concurrently with the looping logic. |
599 | // runs concurrently with the looping logic. |
|
|
600 | // we modify the hash a bit to improve its distribution |
599 | uint32_t hash = STRHSH_NULL; |
601 | uint32_t hash = STRHSH_NULL; |
600 | |
602 | |
601 | while (*s) |
603 | while (*s) |
602 | hash = (hash ^ *s++) * 16777619U; |
604 | hash = (hash ^ *s++) * 16777619U; |
603 | |
605 | |
604 | return hash; |
606 | return hash ^ (hash >> 16); |
605 | } |
607 | } |
606 | |
608 | |
607 | static inline uint32_t |
609 | static inline uint32_t |
608 | memhsh (const char *s, size_t len) |
610 | memhsh (const char *s, size_t len) |
609 | { |
611 | { |
… | |
… | |
659 | } |
661 | } |
660 | }; |
662 | }; |
661 | |
663 | |
662 | // This container blends advantages of linked lists |
664 | // This container blends advantages of linked lists |
663 | // (efficiency) with vectors (random access) by |
665 | // (efficiency) with vectors (random access) by |
664 | // by using an unordered vector and storing the vector |
666 | // using an unordered vector and storing the vector |
665 | // index inside the object. |
667 | // index inside the object. |
666 | // |
668 | // |
667 | // + memory-efficient on most 64 bit archs |
669 | // + memory-efficient on most 64 bit archs |
668 | // + O(1) insert/remove |
670 | // + O(1) insert/remove |
669 | // + free unique (but varying) id for inserted objects |
671 | // + free unique (but varying) id for inserted objects |
… | |
… | |
706 | insert (&obj); |
708 | insert (&obj); |
707 | } |
709 | } |
708 | |
710 | |
709 | void erase (T *obj) |
711 | void erase (T *obj) |
710 | { |
712 | { |
711 | unsigned int pos = obj->*indexmember; |
713 | object_vector_index pos = obj->*indexmember; |
712 | obj->*indexmember = 0; |
714 | obj->*indexmember = 0; |
713 | |
715 | |
714 | if (pos < this->size ()) |
716 | if (pos < this->size ()) |
715 | { |
717 | { |
716 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
718 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
… | |
… | |
724 | { |
726 | { |
725 | erase (&obj); |
727 | erase (&obj); |
726 | } |
728 | } |
727 | }; |
729 | }; |
728 | |
730 | |
|
|
731 | ///////////////////////////////////////////////////////////////////////////// |
|
|
732 | |
|
|
733 | // something like a vector or stack, but without |
|
|
734 | // out of bounds checking |
|
|
735 | template<typename T> |
|
|
736 | struct fixed_stack |
|
|
737 | { |
|
|
738 | T *data; |
|
|
739 | int size; |
|
|
740 | int max; |
|
|
741 | |
|
|
742 | fixed_stack () |
|
|
743 | : size (0), data (0) |
|
|
744 | { |
|
|
745 | } |
|
|
746 | |
|
|
747 | fixed_stack (int max) |
|
|
748 | : size (0), max (max) |
|
|
749 | { |
|
|
750 | data = salloc<T> (max); |
|
|
751 | } |
|
|
752 | |
|
|
753 | void reset (int new_max) |
|
|
754 | { |
|
|
755 | sfree (data, max); |
|
|
756 | size = 0; |
|
|
757 | max = new_max; |
|
|
758 | data = salloc<T> (max); |
|
|
759 | } |
|
|
760 | |
|
|
761 | void free () |
|
|
762 | { |
|
|
763 | sfree (data, max); |
|
|
764 | data = 0; |
|
|
765 | } |
|
|
766 | |
|
|
767 | ~fixed_stack () |
|
|
768 | { |
|
|
769 | sfree (data, max); |
|
|
770 | } |
|
|
771 | |
|
|
772 | T &operator[](int idx) |
|
|
773 | { |
|
|
774 | return data [idx]; |
|
|
775 | } |
|
|
776 | |
|
|
777 | void push (T v) |
|
|
778 | { |
|
|
779 | data [size++] = v; |
|
|
780 | } |
|
|
781 | |
|
|
782 | T &pop () |
|
|
783 | { |
|
|
784 | return data [--size]; |
|
|
785 | } |
|
|
786 | |
|
|
787 | T remove (int idx) |
|
|
788 | { |
|
|
789 | T v = data [idx]; |
|
|
790 | |
|
|
791 | data [idx] = data [--size]; |
|
|
792 | |
|
|
793 | return v; |
|
|
794 | } |
|
|
795 | }; |
|
|
796 | |
|
|
797 | ///////////////////////////////////////////////////////////////////////////// |
|
|
798 | |
729 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
799 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
730 | // returns the number of bytes actually used (including \0) |
800 | // returns the number of bytes actually used (including \0) |
731 | int assign (char *dst, const char *src, int maxsize); |
801 | int assign (char *dst, const char *src, int maxsize); |
732 | |
802 | |
733 | // type-safe version of assign |
803 | // type-safe version of assign |