… | |
… | |
440 | // + free unique (but varying) id for inserted objects |
440 | // + free unique (but varying) id for inserted objects |
441 | // + cache-friendly iteration |
441 | // + cache-friendly iteration |
442 | // - only works for pointers to structs |
442 | // - only works for pointers to structs |
443 | // |
443 | // |
444 | // NOTE: only some forms of erase/insert are available |
444 | // NOTE: only some forms of erase/insert are available |
445 | template<class T, int T::* index> |
445 | typedef int object_vector_index; |
|
|
446 | |
|
|
447 | template<class T, object_vector_index T::*indexmember> |
446 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
448 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
447 | { |
449 | { |
448 | typedef typename object_vector::iterator iterator; |
450 | typedef typename object_vector::iterator iterator; |
449 | |
451 | |
450 | bool contains (const T *obj) const |
452 | bool contains (const T *obj) const |
451 | { |
453 | { |
452 | return obj->*index; |
454 | return obj->*indexmember; |
453 | } |
455 | } |
454 | |
456 | |
455 | iterator find (const T *obj) |
457 | iterator find (const T *obj) |
456 | { |
458 | { |
457 | return obj->*index |
459 | return obj->*indexmember |
458 | ? this->begin () + obj->*index - 1 |
460 | ? this->begin () + obj->*indexmember - 1 |
459 | : this->end (); |
461 | : this->end (); |
460 | } |
462 | } |
461 | |
463 | |
462 | void insert (T *obj) |
464 | void insert (T *obj) |
463 | { |
465 | { |
464 | push_back (obj); |
466 | push_back (obj); |
465 | obj->*index = this->size (); |
467 | obj->*indexmember = this->size (); |
466 | } |
468 | } |
467 | |
469 | |
468 | void insert (T &obj) |
470 | void insert (T &obj) |
469 | { |
471 | { |
470 | insert (&obj); |
472 | insert (&obj); |
471 | } |
473 | } |
472 | |
474 | |
473 | void erase (T *obj) |
475 | void erase (T *obj) |
474 | { |
476 | { |
475 | unsigned int pos = obj->*index; |
477 | unsigned int pos = obj->*indexmember; |
476 | obj->*index = 0; |
478 | obj->*indexmember = 0; |
477 | |
479 | |
478 | if (pos < this->size ()) |
480 | if (pos < this->size ()) |
479 | { |
481 | { |
480 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
482 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
481 | (*this)[pos - 1]->*index = pos; |
483 | (*this)[pos - 1]->*indexmember = pos; |
482 | } |
484 | } |
483 | |
485 | |
484 | this->pop_back (); |
486 | this->pop_back (); |
485 | } |
487 | } |
486 | |
488 | |
487 | void erase (T &obj) |
489 | void erase (T &obj) |
488 | { |
490 | { |
489 | errase (&obj); |
491 | erase (&obj); |
490 | } |
492 | } |
491 | }; |
493 | }; |
492 | |
494 | |
493 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
495 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
494 | void assign (char *dst, const char *src, int maxlen); |
496 | void assign (char *dst, const char *src, int maxlen); |