Plasma Engine  2.0
Loading...
Searching...
No Matches
Set.h
1#pragma once
2
3#include <Foundation/Containers/Deque.h>
4
11template <typename KeyType, typename Comparer>
13{
14private:
15 struct Node;
16
18 struct NilNode
19 {
20 plUInt16 m_uiLevel = 0;
21 Node* m_pParent = nullptr;
22 Node* m_pLink[2] = {nullptr, nullptr};
23 };
24
26 struct Node : public NilNode
27 {
28 KeyType m_Key;
29 };
30
31public:
33 template <bool REVERSE>
35 {
36 using iterator_category = std::forward_iterator_tag;
38 using difference_type = std::ptrdiff_t;
41
42 PL_DECLARE_POD_TYPE();
43
45 PL_ALWAYS_INLINE IteratorBase()
46 : m_pElement(nullptr)
47 {
48 } // [tested]
49
51 PL_ALWAYS_INLINE bool IsValid() const { return (m_pElement != nullptr); } // [tested]
52
54 PL_ALWAYS_INLINE bool operator==(const typename plSetBase<KeyType, Comparer>::IteratorBase<REVERSE>& it2) const { return (m_pElement == it2.m_pElement); }
55 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const typename plSetBase<KeyType, Comparer>::IteratorBase<REVERSE>&);
56
58 PL_FORCE_INLINE const KeyType& Key() const
59 {
60 PL_ASSERT_DEBUG(IsValid(), "Cannot access the 'key' of an invalid iterator.");
61 return m_pElement->m_Key;
62 } // [tested]
63
65 PL_ALWAYS_INLINE const KeyType& operator*() const { return Key(); }
66
68 void Next(); // [tested]
69
71 void Prev(); // [tested]
72
74 PL_ALWAYS_INLINE void operator++() { Next(); } // [tested]
75
77 PL_ALWAYS_INLINE void operator--() { Prev(); } // [tested]
78
79 protected:
80 void Advance(plInt32 dir0, plInt32 dir1);
81
82 friend class plSetBase<KeyType, Comparer>;
83
84 PL_ALWAYS_INLINE explicit IteratorBase(Node* pInit)
85 : m_pElement(pInit)
86 {
87 }
88
89 Node* m_pElement;
90 };
91
92 using Iterator = IteratorBase<false>;
93 using ReverseIterator = IteratorBase<true>;
94
95protected:
97 plSetBase(const Comparer& comparer, plAllocator* pAllocator); // [tested]
98
100 plSetBase(const plSetBase<KeyType, Comparer>& cc, plAllocator* pAllocator); // [tested]
101
103 ~plSetBase(); // [tested]
104
106 void operator=(const plSetBase<KeyType, Comparer>& rhs); // [tested]
107
108public:
110 bool IsEmpty() const; // [tested]
111
113 plUInt32 GetCount() const; // [tested]
114
116 void Clear(); // [tested]
117
119 Iterator GetIterator() const; // [tested]
120
122 ReverseIterator GetReverseIterator() const; // [tested]
123
125 template <typename CompatibleKeyType>
126 Iterator Insert(CompatibleKeyType&& key); // [tested]
127
129 template <typename CompatibleKeyType>
130 bool Remove(const CompatibleKeyType& key); // [tested]
131
133 Iterator Remove(const Iterator& pos); // [tested]
134
136 template <typename CompatibleKeyType>
137 Iterator Find(const CompatibleKeyType& key) const; // [tested]
138
140 template <typename CompatibleKeyType>
141 bool Contains(const CompatibleKeyType& key) const; // [tested]
142
144 bool ContainsSet(const plSetBase<KeyType, Comparer>& operand) const; // [tested]
145
148 template <typename CompatibleKeyType>
149 Iterator LowerBound(const CompatibleKeyType& key) const; // [tested]
150
153 template <typename CompatibleKeyType>
154 Iterator UpperBound(const CompatibleKeyType& key) const; // [tested]
155
157 void Union(const plSetBase<KeyType, Comparer>& operand); // [tested]
158
160 void Difference(const plSetBase<KeyType, Comparer>& operand); // [tested]
161
163 void Intersection(const plSetBase<KeyType, Comparer>& operand); // [tested]
164
166 plAllocator* GetAllocator() const { return m_Elements.GetAllocator(); }
167
169 bool operator==(const plSetBase<KeyType, Comparer>& rhs) const; // [tested]
170 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plSetBase<KeyType, Comparer>&);
171
173 plUInt64 GetHeapMemoryUsage() const { return m_Elements.GetHeapMemoryUsage(); } // [tested]
174
176 void Swap(plSetBase<KeyType, Comparer>& other); // [tested]
177
178private:
179 template <typename CompatibleKeyType>
180 Node* Internal_Find(const CompatibleKeyType& key) const;
181 template <typename CompatibleKeyType>
182 Node* Internal_LowerBound(const CompatibleKeyType& key) const;
183 template <typename CompatibleKeyType>
184 Node* Internal_UpperBound(const CompatibleKeyType& key) const;
185
186private:
187 void Constructor();
188
190 template <typename CompatibleKeyType>
191 Node* AcquireNode(CompatibleKeyType&& key, plUInt16 uiLevel, Node* pParent);
192
194 void ReleaseNode(Node* pNode);
195
199 Node* SkewNode(Node* root);
200 Node* SplitNode(Node* root);
201
202 template <typename CompatibleKeyType>
203 Node* Insert(Node* root, CompatibleKeyType&& key, Node*& pInsertedNode);
204 template <typename CompatibleKeyType>
205 Node* Remove(Node* root, const CompatibleKeyType& key, bool& bRemoved);
206
208 Node* GetLeftMost() const;
209
211 Node* GetRightMost() const;
212
214 void SwapNilNode(Node*& pCurNode, NilNode* pOld, NilNode* pNew);
215
217 Node* m_pRoot;
218
220 NilNode m_NilNode;
221
223 plUInt32 m_uiCount;
224
227
229 Node* m_pFreeElementStack;
230
232 Comparer m_Comparer;
233};
234
236template <typename KeyType, typename Comparer = plCompareHelper<KeyType>, typename AllocatorWrapper = plDefaultAllocatorWrapper>
237class plSet : public plSetBase<KeyType, Comparer>
238{
239public:
240 plSet();
241 explicit plSet(plAllocator* pAllocator);
242 plSet(const Comparer& comparer, plAllocator* pAllocator);
243
246
247 void operator=(const plSet<KeyType, Comparer, AllocatorWrapper>& rhs);
248 void operator=(const plSetBase<KeyType, Comparer>& rhs);
249};
250
251
252template <typename KeyType, typename Comparer>
254{
255 return ref_container.GetIterator();
256}
257
258template <typename KeyType, typename Comparer>
260{
261 return container.GetIterator();
262}
263
264template <typename KeyType, typename Comparer>
266{
267 return container.GetIterator();
268}
269
270template <typename KeyType, typename Comparer>
272{
274}
275
276template <typename KeyType, typename Comparer>
278{
280}
281
282template <typename KeyType, typename Comparer>
284{
286}
287
288
289#include <Foundation/Containers/Implementation/Set_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition Deque_inl.h:970
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition Deque.h:168
Definition Deque.h:270
A set container that only stores whether an element resides in it or not. Similar to STL::set.
Definition Set.h:13
bool ContainsSet(const plSetBase< KeyType, Comparer > &operand) const
Checks whether all keys of the given set are in the container.
Definition Set_inl.h:242
void Intersection(const plSetBase< KeyType, Comparer > &operand)
Makes this set the intersection of itself and the operand.
Definition Set_inl.h:338
Iterator GetIterator() const
Returns a constant Iterator to the very first element.
Definition Set_inl.h:165
void Clear()
Destroys all elements in the set and resets its size to zero.
Definition Set_inl.h:133
void Union(const plSetBase< KeyType, Comparer > &operand)
Makes this set the union of itself and the operand.
Definition Set_inl.h:320
void operator=(const plSetBase< KeyType, Comparer > &rhs)
Copies all keys from the given set into this one.
Definition Set_inl.h:124
plSetBase(const Comparer &comparer, plAllocator *pAllocator)
Initializes the set to be empty.
Definition Set_inl.h:101
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition Set.h:173
void Swap(plSetBase< KeyType, Comparer > &other)
Swaps this map with the other one.
Definition Set_inl.h:749
Iterator Insert(CompatibleKeyType &&key)
Inserts the key into the tree and returns an Iterator to it. O(log n) operation.
Definition Set_inl.h:351
void Difference(const plSetBase< KeyType, Comparer > &operand)
Makes this set the difference of itself and the operand, i.e. subtracts operand.
Definition Set_inl.h:329
plUInt32 GetCount() const
Returns the number of elements currently stored in the set. O(1) operation.
Definition Set_inl.h:158
ReverseIterator GetReverseIterator() const
Returns a constant ReverseIterator to the very last element.
Definition Set_inl.h:171
bool Remove(const CompatibleKeyType &key)
Erases the element with the given key, if it exists. O(log n) operation.
Definition Set_inl.h:364
bool operator==(const plSetBase< KeyType, Comparer > &rhs) const
Comparison operator.
Definition Set_inl.h:684
bool IsEmpty() const
Returns whether there are no elements in the set. O(1) operation.
Definition Set_inl.h:152
bool Contains(const CompatibleKeyType &key) const
Checks whether the given key is in the container.
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition Set.h:166
Iterator UpperBound(const CompatibleKeyType &key) const
Returns an Iterator to the element with a key that is LARGER than the given key. Returns an invalid i...
Iterator Find(const CompatibleKeyType &key) const
Searches for key, returns an Iterator to it or an invalid iterator, if no such key is found....
Iterator LowerBound(const CompatibleKeyType &key) const
Returns an Iterator to the element with a key equal or larger than the given key. Returns an invalid ...
~plSetBase()
Destroys all elements in the set.
Definition Set_inl.h:118
Definition Set.h:238
Base class for all iterators.
Definition Set.h:35
void Prev()
Advances the iterator to the previous element in the set. The iterator will not be valid anymore,...
Definition Set_inl.h:72
PL_ALWAYS_INLINE IteratorBase()
Constructs an invalid iterator.
Definition Set.h:45
PL_ALWAYS_INLINE void operator--()
Shorthand for 'Prev'.
Definition Set.h:77
void Next()
Advances the iterator to the next element in the set. The iterator will not be valid anymore,...
Definition Set_inl.h:58
PL_ALWAYS_INLINE bool operator==(const typename plSetBase< KeyType, Comparer >::IteratorBase< REVERSE > &it2) const
Checks whether the two iterators point to the same element.
Definition Set.h:54
PL_FORCE_INLINE const KeyType & Key() const
Returns the 'key' of the element that this iterator points to.
Definition Set.h:58
PL_ALWAYS_INLINE bool IsValid() const
Checks whether this iterator points to a valid element.
Definition Set.h:51
PL_ALWAYS_INLINE void operator++()
Shorthand for 'Next'.
Definition Set.h:74
PL_ALWAYS_INLINE const KeyType & operator*() const
Returns the 'key' of the element that this iterator points to.
Definition Set.h:65