7#include <QtCore/QVector>
24template <
typename Key,
typename Value>
114 virtual void GetKeys(
Keys& result,
bool doAppend =
false)
const override;
123 typedef QMap<KeyType, int> IndicesMap;
124 typedef QPair<KeyType, ValueType> Pair;
125 typedef QVector<Pair> PairList;
127 mutable IndicesMap m_positionsMap;
128 mutable PairList m_pairList;
134template <
typename Key,
typename Value>
141template <
typename Key,
typename Value>
143: m_parentPtr(parentPtr)
148template <
typename Key,
typename Value>
150: m_parentPtr(map.m_parentPtr),
151 m_positionsMap(map.m_positionsMap),
152 m_pairList(map.m_pairList)
157template <
typename Key,
typename Value>
164template <
typename Key,
typename Value>
167 m_parentPtr = parentPtr;
171template <
typename Key,
typename Value>
174 typename IndicesMap::ConstIterator iter = m_positionsMap.constFind(key);
175 if (iter != m_positionsMap.constEnd()){
183template <
typename Key,
typename Value>
186 int index = FindLocalIndex(key);
188 return &GetLocalValueAt(index);
195template <
typename Key,
typename Value>
198 int index = FindLocalIndex(key);
200 return &GetLocalValueAt(index);
207template <
typename Key,
typename Value>
210 Q_ASSERT(index <
int(m_pairList.size()));
212 return m_pairList[index].first;
216template <
typename Key,
typename Value>
219 Q_ASSERT(index <
int(m_pairList.size()));
221 return m_pairList[index].first;
225template <
typename Key,
typename Value>
228 Q_ASSERT(index <
int(m_pairList.size()));
230 return m_pairList[index].second;
234template <
typename Key,
typename Value>
237 Q_ASSERT(index <
int(m_pairList.size()));
239 return m_pairList[index].second;
243template <
typename Key,
typename Value>
246 return int(m_pairList.size());
250template <
typename Key,
typename Value>
253 typename IndicesMap::ConstIterator iter = m_positionsMap.constFind(key);
254 if (iter != m_positionsMap.constEnd()){
258 int newIndex = int(m_pairList.size());
259 m_positionsMap[key] = newIndex;
261 m_pairList.push_back(Pair(key, value));
267template <
typename Key,
typename Value>
270 m_positionsMap.clear();
275template <
typename Key,
typename Value>
282 for (
typename IndicesMap::const_iterator iter = m_positionsMap.begin();
283 iter != m_positionsMap.end();
285 result.insert(iter.key());
292template <
typename Key,
typename Value>
295 if (m_parentPtr !=
NULL){
296 return m_parentPtr->GetElementsCount() + GetLocalElementsCount();
299 return GetLocalElementsCount();
304template <
typename Key,
typename Value>
307 typename IndicesMap::ConstIterator iter = m_positionsMap.constFind(key);
308 if (iter != m_positionsMap.constEnd()){
309 return m_pairList[iter.value()].second;
312 int newIndex = int(m_pairList.size());
313 m_positionsMap[key] = newIndex;
315 m_pairList.push_back(Pair(key,
ValueType()));
317 return m_pairList.back().second;
321template <
typename Key,
typename Value>
324 return const_cast<TCascadedMap*
>(
this)->
operator[](key);
328template <
typename Key,
typename Value>
331 typename IndicesMap::ConstIterator iter = m_positionsMap.constFind(key);
332 if (iter != m_positionsMap.constEnd()){
336 if (m_parentPtr !=
NULL){
337 int parentIndex = m_parentPtr->
FindIndex(key);
338 if (parentIndex >= 0){
339 return parentIndex + GetLocalElementsCount();
347template <
typename Key,
typename Value>
350 int index = FindIndex(key);
352 return &GetValueAt(index);
359template <
typename Key,
typename Value>
362 GetLocalKeys(result, doAppend);
364 if (m_parentPtr !=
NULL){
365 m_parentPtr->GetKeys(result,
true);
370template <
typename Key,
typename Value>
373 int elementsCount = GetLocalElementsCount();
374 if (index < elementsCount){
375 return GetLocalKeyAt(index);
378 Q_ASSERT(m_parentPtr !=
NULL);
380 return m_parentPtr->GetKeyAt(index - elementsCount);
384template <
typename Key,
typename Value>
387 int elementsCount = GetLocalElementsCount();
388 if (index < elementsCount){
389 return GetLocalValueAt(index);
392 Q_ASSERT(m_parentPtr !=
NULL);
394 return m_parentPtr->GetValueAt(index - elementsCount);
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.