UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
HashMap.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#if (defined(__AUTORTFM) && __AUTORTFM)
6
7#include "StlAllocator.h"
8
9#include <functional>
10#include <type_traits>
11#include <unordered_map>
12
13namespace AutoRTFM
14{
15
16// A simple structure holding a templated key and value pair.
17template<typename KeyType, typename ValueType>
18struct TKeyAndValue
19{
20 KeyType Key;
21 ValueType Value;
22};
23
24// HashMap is an unordered hashmap.
25// TODO(SOL-7652): This currently wraps a std::unordered_map. Reimplement to
26// improve performance and to avoid platform-specific variations in behavior.
27template<typename KeyType, typename ValueType, typename Hash = std::hash<KeyType>, typename Equal = std::equal_to<KeyType>>
28class THashMap
29{
30 using FInnerMap = std::unordered_map<KeyType, ValueType, Hash, Equal, StlAllocator<std::pair<const KeyType, ValueType>>>;
31
32 template<bool bConst>
33 class TIterator
34 {
35 using FInnerIterator = std::conditional_t<bConst, typename FInnerMap::const_iterator, typename FInnerMap::iterator>;
36 using FKeyAndValue = std::conditional_t<bConst, const TKeyAndValue<const KeyType, ValueType>, TKeyAndValue<const KeyType, ValueType>>;
37 public:
38 // Constructor wrapping inner-iterator
40 // Copy constructor
41 TIterator(const TIterator&) = default;
42 // Move constructor
43 TIterator(TIterator&&) = default;
44 // Copy assignment operator
45 TIterator& operator = (const TIterator& Other) = default;
46 // Move assignment operator
47 TIterator& operator = (TIterator&& Other) = default;
48
49 FKeyAndValue& operator*() const
50 {
51 // Super hacky: Reinterpret a std::pair to a TKeyAndValue so
52 // that THashMap's iterator has the interface of a TMap instead of a
53 // std::unordered_map.
54 // TODO(SOL-7652): Remove this filth when THashMap is reimplemented.
55 using FPair = std::conditional_t<bConst,
56 const std::pair<const KeyType, ValueType>,
57 std::pair<const KeyType, ValueType>>;
58 FPair* Pair = &*InnerIterator;
59 FKeyAndValue* KeyAndValue = reinterpret_cast<FKeyAndValue*>(Pair);
60 static_assert(std::is_same_v<decltype(Pair->first), decltype(KeyAndValue->Key)>);
61 static_assert(std::is_same_v<decltype(Pair->second), decltype(KeyAndValue->Value)>);
62 static_assert(offsetof(FPair, first) == offsetof(FKeyAndValue, Key));
63 static_assert(offsetof(FPair, second) == offsetof(FKeyAndValue, Value));
64 static_assert(sizeof(FPair) == sizeof(FKeyAndValue));
65 static_assert(alignof(FPair) == alignof(FKeyAndValue));
66 return *KeyAndValue;
67 }
68
69 TIterator& operator++()
70 {
72 return *this;
73 }
74
75 bool operator == (const TIterator& Other) const
76 {
77 return InnerIterator == Other.InnerIterator;
78 }
79
80 bool operator != (const TIterator& Other) const
81 {
82 return InnerIterator != Other.InnerIterator;
83 }
84
85 private:
87 };
88
89public:
90 using Key = KeyType;
91 using Value = ValueType;
92 using Iterator = TIterator</* bConst */ false>;
93 using ConstIterator = TIterator</* bConst */ true>;
94
95 // Constructor
96 THashMap() = default;
97 // Destructor
98 ~THashMap() = default;
99 // Copy constructor
100 THashMap(const THashMap& Other) = default;
101 // Move constructor
102 THashMap(THashMap&& Other) : Map{std::move(Other.Map)}
103 {
104 if (this != &Other)
105 {
106 Other.Map.clear(); // Ensure Other is cleared.
107 }
108 }
109 // Copy assignment operator
110 THashMap& operator = (const THashMap& Other) = default;
111 // Move assignment operator
112 THashMap& operator = (THashMap&& Other)
113 {
114 if (this != &Other)
115 {
116 Map = std::move(Other.Map);
117 Other.Map.clear(); // Ensure Other is cleared.
118 }
119 return *this;
120 }
121
122 // Set the value associated with a key, replacing any existing entry with
123 // the given key.
124 template<typename K, typename V>
125 void Add(K&& Key, V&& Value)
126 {
127 Map[std::forward<K>(Key)] = std::forward<V>(Value);
128 }
129
130 // Looks up the value with the given key, returning a pointer to the value
131 // if found or nullptr if not found.
132 // Warning: The returned pointer will become invalid if the hash map is
133 // modified.
134 template<typename K>
135 ValueType* Find(K&& Key)
136 {
137 typename FInnerMap::iterator It = Map.find(std::forward<K>(Key));
138 return It == Map.end() ? nullptr : &It->second;
139 }
140
141 // Looks up the value with the given key, returning a reference to the
142 // existing value if found or a reference to a newly added, zero-initialized
143 // value if not found.
144 // Warning: The returned reference will become invalid if the hash map is
145 // modified.
146 template<typename K>
147 ValueType& FindOrAdd(K&& Key)
148 {
149 return Map[std::forward<K>(Key)];
150 }
151
152 // Removes the entry with the given key. This is a no-op if the hash map
153 // does not contain an entry with the given key.
154 void Remove(const KeyType& Key)
155 {
156 Map.erase(Key);
157 }
158
159 // Returns true if the hash map contains an entry with the given key.
160 bool Contains(const KeyType& Key)
161 {
162 return Map.count(Key) != 0;
163 }
164
165 // Removes all entries from the hash map, freeing all allocations.
166 void Empty()
167 {
168 Map.clear();
169 }
170
171 // Removes all entries from the hash map.
172 // TODO(SOL-7652): Preserve any heap allocations made by the hash map.
173 void Reset()
174 {
175 Map.clear();
176 }
177
178 // Returns the number of entries held by the hash map.
179 size_t Num() const
180 {
181 return Map.size();
182 }
183
184 // Returns true if the hash map holds no entries.
185 bool IsEmpty() const
186 {
187 return Map.empty();
188 }
189
190 // Iterator methods
191 ConstIterator begin() const { return ConstIterator{Map.begin()}; }
192 ConstIterator end() const { return ConstIterator{Map.end()}; }
193 Iterator begin() { return Iterator{Map.begin()}; }
194 Iterator end() { return Iterator{Map.end()}; }
195
196private:
198};
199
200} // AutoRTFM
201
202#endif // (defined(__AUTORTFM) && __AUTORTFM)
UE_FORCEINLINE_HINT FLinearColor operator*(float Scalar, const FLinearColor &Color)
Definition Color.h:473
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_FORCEINLINE_HINT bool operator!=(const FIndexedPointer &Other) const
Definition LockFreeList.h:76
@ Num
Definition MetalRHIPrivate.h:234
TIndexedContainerIterator< TArray< FPreviewAttachedObjectPair >, FPreviewAttachedObjectPair, int32 > TIterator
Definition PreviewAssetAttachComponent.h:68
Definition Array.h:64
Definition API.cpp:57
@ Contains
Definition AutomationTest.h:160
FORCEINLINE FStridedReferenceIterator begin(FStridedReferenceView View)
Definition FastReferenceCollector.h:490
FORCEINLINE FStridedReferenceIterator end(FStridedReferenceView View)
Definition FastReferenceCollector.h:491