Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
BiMap.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <unordered_map>
6
7namespace Physics::Utils {
8template <typename TLeft, typename TRight> class BiMap {
9 public:
10 BiMap() = default;
11 ~BiMap() = default;
12
13 BiMap(const BiMap &) = delete;
14 BiMap &operator=(const BiMap &) = delete;
15 BiMap(BiMap &&) noexcept = default;
16 BiMap &operator=(BiMap &&) noexcept = default;
17
18 void Add(const TLeft &left, const TRight &right)
19 {
20 if (auto existingIt = this->_leftToRight.find(left); existingIt != this->_leftToRight.end())
21 {
22 this->_rightToLeft.erase(existingIt->second);
23 }
24 if (auto existingRightIt = this->_rightToLeft.find(right); existingRightIt != this->_rightToLeft.end())
25 {
26 this->_leftToRight.erase(existingRightIt->second);
27 }
28 this->_leftToRight.insert_or_assign(left, right);
29 this->_rightToLeft.insert_or_assign(right, left);
30 }
31 void Remove(const TLeft &left)
32 {
33 auto it = this->_leftToRight.find(left);
34 if (it != this->_leftToRight.end())
35 {
36 this->_rightToLeft.erase(it->second);
37 this->_leftToRight.erase(it);
38 }
39 }
40 void Remove(const TRight &right)
41 {
42 auto it = this->_rightToLeft.find(right);
43 if (it != this->_rightToLeft.end())
44 {
45 this->_leftToRight.erase(it->second);
46 this->_rightToLeft.erase(it);
47 }
48 }
49 auto Size() const { return static_cast<std::uint32_t>(this->_leftToRight.size()); }
50 const TRight &Get(const TLeft &left) const { return this->_leftToRight.at(left); }
51 const TLeft &Get(const TRight &right) const { return this->_rightToLeft.at(right); }
52 bool Contains(const TLeft &left) const { return this->_leftToRight.contains(left); }
53 bool Contains(const TRight &right) const { return this->_rightToLeft.contains(right); }
54
55 private:
56 std::unordered_map<TLeft, TRight> _leftToRight;
57 std::unordered_map<TRight, TLeft> _rightToLeft;
58};
59} // namespace Physics::Utils
void Add(const Engine::EntityId &left, const JPH::BodyID &right)
Definition BiMap.hpp:18
void Remove(const TRight &right)
Definition BiMap.hpp:40
bool Contains(const TLeft &left) const
Definition BiMap.hpp:52
std::unordered_map< JPH::BodyID, Engine::EntityId > _rightToLeft
Definition BiMap.hpp:57
bool Contains(const TRight &right) const
Definition BiMap.hpp:53
BiMap(const BiMap &)=delete
std::unordered_map< Engine::EntityId, JPH::BodyID > _leftToRight
Definition BiMap.hpp:56
void Remove(const TLeft &left)
Definition BiMap.hpp:31
const TLeft & Get(const TRight &right) const
Definition BiMap.hpp:51
BiMap & operator=(const BiMap &)=delete
auto Size() const
Definition BiMap.hpp:49
BiMap(BiMap &&) noexcept=default
const TRight & Get(const TLeft &left) const
Definition BiMap.hpp:50
Definition BiMap.hpp:7