|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Map implementation inlines. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 template <class Tkey, class Tvalue> |
|
20 inline RMap<Tkey, Tvalue>::RMap(const TLinearOrder<Tkey>& anOrder) |
|
21 :iOrder(anOrder) |
|
22 { |
|
23 } |
|
24 |
|
25 template <class Tkey, class Tvalue> |
|
26 inline void RMap<Tkey, Tvalue>::ResetAndDestroy() |
|
27 { |
|
28 iKeys.ResetAndDestroy(); |
|
29 iValues.ResetAndDestroy(); |
|
30 } |
|
31 |
|
32 template <class Tkey, class Tvalue> |
|
33 inline void RMap<Tkey, Tvalue>::Close() |
|
34 { |
|
35 // ResetAndDestroy(); |
|
36 iKeys.Close(); |
|
37 iValues.Close(); |
|
38 } |
|
39 |
|
40 template <class Tkey, class Tvalue> |
|
41 inline void RMap<Tkey, Tvalue>::InsertL(const Tkey* aKey, const Tvalue* aValue) |
|
42 { |
|
43 TInt pos = Find(*aKey); |
|
44 if (pos == KErrNotFound) |
|
45 { |
|
46 User::LeaveIfError(iKeys.InsertInOrder(aKey, iOrder)); |
|
47 pos = Find(*aKey); |
|
48 User::LeaveIfError(iValues.Insert(aValue, pos)); |
|
49 } else |
|
50 { |
|
51 User::LeaveIfError(iKeys.InsertInOrderAllowRepeats(aKey, iOrder)); |
|
52 |
|
53 while (*(iKeys[pos]) == *aKey) |
|
54 { |
|
55 pos++; |
|
56 } |
|
57 |
|
58 //our position should be |
|
59 pos--; |
|
60 |
|
61 User::LeaveIfError(iValues.Insert(aValue, pos)); |
|
62 } |
|
63 } |
|
64 |
|
65 template <class Tkey, class Tvalue> |
|
66 inline const Tvalue* RMap<Tkey, Tvalue>::InsertReplaceL(const Tkey* aKey, const Tvalue* aValue) |
|
67 { |
|
68 TInt pos = Find(*aKey); |
|
69 if (pos == KErrNotFound) |
|
70 { |
|
71 User::LeaveIfError(iKeys.InsertInOrder(aKey, iOrder)); |
|
72 pos = Find(*aKey); |
|
73 User::LeaveIfError(iValues.Insert(aValue, pos)); |
|
74 return aValue; |
|
75 } else |
|
76 { |
|
77 Tvalue* v = iValues[pos]; |
|
78 iValues[pos] = const_cast<Tvalue*>(aValue); |
|
79 return v; |
|
80 } |
|
81 } |
|
82 |
|
83 template <class Tkey, class Tvalue> |
|
84 inline TInt RMap<Tkey, Tvalue>::Find(const Tkey& aKey) const |
|
85 { |
|
86 TInt pos = KErrNotFound; |
|
87 for (TInt i = 0; i < iKeys.Count(); i++) |
|
88 { |
|
89 if (*(iKeys[i]) == aKey) |
|
90 { |
|
91 pos = i; |
|
92 break; |
|
93 } |
|
94 } |
|
95 |
|
96 return pos; |
|
97 } |
|
98 |
|
99 |
|
100 template <class Tkey, class Tvalue> |
|
101 inline TBool RMap<Tkey, Tvalue>::ContainsKey(const Tkey& aKey) const |
|
102 { |
|
103 return (Find(aKey) != KErrNotFound); |
|
104 } |
|
105 |
|
106 template <class Tkey, class Tvalue> |
|
107 inline TBool RMap<Tkey, Tvalue>::ContainsValue(const Tvalue& aValue) const |
|
108 { |
|
109 TInt pos = KErrNotFound; |
|
110 for (TInt i = 0; i < iValues.Count(); i++) |
|
111 { |
|
112 if (*(iValues[i]) == aValue) |
|
113 { |
|
114 pos = i; |
|
115 break; |
|
116 } |
|
117 } |
|
118 return (pos != KErrNotFound); |
|
119 } |
|
120 |
|
121 |
|
122 template <class Tkey, class Tvalue> |
|
123 inline Tvalue& RMap<Tkey, Tvalue>::GetValueL(const Tkey& aKey) const |
|
124 { |
|
125 TInt pos = Find(aKey); |
|
126 |
|
127 if (pos == KErrNotFound) |
|
128 { |
|
129 User::Leave(KErrNotFound); |
|
130 } |
|
131 |
|
132 return *(iValues[pos]); |
|
133 } |
|
134 |
|
135 template <class Tkey, class Tvalue> |
|
136 inline Tkey& RMap<Tkey, Tvalue>::GetKeyL(const Tvalue& aValue) const |
|
137 { |
|
138 TInt pos = KErrNotFound; |
|
139 for (TInt i = 0; i < iValues.Count(); i++) |
|
140 { |
|
141 if (*(iValues[i]) == aValue) |
|
142 { |
|
143 pos = i; |
|
144 break; |
|
145 } |
|
146 } |
|
147 |
|
148 if (pos == KErrNotFound) |
|
149 { |
|
150 User::Leave(KErrNotFound); |
|
151 } |
|
152 |
|
153 return *(iKeys[pos]); |
|
154 } |
|
155 |
|
156 |
|
157 template <class Tkey, class Tvalue> |
|
158 inline void RMap<Tkey, Tvalue>::RemoveL(const Tkey& aKey) |
|
159 { |
|
160 TInt pos = Find(aKey); |
|
161 if (pos == KErrNotFound) |
|
162 { |
|
163 User::Leave(KErrNotFound); |
|
164 } |
|
165 |
|
166 iKeys.Remove(pos); |
|
167 iValues.Remove(pos); |
|
168 iKeys.Compress(); |
|
169 iValues.Compress(); |
|
170 } |
|
171 |
|
172 template <class Tkey, class Tvalue> |
|
173 inline void RMap<Tkey, Tvalue>::RemoveAtL(TInt anIndex, Tkey*& aKey, Tvalue*& aValue) |
|
174 { |
|
175 if (anIndex >= iKeys.Count()) |
|
176 { |
|
177 User::Leave(KErrNotFound); |
|
178 } |
|
179 |
|
180 *aKey = *(iKeys[anIndex]); |
|
181 *aValue = *(iValues[anIndex]); |
|
182 |
|
183 iKeys.Remove(anIndex); |
|
184 iValues.Remove(anIndex); |
|
185 iKeys.Compress(); |
|
186 iValues.Compress(); |
|
187 } |
|
188 |
|
189 template <class Tkey, class Tvalue> |
|
190 inline RSafePointerArray<Tkey>& RMap<Tkey, Tvalue>::Keys() |
|
191 { |
|
192 return iKeys; |
|
193 } |
|
194 |
|
195 template <class Tkey, class Tvalue> |
|
196 inline RSafePointerArray<Tvalue>& RMap<Tkey, Tvalue>::Values() |
|
197 { |
|
198 return iValues; |
|
199 } |
|
200 //key by index |
|
201 template <class Tkey, class Tvalue> |
|
202 inline Tkey& RMap<Tkey, Tvalue>::KeyAt(TInt anIndex) |
|
203 { |
|
204 return *(iKeys[anIndex]); |
|
205 } |
|
206 |
|
207 template <class Tkey, class Tvalue> |
|
208 inline const Tkey& RMap<Tkey, Tvalue>::KeyAt(TInt anIndex) const |
|
209 { |
|
210 return *(iKeys[anIndex]); |
|
211 } |
|
212 //value by index |
|
213 template <class Tkey, class Tvalue> |
|
214 inline Tvalue& RMap<Tkey, Tvalue>::ValueAt(TInt anIndex) |
|
215 { |
|
216 return *(iValues[anIndex]); |
|
217 } |
|
218 |
|
219 template <class Tkey, class Tvalue> |
|
220 inline const Tvalue& RMap<Tkey, Tvalue>::ValueAt(TInt anIndex) const |
|
221 { |
|
222 return *(iValues[anIndex]); |
|
223 } |
|
224 |
|
225 template <class Tkey, class Tvalue> |
|
226 inline TInt RMap<Tkey, Tvalue>::Count() const |
|
227 { |
|
228 return iKeys.Count(); |
|
229 } |
|
230 |
|
231 /* |
|
232 //for test purposes |
|
233 class RDesMap: public RMap<TDesC, TDesC> |
|
234 { |
|
235 public: |
|
236 RDesMap(const TLinearOrder<TDesC>& anOrder) |
|
237 :RMap(anOrder) |
|
238 { |
|
239 } |
|
240 |
|
241 void ResetAndDestroy() |
|
242 { |
|
243 RMap::ResetAndDestroy(); |
|
244 } |
|
245 |
|
246 void InsertL(const TDesC* aKey, const TDesC* aValue) |
|
247 { |
|
248 RMap::InsertL(aKey, aValue); |
|
249 } |
|
250 |
|
251 const TDesC* InsertReplaceL(const TDesC* aKey, const TDesC* aValue) |
|
252 { |
|
253 return RMap::InsertReplaceL(aKey, aValue); |
|
254 } |
|
255 |
|
256 TInt Find(const TDesC& aKey) |
|
257 { |
|
258 return RMap::Find(aKey); |
|
259 } |
|
260 |
|
261 TBool ContainsKey(const TDesC& aKey) |
|
262 { |
|
263 return RMap::ContainsKey(aKey); |
|
264 } |
|
265 |
|
266 void GetValueL(const TDesC& aKey) |
|
267 { |
|
268 RMap::GetValueL(aKey); |
|
269 } |
|
270 |
|
271 void GetKeyL(const TDesC& aValue) |
|
272 { |
|
273 RMap::GetKeyL(aValue); |
|
274 } |
|
275 |
|
276 void RemoveL(const TDesC& aKey) |
|
277 { |
|
278 RMap::RemoveL(aKey); |
|
279 } |
|
280 |
|
281 RSafePointerArray<TDesC>& Keys() |
|
282 { |
|
283 return RMap::Keys(); |
|
284 } |
|
285 |
|
286 RSafePointerArray<TDesC>& Values() |
|
287 { |
|
288 return RMap::Values(); |
|
289 } |
|
290 |
|
291 TDesC& KeyAt(TInt anIndex) |
|
292 { |
|
293 return RMap::KeyAt(anIndex); |
|
294 } |
|
295 |
|
296 const TDesC& KeyAt(TInt anIndex) const |
|
297 { |
|
298 return RMap::KeyAt(anIndex); |
|
299 } |
|
300 |
|
301 TDesC& ValueAt(TInt anIndex) |
|
302 { |
|
303 return RMap::ValueAt(anIndex); |
|
304 } |
|
305 |
|
306 const TDesC& ValueAt(TInt anIndex) const |
|
307 { |
|
308 return RMap::ValueAt(anIndex); |
|
309 } |
|
310 |
|
311 TInt Count() |
|
312 { |
|
313 return RMap::Count(); |
|
314 } |
|
315 |
|
316 static void InsertEntryL(RDesMap& aMap, const TDesC& aKey,const TDesC& aValue) |
|
317 { |
|
318 HBufC* k = HBufC::NewL(aKey.Length()); |
|
319 *k = aKey; |
|
320 HBufC* v = HBufC::NewL(aValue.Length()); |
|
321 *v = aValue; |
|
322 |
|
323 aMap.InsertL(k, v); |
|
324 //MGLOG2(*k, *v); |
|
325 } |
|
326 |
|
327 static void InsertEntryReplaceL(RDesMap& aMap, const TDesC& aKey,const TDesC& aValue) |
|
328 { |
|
329 HBufC* k = HBufC::NewL(aKey.Length()); |
|
330 *k = aKey; |
|
331 HBufC* v = HBufC::NewL(aValue.Length()); |
|
332 *v = aValue; |
|
333 |
|
334 const HBufC* result = static_cast<const HBufC*>(aMap.InsertReplaceL(k, v)); |
|
335 if (result != v) |
|
336 { |
|
337 //MGLOG2(_L("Deleted"), *result); |
|
338 delete result; |
|
339 } |
|
340 |
|
341 //MGLOG2(*k, *v); |
|
342 } |
|
343 |
|
344 static void DoTest() |
|
345 { |
|
346 TLinearOrder<TDesC> order(RDesMap::CompareAlphabetDes); |
|
347 RDesMap map(order); |
|
348 InsertEntryL(map, _L("bbb"), _L("My name is Mud")); |
|
349 InsertEntryL(map, _L("aaa"), _L("Hello big brother")); |
|
350 InsertEntryL(map, _L("aaa"), _L("Hello big brother 1")); |
|
351 InsertEntryL(map, _L("ddd"), _L("Ala bala nica")); |
|
352 InsertEntryL(map, _L("aaa"), _L("Hello big brother 2")); |
|
353 InsertEntryL(map, _L("cc"), _L("Ala bala")); |
|
354 InsertEntryL(map, _L("x"), _L("Ala bala nica turska panica")); |
|
355 InsertEntryL(map, _L("fff"), _L("Ala bala nica turska")); |
|
356 |
|
357 //MGLOG1(_L("")); //new line |
|
358 |
|
359 for (TInt i = 0; i<map.Count(); i++) |
|
360 { |
|
361 //MGLOG2(*(map.Keys()[i]), *(map.Values()[i])); |
|
362 } |
|
363 |
|
364 map.ResetAndDestroy(); |
|
365 |
|
366 //MGLOG1(_L("")); //new line |
|
367 InsertEntryReplaceL(map, _L("bbb"), _L("My name is Mud")); |
|
368 InsertEntryReplaceL(map, _L("aaa"), _L("Hello big brother")); |
|
369 InsertEntryReplaceL(map, _L("aaa"), _L("Hello big brother 1")); |
|
370 InsertEntryReplaceL(map, _L("ddd"), _L("Ala bala nica")); |
|
371 InsertEntryReplaceL(map, _L("aaa"), _L("Hello big brother 2")); |
|
372 InsertEntryReplaceL(map, _L("cc"), _L("Ala bala")); |
|
373 InsertEntryReplaceL(map, _L("x"), _L("Ala bala nica turska panica")); |
|
374 InsertEntryReplaceL(map, _L("fff"), _L("Ala bala nica turska")); |
|
375 |
|
376 //MGLOG1(_L("")); //new line |
|
377 |
|
378 for (TInt i = 0; i<map.Count(); i++) |
|
379 { |
|
380 //MGLOG2(*(map.Keys()[i]), *(map.Values()[i])); |
|
381 } |
|
382 } |
|
383 |
|
384 }; |
|
385 |
|
386 |
|
387 //for test purposes |
|
388 class RIntMap: public RMap<TInt64, TInt64> |
|
389 { |
|
390 public: |
|
391 RIntMap(const TLinearOrder<TInt64>& anOrder) |
|
392 :RMap(anOrder) |
|
393 { |
|
394 } |
|
395 |
|
396 void ResetAndDestroy() |
|
397 { |
|
398 RMap::ResetAndDestroy(); |
|
399 } |
|
400 |
|
401 void InsertL(const TInt64* aKey, const TInt64* aValue) |
|
402 { |
|
403 RMap::InsertL(aKey, aValue); |
|
404 } |
|
405 |
|
406 const TInt64* InsertReplaceL(const TInt64* aKey, const TInt64* aValue) |
|
407 { |
|
408 return RMap::InsertReplaceL(aKey, aValue); |
|
409 } |
|
410 |
|
411 TInt Find(const TInt64& aKey) |
|
412 { |
|
413 return RMap::Find(aKey); |
|
414 } |
|
415 |
|
416 TBool ContainsKey(const TInt64& aKey) |
|
417 { |
|
418 return RMap::ContainsKey(aKey); |
|
419 } |
|
420 |
|
421 TInt64 GetValueL(const TInt64& aKey) |
|
422 { |
|
423 return RMap::GetValueL(aKey); |
|
424 } |
|
425 |
|
426 TInt64 GetKeyL(const TInt64& aValue) |
|
427 { |
|
428 return RMap::GetKeyL(aValue); |
|
429 } |
|
430 |
|
431 void RemoveL(const TInt64& aKey) |
|
432 { |
|
433 RMap::RemoveL(aKey); |
|
434 } |
|
435 |
|
436 RSafePointerArray<TInt64>& Keys() |
|
437 { |
|
438 return RMap::Keys(); |
|
439 } |
|
440 |
|
441 RSafePointerArray<TInt64>& Values() |
|
442 { |
|
443 return RMap::Values(); |
|
444 } |
|
445 |
|
446 TInt64& KeyAt(TInt anIndex) |
|
447 { |
|
448 return RMap::KeyAt(anIndex); |
|
449 } |
|
450 |
|
451 const TInt64& KeyAt(TInt anIndex) const |
|
452 { |
|
453 return RMap::KeyAt(anIndex); |
|
454 } |
|
455 |
|
456 TInt64& ValueAt(TInt anIndex) |
|
457 { |
|
458 return RMap::ValueAt(anIndex); |
|
459 } |
|
460 |
|
461 const TInt64& ValueAt(TInt anIndex) const |
|
462 { |
|
463 return RMap::ValueAt(anIndex); |
|
464 } |
|
465 |
|
466 TInt Count() |
|
467 { |
|
468 return RMap::Count(); |
|
469 } |
|
470 |
|
471 static void InsertEntryL(RIntMap& aMap, const TInt aKey,const TInt aValue) |
|
472 { |
|
473 TBuf<16> k; |
|
474 k.Num(aKey); |
|
475 |
|
476 TBuf<16> v; |
|
477 v.Num(aValue); |
|
478 |
|
479 TInt64* k64 = new TInt64; |
|
480 *k64 = aKey; |
|
481 TInt64* v64 = new TInt64; |
|
482 *v64 = aValue; |
|
483 |
|
484 aMap.InsertL(k64, v64); |
|
485 //MGLOG2(k, v); |
|
486 } |
|
487 |
|
488 static void InsertEntryReplaceL(RIntMap& aMap, const TInt aKey,const TInt aValue) |
|
489 { |
|
490 TBuf<16> k; |
|
491 k.Num(aKey); |
|
492 |
|
493 TBuf<16> v; |
|
494 v.Num(aValue); |
|
495 |
|
496 TInt64* k64 = new TInt64; |
|
497 *k64 = aKey; |
|
498 TInt64* v64 = new TInt64; |
|
499 *v64 = aValue; |
|
500 |
|
501 const TInt64* result = static_cast<const TInt64*>(aMap.InsertReplaceL(k64, v64)); |
|
502 if (result != v64) |
|
503 { |
|
504 //MGLOG2(_L("Deleted"), (*result).GetTInt()); |
|
505 delete result; |
|
506 } |
|
507 |
|
508 //MGLOG2(k, v); |
|
509 } |
|
510 |
|
511 static void DoTest() |
|
512 { |
|
513 TLinearOrder<TInt64> order(RMap::CompareInt); |
|
514 RIntMap map(order); |
|
515 InsertEntryL(map, 2, 325); |
|
516 InsertEntryL(map, 1, 226); |
|
517 InsertEntryL(map, 1, 227); |
|
518 InsertEntryL(map, 4, 6623); |
|
519 InsertEntryL(map, 1, 228); |
|
520 InsertEntryL(map, 3, 6534); |
|
521 InsertEntryL(map, 9, 7443); |
|
522 InsertEntryL(map, 7, 8643); |
|
523 |
|
524 //MGLOG1(_L("")); //new line |
|
525 |
|
526 for (TInt i = 0; i<map.Count(); i++) |
|
527 { |
|
528 TBuf<16> k; |
|
529 k.Num(*(map.Keys()[i])); |
|
530 |
|
531 TBuf<16> v; |
|
532 v.Num(*(map.Values()[i])); |
|
533 //MGLOG2(k, v); |
|
534 } |
|
535 |
|
536 map.ResetAndDestroy(); |
|
537 |
|
538 //MGLOG1(_L("")); //new line |
|
539 InsertEntryReplaceL(map, 2, 325); |
|
540 InsertEntryReplaceL(map, 1, 226); |
|
541 InsertEntryReplaceL(map, 1, 227); |
|
542 InsertEntryReplaceL(map, 4, 6623); |
|
543 InsertEntryReplaceL(map, 1, 228); |
|
544 InsertEntryReplaceL(map, 3, 6534); |
|
545 InsertEntryReplaceL(map, 9, 7443); |
|
546 InsertEntryReplaceL(map, 7, 8643); |
|
547 |
|
548 //MGLOG1(_L("")); //new line |
|
549 |
|
550 for (TInt i = 0; i<map.Count(); i++) |
|
551 { |
|
552 TBuf<16> k; |
|
553 k.Num(*(map.Keys()[i])); |
|
554 |
|
555 TBuf<16> v; |
|
556 v.Num(*(map.Values()[i])); |
|
557 //MGLOG2(k, v); |
|
558 } |
|
559 } |
|
560 |
|
561 }; |
|
562 */ |