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.47 by root, Sat Jun 2 03:48:29 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
64// (note: works great for pointers) 64// (note: works great for pointers)
65// most ugly macro I ever wrote 65// most ugly macro I ever wrote
66#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) 66#define statementvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
67 67
68// in range including end 68// in range including end
69#define IN_RANGE_INC(val,beg,end) \ 69#define IN_RANGE_INC(val,beg,end) \
70 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) 70 ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
71 71
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{
448 typedef typename object_vector::iterator iterator;
449
450 bool contains (const T *obj) const
451 {
452 return obj->*index;
453 }
454
455 iterator find (const T *obj)
456 {
457 return obj->*index
458 ? this->begin () + obj->*index - 1
459 : this->end ();
460 }
461
432 void insert (T *obj) 462 void insert (T *obj)
433 { 463 {
434 assert (!(obj->*index));
435 push_back (obj); 464 push_back (obj);
436 obj->*index = this->size (); 465 obj->*index = this->size ();
437 } 466 }
438 467
439 void insert (T &obj) 468 void insert (T &obj)
441 insert (&obj); 470 insert (&obj);
442 } 471 }
443 472
444 void erase (T *obj) 473 void erase (T *obj)
445 { 474 {
446 assert (obj->*index);
447 unsigned int pos = obj->*index; 475 unsigned int pos = obj->*index;
448 obj->*index = 0; 476 obj->*index = 0;
449 477
450 if (pos < this->size ()) 478 if (pos < this->size ())
451 { 479 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines