TCascadedMap.h
Go to the documentation of this file.
1 #ifndef istd_TCascadedMap_included
2 #define istd_TCascadedMap_included
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 
14 namespace istd
15 {
16 
17 
24 template <typename Key, typename Value>
25 class TCascadedMap: virtual public TIMap<Key, Value>
26 {
27 public:
31  TCascadedMap();
35  explicit TCascadedMap(const TCascadedMap<Key, Value>* parentPtr);
39  TCascadedMap(const TCascadedMap& map);
40 
44  const TCascadedMap<Key, Value>* GetParent() const;
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 
66  ValueType* FindLocalElement(const KeyType& key);
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;
110  virtual ValueType& operator[](const KeyType& key);
111  virtual const ValueType& operator[](const KeyType& key) const;
112  virtual int FindIndex(const KeyType& key) const;
113  virtual const ValueType* FindElement(const KeyType& key) const;
114  virtual void GetKeys(Keys& result, bool doAppend = false) const;
115  virtual const KeyType& GetKeyAt(int index) const;
116  virtual const ValueType& GetValueAt(int index) const;
117 
118  // TODO: add element removing and correct comment in class header.
119 
120 private:
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 
134 template <typename Key, typename Value>
136 : m_parentPtr(NULL)
137 {
138 }
139 
140 
141 template <typename Key, typename Value>
143 : m_parentPtr(parentPtr)
144 {
145 }
146 
147 
148 template <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 
157 template <typename Key, typename Value>
159 {
160  return m_parentPtr;
161 }
162 
163 
164 template <typename Key, typename Value>
166 {
167  m_parentPtr = parentPtr;
168 }
169 
170 
171 template <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 
183 template <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 
195 template <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 
207 template <typename Key, typename Value>
209 {
210  Q_ASSERT(index < int(m_pairList.size()));
211 
212  return m_pairList[index].first;
213 }
214 
215 
216 template <typename Key, typename Value>
218 {
219  Q_ASSERT(index < int(m_pairList.size()));
220 
221  return m_pairList[index].first;
222 }
223 
224 
225 template <typename Key, typename Value>
227 {
228  Q_ASSERT(index < int(m_pairList.size()));
229 
230  return m_pairList[index].second;
231 }
232 
233 
234 template <typename Key, typename Value>
236 {
237  Q_ASSERT(index < int(m_pairList.size()));
238 
239  return m_pairList[index].second;
240 }
241 
242 
243 template <typename Key, typename Value>
245 {
246  return int(m_pairList.size());
247 }
248 
249 
250 template <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 
267 template <typename Key, typename Value>
269 {
270  m_positionsMap.clear();
271  m_pairList.clear();
272 }
273 
274 
275 template <typename Key, typename Value>
276 void 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 
292 template <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 
304 template <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 
321 template <typename Key, typename Value>
323 {
324  return const_cast<TCascadedMap*>(this)->operator[](key);
325 }
326 
327 
328 template <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 
347 template <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 
359 template <typename Key, typename Value>
360 void 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 
370 template <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 
384 template <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 #endif // !istd_TCascadedMap_included
402 
403 
virtual const KeyType & GetKeyAt(int index) const
Get key value at specified index.
Definition: TCascadedMap.h:371
Value ValueType
Definition: TIMap.h:24
const TCascadedMap< Key, Value > * GetParent() const
Get access to parent map instance.
Definition: TCascadedMap.h:158
virtual void GetKeys(Keys &result, bool doAppend=false) const
Get list of keys stored in this map.
Definition: TCascadedMap.h:360
void SetParent(const TIMap< Key, Value > *parentPtr)
Set instance of parent map.
Definition: TCascadedMap.h:165
Generic interface for a key/value mapping.
Definition: TIMap.h:20
const KeyType & GetLocalKeyAt(int index) const
Get key value from local context at specified index.
Definition: TCascadedMap.h:208
virtual int FindIndex(const KeyType &key) const
Find index index of specified key.
Definition: TCascadedMap.h:329
virtual ValueType & operator[](const KeyType &key)
Element access operator.
Definition: TCascadedMap.h:305
const ValueType * FindLocalElement(const KeyType &key) const
Find value element associated with specified key using local context only.
Definition: TCascadedMap.h:184
Helper class used to manage list of many connected in cascade maps.
Definition: TCascadedMap.h:25
TCascadedMap()
Default constructor.
Definition: TCascadedMap.h:135
virtual int GetElementsCount() const
Get number of elements.
Definition: TCascadedMap.h:293
Key KeyType
Definition: TIMap.h:23
virtual const ValueType * FindElement(const KeyType &key) const
Find value element associated with specified key.
Definition: TCascadedMap.h:348
void ResetLocal()
Removes all elements from local context.
Definition: TCascadedMap.h:268
const ValueType & GetLocalValueAt(int index) const
Get mapped value from local context at specified index.
Definition: TCascadedMap.h:235
int GetLocalElementsCount() const
Get number of elements in local context.
Definition: TCascadedMap.h:244
#define NULL
Definition: istd.h:64
bool InsertLocal(const KeyType &key, const ValueType &value)
Insert element in local context.
Definition: TCascadedMap.h:251
int FindLocalIndex(const KeyType &key) const
Find index index of specified key using local context only.
Definition: TCascadedMap.h:172
virtual const ValueType & GetValueAt(int index) const
Get mapped value at specified index.
Definition: TCascadedMap.h:385
void GetLocalKeys(Keys &result, bool doAppend=false) const
Get list of local keys stored in this map.
Definition: TCascadedMap.h:276
QSet< KeyType > Keys
Definition: TIMap.h:25

© 2007-2017 Witold Gantzke and Kirill Lepskiy