2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: Pointermap class declaration
25 template <typename K, typename V>
28 public: // Constructors and destructor
30 RSenPointerMap(TBool aTakeOwnershipKey, TBool aTakeOwnershipValue)
32 iTakeOwnershipKey(aTakeOwnershipKey),
33 iTakeOwnershipValue(aTakeOwnershipValue)
43 TInt Append(const K* aKey, const V* aValue)
45 TInt err = iKeys.Append(aKey);
48 err = iValues.Append(aValue);
51 // last element of iKeys should be removed
52 TInt lastElementIndex = iKeys.Count() - 1;
53 iKeys.Remove(lastElementIndex);
59 TInt Find(const K& aKey) const
61 TInt index = KErrNotFound;
62 for (int i = 0; i < iKeys.Count(); i++)
64 if (*iKeys[i] == aKey)
73 // @return the index of removed key-value pair, or
74 // KErrNotFound, if such key was not found
75 TInt RemoveByKey(const K& aKey)
77 TInt index = Find(aKey);
78 if (index != KErrNotFound)
82 K* key = KeyAt(index);
85 if(iTakeOwnershipValue)
87 V* value = iValues[index];
91 iValues.Remove(index);
96 // @return the index of removed key-value pair, or
97 // KErrNotFound, if such key was not found
98 TInt Remove(const V& aValue)
100 TInt index = FindValue(aValue);
101 if (index != KErrNotFound)
103 if (iTakeOwnershipValue)
105 V* value = iValues[index];
108 if (iTakeOwnershipKey)
110 K* key = iKeys[index];
113 iValues.Remove(index);
119 TInt FindValue(const V& aValue) const
121 TInt index = KErrNotFound;
122 for (int i = 0; i < iValues.Count(); i++)
124 if ((iValues[i]) && (*iValues[i] == aValue))
134 // Note: deletes the current value of this key
135 TInt UpdateValue(const K* aKey, const V* aValue)
137 TInt index=Find(*aKey);
138 if (index==KErrNotFound)
140 return Append(aKey, aValue);
143 V* bValue=iValues[index];
144 if (iTakeOwnershipValue)
146 // Since OWNED value is going to be replaced with aValue,
147 // destroy old value instance first:
151 iValues[index]=(V*)aValue;
156 K* KeyAt(TInt aIndex)
158 return iKeys[aIndex];
161 const V* ValueAt(TInt aIndex) const
163 return iValues[aIndex];
168 return iKeys.Count();
173 if ( iTakeOwnershipKey )
175 iKeys.ResetAndDestroy();
182 if ( iTakeOwnershipValue )
184 iValues.ResetAndDestroy();
192 TInt Insert(const K* aKey, const V* aValue)
194 TInt count=iKeys.Count();
196 TBool inserted=EFalse;
198 for(TInt i=0; i<count; i++)
200 if(*iKeys[i] >= *aKey)
202 err = iKeys.Insert(aKey, i);
205 err = iValues.Insert(aValue, i);
208 // inserted element of iKeys should be removed
222 err = iKeys.Append(aKey);
225 err = iValues.Append(aValue);
228 // last element of iKeys should be removed
229 TInt lastElementIndex = iKeys.Count() - 1;
230 iKeys.Remove(lastElementIndex);
238 TBool iTakeOwnershipKey;
239 TBool iTakeOwnershipValue;
240 RPointerArray<K> iKeys;
241 RPointerArray<V> iValues;
244 #endif // POINTERMAP_H