… | |
… | |
33 | # define is_constant(c) 0 |
33 | # define is_constant(c) 0 |
34 | # define expect(expr,value) (expr) |
34 | # define expect(expr,value) (expr) |
35 | # define prefetch(addr,rw,locality) |
35 | # define prefetch(addr,rw,locality) |
36 | #endif |
36 | #endif |
37 | |
37 | |
|
|
38 | #if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) |
|
|
39 | # define decltype(x) typeof(x) |
|
|
40 | #endif |
|
|
41 | |
38 | // put into ifs if you are very sure that the expression |
42 | // put into ifs if you are very sure that the expression |
39 | // is mostly true or mosty false. note that these return |
43 | // is mostly true or mosty false. note that these return |
40 | // booleans, not the expression. |
44 | // booleans, not the expression. |
41 | #define expect_false(expr) expect ((expr) != 0, 0) |
45 | #define expect_false(expr) expect ((expr) != 0, 0) |
42 | #define expect_true(expr) expect ((expr) != 0, 1) |
46 | #define expect_true(expr) expect ((expr) != 0, 1) |
… | |
… | |
49 | #include <glib.h> |
53 | #include <glib.h> |
50 | |
54 | |
51 | #include <shstr.h> |
55 | #include <shstr.h> |
52 | #include <traits.h> |
56 | #include <traits.h> |
53 | |
57 | |
54 | // 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) |
55 | #define auto(var,expr) typeof(expr) var = (expr) |
59 | #define auto(var,expr) decltype(expr) var = (expr) |
56 | |
60 | |
57 | // very ugly macro that basicaly declares and initialises a variable |
61 | // very ugly macro that basicaly declares and initialises a variable |
58 | // that is in scope for the next statement only |
62 | // that is in scope for the next statement only |
59 | // 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 |
60 | // (note: works great for pointers) |
64 | // (note: works great for pointers) |
61 | // most ugly macro I ever wrote |
65 | // most ugly macro I ever wrote |
62 | #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) |
63 | |
67 | |
64 | // in range including end |
68 | // in range including end |
65 | #define IN_RANGE_INC(val,beg,end) \ |
69 | #define IN_RANGE_INC(val,beg,end) \ |
66 | ((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)) |
67 | |
71 | |
… | |
… | |
401 | { |
405 | { |
402 | return !strcmp (a, b); |
406 | return !strcmp (a, b); |
403 | } |
407 | } |
404 | }; |
408 | }; |
405 | |
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 |
406 | template<class T> |
414 | template<class T> |
407 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
415 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
408 | { |
416 | { |
409 | typedef typename unordered_vector::iterator iterator; |
417 | typedef typename unordered_vector::iterator iterator; |
410 | |
418 | |
… | |
… | |
420 | { |
428 | { |
421 | erase ((unsigned int )(i - this->begin ())); |
429 | erase ((unsigned int )(i - this->begin ())); |
422 | } |
430 | } |
423 | }; |
431 | }; |
424 | |
432 | |
425 | 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> |
426 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
448 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
427 | { |
449 | { |
|
|
450 | typedef typename object_vector::iterator iterator; |
|
|
451 | |
|
|
452 | bool contains (const T *obj) const |
|
|
453 | { |
|
|
454 | return obj->*indexmember; |
|
|
455 | } |
|
|
456 | |
|
|
457 | iterator find (const T *obj) |
|
|
458 | { |
|
|
459 | return obj->*indexmember |
|
|
460 | ? this->begin () + obj->*indexmember - 1 |
|
|
461 | : this->end (); |
|
|
462 | } |
|
|
463 | |
428 | void insert (T *obj) |
464 | void insert (T *obj) |
429 | { |
465 | { |
430 | assert (!(obj->*index)); |
|
|
431 | push_back (obj); |
466 | push_back (obj); |
432 | obj->*index = this->size (); |
467 | obj->*indexmember = this->size (); |
433 | } |
468 | } |
434 | |
469 | |
435 | void insert (T &obj) |
470 | void insert (T &obj) |
436 | { |
471 | { |
437 | insert (&obj); |
472 | insert (&obj); |
438 | } |
473 | } |
439 | |
474 | |
440 | void erase (T *obj) |
475 | void erase (T *obj) |
441 | { |
476 | { |
442 | assert (obj->*index); |
|
|
443 | unsigned int pos = obj->*index; |
477 | unsigned int pos = obj->*indexmember; |
444 | obj->*index = 0; |
478 | obj->*indexmember = 0; |
445 | |
479 | |
446 | if (pos < this->size ()) |
480 | if (pos < this->size ()) |
447 | { |
481 | { |
448 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
482 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
449 | (*this)[pos - 1]->*index = pos; |
483 | (*this)[pos - 1]->*indexmember = pos; |
450 | } |
484 | } |
451 | |
485 | |
452 | this->pop_back (); |
486 | this->pop_back (); |
453 | } |
487 | } |
454 | |
488 | |
455 | void erase (T &obj) |
489 | void erase (T &obj) |
456 | { |
490 | { |
457 | errase (&obj); |
491 | erase (&obj); |
458 | } |
492 | } |
459 | }; |
493 | }; |
460 | |
494 | |
461 | // 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 |
462 | void assign (char *dst, const char *src, int maxlen); |
496 | void assign (char *dst, const char *src, int maxlen); |