Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef IndexedMapHPP
00033 #define IndexedMapHPP
00034
00035 #include <zeusbase/System/Iterators.hpp>
00036 #include <zeusbase/System/Map.hpp>
00037 #include <zeusbase/System/SingleLinkedList.hpp>
00038 #include <zeusbase/System/Interfaces/IIndexedMap.hpp>
00039
00040 BEGIN_NAMESPACE_Zeus
00041
00042
00048
00049 template <class TKeyType, class TValueType> class TIndexedMap : public IIndexedMap<TKeyType, TValueType>
00050 {
00051 public:
00052
00055
00056 inline TIndexedMap()
00057 {
00058 }
00059
00060
00063
00064 inline TIndexedMap(const TIndexedMap<TKeyType, TValueType>& rMappedList)
00065 {
00066 m_map = rMappedList.m_map;
00067 m_lstValues = rMappedList.m_lstValues;
00068 m_lstKeys = rMappedList.m_lstKeys;
00069 }
00070
00071
00074
00075 inline TIndexedMap(const IMap<TKeyType, TValueType>& rMap)
00076 {
00077 TConstMapIterator<TKeyType, TValueType> ItMap = rMap.getConstIterator();
00078 while (ItMap.hasNextItem())
00079 {
00080 TKeyType key;
00081 const TValueType& rItem = ItMap.getNextItemWithKeyConst(key);
00082 setItem(key, rItem);
00083 }
00084 }
00085
00086
00089
00090 virtual ~TIndexedMap()
00091 {
00092 }
00093
00094
00095
00096
00097
00100
00101 inline virtual void MQUALIFIER clear()
00102 {
00103 m_map.clear();
00104 m_lstValues.clear();
00105 m_lstKeys.clear();
00106 }
00107
00108
00111
00112 inline virtual void MQUALIFIER copyToMap(IMap<TKeyType, TValueType>& rMap) const
00113 {
00114 m_map.copyToMap(rMap);
00115 }
00116
00117
00120
00121 inline virtual void MQUALIFIER copyValuesToList(IList<TValueType>& rList) const
00122 {
00123 m_map.copyValuesToList(rList);
00124 }
00125
00126
00129
00130 inline virtual void MQUALIFIER copyKeysToList(IList<TKeyType>& rList) const
00131 {
00132 m_map.copyKeysToList(rList);
00133 }
00134
00135
00138
00139 inline virtual bool MQUALIFIER deleteItem(const TKeyType& rKey)
00140 {
00141 Int iIndex = getIndexByKey(rKey);
00142 m_lstKeys.deleteItem(iIndex);
00143 m_lstValues.deleteItem(iIndex);
00144 return m_map.deleteItem(rKey);
00145 }
00146
00147
00150
00151 inline virtual bool MQUALIFIER equals(const IMap<TKeyType, TValueType>& rMap) const
00152 {
00153 return m_map.equals(rMap);
00154 }
00155
00156
00159
00160 inline virtual Int MQUALIFIER getCount() const
00161 {
00162 return m_map.getCount();
00163 }
00164
00165
00168
00169 inline virtual TValueType& MQUALIFIER getItem(const TKeyType& rKey)
00170 {
00171 return m_map.getItem(rKey);
00172 }
00173
00174
00177
00178 inline virtual const TValueType& MQUALIFIER getItemConst(const TKeyType& rKey) const
00179 {
00180 return m_map.getItemConst(rKey);
00181 }
00182
00183
00186
00187 inline virtual TValueType& MQUALIFIER getOrCreateItem(const TKeyType& rKey)
00188 {
00189 if (!m_map.hasItem(rKey))
00190 {
00191 m_lstValues.add(m_map.getOrCreateItem(rKey));;
00192 m_lstKeys.add(rKey);
00193 }
00194
00195 return m_map.getItem(rKey);
00196 }
00197
00198
00201
00202 inline virtual const IMapIterator<TKeyType, TValueType>* MQUALIFIER getConstIterator() const
00203 {
00204 return m_map.getConstIterator();
00205 }
00206
00207
00210
00211 inline virtual const IListIterator<TKeyType>* MQUALIFIER getConstKeyIterator() const
00212 {
00213 return m_lstKeys.getConstIterator();
00214 }
00215
00216
00219
00220 inline virtual const IListIterator<TValueType>* MQUALIFIER getConstValueIterator() const
00221 {
00222 return m_lstValues.getConstIterator();
00223 }
00224
00225
00228
00229 virtual bool MQUALIFIER hasItem(const TKeyType& rKey) const
00230 {
00231 return m_map.hasItem(rKey);
00232 }
00233
00234
00237
00238 inline virtual bool MQUALIFIER isEmpty() const
00239 {
00240 return m_map.isEmpty();
00241 }
00242
00243
00246
00247 virtual inline void MQUALIFIER releaseIterator(const IListIterator<TValueType>* pIterator) const
00248 {
00249 m_map.releaseIterator(pIterator);
00250 m_lstValues.releaseIterator(pIterator);
00251 }
00252
00253
00256
00257 virtual inline void MQUALIFIER releaseKeyIterator(const IListIterator<TKeyType>* pIterator) const
00258 {
00259 m_lstKeys.releaseIterator(pIterator);
00260 }
00261
00262
00265
00266 inline virtual void MQUALIFIER setItem(const TKeyType& rKey, const TValueType& rValue)
00267 {
00268 if (m_map.hasItem(rKey))
00269 {
00270 Int iIndex = getIndexByKey(rKey);
00271 m_lstValues[iIndex] = rValue;
00272 }
00273 else
00274 {
00275 m_lstValues.add(rValue);
00276 m_lstKeys.add(rKey);
00277 }
00278
00279 m_map.setItem(rKey, rValue);
00280 }
00281
00282
00283
00284
00285
00288
00289 inline virtual void MQUALIFIER copyToIndexedMap(TIndexedMap<TKeyType, TValueType>& rIndexedMap) const
00290 {
00291 TConstIterator<TKeyType> ItKeys = m_lstKeys .getConstIterator();
00292 TConstIterator<TValueType> ItValues = m_lstValues.getConstIterator();
00293
00294 while (ItKeys.hasNextItem())
00295 {
00296 rIndexedMap.setItem(ItKeys .getNextItemConst(),
00297 ItValues.getNextItemConst());
00298 }
00299 }
00300
00301
00304
00305 inline virtual bool MQUALIFIER deleteItemByIndex(Int iIndex)
00306 {
00307 TKeyType key = m_lstKeys.getItem(iIndex);
00308 m_lstValues.deleteItem(iIndex);
00309 m_lstKeys.deleteItem(iIndex);
00310 return m_map.deleteItem(key);
00311 }
00312
00313
00316
00317 inline virtual bool MQUALIFIER equals(const IIndexedMap<TKeyType, TValueType>& rIndexedMap) const
00318 {
00319 return m_map.equals(rIndexedMap.getMapConst());
00320 }
00321
00322
00325
00326 inline virtual bool equals(const TIndexedMap<TKeyType, TValueType>& rIndexedMap) const
00327 {
00328 return m_map.equals(rIndexedMap.getMapConst());
00329 }
00330
00331
00334
00335 inline virtual Int MQUALIFIER getIndexByKey(const TKeyType& rKey)
00336 {
00337 return m_lstKeys.indexOf(rKey);
00338 }
00339
00340
00343
00344 inline virtual const Int MQUALIFIER getIndexByKeyConst(const TKeyType& rKey) const
00345 {
00346 return m_lstKeys.indexOf(rKey);
00347 }
00348
00349
00352
00353 inline virtual TKeyType MQUALIFIER getKeyByIndex(Int iIndex)
00354 {
00355 return m_lstKeys.getItem(iIndex);
00356 }
00357
00358
00361
00362 inline virtual const TKeyType MQUALIFIER getKeyByIndexConst(Int iIndex) const
00363 {
00364 return m_lstKeys.getItemConst(iIndex);
00365 }
00366
00367
00370
00371 inline virtual TValueType& MQUALIFIER getValueByIndex(Int iIndex)
00372 {
00373 return m_lstValues.getItem(iIndex);
00374 }
00375
00376
00379
00380 inline virtual const TValueType& MQUALIFIER getValueByIndexConst(Int iIndex) const
00381 {
00382 return m_lstValues.getItemConst(iIndex);
00383 }
00384
00385
00388
00389 inline virtual const IMap<TKeyType, TValueType>& MQUALIFIER getMapConst() const
00390 {
00391 return m_map;
00392 }
00393
00394
00395
00396
00397
00402
00403 inline virtual TIndexedMap<TKeyType, TValueType>& operator= (const TIndexedMap<TKeyType, TValueType>& rIndexedMap)
00404 {
00405
00406 clear();
00407
00408
00409 TConstMapIterator<TKeyType, TValueType> ItMap = rIndexedMap.getConstIterator();
00410
00411 while (ItMap.hasNextItem())
00412 {
00413 TKeyType key;
00414 const TValueType& rItem = ItMap.getNextItemWithKeyConst(key);
00415 setItem(key, rItem);
00416 }
00417
00418 return *this;
00419 }
00420
00421
00426
00427 inline virtual TIndexedMap<TKeyType, TValueType>& operator= (const IMap<TKeyType, TValueType>& rMap)
00428 {
00429
00430 clear();
00431
00432
00433 TConstMapIterator<TKeyType, TValueType> ItMap = rMap.getConstIterator();
00434
00435 while (ItMap.hasNextItem())
00436 {
00437 TKeyType key;
00438 const TValueType& rItem = ItMap.getNextItemWithKeyConst(key);
00439 setItem(key, rItem);
00440 }
00441
00442 return *this;
00443 }
00444
00445
00448
00449 inline virtual bool operator== (const TIndexedMap<TKeyType, TValueType>& rIndexedMap) const
00450 {
00451 return equals(rIndexedMap);
00452 }
00453
00454
00457
00458 inline virtual bool operator== (const IIndexedMap<TKeyType, TValueType>& rIndexedMap) const
00459 {
00460 return equals(rIndexedMap);
00461 }
00462
00463
00466
00467 inline virtual bool operator== (const IMap<TKeyType, TValueType>& rMap) const
00468 {
00469 return equals(rMap);
00470 }
00471
00472
00475
00476 inline virtual bool operator!= (const TIndexedMap<TKeyType, TValueType>& rIndexedMap) const
00477 {
00478 return !equals(rIndexedMap);
00479 }
00480
00481
00484
00485 inline virtual bool operator!= (const IIndexedMap<TKeyType, TValueType>& rIndexedMap) const
00486 {
00487 return !equals(rIndexedMap);
00488 }
00489
00490
00493
00494 inline virtual bool operator!= (const IMap<TKeyType, TValueType>& rMap) const
00495 {
00496 return !equals(rMap);
00497 }
00498
00499 protected:
00500
00509
00510 inline virtual IMapIterator<TKeyType, TValueType>* MQUALIFIER getIterator()
00511 {
00512 return NULL;
00513 }
00514
00515 private:
00517 TMap<TKeyType, TValueType> m_map;
00518
00520 TSingleLinkedList<TValueType> m_lstValues;
00521
00523 TSingleLinkedList<TKeyType> m_lstKeys;
00524 };
00525
00526
00527 END_NAMESPACE_Zeus
00528
00529 #endif
00530