diff --git a/libraries/octree/src/OctreeElementBag.cpp b/libraries/octree/src/OctreeElementBag.cpp index 4634c05a06..10d80e5799 100644 --- a/libraries/octree/src/OctreeElementBag.cpp +++ b/libraries/octree/src/OctreeElementBag.cpp @@ -16,26 +16,25 @@ void OctreeElementBag::deleteAll() { _bagElements = Bag(); } +/// does the bag contain elements? +/// if all of the contained elements are expired, they will not report as empty, and +/// a single last item will be returned by extract as a null pointer bool OctreeElementBag::isEmpty() { - // Pop all expired front elements - while (!_bagElements.empty() && _bagElements.front().expired()) { - _bagElements.pop(); - } - return _bagElements.empty(); } void OctreeElementBag::insert(OctreeElementPointer element) { - _bagElements.push(element); + _bagElements[element.get()] = element; } OctreeElementPointer OctreeElementBag::extract() { OctreeElementPointer result; // Find the first element still alive - while (!result && !_bagElements.empty()) { - result = _bagElements.front().lock(); // Grab head's shared_ptr - _bagElements.pop(); + Bag::iterator it = _bagElements.begin(); + while (it != _bagElements.end() && !result) { + result = it->second.lock(); + it = _bagElements.erase(it); } return result; } diff --git a/libraries/octree/src/OctreeElementBag.h b/libraries/octree/src/OctreeElementBag.h index 02423b640c..97ff019513 100644 --- a/libraries/octree/src/OctreeElementBag.h +++ b/libraries/octree/src/OctreeElementBag.h @@ -16,17 +16,22 @@ #ifndef hifi_OctreeElementBag_h #define hifi_OctreeElementBag_h -#include +#include #include "OctreeElement.h" class OctreeElementBag { - using Bag = std::queue; + using Bag = std::unordered_map; public: void insert(OctreeElementPointer element); // put a element into the bag - OctreeElementPointer extract(); // pull a element out of the bag (could come in any order) - bool isEmpty(); + + OctreeElementPointer extract(); /// pull a element out of the bag (could come in any order) and if all of the + /// elements have expired, a single null pointer will be returned + + bool isEmpty(); /// does the bag contain elements, + /// if all of the contained elements are expired, they will not report as empty, and + /// a single last item will be returned by extract as a null pointer void deleteAll();