Skip to content

Commit

Permalink
use unordered maps for component lookups in views
Browse files Browse the repository at this point in the history
Signed-off-by: Ashton Larkin <[email protected]>
  • Loading branch information
adlarkin committed Apr 9, 2021
1 parent dfb43a0 commit 13ac7ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/ignition/gazebo/detail/ComponentStorageBase.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef IGNITION_GAZEBO_DETAIL_COMPONENTSTORAGEBASE_HH_
#define IGNITION_GAZEBO_DETAIL_COMPONENTSTORAGEBASE_HH_

#include <map>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ignition/gazebo/components/Component.hh"
Expand Down Expand Up @@ -212,7 +212,7 @@ namespace ignition
private: ComponentId idCounter = 0;

/// \brief Map of ComponentId to Components (see the components vector).
private: std::map<ComponentId, int> idMap;
private: std::unordered_map<ComponentId, int> idMap;

/// \brief Sequential storage of components.
public: std::vector<ComponentTypeT> components;
Expand Down
29 changes: 26 additions & 3 deletions include/ignition/gazebo/detail/View.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#ifndef IGNITION_GAZEBO_DETAIL_VIEW_HH_
#define IGNITION_GAZEBO_DETAIL_VIEW_HH_

#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include "ignition/gazebo/components/Component.hh"
#include "ignition/gazebo/Entity.hh"
Expand Down Expand Up @@ -126,9 +126,32 @@ class IGNITION_GAZEBO_VISIBLE View
/// \brief List of entities about to be removed
public: std::set<Entity> toRemoveEntities;

/// \brief Hash functor for std::pair<Entity, ComponentTypeId>
public: struct Hasher
{
std::size_t operator()(
std::pair<Entity, ComponentTypeId> _pair) const
{
_pair.first ^= _pair.second + 0x9e3779b9 + (_pair.second << 6)
+ (_pair.second >> 2);
return _pair.first;
}
};

/// \brief Equality functor for std::pair<Entity, ComponentTypeId>
public: struct EqualTo
{
bool operator()(const std::pair<Entity, ComponentTypeId> &_lhs,
const std::pair<Entity, ComponentTypeId> &_rhs) const
{
return _lhs.first == _rhs.first &&
_lhs.second == _rhs.second;
}
};

/// \brief All of the components for each entity.
public: std::map<std::pair<Entity, ComponentTypeId>,
ComponentId> components;
public: std::unordered_map<std::pair<Entity, ComponentTypeId>, ComponentId,
Hasher, EqualTo> components;
};
/// \endcond
}
Expand Down

0 comments on commit 13ac7ff

Please sign in to comment.