1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
2 | * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 | * |
5 | * |
6 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
6 | * Crossfire TRT is free software: you can redistribute it and/or modify |
7 | * under the terms of the GNU General Public License as published by the Free |
7 | * it under the terms of the GNU General Public License as published by |
8 | * Software Foundation; either version 2 of the License, or (at your option) |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * any later version. |
9 | * (at your option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, but |
11 | * This program is distributed in the hope that it will be useful, |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
16 | * You should have received a copy of the GNU General Public License |
17 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
19 | * |
18 | * |
20 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
19 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 | */ |
20 | */ |
22 | |
21 | |
23 | #ifndef UTIL_H__ |
22 | #ifndef UTIL_H__ |
… | |
… | |
406 | return !strcmp (a, b); |
405 | return !strcmp (a, b); |
407 | } |
406 | } |
408 | }; |
407 | }; |
409 | |
408 | |
410 | // Mostly the same as std::vector, but insert/erase can reorder |
409 | // Mostly the same as std::vector, but insert/erase can reorder |
411 | // the elements, making insret/remove O(1) instead of O(n). |
410 | // the elements, making append(=insert)/remove O(1) instead of O(n). |
412 | // |
411 | // |
413 | // NOTE: only some forms of erase/insert are available |
412 | // NOTE: only some forms of erase are available |
414 | template<class T> |
413 | template<class T> |
415 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
414 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
416 | { |
415 | { |
417 | typedef typename unordered_vector::iterator iterator; |
416 | typedef typename unordered_vector::iterator iterator; |
418 | |
417 | |
… | |
… | |
440 | // + free unique (but varying) id for inserted objects |
439 | // + free unique (but varying) id for inserted objects |
441 | // + cache-friendly iteration |
440 | // + cache-friendly iteration |
442 | // - only works for pointers to structs |
441 | // - only works for pointers to structs |
443 | // |
442 | // |
444 | // NOTE: only some forms of erase/insert are available |
443 | // NOTE: only some forms of erase/insert are available |
445 | template<class T, int T::* index> |
444 | typedef int object_vector_index; |
|
|
445 | |
|
|
446 | template<class T, object_vector_index T::*indexmember> |
446 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
447 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
447 | { |
448 | { |
448 | typedef typename object_vector::iterator iterator; |
449 | typedef typename object_vector::iterator iterator; |
449 | |
450 | |
450 | bool contains (const T *obj) const |
451 | bool contains (const T *obj) const |
451 | { |
452 | { |
452 | return obj->*index; |
453 | return obj->*indexmember; |
453 | } |
454 | } |
454 | |
455 | |
455 | iterator find (const T *obj) |
456 | iterator find (const T *obj) |
456 | { |
457 | { |
457 | return obj->*index |
458 | return obj->*indexmember |
458 | ? this->begin () + obj->*index - 1 |
459 | ? this->begin () + obj->*indexmember - 1 |
459 | : this->end (); |
460 | : this->end (); |
460 | } |
461 | } |
461 | |
462 | |
|
|
463 | void push_back (T *obj) |
|
|
464 | { |
|
|
465 | std::vector<T *, slice_allocator<T *> >::push_back (obj); |
|
|
466 | obj->*indexmember = this->size (); |
|
|
467 | } |
|
|
468 | |
462 | void insert (T *obj) |
469 | void insert (T *obj) |
463 | { |
470 | { |
464 | push_back (obj); |
471 | push_back (obj); |
465 | obj->*index = this->size (); |
|
|
466 | } |
472 | } |
467 | |
473 | |
468 | void insert (T &obj) |
474 | void insert (T &obj) |
469 | { |
475 | { |
470 | insert (&obj); |
476 | insert (&obj); |
471 | } |
477 | } |
472 | |
478 | |
473 | void erase (T *obj) |
479 | void erase (T *obj) |
474 | { |
480 | { |
475 | unsigned int pos = obj->*index; |
481 | unsigned int pos = obj->*indexmember; |
476 | obj->*index = 0; |
482 | obj->*indexmember = 0; |
477 | |
483 | |
478 | if (pos < this->size ()) |
484 | if (pos < this->size ()) |
479 | { |
485 | { |
480 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
486 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
481 | (*this)[pos - 1]->*index = pos; |
487 | (*this)[pos - 1]->*indexmember = pos; |
482 | } |
488 | } |
483 | |
489 | |
484 | this->pop_back (); |
490 | this->pop_back (); |
485 | } |
491 | } |
486 | |
492 | |
487 | void erase (T &obj) |
493 | void erase (T &obj) |
488 | { |
494 | { |
489 | errase (&obj); |
495 | erase (&obj); |
490 | } |
496 | } |
491 | }; |
497 | }; |
492 | |
498 | |
493 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
499 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
494 | void assign (char *dst, const char *src, int maxlen); |
500 | void assign (char *dst, const char *src, int maxlen); |