Plasma Engine  2.0
Loading...
Searching...
No Matches
HashSet.h
1#pragma once
2
3#include <Foundation/Algorithm/HashingUtils.h>
4#include <Foundation/Math/Math.h>
5#include <Foundation/Memory/AllocatorWrapper.h>
6
15
17template <typename KeyType, typename Hasher>
19{
20public:
23 {
24 public:
26 bool IsValid() const; // [tested]
27
29 bool operator==(const typename plHashSetBase<KeyType, Hasher>::ConstIterator& rhs) const;
30
31 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const typename plHashSetBase<KeyType, Hasher>::ConstIterator&);
32
34 const KeyType& Key() const; // [tested]
35
37 PL_ALWAYS_INLINE const KeyType& operator*() const { return Key(); } // [tested]
38
40 void Next(); // [tested]
41
43 void operator++(); // [tested]
44
45 protected:
46 friend class plHashSetBase<KeyType, Hasher>;
47
48 explicit ConstIterator(const plHashSetBase<KeyType, Hasher>& hashSet);
49 void SetToBegin();
50 void SetToEnd();
51
52 const plHashSetBase<KeyType, Hasher>* m_pHashSet = nullptr;
53 plUInt32 m_uiCurrentIndex = 0; // current element index that this iterator points to.
54 plUInt32 m_uiCurrentCount = 0; // current number of valid elements that this iterator has found so far.
55 };
56
57protected:
59 explicit plHashSetBase(plAllocator* pAllocator); // [tested]
60
62 plHashSetBase(const plHashSetBase<KeyType, Hasher>& rhs, plAllocator* pAllocator); // [tested]
63
66
68 ~plHashSetBase(); // [tested]
69
71 void operator=(const plHashSetBase<KeyType, Hasher>& rhs); // [tested]
72
75
76public:
78 bool operator==(const plHashSetBase<KeyType, Hasher>& rhs) const; // [tested]
79 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plHashSetBase<KeyType, Hasher>&);
80
83 void Reserve(plUInt32 uiCapacity); // [tested]
84
89 void Compact(); // [tested]
90
92 plUInt32 GetCount() const; // [tested]
93
95 bool IsEmpty() const; // [tested]
96
98 void Clear(); // [tested]
99
101 template <typename CompatibleKeyType>
102 bool Insert(CompatibleKeyType&& key); // [tested]
103
105 template <typename CompatibleKeyType>
106 bool Remove(const CompatibleKeyType& key); // [tested]
107
109 ConstIterator Remove(const ConstIterator& pos); // [tested]
110
112 template <typename CompatibleKeyType>
113 bool Contains(const CompatibleKeyType& key) const; // [tested]
114
116 bool ContainsSet(const plHashSetBase<KeyType, Hasher>& operand) const; // [tested]
117
119 void Union(const plHashSetBase<KeyType, Hasher>& operand); // [tested]
120
122 void Difference(const plHashSetBase<KeyType, Hasher>& operand); // [tested]
123
125 void Intersection(const plHashSetBase<KeyType, Hasher>& operand); // [tested]
126
128 ConstIterator GetIterator() const; // [tested]
129
133
135 plAllocator* GetAllocator() const;
136
138 plUInt64 GetHeapMemoryUsage() const; // [tested]
139
141 void Swap(plHashSetBase<KeyType, Hasher>& other); // [tested]
142
144 template <typename CompatibleKeyType>
145 ConstIterator Find(const CompatibleKeyType& key) const;
146
147private:
148 KeyType* m_pEntries;
149 plUInt32* m_pEntryFlags;
150
151 plUInt32 m_uiCount;
152 plUInt32 m_uiCapacity;
153
154 plAllocator* m_pAllocator;
155
156 enum
157 {
158 FREE_ENTRY = 0,
159 VALID_ENTRY = 1,
160 DELETED_ENTRY = 2,
161 FLAGS_MASK = 3,
162 CAPACITY_ALIGNMENT = 32
163 };
164
165 void SetCapacity(plUInt32 uiCapacity);
166
167 void RemoveInternal(plUInt32 uiIndex);
168
169 template <typename CompatibleKeyType>
170 plUInt32 FindEntry(const CompatibleKeyType& key) const;
171
172 template <typename CompatibleKeyType>
173 plUInt32 FindEntry(plUInt32 uiHash, const CompatibleKeyType& key) const;
174
175 plUInt32 GetFlagsCapacity() const;
176 plUInt32 GetFlags(plUInt32* pFlags, plUInt32 uiEntryIndex) const;
177 void SetFlags(plUInt32 uiEntryIndex, plUInt32 uiFlags);
178
179 bool IsFreeEntry(plUInt32 uiEntryIndex) const;
180 bool IsValidEntry(plUInt32 uiEntryIndex) const;
181 bool IsDeletedEntry(plUInt32 uiEntryIndex) const;
182
183 void MarkEntryAsFree(plUInt32 uiEntryIndex);
184 void MarkEntryAsValid(plUInt32 uiEntryIndex);
185 void MarkEntryAsDeleted(plUInt32 uiEntryIndex);
186};
187
189template <typename KeyType, typename Hasher = plHashHelper<KeyType>, typename AllocatorWrapper = plDefaultAllocatorWrapper>
190class plHashSet : public plHashSetBase<KeyType, Hasher>
191{
192public:
193 plHashSet();
194 explicit plHashSet(plAllocator* pAllocator);
195
198
201
202 void operator=(const plHashSet<KeyType, Hasher, AllocatorWrapper>& rhs);
203 void operator=(const plHashSetBase<KeyType, Hasher>& rhs);
204
206 void operator=(plHashSetBase<KeyType, Hasher>&& rhs);
207};
208
209template <typename KeyType, typename Hasher>
211{
212 return set.GetIterator();
213}
214
215template <typename KeyType, typename Hasher>
217{
218 return set.GetIterator();
219}
220
221template <typename KeyType, typename Hasher>
223{
224 return set.GetEndIterator();
225}
226
227template <typename KeyType, typename Hasher>
229{
230 return set.GetEndIterator();
231}
232
233#include <Foundation/Containers/Implementation/HashSet_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
Const iterator.
Definition HashSet.h:23
bool IsValid() const
Checks whether this iterator points to a valid element.
Definition HashSet_inl.h:37
void operator++()
Shorthand for 'Next'.
Definition HashSet_inl.h:75
void Next()
Advances the iterator to the next element in the map. The iterator will not be valid anymore,...
Definition HashSet_inl.h:55
PL_ALWAYS_INLINE const KeyType & operator*() const
Returns the 'key' of the element that this iterator points to.
Definition HashSet.h:37
const KeyType & Key() const
Returns the 'key' of the element that this iterator points to.
Definition HashSet_inl.h:49
bool operator==(const typename plHashSetBase< KeyType, Hasher >::ConstIterator &rhs) const
Checks whether the two iterators point to the same element.
Definition HashSet_inl.h:43
Implementation of a hashset.
Definition HashSet.h:19
bool operator==(const plHashSetBase< KeyType, Hasher > &rhs) const
Compares this table to another table.
Definition HashSet_inl.h:185
bool Contains(const CompatibleKeyType &key) const
Returns if an entry with given key exists in the table.
plHashSetBase(plHashSetBase< KeyType, Hasher > &&rhs, plAllocator *pAllocator)
Moves data from an existing hashtable into this one.
void Clear()
Clears the table.
Definition HashSet_inl.h:254
bool Insert(CompatibleKeyType &&key)
Inserts the key. Returns whether the key was already existing.
Definition HashSet_inl.h:270
void Intersection(const plHashSetBase< KeyType, Hasher > &operand)
Makes this set the intersection of itself and the operand.
Definition HashSet_inl.h:425
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition HashSet_inl.h:453
void Compact()
Tries to compact the hashset to avoid wasting memory.
Definition HashSet_inl.h:224
void Union(const plHashSetBase< KeyType, Hasher > &operand)
Makes this set the union of itself and the operand.
Definition HashSet_inl.h:406
ConstIterator Remove(const ConstIterator &pos)
Erases the key at the given Iterator. Returns an iterator to the element after the given iterator.
~plHashSetBase()
Destructor.
Definition HashSet_inl.h:118
bool IsEmpty() const
Returns true, if the hashset does not contain any elements.
Definition HashSet_inl.h:248
ConstIterator Find(const CompatibleKeyType &key) const
Searches for key, returns a ConstIterator to it or an invalid iterator, if no such key is found....
bool ContainsSet(const plHashSetBase< KeyType, Hasher > &operand) const
Checks whether all keys of the given set are in the container.
Definition HashSet_inl.h:394
plHashSetBase(plAllocator *pAllocator)
Creates an empty hashset. Does not allocate any data yet.
Definition HashSet_inl.h:84
plUInt32 GetCount() const
Returns the number of active entries in the table.
Definition HashSet_inl.h:242
void Swap(plHashSetBase< KeyType, Hasher > &other)
Swaps this map with the other one.
Definition HashSet_inl.h:663
void Reserve(plUInt32 uiCapacity)
Expands the hashset by over-allocating the internal storage so that the load factor is lower or equal...
Definition HashSet_inl.h:206
void Difference(const plHashSetBase< KeyType, Hasher > &operand)
Makes this set the difference of itself and the operand, i.e. subtracts operand.
Definition HashSet_inl.h:416
void operator=(plHashSetBase< KeyType, Hasher > &&rhs)
Moves data from an existing hashset into this one.
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition HashSet_inl.h:459
ConstIterator GetIterator() const
Returns a constant Iterator to the very first element.
Definition HashSet_inl.h:437
plHashSetBase(const plHashSetBase< KeyType, Hasher > &rhs, plAllocator *pAllocator)
Creates a copy of the given hashset.
void operator=(const plHashSetBase< KeyType, Hasher > &rhs)
Copies the data from another hashset into this one.
ConstIterator GetEndIterator() const
Returns a constant Iterator to the first element that is not part of the hashset. Needed to implement...
Definition HashSet_inl.h:445
bool Remove(const CompatibleKeyType &key)
Removes the entry with the given key. Returns if an entry was removed.
Definition HashSet_inl.h:310
Definition HashSet.h:191