From 477dfe1b103fdaa99a3ac9a294ee73fc9e9fb550 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Thu, 11 Feb 2016 20:42:42 -0800 Subject: [PATCH] use std::unordered_map<> instead of std::unordered_set<> --- libraries/octree/src/OctreeElementBag.cpp | 10 ++--- libraries/octree/src/OctreeElementBag.h | 7 +-- libraries/shared/src/shared/WeakPointerHash.h | 44 ------------------- 3 files changed, 7 insertions(+), 54 deletions(-) delete mode 100644 libraries/shared/src/shared/WeakPointerHash.h diff --git a/libraries/octree/src/OctreeElementBag.cpp b/libraries/octree/src/OctreeElementBag.cpp index 2dd7534983..10d80e5799 100644 --- a/libraries/octree/src/OctreeElementBag.cpp +++ b/libraries/octree/src/OctreeElementBag.cpp @@ -24,17 +24,17 @@ bool OctreeElementBag::isEmpty() { } void OctreeElementBag::insert(OctreeElementPointer element) { - _bagElements.insert(element); + _bagElements[element.get()] = element; } OctreeElementPointer OctreeElementBag::extract() { OctreeElementPointer result; // Find the first element still alive - while (!result && !_bagElements.empty()) { - Bag::iterator it = _bagElements.begin(); - result = (*it).lock(); // Grab head's shared_ptr - _bagElements.erase(it); + 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 988021fee3..97ff019513 100644 --- a/libraries/octree/src/OctreeElementBag.h +++ b/libraries/octree/src/OctreeElementBag.h @@ -16,15 +16,12 @@ #ifndef hifi_OctreeElementBag_h #define hifi_OctreeElementBag_h -#include -#include +#include #include "OctreeElement.h" class OctreeElementBag { - using OctreeElementWeakPointerHash = WeakPointerHash; - using OctreeElementWeakPointerEqual = WeakPointerEqual; - using Bag = std::unordered_set; + using Bag = std::unordered_map; public: void insert(OctreeElementPointer element); // put a element into the bag diff --git a/libraries/shared/src/shared/WeakPointerHash.h b/libraries/shared/src/shared/WeakPointerHash.h deleted file mode 100644 index 9ad3996f47..0000000000 --- a/libraries/shared/src/shared/WeakPointerHash.h +++ /dev/null @@ -1,44 +0,0 @@ -// -// WeakPointerHash.h -// libraries/shared -// -// Created by Brad Hefta-Gaub on 2/11/2015. -// Copyright 2015 High Fidelity, Inc. -// -// std::hash<> compatible Hash and Equal functions for std::weak_ptr<> classes -// -// borrowed heavily from: -// http://stackoverflow.com/questions/13695640/how-to-make-a-c11-stdunordered-set-of-stdweak-ptr -// -// warning: -// there is some question as to whether or not this hash function is really safe to use because -// it doesn't guarentee that the hash for the weak_ptr() is the same for every requested hash -// as noted on the link above, this behavior may cause instability or undefined behavior -// in some hash users when the weak_ptr<> has expired. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -#ifndef hifi_WeakPointerHash_h -#define hifi_WeakPointerHash_h - -#include -#include - -template -struct WeakPointerHash : public std::unary_function, size_t> { - size_t operator()(const std::weak_ptr& weakPointer) { - auto sharedPointer = weakPointer.lock(); - return std::hash()(sharedPointer); - } -}; - -template -struct WeakPointerEqual : public std::unary_function, bool> { - bool operator()(const std::weak_ptr& left, const std::weak_ptr& right) { - return !left.owner_before(right) && !right.owner_before(left); - } -}; - -#endif // hifi_WeakPointerHash_h \ No newline at end of file