ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.48 by root, Mon Jun 4 12:19:08 2007 UTC vs.
Revision 1.49 by root, Mon Jun 4 13:04:00 2007 UTC

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
410template<class T> 414template<class T>
411struct unordered_vector : std::vector<T, slice_allocator<T> > 415struct 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
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
429template<class T, int T::* index> 445template<class T, int T::* index>
430struct object_vector : std::vector<T *, slice_allocator<T *> > 446struct object_vector : std::vector<T *, slice_allocator<T *> >
431{ 447{
432 typedef typename object_vector::iterator iterator; 448 typedef typename object_vector::iterator iterator;
433 449
443 : this->end (); 459 : this->end ();
444 } 460 }
445 461
446 void insert (T *obj) 462 void insert (T *obj)
447 { 463 {
448 assert (!(obj->*index));
449 push_back (obj); 464 push_back (obj);
450 obj->*index = this->size (); 465 obj->*index = this->size ();
451 } 466 }
452 467
453 void insert (T &obj) 468 void insert (T &obj)
455 insert (&obj); 470 insert (&obj);
456 } 471 }
457 472
458 void erase (T *obj) 473 void erase (T *obj)
459 { 474 {
460 assert (obj->*index);
461 unsigned int pos = obj->*index; 475 unsigned int pos = obj->*index;
462 obj->*index = 0; 476 obj->*index = 0;
463 477
464 if (pos < this->size ()) 478 if (pos < this->size ())
465 { 479 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines