Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
UnorderedMap.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2024 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
9
11
13template <class Key, class Value>
15{
16public:
18 static const Key & sGetKey(const std::pair<Key, Value> &inKeyValue)
19 {
20 return inKeyValue.first;
21 }
22};
23
29template <class Key, class Value, class Hash, class KeyEqual>
30class UnorderedMap : public HashTable<Key, std::pair<Key, Value>, UnorderedMapDetail<Key, Value>, Hash, KeyEqual>
31{
33
34public:
35 using size_type = typename Base::size_type;
36 using iterator = typename Base::iterator;
37 using const_iterator = typename Base::const_iterator;
38 using value_type = typename Base::value_type;
39
40 Value & operator [] (const Key &inKey)
41 {
42 size_type index;
43 bool inserted = this->InsertKey(inKey, index);
44 value_type &key_value = this->GetElement(index);
45 if (inserted)
46 new (&key_value) value_type(inKey, Value());
47 return key_value.second;
48 }
49
50 template<class... Args>
51 std::pair<iterator, bool> try_emplace(const Key &inKey, Args &&...inArgs)
52 {
53 size_type index;
54 bool inserted = this->InsertKey(inKey, index);
55 if (inserted)
56 new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(inKey), std::forward_as_tuple(std::forward<Args>(inArgs)...));
57 return std::make_pair(iterator(this, index), inserted);
58 }
59
60 template<class... Args>
61 std::pair<iterator, bool> try_emplace(Key &&inKey, Args &&...inArgs)
62 {
63 size_type index;
64 bool inserted = this->InsertKey(inKey, index);
65 if (inserted)
66 new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(std::move(inKey)), std::forward_as_tuple(std::forward<Args>(inArgs)...));
67 return std::make_pair(iterator(this, index), inserted);
68 }
69
71 using Base::find;
72
74 iterator find(const Key &inKey)
75 {
76 const_iterator it = Base::find(inKey);
77 return iterator(this, it.mIndex);
78 }
79};
80
#define JPH_NAMESPACE_END
Definition Core.h:425
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
Definition HashTable.h:16
std::pair< Key, Value > & GetElement(size_type inIndex) const
Definition HashTable.h:225
const_iterator find(const Key &inKey) const
Definition HashTable.h:631
bool InsertKey(const Key &inKey, size_type &outIndex)
Definition HashTable.h:233
Internal helper class to provide context for UnorderedMap.
Definition UnorderedMap.h:15
static const Key & sGetKey(const std::pair< Key, Value > &inKeyValue)
Get key from key value pair.
Definition UnorderedMap.h:18
Definition UnorderedMap.h:31
typename Base::const_iterator const_iterator
Definition UnorderedMap.h:37
Value & operator[](const Key &inKey)
Definition UnorderedMap.h:40
typename Base::value_type value_type
Definition UnorderedMap.h:38
std::pair< iterator, bool > try_emplace(const Key &inKey, Args &&...inArgs)
Definition UnorderedMap.h:51
iterator find(const Key &inKey)
Non-const version of find.
Definition UnorderedMap.h:74
std::pair< iterator, bool > try_emplace(Key &&inKey, Args &&...inArgs)
Definition UnorderedMap.h:61
typename Base::size_type size_type
Definition UnorderedMap.h:35
typename Base::iterator iterator
Definition UnorderedMap.h:36
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59