|
1 /* |
|
2 * Copyright (c) 2002-2005 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: Header declaration |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 #ifndef POINTERMAP_H |
|
29 #define POINTERMAP_H |
|
30 |
|
31 // INCLUDES |
|
32 #include <e32std.h> |
|
33 |
|
34 // CLASS DECLARATION |
|
35 template <typename K, typename V> |
|
36 class RSenPointerMap |
|
37 { |
|
38 public: // Constructors and destructor |
|
39 |
|
40 RSenPointerMap(TBool aTakeOwnershipKey, TBool aTakeOwnershipValue) |
|
41 : |
|
42 iTakeOwnershipKey(aTakeOwnershipKey), |
|
43 iTakeOwnershipValue(aTakeOwnershipValue) |
|
44 {} |
|
45 |
|
46 ~RSenPointerMap() |
|
47 { |
|
48 Reset(); |
|
49 } |
|
50 |
|
51 // New functions |
|
52 |
|
53 TInt Append(const K* aKey, const V* aValue) |
|
54 { |
|
55 TInt err = iKeys.Append(aKey); |
|
56 if (err == KErrNone) |
|
57 { |
|
58 err = iValues.Append(aValue); |
|
59 if (err != KErrNone) |
|
60 { |
|
61 // last element of iKeys should be removed |
|
62 TInt lastElementIndex = iKeys.Count() - 1; |
|
63 iKeys.Remove(lastElementIndex); |
|
64 } |
|
65 } |
|
66 return err; |
|
67 } |
|
68 |
|
69 TInt Find(const K& aKey) const |
|
70 { |
|
71 TInt index( KErrNotFound ); |
|
72 for (TInt i = 0; i < iKeys.Count(); i++) |
|
73 { |
|
74 if (*iKeys[i] == aKey) |
|
75 { |
|
76 index = i; |
|
77 break; |
|
78 } |
|
79 } |
|
80 return index; |
|
81 } |
|
82 |
|
83 TInt FindReverse(const K& aKey) const |
|
84 { |
|
85 TInt index( KErrNotFound ); |
|
86 TInt count( iKeys.Count() ); |
|
87 for (TInt i = count-1; i >=0 ; i--) |
|
88 { |
|
89 if (*iKeys[i] == aKey) |
|
90 { |
|
91 index = i; |
|
92 break; |
|
93 } |
|
94 } |
|
95 return index; |
|
96 } |
|
97 |
|
98 |
|
99 TInt RemoveAt( TInt aIndex ) |
|
100 { |
|
101 K* key = KeyAt( aIndex ); |
|
102 return RemoveByKey( *key ); |
|
103 } |
|
104 |
|
105 // @return the index of removed key-value pair, or |
|
106 // KErrNotFound, if such key was not found |
|
107 TInt RemoveByKey(const K& aKey) |
|
108 { |
|
109 TInt index = Find(aKey); |
|
110 if (index != KErrNotFound) |
|
111 { |
|
112 if(iTakeOwnershipKey) |
|
113 { |
|
114 K* key = KeyAt(index); |
|
115 delete key; |
|
116 } |
|
117 if(iTakeOwnershipValue) |
|
118 { |
|
119 V* value = iValues[index]; |
|
120 delete value; |
|
121 } |
|
122 iKeys.Remove(index); |
|
123 iValues.Remove(index); |
|
124 } |
|
125 return index; |
|
126 } |
|
127 |
|
128 // @return the index of removed key-value pair, or |
|
129 // KErrNotFound, if such key was not found |
|
130 TInt Remove(const V& aValue) |
|
131 { |
|
132 TInt index = FindValue(aValue); |
|
133 if (index != KErrNotFound) |
|
134 { |
|
135 if (iTakeOwnershipValue) |
|
136 { |
|
137 V* value = iValues[index]; |
|
138 delete value; |
|
139 } |
|
140 if (iTakeOwnershipKey) |
|
141 { |
|
142 K* key = iKeys[index]; |
|
143 delete key; |
|
144 } |
|
145 iValues.Remove(index); |
|
146 iKeys.Remove(index); |
|
147 } |
|
148 return index; |
|
149 } |
|
150 |
|
151 TInt FindValue(const V& aValue) const |
|
152 { |
|
153 TInt index = KErrNotFound; |
|
154 for (TInt i = 0; i < iValues.Count(); i++) |
|
155 { |
|
156 if ((iValues[i]) && (*iValues[i] == aValue)) |
|
157 { |
|
158 index = i; |
|
159 break; |
|
160 } |
|
161 } |
|
162 return index; |
|
163 } |
|
164 |
|
165 |
|
166 // Note: deletes the current value of this key |
|
167 TInt UpdateValue(const K* aKey, const V* aValue) |
|
168 { |
|
169 TInt index=Find(*aKey); |
|
170 if (index==KErrNotFound) |
|
171 { |
|
172 return Append(aKey, aValue); |
|
173 } |
|
174 |
|
175 V* bValue=iValues[index]; |
|
176 if (iTakeOwnershipValue) |
|
177 { |
|
178 // Since OWNED value is going to be replaced with aValue, |
|
179 // destroy old value instance first: |
|
180 delete bValue; |
|
181 } |
|
182 |
|
183 iValues[index]=(V*)aValue; |
|
184 return KErrNone; |
|
185 } |
|
186 |
|
187 |
|
188 K* KeyAt(TInt aIndex) |
|
189 { |
|
190 return iKeys[aIndex]; |
|
191 } |
|
192 |
|
193 const V* ValueAt(TInt aIndex) const |
|
194 { |
|
195 return iValues[aIndex]; |
|
196 } |
|
197 |
|
198 TInt Count() const |
|
199 { |
|
200 return iKeys.Count(); |
|
201 } |
|
202 |
|
203 void Reset() |
|
204 { |
|
205 if ( iTakeOwnershipKey ) |
|
206 { |
|
207 iKeys.ResetAndDestroy(); |
|
208 } |
|
209 else |
|
210 { |
|
211 iKeys.Reset(); |
|
212 } |
|
213 |
|
214 if ( iTakeOwnershipValue ) |
|
215 { |
|
216 iValues.ResetAndDestroy(); |
|
217 } |
|
218 else |
|
219 { |
|
220 iValues.Reset(); |
|
221 } |
|
222 } |
|
223 |
|
224 TInt Insert(const K* aKey, const V* aValue) |
|
225 { |
|
226 TInt count=iKeys.Count(); |
|
227 TInt err=KErrNone; |
|
228 TBool inserted=EFalse; |
|
229 |
|
230 for(TInt i=0; i<count; i++) |
|
231 { |
|
232 if(*iKeys[i] >= *aKey) |
|
233 { |
|
234 err = iKeys.Insert(aKey, i); |
|
235 if (err == KErrNone) |
|
236 { |
|
237 err = iValues.Insert(aValue, i); |
|
238 if (err != KErrNone) |
|
239 { |
|
240 // inserted element of iKeys should be removed |
|
241 iKeys.Remove(i); |
|
242 } |
|
243 else |
|
244 { |
|
245 inserted=ETrue; |
|
246 } |
|
247 } |
|
248 break; |
|
249 } |
|
250 } |
|
251 |
|
252 if(!inserted) |
|
253 { |
|
254 err = iKeys.Append(aKey); |
|
255 if (err == KErrNone) |
|
256 { |
|
257 err = iValues.Append(aValue); |
|
258 if (err != KErrNone) |
|
259 { |
|
260 // last element of iKeys should be removed |
|
261 TInt lastElementIndex = iKeys.Count() - 1; |
|
262 iKeys.Remove(lastElementIndex); |
|
263 } |
|
264 } |
|
265 } |
|
266 return err; |
|
267 } |
|
268 |
|
269 private: // Data |
|
270 TBool iTakeOwnershipKey; |
|
271 TBool iTakeOwnershipValue; |
|
272 RPointerArray<K> iKeys; |
|
273 RPointerArray<V> iValues; |
|
274 }; |
|
275 |
|
276 #endif // POINTERMAP_H |
|
277 |
|
278 // End of File |
|
279 |