diff options
author | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-06-29 13:00:59 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au> | 2012-06-29 13:00:59 +1000 |
commit | 9e0d0b59f44d781d2cbbff4f83d2ab71b7dc0121 (patch) | |
tree | 3b147e06c3fab46d4086f7ab979b7528edbaa332 /impl/IdSet.hpp | |
parent | fcc4343ddc74fe70aa9e2b2eb3d18e25cdf7b270 (diff) |
Speed up IdSet a bit. I think.
Diffstat (limited to 'impl/IdSet.hpp')
-rw-r--r-- | impl/IdSet.hpp | 46 |
1 files changed, 16 insertions, 30 deletions
diff --git a/impl/IdSet.hpp b/impl/IdSet.hpp index 8c64051..2ba88d3 100644 --- a/impl/IdSet.hpp +++ b/impl/IdSet.hpp @@ -3,7 +3,7 @@ #include <ostream> -#define CELL_TYPE char +#define CELL_TYPE unsigned int #define CELL_SIZE (8 * sizeof(CELL_TYPE)) #define DIV_ROUND_UP(NUM, DEM) ((NUM + DEM - 1) / DEM) @@ -68,20 +68,6 @@ class IdSet { if (id >= _length) { throw "Array out of bounds;"; } - - unsigned int cell = id / CELL_SIZE; - unsigned int bit = id % CELL_SIZE; - if (this->contains(obj)) { - std::cout << obj.id() << " -> "; - } else { - std::cout << "null" << " -> "; - } - _values[cell] &= ~(1 << bit); - if (this->contains(obj)) { - std::cout << "still here" << std::endl; - } else { - std::cout << "Gone!" << std::endl; - } } void clear() { for (unsigned int i = 0; i < DIV_ROUND_UP(_length, CELL_SIZE); ++i) { @@ -121,20 +107,16 @@ class IdSet { return !(*this == other); } - struct iterator { - iterator(const IdSet& set, unsigned int index) - : set(set), index(index) { - unsigned int cell, bit; - cell = index / CELL_SIZE; - bit = index % CELL_SIZE; - while (index < set._length && - (set._values[cell] & (1 << bit)) == 0) { - index++; - cell = index / CELL_SIZE; - bit = index % CELL_SIZE; + iterator(const IdSet& set) + : set(set), index(0) { + if (set._values[0] & 1) { + return; // we have a zero } + ++(*this); } + iterator(const IdSet& set, unsigned int index) + : set(set), index(index) { } unsigned int operator*() const { return index; } @@ -142,10 +124,14 @@ class IdSet { unsigned int cell, bit; do { index++; - if (index == set._length) { - return *this; + for (;; index += CELL_SIZE) { + if (index == set._length) { + return *this; + } + cell = index / CELL_SIZE; + if (set._values[cell] != 0) + break; } - cell = index / CELL_SIZE; bit = index % CELL_SIZE; } while ((set._values[cell] & (1 << bit)) == 0); return *this; @@ -161,7 +147,7 @@ class IdSet { unsigned int index; }; iterator begin() const { - return iterator(*this, 0); + return iterator(*this); } iterator end() const { return iterator(*this, _length); |