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 | |
… | |
… | |
459 | return obj->*indexmember |
458 | return obj->*indexmember |
460 | ? this->begin () + obj->*indexmember - 1 |
459 | ? this->begin () + obj->*indexmember - 1 |
461 | : this->end (); |
460 | : this->end (); |
462 | } |
461 | } |
463 | |
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 | |
464 | void insert (T *obj) |
469 | void insert (T *obj) |
465 | { |
470 | { |
466 | push_back (obj); |
471 | push_back (obj); |
467 | obj->*indexmember = this->size (); |
|
|
468 | } |
472 | } |
469 | |
473 | |
470 | void insert (T &obj) |
474 | void insert (T &obj) |
471 | { |
475 | { |
472 | insert (&obj); |
476 | insert (&obj); |