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,2011,2012 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__ |
… | |
… | |
237 | #if 0 |
237 | #if 0 |
238 | // and has a max. error of 6 in the range -100..+100. |
238 | // and has a max. error of 6 in the range -100..+100. |
239 | #else |
239 | #else |
240 | // and has a max. error of 9 in the range -100..+100. |
240 | // and has a max. error of 9 in the range -100..+100. |
241 | #endif |
241 | #endif |
242 | inline int |
242 | inline int |
243 | idistance (int dx, int dy) |
243 | idistance (int dx, int dy) |
244 | { |
244 | { |
245 | unsigned int dx_ = abs (dx); |
245 | unsigned int dx_ = abs (dx); |
246 | unsigned int dy_ = abs (dy); |
246 | unsigned int dy_ = abs (dy); |
247 | |
247 | |
248 | #if 0 |
248 | #if 0 |
249 | return dx_ > dy_ |
249 | return dx_ > dy_ |
… | |
… | |
407 | typedef const Tp *const_pointer; |
407 | typedef const Tp *const_pointer; |
408 | typedef Tp &reference; |
408 | typedef Tp &reference; |
409 | typedef const Tp &const_reference; |
409 | typedef const Tp &const_reference; |
410 | typedef Tp value_type; |
410 | typedef Tp value_type; |
411 | |
411 | |
412 | template <class U> |
412 | template <class U> |
413 | struct rebind |
413 | struct rebind |
414 | { |
414 | { |
415 | typedef slice_allocator<U> other; |
415 | typedef slice_allocator<U> other; |
416 | }; |
416 | }; |
417 | |
417 | |
… | |
… | |
460 | refcnt_buf (void *data, size_t size); |
460 | refcnt_buf (void *data, size_t size); |
461 | |
461 | |
462 | refcnt_buf (const refcnt_buf &src) |
462 | refcnt_buf (const refcnt_buf &src) |
463 | { |
463 | { |
464 | data = src.data; |
464 | data = src.data; |
465 | ++_refcnt (); |
465 | inc (); |
466 | } |
466 | } |
467 | |
467 | |
468 | ~refcnt_buf (); |
468 | ~refcnt_buf (); |
469 | |
469 | |
470 | refcnt_buf &operator =(const refcnt_buf &src); |
470 | refcnt_buf &operator =(const refcnt_buf &src); |
… | |
… | |
479 | return _size (); |
479 | return _size (); |
480 | } |
480 | } |
481 | |
481 | |
482 | protected: |
482 | protected: |
483 | enum { |
483 | enum { |
484 | overhead = sizeof (unsigned int) * 2 |
484 | overhead = sizeof (uint32_t) * 2 |
485 | }; |
485 | }; |
486 | |
486 | |
487 | unsigned int &_size () const |
487 | uint32_t &_size () const |
488 | { |
488 | { |
489 | return ((unsigned int *)data)[-2]; |
489 | return ((unsigned int *)data)[-2]; |
490 | } |
490 | } |
491 | |
491 | |
492 | unsigned int &_refcnt () const |
492 | uint32_t &_refcnt () const |
493 | { |
493 | { |
494 | return ((unsigned int *)data)[-1]; |
494 | return ((unsigned int *)data)[-1]; |
495 | } |
495 | } |
496 | |
496 | |
497 | void _alloc (unsigned int size) |
497 | void _alloc (uint32_t size) |
498 | { |
498 | { |
499 | data = ((char *)salloc<char> (size + overhead)) + overhead; |
499 | data = ((char *)salloc<char> (size + overhead)) + overhead; |
500 | _size () = size; |
500 | _size () = size; |
501 | _refcnt () = 1; |
501 | _refcnt () = 1; |
502 | } |
502 | } |
503 | |
503 | |
|
|
504 | void _dealloc (); |
|
|
505 | |
|
|
506 | void inc () |
|
|
507 | { |
|
|
508 | ++_refcnt (); |
|
|
509 | } |
|
|
510 | |
504 | void dec () |
511 | void dec () |
505 | { |
512 | { |
506 | if (!--_refcnt ()) |
513 | if (!--_refcnt ()) |
507 | sfree<char> (data - overhead, size () + overhead); |
514 | _dealloc (); |
508 | } |
515 | } |
509 | }; |
516 | }; |
510 | |
517 | |
511 | INTERFACE_CLASS (attachable) |
518 | INTERFACE_CLASS (attachable) |
512 | struct refcnt_base |
519 | struct refcnt_base |
… | |
… | |
590 | // with good distribution. |
597 | // with good distribution. |
591 | // FNV-1a is faster on many cpus because the multiplication |
598 | // FNV-1a is faster on many cpus because the multiplication |
592 | // runs concurrently with the looping logic. |
599 | // runs concurrently with the looping logic. |
593 | // we modify the hash a bit to improve its distribution |
600 | // we modify the hash a bit to improve its distribution |
594 | uint32_t hash = STRHSH_NULL; |
601 | uint32_t hash = STRHSH_NULL; |
595 | |
602 | |
596 | while (*s) |
603 | while (*s) |
597 | hash = (hash ^ *s++) * 16777619U; |
604 | hash = (hash ^ *s++) * 16777619U; |
598 | |
605 | |
599 | return hash ^ (hash >> 16); |
606 | return hash ^ (hash >> 16); |
600 | } |
607 | } |
601 | |
608 | |
602 | static inline uint32_t |
609 | static inline uint32_t |
603 | memhsh (const char *s, size_t len) |
610 | memhsh (const char *s, size_t len) |
604 | { |
611 | { |
605 | uint32_t hash = STRHSH_NULL; |
612 | uint32_t hash = STRHSH_NULL; |
606 | |
613 | |
607 | while (len--) |
614 | while (len--) |
608 | hash = (hash ^ *s++) * 16777619U; |
615 | hash = (hash ^ *s++) * 16777619U; |
609 | |
616 | |
610 | return hash; |
617 | return hash; |
611 | } |
618 | } |
… | |
… | |
654 | } |
661 | } |
655 | }; |
662 | }; |
656 | |
663 | |
657 | // This container blends advantages of linked lists |
664 | // This container blends advantages of linked lists |
658 | // (efficiency) with vectors (random access) by |
665 | // (efficiency) with vectors (random access) by |
659 | // by using an unordered vector and storing the vector |
666 | // using an unordered vector and storing the vector |
660 | // index inside the object. |
667 | // index inside the object. |
661 | // |
668 | // |
662 | // + memory-efficient on most 64 bit archs |
669 | // + memory-efficient on most 64 bit archs |
663 | // + O(1) insert/remove |
670 | // + O(1) insert/remove |
664 | // + free unique (but varying) id for inserted objects |
671 | // + free unique (but varying) id for inserted objects |
… | |
… | |
701 | insert (&obj); |
708 | insert (&obj); |
702 | } |
709 | } |
703 | |
710 | |
704 | void erase (T *obj) |
711 | void erase (T *obj) |
705 | { |
712 | { |
706 | unsigned int pos = obj->*indexmember; |
713 | object_vector_index pos = obj->*indexmember; |
707 | obj->*indexmember = 0; |
714 | obj->*indexmember = 0; |
708 | |
715 | |
709 | if (pos < this->size ()) |
716 | if (pos < this->size ()) |
710 | { |
717 | { |
711 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
718 | (*this)[pos - 1] = (*this)[this->size () - 1]; |