ACF $AcfVersion:0$
TCascadedMap.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QPair>
7#include <QtCore/QVector>
8#include <QtCore/QMap>
9
10// ACF includes
11#include <istd/TIMap.h>
12
13
14namespace istd
15{
16
17
24template <typename Key, typename Value>
25class TCascadedMap: virtual public TIMap<Key, Value>
26{
27public:
35 explicit TCascadedMap(const TCascadedMap<Key, Value>* parentPtr);
39 TCascadedMap(const TCascadedMap& map);
40
45
49 void SetParent(const TIMap<Key, Value>* parentPtr);
50
54 int FindLocalIndex(const KeyType& key) const;
55
60 const ValueType* FindLocalElement(const KeyType& key) const;
61
67
71 const KeyType& GetLocalKeyAt(int index) const;
75 KeyType& GetLocalKeyAt(int index);
76
80 const ValueType& GetLocalValueAt(int index) const;
81
85 ValueType& GetLocalValueAt(int index);
86
90 int GetLocalElementsCount() const;
91
95 bool InsertLocal(const KeyType& key, const ValueType& value);
96
100 void ResetLocal();
101
106 void GetLocalKeys(Keys& result, bool doAppend = false) const;
107
108 // reimplemented (istd::TIMap)
109 virtual int GetElementsCount() const override;
110 virtual ValueType& operator[](const KeyType& key) override;
111 virtual const ValueType& operator[](const KeyType& key) const override;
112 virtual int FindIndex(const KeyType& key) const override;
113 virtual const ValueType* FindElement(const KeyType& key) const override;
114 virtual void GetKeys(Keys& result, bool doAppend = false) const override;
115 virtual const KeyType& GetKeyAt(int index) const override;
116 virtual const ValueType& GetValueAt(int index) const override;
117
118 // TODO: add element removing and correct comment in class header.
119
120private:
121 const TIMap<Key, Value>* m_parentPtr;
122
123 typedef QMap<KeyType, int> IndicesMap;
124 typedef QPair<KeyType, ValueType> Pair;
125 typedef QVector<Pair> PairList;
126
127 mutable IndicesMap m_positionsMap;
128 mutable PairList m_pairList;
129};
130
131
132// public methods
133
134template <typename Key, typename Value>
136: m_parentPtr(NULL)
137{
138}
139
140
141template <typename Key, typename Value>
143: m_parentPtr(parentPtr)
144{
145}
146
147
148template <typename Key, typename Value>
150: m_parentPtr(map.m_parentPtr),
151 m_positionsMap(map.m_positionsMap),
152 m_pairList(map.m_pairList)
153{
154}
155
156
157template <typename Key, typename Value>
159{
160 return m_parentPtr;
161}
162
163
164template <typename Key, typename Value>
166{
167 m_parentPtr = parentPtr;
168}
169
170
171template <typename Key, typename Value>
173{
174 typename IndicesMap::ConstIterator iter = m_positionsMap.constFind(key);
175 if (iter != m_positionsMap.constEnd()){
176 return iter.value();
177 }
178
179 return -1;
180}
181
182
183template <typename Key, typename Value>
185{
186 int index = FindLocalIndex(key);
187 if (index >= 0){
188 return &GetLocalValueAt(index);
189 }
190
191 return NULL;
192}
193
194
195template <typename Key, typename Value>
197{
198 int index = FindLocalIndex(key);
199 if (index >= 0){
200 return &GetLocalValueAt(index);
201 }
202
203 return NULL;
204}
205
206
207template <typename Key, typename Value>
209{
210 Q_ASSERT(index < int(m_pairList.size()));
211
212 return m_pairList[index].first;
213}
214
215
216template <typename Key, typename Value>
218{
219 Q_ASSERT(index < int(m_pairList.size()));
220
221 return m_pairList[index].first;
222}
223
224
225template <typename Key, typename Value>
227{
228 Q_ASSERT(index < int(m_pairList.size()));
229
230 return m_pairList[index].second;
231}
232
233
234template <typename Key, typename Value>
236{
237 Q_ASSERT(index < int(m_pairList.size()));
238
239 return m_pairList[index].second;
240}
241
242
243template <typename Key, typename Value>
245{
246 return int(m_pairList.size());
247}
248
249
250template <typename Key, typename Value>
252{
253 typename IndicesMap::ConstIterator iter = m_positionsMap.constFind(key);
254 if (iter != m_positionsMap.constEnd()){
255 return false;
256 }
257
258 int newIndex = int(m_pairList.size());
259 m_positionsMap[key] = newIndex;
260
261 m_pairList.push_back(Pair(key, value));
262
263 return true;
264}
265
266
267template <typename Key, typename Value>
269{
270 m_positionsMap.clear();
271 m_pairList.clear();
272}
273
274
275template <typename Key, typename Value>
276void TCascadedMap<Key, Value>::GetLocalKeys(Keys& result, bool doAppend) const
277{
278 if (!doAppend){
279 result.clear();
280 }
281
282 for ( typename IndicesMap::const_iterator iter = m_positionsMap.begin();
283 iter != m_positionsMap.end();
284 ++iter){
285 result.insert(iter.key());
286 }
287}
288
289
290// reimplemented (istd::TIMap)
291
292template <typename Key, typename Value>
294{
295 if (m_parentPtr != NULL){
296 return m_parentPtr->GetElementsCount() + GetLocalElementsCount();
297 }
298 else{
299 return GetLocalElementsCount();
300 }
301}
302
303
304template <typename Key, typename Value>
306{
307 typename IndicesMap::ConstIterator iter = m_positionsMap.constFind(key);
308 if (iter != m_positionsMap.constEnd()){
309 return m_pairList[iter.value()].second;
310 }
311
312 int newIndex = int(m_pairList.size());
313 m_positionsMap[key] = newIndex;
314
315 m_pairList.push_back(Pair(key, ValueType()));
316
317 return m_pairList.back().second;
318}
319
320
321template <typename Key, typename Value>
323{
324 return const_cast<TCascadedMap*>(this)->operator[](key);
325}
326
327
328template <typename Key, typename Value>
330{
331 typename IndicesMap::ConstIterator iter = m_positionsMap.constFind(key);
332 if (iter != m_positionsMap.constEnd()){
333 return iter.value();
334 }
335
336 if (m_parentPtr != NULL){
337 int parentIndex = m_parentPtr->FindIndex(key);
338 if (parentIndex >= 0){
339 return parentIndex + GetLocalElementsCount();
340 }
341 }
342
343 return -1;
344}
345
346
347template <typename Key, typename Value>
349{
350 int index = FindIndex(key);
351 if (index >= 0){
352 return &GetValueAt(index);
353 }
354
355 return NULL;
356}
357
358
359template <typename Key, typename Value>
360void TCascadedMap<Key, Value>::GetKeys(Keys& result, bool doAppend) const
361{
362 GetLocalKeys(result, doAppend);
363
364 if (m_parentPtr != NULL){
365 m_parentPtr->GetKeys(result, true);
366 }
367}
368
369
370template <typename Key, typename Value>
372{
373 int elementsCount = GetLocalElementsCount();
374 if (index < elementsCount){
375 return GetLocalKeyAt(index);
376 }
377
378 Q_ASSERT(m_parentPtr != NULL); // Index from outside this map!
379
380 return m_parentPtr->GetKeyAt(index - elementsCount);
381}
382
383
384template <typename Key, typename Value>
386{
387 int elementsCount = GetLocalElementsCount();
388 if (index < elementsCount){
389 return GetLocalValueAt(index);
390 }
391
392 Q_ASSERT(m_parentPtr != NULL); // Index from outside this map!
393
394 return m_parentPtr->GetValueAt(index - elementsCount);
395}
396
397
398}//namespace istd
399
400
401
402
Helper class used to manage list of many connected in cascade maps.
virtual const ValueType & GetValueAt(int index) const override
Get mapped value at specified index.
virtual int GetElementsCount() const override
Get number of elements.
const ValueType & GetLocalValueAt(int index) const
Get mapped value from local context at specified index.
void ResetLocal()
Removes all elements from local context.
virtual void GetKeys(Keys &result, bool doAppend=false) const override
Get list of keys stored in this map.
bool InsertLocal(const KeyType &key, const ValueType &value)
Insert element in local context.
virtual int FindIndex(const KeyType &key) const override
Find index index of specified key.
void SetParent(const TIMap< Key, Value > *parentPtr)
Set instance of parent map.
const ValueType * FindLocalElement(const KeyType &key) const
Find value element associated with specified key using local context only.
virtual const ValueType * FindElement(const KeyType &key) const override
Find value element associated with specified key.
int GetLocalElementsCount() const
Get number of elements in local context.
virtual const KeyType & GetKeyAt(int index) const override
Get key value at specified index.
virtual ValueType & operator[](const KeyType &key) override
Element access operator.
const KeyType & GetLocalKeyAt(int index) const
Get key value from local context at specified index.
void GetLocalKeys(Keys &result, bool doAppend=false) const
Get list of local keys stored in this map.
int FindLocalIndex(const KeyType &key) const
Find index index of specified key using local context only.
const TCascadedMap< Key, Value > * GetParent() const
Get access to parent map instance.
TCascadedMap()
Default constructor.
Generic interface for a key/value mapping.
Definition TIMap.h:21
QSet< KeyType > Keys
Definition TIMap.h:25
Value ValueType
Definition TIMap.h:24
Key KeyType
Definition TIMap.h:23
#define NULL
Definition istd.h:74
Standard library.
Definition IComponent.h:17