|
1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // RMap template class implementation. |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 |
|
20 Template function that compares two objects of type TPair<KEY, DATA> and |
|
21 returns {negative, 0, positive} depending on the comparison result. |
|
22 In order the comparison to work, the KEY template parameter has to have |
|
23 "-" operation defined - either native or overloaded "-" operator. |
|
24 Used by RMap<KEY, DATA> template class. |
|
25 Note: This template function works only on objects, which have defined a "-" operation. |
|
26 @param aLeft The first TPair<KEY, DATA> to be compared |
|
27 @param aRight The second TPair<KEY, DATA> to be compared |
|
28 @return An integer value, which is: |
|
29 0, if the objects are equal; |
|
30 negative number, the first object is bigger than the second object |
|
31 positive number, the first object is smaller than the second object |
|
32 @internalComponent |
|
33 */ |
|
34 template <class KEY, class DATA> |
|
35 TInt Compare(const TPair<KEY, DATA>& aLeft, const TPair<KEY, DATA>& aRight) |
|
36 { |
|
37 return aRight.iKey - aLeft.iKey; |
|
38 } |
|
39 |
|
40 /** |
|
41 @param aKey Key part of TPair object |
|
42 @param aData Data part of TPair object |
|
43 */ |
|
44 template <class KEY, class DATA> |
|
45 TPair<KEY, DATA>::TPair(const KEY& aKey, const DATA& aData) : |
|
46 iKey(aKey), |
|
47 iData(aData) |
|
48 { |
|
49 } |
|
50 |
|
51 /** |
|
52 @param aKey Key part of TPair object |
|
53 */ |
|
54 template <class KEY, class DATA> |
|
55 TPair<KEY, DATA>::TPair(const KEY& aKey) : |
|
56 iKey(aKey) |
|
57 { |
|
58 } |
|
59 |
|
60 /** |
|
61 */ |
|
62 template <class KEY, class DATA> |
|
63 TPair<KEY, DATA>::TPair() |
|
64 { |
|
65 } |
|
66 |
|
67 /** |
|
68 @param aOrder An object of TLinearOrder< TPair<KEY, DATA> type, which will be used when |
|
69 new elements are inserted into the collection. |
|
70 */ |
|
71 template <class KEY, class DATA> |
|
72 RMap<KEY, DATA>::RMap(const TLinearOrder< TPair<KEY, DATA> >& aOrder) : |
|
73 iOrder(aOrder) |
|
74 { |
|
75 } |
|
76 |
|
77 /** |
|
78 Closes RMap instance and destroy all RMap data. |
|
79 */ |
|
80 template <class KEY, class DATA> |
|
81 void RMap<KEY, DATA>::Close() |
|
82 { |
|
83 iCol.Close(); |
|
84 } |
|
85 |
|
86 /** |
|
87 This method will create and insert new (KEY, DATA) pair in the map. |
|
88 If the operation fails, the method will return some of system wide error codes. |
|
89 RMap instance does not allow duplicated TPair objects in the collection. |
|
90 Actually it dependes on the supplied in the constructor aOrder instance. |
|
91 @param aKey The key part of TPair object, which will be inserted |
|
92 @param aData The data part of TPair object, which will be inserted |
|
93 @return KErrNone if successful, otherwise one of the system-wide error codes |
|
94 */ |
|
95 template <class KEY, class DATA> |
|
96 TInt RMap<KEY, DATA>::Insert(const KEY& aKey, const DATA& aData) |
|
97 { |
|
98 return iCol.InsertInOrder(TPair<KEY, DATA>(aKey, aData), iOrder); |
|
99 } |
|
100 |
|
101 /** |
|
102 This method removes an element with aKey key from the map. |
|
103 If there is no such element in the map, the debug verison of the method will panic. |
|
104 The destructor of the object, which is about to be removed, is not called. |
|
105 @param aKey The key of TPair object, which has to be removed from the collection. |
|
106 */ |
|
107 template <class KEY, class DATA> |
|
108 void RMap<KEY, DATA>::Remove(const KEY& aKey) |
|
109 { |
|
110 TInt index = iCol.FindInOrder(TPair<KEY, DATA>(aKey), iOrder); |
|
111 index != KErrNotFound ? iCol.Remove(index) : __ASSERT(0); |
|
112 } |
|
113 |
|
114 /** |
|
115 This method performs a search for an element with aKey key in the map. |
|
116 If such element exists, the method will return a const reference to the element's data. |
|
117 If not, then the method will panic. |
|
118 @param aKey The key of TPair object, a reference to which DATA will be returned |
|
119 @return A const reference to the matching DATA object. |
|
120 */ |
|
121 template <class KEY, class DATA> |
|
122 const DATA& RMap<KEY, DATA>::operator[](const KEY& aKey) const |
|
123 { |
|
124 TInt index = iCol.FindInOrder(TPair<KEY, DATA>(aKey), iOrder); |
|
125 __ASSERT_ALWAYS(index != KErrNotFound, User::Invariant()); |
|
126 return iCol[index].iData; |
|
127 } |
|
128 |
|
129 /** |
|
130 This method performs a search for an element with aKey key in the map. |
|
131 If such element exists, the method will set the element's data in aData parameter and |
|
132 return KErrNone. |
|
133 If not, then the method will return KErrNotFound. |
|
134 @param aKey The key of TPair object, a reference to which DATA will be returned in aData |
|
135 parameter, if found |
|
136 @param aData An output parameter, it references the location, where the found data will be stored. |
|
137 @return KErrNone An element with aKey key exists in the map, the data part is stored in aData. |
|
138 @return KErrNotFound No map element with aKey key found. |
|
139 */ |
|
140 template <class KEY, class DATA> |
|
141 TInt RMap<KEY, DATA>::Find(const KEY& aKey, DATA& aData) const |
|
142 { |
|
143 TInt index = iCol.FindInOrder(TPair<KEY, DATA>(aKey), iOrder); |
|
144 if(index != KErrNotFound) |
|
145 { |
|
146 aData = iCol[index].iData; |
|
147 } |
|
148 return index; |
|
149 } |
|
150 |
|
151 /** |
|
152 This method returns the number of elements in the map. |
|
153 @return Map elements count. |
|
154 */ |
|
155 template <class KEY, class DATA> |
|
156 TInt RMap<KEY, DATA>::Count() const |
|
157 { |
|
158 return iCol.Count(); |
|
159 } |
|
160 |
|
161 /** |
|
162 Initializes TMapIterator instance. |
|
163 @param aMap A const reference to the RMap object, which will be iterated. |
|
164 */ |
|
165 template <class KEY, class DATA> |
|
166 TMapIterator<KEY, DATA>::TMapIterator(const RMap<KEY, DATA>& aMap) : |
|
167 iMap(aMap), |
|
168 iIndex(0) |
|
169 { |
|
170 } |
|
171 |
|
172 /** |
|
173 Resets TMapIterator iterator for a new scan from the beginning of the controlled map sequence. |
|
174 */ |
|
175 template <class KEY, class DATA> |
|
176 void TMapIterator<KEY, DATA>::Reset() |
|
177 { |
|
178 iIndex = 0; |
|
179 } |
|
180 |
|
181 /** |
|
182 If iterator's current position is not beyond the end of the map, |
|
183 the iterator will retrieve current map entry into aPair argument, advance iterator position |
|
184 by 1 and return ETrue. |
|
185 Otherwise the iterator will return EFalse. |
|
186 @param aPair An output parameter, which references the location, where the next RMap element will be stored. |
|
187 @return ETrue The next RMap element successfully loaded into aPair parameter. This is not the |
|
188 end of RMap collection. |
|
189 @return EFalse The next RMap element successfully loaded into aPair parameter. This is the |
|
190 end of RMap collection. Do not call Next() anymore. |
|
191 */ |
|
192 template <class KEY, class DATA> |
|
193 TBool TMapIterator<KEY, DATA>::Next(TPair<KEY, DATA>& aPair) const |
|
194 { |
|
195 return (iIndex < iMap.iCol.Count()) ? aPair = iMap.iCol[iIndex++], ETrue : EFalse; |
|
196 } |