added contains() and remove() methods

This commit is contained in:
ZappoMan 2013-05-08 14:22:03 -07:00
parent 2f3a7f8ea8
commit 24501e5c68
2 changed files with 38 additions and 0 deletions

View file

@ -82,3 +82,39 @@ VoxelNode* VoxelNodeBag::extract() {
}
return NULL;
}
bool VoxelNodeBag::contains(VoxelNode* node) {
for (int i = 0; i < _elementsInUse; i++) {
// just compare the pointers... that's good enough
if (_bagElements[i] == node) {
return true; // exit early!!
}
// if we're past where it should be, then it's not here!
if (_bagElements[i] > node) {
return false;
}
}
// if we made it through the entire bag, it's not here!
return false;
}
void VoxelNodeBag::remove(VoxelNode* node) {
int foundAt = -1;
for (int i = 0; i < _elementsInUse; i++) {
// just compare the pointers... that's good enough
if (_bagElements[i] == node) {
foundAt = i;
break;
}
// if we're past where it should be, then it's not here!
if (_bagElements[i] > node) {
break;
}
}
// if we found it, then we need to remove it....
if (foundAt != -1) {
memmove(&_bagElements[foundAt], &_bagElements[foundAt + 1], (_elementsInUse - foundAt) * sizeof(VoxelNode*));
_elementsInUse--;
}
}

View file

@ -28,6 +28,8 @@ public:
void insert(VoxelNode* node); // put a node into the bag
VoxelNode* extract(); // pull a node out of the bag (could come in any order)
bool contains(VoxelNode* node); // is this node in the bag?
void remove(VoxelNode* node); // remove a specific item from the bag
bool isEmpty() const { return (_elementsInUse == 0); };
int count() const { return _elementsInUse; };