Plasma Engine  2.0
Loading...
Searching...
No Matches
ArrayMap.h
1#pragma once
2
3#include <Foundation/Containers/DynamicArray.h>
4
12template <typename KEY, typename VALUE>
14{
16
17public:
18 struct Pair
19 {
20 KEY key;
21 VALUE value;
22
23 PL_DETECT_TYPE_CLASS(KEY, VALUE);
24
25 PL_ALWAYS_INLINE bool operator<(const Pair& rhs) const { return key < rhs.key; }
26
27 PL_ALWAYS_INLINE bool operator==(const Pair& rhs) const { return key == rhs.key; }
28 };
29
31 explicit plArrayMapBase(plAllocator* pAllocator); // [tested]
32
34 plArrayMapBase(const plArrayMapBase& rhs, plAllocator* pAllocator); // [tested]
35
37 void operator=(const plArrayMapBase& rhs); // [tested]
38
40 plUInt32 GetCount() const; // [tested]
41
43 bool IsEmpty() const; // [tested]
44
46 void Clear(); // [tested]
47
50 template <typename CompatibleKeyType, typename CompatibleValueType>
51 plUInt32 Insert(CompatibleKeyType&& key, CompatibleValueType&& value); // [tested]
52
54 void Sort() const; // [tested]
55
58 template <typename CompatibleKeyType>
59 plUInt32 Find(const CompatibleKeyType& key) const; // [tested]
60
64 template <typename CompatibleKeyType>
65 plUInt32 LowerBound(const CompatibleKeyType& key) const; // [tested]
66
70 template <typename CompatibleKeyType>
71 plUInt32 UpperBound(const CompatibleKeyType& key) const; // [tested]
72
74 const KEY& GetKey(plUInt32 uiIndex) const; // [tested]
75
77 const VALUE& GetValue(plUInt32 uiIndex) const; // [tested]
78
80 VALUE& GetValue(plUInt32 uiIndex); // [tested]
81
84
86 const plDynamicArray<Pair>& GetData() const;
87
89 template <typename CompatibleKeyType>
90 VALUE& FindOrAdd(const CompatibleKeyType& key, bool* out_pExisted = nullptr); // [tested]
91
93 template <typename CompatibleKeyType>
94 VALUE& operator[](const CompatibleKeyType& key); // [tested]
95
97 const Pair& GetPair(plUInt32 uiIndex) const; // [tested]
98
104 void RemoveAtAndCopy(plUInt32 uiIndex, bool bKeepSorted = false);
105
112 template <typename CompatibleKeyType>
113 bool RemoveAndCopy(const CompatibleKeyType& key, bool bKeepSorted = false); // [tested]
114
116 template <typename CompatibleKeyType>
117 bool Contains(const CompatibleKeyType& key) const; // [tested]
118
120 template <typename CompatibleKeyType>
121 bool Contains(const CompatibleKeyType& key, const VALUE& value) const; // [tested]
122
124 void Reserve(plUInt32 uiSize); // [tested]
125
127 void Compact(); // [tested]
128
130 bool operator==(const plArrayMapBase<KEY, VALUE>& rhs) const; // [tested]
131 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plArrayMapBase<KEY, VALUE>&);
132
134 plUInt64 GetHeapMemoryUsage() const { return m_Data.GetHeapMemoryUsage(); } // [tested]
135
136 using const_iterator = typename plDynamicArray<Pair>::const_iterator;
137 using const_reverse_iterator = typename plDynamicArray<Pair>::const_reverse_iterator;
138 using iterator = typename plDynamicArray<Pair>::iterator;
139 using reverse_iterator = typename plDynamicArray<Pair>::reverse_iterator;
140
141private:
142 mutable bool m_bSorted;
143 mutable plDynamicArray<Pair> m_Data;
144};
145
147template <typename KEY, typename VALUE, typename AllocatorWrapper = plDefaultAllocatorWrapper>
148class plArrayMap : public plArrayMapBase<KEY, VALUE>
149{
150 PL_DECLARE_MEM_RELOCATABLE_TYPE();
151
152public:
153 plArrayMap();
154 explicit plArrayMap(plAllocator* pAllocator);
155
158
159 void operator=(const plArrayMap<KEY, VALUE, AllocatorWrapper>& rhs);
160 void operator=(const plArrayMapBase<KEY, VALUE>& rhs);
161};
162
163
164template <typename KEY, typename VALUE>
165typename plArrayMapBase<KEY, VALUE>::iterator begin(plArrayMapBase<KEY, VALUE>& ref_container)
166{
167 return begin(ref_container.GetData());
168}
169
170template <typename KEY, typename VALUE>
171typename plArrayMapBase<KEY, VALUE>::const_iterator begin(const plArrayMapBase<KEY, VALUE>& container)
172{
173 return begin(container.GetData());
174}
175template <typename KEY, typename VALUE>
176typename plArrayMapBase<KEY, VALUE>::const_iterator cbegin(const plArrayMapBase<KEY, VALUE>& container)
177{
178 return cbegin(container.GetData());
179}
180
181template <typename KEY, typename VALUE>
182typename plArrayMapBase<KEY, VALUE>::reverse_iterator rbegin(plArrayMapBase<KEY, VALUE>& ref_container)
183{
184 return rbegin(ref_container.GetData());
185}
186
187template <typename KEY, typename VALUE>
188typename plArrayMapBase<KEY, VALUE>::const_reverse_iterator rbegin(const plArrayMapBase<KEY, VALUE>& container)
189{
190 return rbegin(container.GetData());
191}
192
193template <typename KEY, typename VALUE>
194typename plArrayMapBase<KEY, VALUE>::const_reverse_iterator crbegin(const plArrayMapBase<KEY, VALUE>& container)
195{
196 return crbegin(container.GetData());
197}
198
199template <typename KEY, typename VALUE>
200typename plArrayMapBase<KEY, VALUE>::iterator end(plArrayMapBase<KEY, VALUE>& ref_container)
201{
202 return end(ref_container.GetData());
203}
204
205template <typename KEY, typename VALUE>
206typename plArrayMapBase<KEY, VALUE>::const_iterator end(const plArrayMapBase<KEY, VALUE>& container)
207{
208 return end(container.GetData());
209}
210
211template <typename KEY, typename VALUE>
212typename plArrayMapBase<KEY, VALUE>::const_iterator cend(const plArrayMapBase<KEY, VALUE>& container)
213{
214 return cend(container.GetData());
215}
216
217template <typename KEY, typename VALUE>
218typename plArrayMapBase<KEY, VALUE>::reverse_iterator rend(plArrayMapBase<KEY, VALUE>& ref_container)
219{
220 return rend(ref_container.GetData());
221}
222
223template <typename KEY, typename VALUE>
224typename plArrayMapBase<KEY, VALUE>::const_reverse_iterator rend(const plArrayMapBase<KEY, VALUE>& container)
225{
226 return rend(container.GetData());
227}
228
229template <typename KEY, typename VALUE>
230typename plArrayMapBase<KEY, VALUE>::const_reverse_iterator crend(const plArrayMapBase<KEY, VALUE>& container)
231{
232 return crend(container.GetData());
233}
234
235
236#include <Foundation/Containers/Implementation/ArrayMap_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
An associative container, similar to plMap, but all data is stored in a sorted contiguous array,...
Definition ArrayMap.h:14
bool operator==(const plArrayMapBase< KEY, VALUE > &rhs) const
Compares the two containers for equality.
Definition ArrayMap_inl.h:294
plArrayMapBase(plAllocator *pAllocator)
Constructor.
Definition ArrayMap_inl.h:4
VALUE & FindOrAdd(const CompatibleKeyType &key, bool *out_pExisted=nullptr)
Returns the value stored at the given key. If none exists, one is created. bExisted indicates whether...
Definition ArrayMap_inl.h:195
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition ArrayMap.h:134
plDynamicArray< Pair > & GetData()
Returns a reference to the map data array.
Definition ArrayMap_inl.h:181
VALUE & operator[](const CompatibleKeyType &key)
Same as FindOrAdd.
void Sort() const
Ensures the internal data structure is sorted. This is done automatically every time a lookup needs t...
Definition ArrayMap_inl.h:56
bool RemoveAndCopy(const CompatibleKeyType &key, bool bKeepSorted=false)
Removes one element with the given key. Returns true, if one was found and removed....
Definition ArrayMap_inl.h:239
const Pair & GetPair(plUInt32 uiIndex) const
Returns the key/value pair at the given index.
Definition ArrayMap_inl.h:218
const VALUE & GetValue(plUInt32 uiIndex) const
Returns the value that is stored at the given index.
Definition ArrayMap_inl.h:169
void Clear()
Purges all elements from the map.
Definition ArrayMap_inl.h:38
bool IsEmpty() const
True if the map contains no elements.
Definition ArrayMap_inl.h:32
bool Contains(const CompatibleKeyType &key) const
Returns whether an element with the given key exists.
const KEY & GetKey(plUInt32 uiIndex) const
Returns the key that is stored at the given index.
Definition ArrayMap_inl.h:163
void RemoveAtAndCopy(plUInt32 uiIndex, bool bKeepSorted=false)
Removes the element at the given index.
Definition ArrayMap_inl.h:224
plUInt32 Insert(CompatibleKeyType &&key, CompatibleValueType &&value)
Always inserts a new value under the given key. Duplicates are allowed. Returns the index of the newl...
Definition ArrayMap_inl.h:46
plUInt32 GetCount() const
Returns the number of elements stored in the map.
Definition ArrayMap_inl.h:26
plUInt32 Find(const CompatibleKeyType &key) const
Returns an index to one element with the given key. If the key is inserted multiple times,...
Definition ArrayMap_inl.h:67
void operator=(const plArrayMapBase &rhs)
Copy assignment operator.
Definition ArrayMap_inl.h:19
void Reserve(plUInt32 uiSize)
Reserves enough memory to store size elements.
Definition ArrayMap_inl.h:282
void Compact()
Compacts the internal memory to not waste any space.
Definition ArrayMap_inl.h:288
plUInt32 UpperBound(const CompatibleKeyType &key) const
Returns the index to the first element with a key that is LARGER than the given key....
Definition ArrayMap_inl.h:132
plUInt32 LowerBound(const CompatibleKeyType &key) const
Returns the index to the first element with a key equal or larger than the given key....
Definition ArrayMap_inl.h:100
See plArrayMapBase for details.
Definition ArrayMap.h:149
Definition DynamicArray.h:81
Base class for Pointer like reverse iterators.
Definition ArrayIterator.h:152
Definition ArrayMap.h:19
Non-Const class for Pointer like reverse iterators.
Definition ArrayIterator.h:216