… | |
… | |
53 | #include <glib.h> |
53 | #include <glib.h> |
54 | |
54 | |
55 | #include <shstr.h> |
55 | #include <shstr.h> |
56 | #include <traits.h> |
56 | #include <traits.h> |
57 | |
57 | |
58 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
58 | // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) |
59 | #define auto(var,expr) decltype(expr) var = (expr) |
59 | #define auto(var,expr) decltype(expr) var = (expr) |
60 | |
60 | |
61 | // very ugly macro that basicaly declares and initialises a variable |
61 | // very ugly macro that basicaly declares and initialises a variable |
62 | // that is in scope for the next statement only |
62 | // that is in scope for the next statement only |
63 | // works only for stuff that can be assigned 0 and converts to false |
63 | // works only for stuff that can be assigned 0 and converts to false |
… | |
… | |
405 | { |
405 | { |
406 | return !strcmp (a, b); |
406 | return !strcmp (a, b); |
407 | } |
407 | } |
408 | }; |
408 | }; |
409 | |
409 | |
|
|
410 | // Mostly the same as std::vector, but insert/erase can reorder |
|
|
411 | // the elements, making insret/remove O(1) instead of O(n). |
|
|
412 | // |
|
|
413 | // NOTE: only some forms of erase/insert are available |
410 | template<class T> |
414 | template<class T> |
411 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
415 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
412 | { |
416 | { |
413 | typedef typename unordered_vector::iterator iterator; |
417 | typedef typename unordered_vector::iterator iterator; |
414 | |
418 | |
… | |
… | |
424 | { |
428 | { |
425 | erase ((unsigned int )(i - this->begin ())); |
429 | erase ((unsigned int )(i - this->begin ())); |
426 | } |
430 | } |
427 | }; |
431 | }; |
428 | |
432 | |
429 | template<class T, int T::* index> |
433 | // This container blends advantages of linked lists |
|
|
434 | // (efficiency) with vectors (random access) by |
|
|
435 | // by using an unordered vector and storing the vector |
|
|
436 | // index inside the object. |
|
|
437 | // |
|
|
438 | // + memory-efficient on most 64 bit archs |
|
|
439 | // + O(1) insert/remove |
|
|
440 | // + free unique (but varying) id for inserted objects |
|
|
441 | // + cache-friendly iteration |
|
|
442 | // - only works for pointers to structs |
|
|
443 | // |
|
|
444 | // NOTE: only some forms of erase/insert are available |
|
|
445 | typedef int object_vector_index; |
|
|
446 | |
|
|
447 | template<class T, object_vector_index T::*indexmember> |
430 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
448 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
431 | { |
449 | { |
432 | typedef typename object_vector::iterator iterator; |
450 | typedef typename object_vector::iterator iterator; |
433 | |
451 | |
434 | bool contains (const T *obj) const |
452 | bool contains (const T *obj) const |
435 | { |
453 | { |
436 | return obj->*index; |
454 | return obj->*indexmember; |
437 | } |
455 | } |
438 | |
456 | |
439 | iterator find (const T *obj) |
457 | iterator find (const T *obj) |
440 | { |
458 | { |
441 | return obj->*index |
459 | return obj->*indexmember |
442 | ? this->begin () + obj->*index - 1 |
460 | ? this->begin () + obj->*indexmember - 1 |
443 | : this->end (); |
461 | : this->end (); |
444 | } |
462 | } |
445 | |
463 | |
446 | void insert (T *obj) |
464 | void insert (T *obj) |
447 | { |
465 | { |
448 | assert (!(obj->*index)); |
|
|
449 | push_back (obj); |
466 | push_back (obj); |
450 | obj->*index = this->size (); |
467 | obj->*indexmember = this->size (); |
451 | } |
468 | } |
452 | |
469 | |
453 | void insert (T &obj) |
470 | void insert (T &obj) |
454 | { |
471 | { |
455 | insert (&obj); |
472 | insert (&obj); |
456 | } |
473 | } |
457 | |
474 | |
458 | void erase (T *obj) |
475 | void erase (T *obj) |
459 | { |
476 | { |
460 | assert (obj->*index); |
|
|
461 | unsigned int pos = obj->*index; |
477 | unsigned int pos = obj->*indexmember; |
462 | obj->*index = 0; |
478 | obj->*indexmember = 0; |
463 | |
479 | |
464 | if (pos < this->size ()) |
480 | if (pos < this->size ()) |
465 | { |
481 | { |
466 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
482 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
467 | (*this)[pos - 1]->*index = pos; |
483 | (*this)[pos - 1]->*indexmember = pos; |
468 | } |
484 | } |
469 | |
485 | |
470 | this->pop_back (); |
486 | this->pop_back (); |
471 | } |
487 | } |
472 | |
488 | |
473 | void erase (T &obj) |
489 | void erase (T &obj) |
474 | { |
490 | { |
475 | errase (&obj); |
491 | erase (&obj); |
476 | } |
492 | } |
477 | }; |
493 | }; |
478 | |
494 | |
479 | // 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 |
480 | void assign (char *dst, const char *src, int maxlen); |
496 | void assign (char *dst, const char *src, int maxlen); |