From 13ac7ff493583f11b9a0c4dfea9e8ae8220e2585 Mon Sep 17 00:00:00 2001 From: Ashton Larkin Date: Fri, 9 Apr 2021 12:19:12 -0400 Subject: [PATCH] use unordered maps for component lookups in views Signed-off-by: Ashton Larkin --- .../gazebo/detail/ComponentStorageBase.hh | 4 +-- include/ignition/gazebo/detail/View.hh | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/include/ignition/gazebo/detail/ComponentStorageBase.hh b/include/ignition/gazebo/detail/ComponentStorageBase.hh index ca9e5e8960..f095704884 100644 --- a/include/ignition/gazebo/detail/ComponentStorageBase.hh +++ b/include/ignition/gazebo/detail/ComponentStorageBase.hh @@ -17,7 +17,7 @@ #ifndef IGNITION_GAZEBO_DETAIL_COMPONENTSTORAGEBASE_HH_ #define IGNITION_GAZEBO_DETAIL_COMPONENTSTORAGEBASE_HH_ -#include +#include #include #include #include "ignition/gazebo/components/Component.hh" @@ -212,7 +212,7 @@ namespace ignition private: ComponentId idCounter = 0; /// \brief Map of ComponentId to Components (see the components vector). - private: std::map idMap; + private: std::unordered_map idMap; /// \brief Sequential storage of components. public: std::vector components; diff --git a/include/ignition/gazebo/detail/View.hh b/include/ignition/gazebo/detail/View.hh index 654a146291..f3b9ed327c 100644 --- a/include/ignition/gazebo/detail/View.hh +++ b/include/ignition/gazebo/detail/View.hh @@ -17,9 +17,9 @@ #ifndef IGNITION_GAZEBO_DETAIL_VIEW_HH_ #define IGNITION_GAZEBO_DETAIL_VIEW_HH_ -#include #include #include +#include #include #include "ignition/gazebo/components/Component.hh" #include "ignition/gazebo/Entity.hh" @@ -126,9 +126,32 @@ class IGNITION_GAZEBO_VISIBLE View /// \brief List of entities about to be removed public: std::set toRemoveEntities; + /// \brief Hash functor for std::pair + public: struct Hasher + { + std::size_t operator()( + std::pair _pair) const + { + _pair.first ^= _pair.second + 0x9e3779b9 + (_pair.second << 6) + + (_pair.second >> 2); + return _pair.first; + } + }; + + /// \brief Equality functor for std::pair + public: struct EqualTo + { + bool operator()(const std::pair &_lhs, + const std::pair &_rhs) const + { + return _lhs.first == _rhs.first && + _lhs.second == _rhs.second; + } + }; + /// \brief All of the components for each entity. - public: std::map, - ComponentId> components; + public: std::unordered_map, ComponentId, + Hasher, EqualTo> components; }; /// \endcond }