diff --git a/libraries/voxels/src/VoxelNodeBag.cpp b/libraries/voxels/src/VoxelNodeBag.cpp index 9646c2d2fc..20a469dec8 100644 --- a/libraries/voxels/src/VoxelNodeBag.cpp +++ b/libraries/voxels/src/VoxelNodeBag.cpp @@ -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--; + } +} + diff --git a/libraries/voxels/src/VoxelNodeBag.h b/libraries/voxels/src/VoxelNodeBag.h index 9eb6a91e6a..ee6e5045d0 100644 --- a/libraries/voxels/src/VoxelNodeBag.h +++ b/libraries/voxels/src/VoxelNodeBag.h @@ -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; };