3#include <Foundation/Containers/Deque.h>
5template <
typename KeyType,
typename ValueType,
typename Comparer>
9template <
typename KeyType,
typename ValueType,
typename Comparer,
bool REVERSE>
12 using iterator_category = std::forward_iterator_tag;
14 using difference_type = std::ptrdiff_t;
18 PL_DECLARE_POD_TYPE();
27 PL_ALWAYS_INLINE
bool IsValid()
const {
return (m_pElement !=
nullptr); }
34 PL_FORCE_INLINE
const KeyType&
Key()
const
36 PL_ASSERT_DEBUG(
IsValid(),
"Cannot access the 'key' of an invalid iterator.");
37 return m_pElement->m_Key;
41 PL_FORCE_INLINE
const ValueType&
Value()
const
43 PL_ASSERT_DEBUG(
IsValid(),
"Cannot access the 'value' of an invalid iterator.");
44 return m_pElement->m_Value;
63 void Advance(
const plInt32 dir0,
const plInt32 dir1);
65 friend class plMapBase<KeyType, ValueType, Comparer>;
72 typename plMapBase<KeyType, ValueType, Comparer>::Node* m_pElement;
74#if PL_ENABLED(PL_USE_CPP20_OPERATORS)
78 std::pair<const KeyType&, const ValueType&> value;
79 const std::pair<const KeyType&, const ValueType&>* operator->()
const {
return &value; }
82 PL_ALWAYS_INLINE Pointer operator->()
const
84 return Pointer{.value = {
Key(),
Value()}};
89 template <std::
size_t Index>
90 std::tuple_element_t<Index, plMapBaseConstIteratorBase>& get()
const
92 if constexpr (Index == 0)
94 if constexpr (Index == 1)
101template <
typename KeyType,
typename ValueType,
typename Comparer,
bool REVERSE>
104 using iterator_category = std::forward_iterator_tag;
106 using difference_type = std::ptrdiff_t;
110 PL_DECLARE_POD_TYPE();
121 PL_ASSERT_DEBUG(this->
IsValid(),
"Cannot access the 'value' of an invalid iterator.");
122 return this->m_pElement->m_Value;
126 PL_FORCE_INLINE ValueType&
Value()
const
128 PL_ASSERT_DEBUG(this->
IsValid(),
"Cannot access the 'value' of an invalid iterator.");
129 return this->m_pElement->m_Value;
136 friend class plMapBase<KeyType, ValueType, Comparer>;
138 PL_ALWAYS_INLINE
explicit plMapBaseIteratorBase(
typename plMapBase<KeyType, ValueType, Comparer>::Node* pInit)
143#if PL_ENABLED(PL_USE_CPP20_OPERATORS)
147 std::pair<const KeyType&, ValueType&> value;
148 const std::pair<const KeyType&, ValueType&>* operator->()
const {
return &value; }
151 PL_ALWAYS_INLINE Pointer operator->()
const
160 template <std::
size_t Index>
161 std::tuple_element_t<Index, plMapBaseIteratorBase>& get()
163 if constexpr (Index == 0)
165 if constexpr (Index == 1)
169 template <std::
size_t Index>
170 std::tuple_element_t<Index, plMapBaseIteratorBase>& get()
const
172 if constexpr (Index == 0)
174 if constexpr (Index == 1)
191template <
typename KeyType,
typename ValueType,
typename Comparer>
212 Node* m_pParent =
nullptr;
213 Node* m_pLink[2] = {
nullptr,
nullptr};
214 plUInt8 m_uiLevel = 0;
218 struct Node :
public NilNode
260 template <
typename CompatibleKeyType,
typename CompatibleValueType>
261 Iterator Insert(CompatibleKeyType&& key, CompatibleValueType&& value);
264 template <
typename CompatibleKeyType>
265 bool Remove(
const CompatibleKeyType& key);
273 template <
typename CompatibleKeyType>
278 template <
typename CompatibleKeyType>
279 ValueType&
operator[](
const CompatibleKeyType& key);
282 template <
typename CompatibleKeyType>
283 bool TryGetValue(
const CompatibleKeyType& key, ValueType& out_value)
const;
286 template <
typename CompatibleKeyType>
287 bool TryGetValue(
const CompatibleKeyType& key,
const ValueType*& out_pValue)
const;
290 template <
typename CompatibleKeyType>
291 bool TryGetValue(
const CompatibleKeyType& key, ValueType*& out_pValue)
const;
294 template <
typename CompatibleKeyType>
295 const ValueType*
GetValue(
const CompatibleKeyType& key)
const;
298 template <
typename CompatibleKeyType>
302 template <
typename CompatibleKeyType>
303 const ValueType&
GetValueOrDefault(
const CompatibleKeyType& key,
const ValueType& defaultValue)
const;
306 template <
typename CompatibleKeyType>
311 template <
typename CompatibleKeyType>
316 template <
typename CompatibleKeyType>
320 template <
typename CompatibleKeyType>
324 template <
typename CompatibleKeyType>
329 template <
typename CompatibleKeyType>
334 template <
typename CompatibleKeyType>
351 template <
typename CompatibleKeyType>
352 Node* Internal_Find(
const CompatibleKeyType& key)
const;
353 template <
typename CompatibleKeyType>
354 Node* Internal_LowerBound(
const CompatibleKeyType& key)
const;
355 template <
typename CompatibleKeyType>
356 Node* Internal_UpperBound(
const CompatibleKeyType& key)
const;
362 template <
typename CompatibleKeyType>
363 Node* AcquireNode(CompatibleKeyType&& key, ValueType&& value, plUInt8 uiLevel, Node* pParent);
366 void ReleaseNode(Node* pNode);
370 Node* SkewNode(Node* root);
371 Node* SplitNode(Node* root);
372 void Insert(
const KeyType& key,
const ValueType& value, Node*& pInsertedNode);
373 template <
typename CompatibleKeyType>
374 Node*
Remove(Node* root,
const CompatibleKeyType& key,
bool& bRemoved);
377 Node* GetLeftMost()
const;
380 Node* GetRightMost()
const;
383 void SwapNilNode(Node*& pCurNode, NilNode* pOld, NilNode* pNew);
389 Node* m_pFreeElementStack;
406template <
typename KeyType,
typename ValueType,
typename Comparer = plCompareHelper<KeyType>,
typename AllocatorWrapper = plDefaultAllocatorWrapper>
421template <
typename KeyType,
typename ValueType,
typename Comparer>
427template <
typename KeyType,
typename ValueType,
typename Comparer>
433template <
typename KeyType,
typename ValueType,
typename Comparer>
439template <
typename KeyType,
typename ValueType,
typename Comparer>
445template <
typename KeyType,
typename ValueType,
typename Comparer>
451template <
typename KeyType,
typename ValueType,
typename Comparer>
457#include <Foundation/Containers/Implementation/Map_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
An associative container. Similar to STL::map.
Definition Map.h:193
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 Map.h:338
ConstIterator 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)
Searches for key, returns an Iterator to it or an invalid iterator, if no such key is found....
ConstIterator Find(const CompatibleKeyType &key) const
Searches for key, returns an Iterator to it or an invalid iterator, if no such key is found....
void Clear()
Destroys all elements in the map and resets its size to zero.
Definition Map_inl.h:175
~plMapBase()
Destroys all elements from the map.
Definition Map_inl.h:160
bool operator==(const plMapBase< KeyType, ValueType, Comparer > &rhs) const
Comparison operator.
Definition Map_inl.h:804
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition Map.h:345
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...
bool IsEmpty() const
Returns whether there are no elements in the map. O(1) operation.
Definition Map_inl.h:194
Iterator LowerBound(const CompatibleKeyType &key)
Returns an Iterator to the element with a key equal or larger than the given key. Returns an invalid ...
ValueType & operator[](const CompatibleKeyType &key)
Allows read/write access to the value stored under the given key. If there is no such key,...
Definition Map_inl.h:450
ReverseIterator GetReverseIterator()
Returns a ReverseIterator to the very last element.
Definition Map_inl.h:219
Iterator Insert(CompatibleKeyType &&key, CompatibleValueType &&value)
Inserts the key/value pair into the tree and returns an Iterator to it. O(log n) operation.
Definition Map_inl.h:535
bool Remove(const CompatibleKeyType &key)
Erases the key/value pair with the given key, if it exists. O(log n) operation.
Definition Map_inl.h:545
plUInt32 GetCount() const
Returns the number of elements currently stored in the map. O(1) operation.
Definition Map_inl.h:200
Iterator FindOrAdd(CompatibleKeyType &&key, bool *out_pExisted=nullptr)
Searches for the given key and returns an iterator to it. If it did not exist yet,...
Definition Map_inl.h:457
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.
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...
Iterator GetIterator()
Returns an Iterator to the very first element.
Definition Map_inl.h:207
void operator=(const plMapBase< KeyType, ValueType, Comparer > &rhs)
Copies all key/value pairs from the given map into this one.
Definition Map_inl.h:166
void Swap(plMapBase< KeyType, ValueType, Comparer > &other)
Swaps this map with the other one.
Definition Map_inl.h:873
ValueType * GetValue(const CompatibleKeyType &key)
Returns a pointer to the value of the entry with the given key if found, otherwise returns nullptr.
plMapBase(const Comparer &comparer, plAllocator *pAllocator)
Initializes the map to be empty.
Definition Map_inl.h:143
ConstIterator LowerBound(const CompatibleKeyType &key) const
Returns an Iterator to the element with a key equal or larger than the given key. Returns an invalid ...
const ValueType & GetValueOrDefault(const CompatibleKeyType &key, const ValueType &defaultValue) const
Either returns the value of the entry with the given key, if found, or the provided default value.
Iterator UpperBound(const CompatibleKeyType &key)
Returns an Iterator to the element with a key that is LARGER than the given key. Returns an invalid i...
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...
Base class for all iterators.
Definition Map.h:11
PL_ALWAYS_INLINE bool operator==(const plMapBaseConstIteratorBase &it2) const
Checks whether the two iterators point to the same element.
Definition Map.h:30
PL_ALWAYS_INLINE bool IsValid() const
Checks whether this iterator points to a valid element.
Definition Map.h:27
PL_FORCE_INLINE const KeyType & Key() const
Returns the 'key' of the element that this iterator points to.
Definition Map.h:34
void Prev()
Advances the iterator to the previous element in the map. The iterator will not be valid anymore,...
Definition Map_inl.h:71
PL_FORCE_INLINE const ValueType & Value() const
Returns the 'value' of the element that this iterator points to.
Definition Map.h:41
PL_ALWAYS_INLINE void operator--()
Shorthand for 'Prev'.
Definition Map.h:60
PL_ALWAYS_INLINE plMapBaseConstIteratorBase & operator*()
Returns '*this' to enable foreach.
Definition Map.h:48
PL_ALWAYS_INLINE void operator++()
Shorthand for 'Next'.
Definition Map.h:57
PL_ALWAYS_INLINE plMapBaseConstIteratorBase()
Constructs an invalid iterator.
Definition Map.h:21
void Next()
Advances the iterator to the next element in the map. The iterator will not be valid anymore,...
Definition Map_inl.h:58
Forward Iterator to iterate over all elements in sorted order.
Definition Map.h:103
PL_ALWAYS_INLINE plMapBaseIteratorBase()
Constructs an invalid iterator.
Definition Map.h:113
PL_ALWAYS_INLINE plMapBaseIteratorBase & operator*()
Returns '*this' to enable foreach.
Definition Map.h:133
PL_FORCE_INLINE ValueType & Value() const
Returns the 'value' of the element that this iterator points to.
Definition Map.h:126
PL_FORCE_INLINE ValueType & Value()
Returns the 'value' of the element that this iterator points to.
Definition Map.h:119