Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/UnorderedMap.h

raw symbols SCIP
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
7 #include <Jolt/Core/HashTable.h>
8
9 JPH_NAMESPACE_BEGIN
10
11 /// Internal helper class to provide context for UnorderedMap
12 template <class Key, class Value>
13 class UnorderedMapDetail
14 {
15 public:
16 /// Get key from key value pair
17 static const Key & sGetKey(const std::pair<Key, Value> &inKeyValue)
18 {
19 return inKeyValue.first;
20 }
21 };
22
23 /// Hash Map class
24 /// @tparam Key Key type
25 /// @tparam Value Value type
26 /// @tparam Hash Hash function (note should be 64-bits)
27 /// @tparam KeyEqual Equality comparison function
28 template <class Key, class Value, class Hash = JPH::Hash<Key>, class KeyEqual = std::equal_to<Key>>
29 class UnorderedMap : public HashTable<Key, std::pair<Key, Value>, UnorderedMapDetail<Key, Value>, Hash, KeyEqual>
30 {
31 using Base = HashTable<Key, std::pair<Key, Value>, UnorderedMapDetail<Key, Value>, Hash, KeyEqual>;
32
33 public:
34 using size_type = typename Base::size_type;
35 using iterator = typename Base::iterator;
36 using const_iterator = typename Base::const_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
69 /// Const version of find
70 using Base::find;
71
72 /// Non-const version of find
73 iterator find(const Key &inKey)
74 {
75 const_iterator it = Base::find(inKey);
76 return iterator(this, it.mIndex);
77 }
78 };
79
80 JPH_NAMESPACE_END