3#include <Foundation/Algorithm/HashingUtils.h>
4#include <Foundation/Math/Math.h>
5#include <Foundation/Memory/AllocatorWrapper.h>
7template <
typename KeyType,
typename ValueType,
typename Hasher>
11template <
typename KeyType,
typename ValueType,
typename Hasher>
14 using iterator_category = std::forward_iterator_tag;
16 using difference_type = std::ptrdiff_t;
20 PL_DECLARE_POD_TYPE();
30 const KeyType&
Key()
const;
33 const ValueType&
Value()
const;
52 plUInt32 m_uiCurrentIndex = 0;
53 plUInt32 m_uiCurrentCount = 0;
55#if PL_ENABLED(PL_USE_CPP20_OPERATORS)
59 std::pair<const KeyType&, const ValueType&> value;
60 const std::pair<const KeyType&, const ValueType&>* operator->()
const {
return &value; }
63 PL_ALWAYS_INLINE Pointer operator->()
const
65 return Pointer{.value = {
Key(),
Value()}};
70 template <std::
size_t Index>
71 std::tuple_element_t<Index, plHashTableBaseConstIterator>& get()
const
73 if constexpr (Index == 0)
75 if constexpr (Index == 1)
82template <
typename KeyType,
typename ValueType,
typename Hasher>
85 PL_DECLARE_POD_TYPE();
97 PL_FORCE_INLINE ValueType&
Value();
100 PL_FORCE_INLINE ValueType&
Value()
const;
110#if PL_ENABLED(PL_USE_CPP20_OPERATORS)
114 std::pair<const KeyType&, ValueType&> value;
115 const std::pair<const KeyType&, ValueType&>* operator->()
const {
return &value; }
118 PL_ALWAYS_INLINE Pointer operator->()
const
125 template <std::
size_t Index>
126 std::tuple_element_t<Index, plHashTableBaseIterator>& get()
128 if constexpr (Index == 0)
130 if constexpr (Index == 1)
134 template <std::
size_t Index>
135 std::tuple_element_t<Index, plHashTableBaseIterator>& get()
const
137 if constexpr (Index == 0)
139 if constexpr (Index == 1)
155template <
typename KeyType,
typename ValueType,
typename Hasher>
188 void Reserve(plUInt32 uiCapacity);
208 template <
typename CompatibleKeyType,
typename CompatibleValueType>
209 bool Insert(CompatibleKeyType&& key, CompatibleValueType&& value, ValueType* out_pOldValue =
nullptr);
212 template <
typename CompatibleKeyType>
213 bool Remove(
const CompatibleKeyType& key, ValueType* out_pOldValue =
nullptr);
222 template <
typename CompatibleKeyType>
223 bool TryGetValue(
const CompatibleKeyType& key, ValueType& out_value)
const;
226 template <
typename CompatibleKeyType>
227 bool TryGetValue(
const CompatibleKeyType& key,
const ValueType*& out_pValue)
const;
230 template <
typename CompatibleKeyType>
231 bool TryGetValue(
const CompatibleKeyType& key, ValueType*& out_pValue)
const;
234 template <
typename CompatibleKeyType>
238 template <
typename CompatibleKeyType>
242 template <
typename CompatibleKeyType>
243 const ValueType*
GetValue(
const CompatibleKeyType& key)
const;
246 template <
typename CompatibleKeyType>
253 ValueType&
FindOrAdd(
const KeyType& key,
bool* out_pExisted =
nullptr);
256 template <
typename CompatibleKeyType>
290 Entry* m_pEntries =
nullptr;
291 plUInt32* m_pEntryFlags =
nullptr;
293 plUInt32 m_uiCount = 0;
294 plUInt32 m_uiCapacity = 0;
304 CAPACITY_ALIGNMENT = 32
307 void SetCapacity(plUInt32 uiCapacity);
309 void RemoveInternal(plUInt32 uiIndex);
311 template <
typename CompatibleKeyType>
312 plUInt32 FindEntry(
const CompatibleKeyType& key)
const;
314 template <
typename CompatibleKeyType>
315 plUInt32 FindEntry(plUInt32 uiHash,
const CompatibleKeyType& key)
const;
317 plUInt32 GetFlagsCapacity()
const;
318 plUInt32 GetFlags(plUInt32* pFlags, plUInt32 uiEntryIndex)
const;
319 void SetFlags(plUInt32 uiEntryIndex, plUInt32 uiFlags);
321 bool IsFreeEntry(plUInt32 uiEntryIndex)
const;
322 bool IsValidEntry(plUInt32 uiEntryIndex)
const;
323 bool IsDeletedEntry(plUInt32 uiEntryIndex)
const;
325 void MarkEntryAsFree(plUInt32 uiEntryIndex);
326 void MarkEntryAsValid(plUInt32 uiEntryIndex);
327 void MarkEntryAsDeleted(plUInt32 uiEntryIndex);
331template <
typename KeyType,
typename ValueType,
typename Hasher = plHashHelper<KeyType>,
typename AllocatorWrapper = plDefaultAllocatorWrapper>
355template <
typename KeyType,
typename ValueType,
typename Hasher>
361template <
typename KeyType,
typename ValueType,
typename Hasher>
367template <
typename KeyType,
typename ValueType,
typename Hasher>
373template <
typename KeyType,
typename ValueType,
typename Hasher>
379template <
typename KeyType,
typename ValueType,
typename Hasher>
385template <
typename KeyType,
typename ValueType,
typename Hasher>
391#include <Foundation/Containers/Implementation/HashTable_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
Implementation of a hashtable which stores key/value pairs.
Definition HashTable.h:157
bool TryGetValue(const CompatibleKeyType &key, const ValueType *&out_pValue) const
Returns whether an entry with the given key was found and if found writes out the pointer to the corr...
bool operator==(const plHashTableBase< KeyType, ValueType, Hasher > &rhs) const
Compares this table to another table.
Definition HashTable_inl.h:282
ValueType & FindOrAdd(const KeyType &key, bool *out_pExisted=nullptr)
Returns the value stored at the given key. If none exists, one is created. bExisted indicates whether...
Definition HashTable_inl.h:583
Iterator GetIterator()
Returns an Iterator to the very first element.
Definition HashTable_inl.h:625
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition HashTable_inl.h:657
bool Contains(const CompatibleKeyType &key) const
Returns if an entry with given key exists in the table.
void Remove(const ConstIterator &pos)=delete
Cannot remove an element with just a plHashTableBaseConstIterator.
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition HashTable_inl.h:663
void operator=(const plHashTableBase< KeyType, ValueType, Hasher > &rhs)
Copies the data from another hashtable into this one.
plUInt32 GetCount() const
Returns the number of active entries in the table.
Definition HashTable_inl.h:343
bool TryGetValue(const CompatibleKeyType &key, ValueType &out_value) const
Returns whether an entry with the given key was found and if found writes out the corresponding value...
ValueType & operator[](const KeyType &key)
Returns the value to the given key if found or creates a new entry with the given key and a default c...
Definition HashTable_inl.h:577
const ValueType * GetValue(const CompatibleKeyType &key) const
Returns a pointer to the value of the entry with the given key if found, otherwise returns nullptr.
ConstIterator Find(const CompatibleKeyType &key) const
Searches for key, returns a plHashTableBaseConstIterator to it or an invalid iterator,...
Definition HashTable_inl.h:528
Iterator Remove(const Iterator &pos)
Erases the key/value pair at the given Iterator. Returns an iterator to the element after the given i...
~plHashTableBase()
Destructor.
Definition HashTable_inl.h:215
Iterator GetEndIterator()
Returns an Iterator to the first element that is not part of the hash-table. Needed to support range ...
Definition HashTable_inl.h:633
void Compact()
Tries to compact the hashtable to avoid wasting memory.
Definition HashTable_inl.h:325
plHashTableBase(plHashTableBase< KeyType, ValueType, Hasher > &&rhs, plAllocator *pAllocator)
Moves data from an existing hashtable into this one.
bool IsEmpty() const
Returns true, if the hashtable does not contain any elements.
Definition HashTable_inl.h:349
bool Insert(CompatibleKeyType &&key, CompatibleValueType &&value, ValueType *out_pOldValue=nullptr)
Inserts the key value pair or replaces value if an entry with the given key already exists.
plHashTableBase(const plHashTableBase< KeyType, ValueType, Hasher > &rhs, plAllocator *pAllocator)
Creates a copy of the given hashtable.
void Clear()
Clears the table.
Definition HashTable_inl.h:355
void Reserve(plUInt32 uiCapacity)
Expands the hashtable by over-allocating the internal storage so that the load factor is lower or equ...
Definition HashTable_inl.h:307
void operator=(plHashTableBase< KeyType, ValueType, Hasher > &&rhs)
Moves data from an existing hashtable into this one.
bool Remove(const CompatibleKeyType &key, ValueType *out_pOldValue=nullptr)
Removes the entry with the given key. Returns whether an entry was removed and optionally writes out ...
bool TryGetValue(const CompatibleKeyType &key, ValueType *&out_pValue) const
Returns whether an entry with the given key was found and if found writes out the pointer to the corr...
void Swap(plHashTableBase< KeyType, ValueType, Hasher > &other)
Swaps this map with the other one.
Definition HashTable_inl.h:867
plHashTableBase(plAllocator *pAllocator)
Creates an empty hashtable. Does not allocate any data yet.
Definition HashTable_inl.h:181
ValueType * GetValue(const CompatibleKeyType &key)
Returns a pointer to the value of the entry with the given key if found, otherwise returns nullptr.
Definition HashTable.h:333
Const iterator.
Definition HashTable.h:13
void operator++()
Shorthand for 'Next'.
Definition HashTable_inl.h:88
void Next()
Advances the iterator to the next element in the map. The iterator will not be valid anymore,...
Definition HashTable_inl.h:62
PL_ALWAYS_INLINE plHashTableBaseConstIterator & operator*()
Returns '*this' to enable foreach.
Definition HashTable.h:42
const ValueType & Value() const
Returns the 'value' of the element that this iterator points to.
Definition HashTable_inl.h:56
const KeyType & Key() const
Returns the 'key' of the element that this iterator points to.
Definition HashTable_inl.h:50
bool IsValid() const
Checks whether this iterator points to a valid element.
Definition HashTable_inl.h:38
bool operator==(const plHashTableBaseConstIterator &rhs) const
Checks whether the two iterators point to the same element.
Definition HashTable_inl.h:44
Iterator with write access.
Definition HashTable.h:84
PL_ALWAYS_INLINE plHashTableBaseIterator & operator*()
Returns '*this' to enable foreach.
Definition HashTable.h:103
PL_ALWAYS_INLINE plHashTableBaseIterator(const plHashTableBaseIterator &rhs)
Creates a new iterator from another.
PL_ALWAYS_INLINE void operator=(const plHashTableBaseIterator &rhs)
Assigns one iterator no another.
Definition HashTable_inl.h:134
PL_FORCE_INLINE ValueType & Value()
Returns the 'value' of the element that this iterator points to.
Definition HashTable_inl.h:142