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
8
10
12template <class Key, class Value>
14{
15public:
17 static const Key & sGetKey(const std::pair<Key, Value> &inKeyValue)
18 {
19 return inKeyValue.first;
20 }
21};
22
28template <class Key, class Value, class Hash = JPH::Hash<Key>, class KeyEqual = std::equal_to<Key>>
29class UnorderedMap : public HashTable<Key, std::pair<Key, Value>, UnorderedMapDetail<Key, Value>, Hash, KeyEqual>
30{
32
33public:
34 using size_type = typename Base::size_type;
35 using iterator = typename Base::iterator;
37 using value_type = typename Base::value_type;
38
39 Value & operator [] (const Key &inKey)
40 {
41 size_type index;
42 bool inserted = this->InsertKey(inKey, index);
43 value_type &key_value = this->GetElement(index);
44 if (inserted)
45 ::new (&key_value) value_type(inKey, Value());
46 return key_value.second;
47 }
48
49 template<class... Args>
50 std::pair<iterator, bool> try_emplace(const Key &inKey, Args &&...inArgs)
51 {
52 size_type index;
53 bool inserted = this->InsertKey(inKey, index);
54 if (inserted)
55 ::new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(inKey), std::forward_as_tuple(std::forward<Args>(inArgs)...));
56 return std::make_pair(iterator(this, index), inserted);
57 }
58
59 template<class... Args>
60 std::pair<iterator, bool> try_emplace(Key &&inKey, Args &&...inArgs)
61 {
62 size_type index;
63 bool inserted = this->InsertKey(inKey, index);
64 if (inserted)
65 ::new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(std::move(inKey)), std::forward_as_tuple(std::forward<Args>(inArgs)...));
66 return std::make_pair(iterator(this, index), inserted);
67 }
68
70 using Base::find;
71
73 iterator find(const Key &inKey)
74 {
75 const_iterator it = Base::find(inKey);
76 return iterator(this, it.mIndex);
77 }
78};
79
#define JPH_NAMESPACE_END
Definition Core.h:379
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
Const iterator.
Definition HashTable.h:357
Non-const iterator.
Definition HashTable.h:320
Definition HashTable.h:16
KeyValue & GetElement(size_type inIndex) const
Get an element by index.
Definition HashTable.h:200
const_iterator find(const Key &inKey) const
Find an element, returns iterator to element or end() if not found.
Definition HashTable.h:520
KeyValue value_type
Properties.
Definition HashTable.h:19
bool InsertKey(const Key &inKey, size_type &outIndex)
Definition HashTable.h:208
uint32 size_type
Definition HashTable.h:20
Internal helper class to provide context for UnorderedMap.
Definition UnorderedMap.h:14
static const Key & sGetKey(const std::pair< Key, Value > &inKeyValue)
Get key from key value pair.
Definition UnorderedMap.h:17
Definition UnorderedMap.h:30
typename Base::const_iterator const_iterator
Definition UnorderedMap.h:36
Value & operator[](const Key &inKey)
Definition UnorderedMap.h:39
typename Base::value_type value_type
Definition UnorderedMap.h:37
std::pair< iterator, bool > try_emplace(const Key &inKey, Args &&...inArgs)
Definition UnorderedMap.h:50
iterator find(const Key &inKey)
Non-const version of find.
Definition UnorderedMap.h:73
std::pair< iterator, bool > try_emplace(Key &&inKey, Args &&...inArgs)
Definition UnorderedMap.h:60
typename Base::size_type size_type
Definition UnorderedMap.h:34
typename Base::iterator iterator
Definition UnorderedMap.h:35
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59