summaryrefslogtreecommitdiff
path: root/impl/IdSet.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'impl/IdSet.hpp')
-rw-r--r--impl/IdSet.hpp46
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);