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 "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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: Pointermap class declaration
28 template <typename K, typename V>
31 public: // Constructors and destructor
33 RSenPointerMap(TBool aTakeOwnershipKey, TBool aTakeOwnershipValue)
35 iTakeOwnershipKey(aTakeOwnershipKey),
36 iTakeOwnershipValue(aTakeOwnershipValue)
46 TInt Append(const K* aKey, const V* aValue)
48 TInt err = iKeys.Append(aKey);
51 err = iValues.Append(aValue);
54 // last element of iKeys should be removed
55 TInt lastElementIndex = iKeys.Count() - 1;
56 iKeys.Remove(lastElementIndex);
62 TInt Find(const K& aKey) const
64 TInt index( KErrNotFound );
65 for (TInt i = 0; i < iKeys.Count(); i++)
67 if (*iKeys[i] == aKey)
76 TInt FindReverse(const K& aKey) const
78 TInt index( KErrNotFound );
79 TInt count( iKeys.Count() );
80 for (TInt i = count-1; i >=0 ; i--)
82 if (*iKeys[i] == aKey)
92 TInt RemoveAt( TInt aIndex )
94 K* key = KeyAt( aIndex );
95 return RemoveByKey( *key );
98 // @return the index of removed key-value pair, or
99 // KErrNotFound, if such key was not found
100 TInt RemoveByKey(const K& aKey)
102 TInt index = Find(aKey);
103 if (index != KErrNotFound)
105 if(iTakeOwnershipKey)
107 K* key = KeyAt(index);
110 if(iTakeOwnershipValue)
112 V* value = iValues[index];
116 iValues.Remove(index);
121 // @return the index of removed key-value pair, or
122 // KErrNotFound, if such key was not found
123 TInt Remove(const V& aValue)
125 TInt index = FindValue(aValue);
126 if (index != KErrNotFound)
128 if (iTakeOwnershipValue)
130 V* value = iValues[index];
133 if (iTakeOwnershipKey)
135 K* key = iKeys[index];
138 iValues.Remove(index);
144 TInt FindValue(const V& aValue) const
146 TInt index = KErrNotFound;
147 for (TInt i = 0; i < iValues.Count(); i++)
149 if ((iValues[i]) && (*iValues[i] == aValue))
159 // Note: deletes the current value of this key
160 TInt UpdateValue(const K* aKey, const V* aValue)
162 TInt index=Find(*aKey);
163 if (index==KErrNotFound)
165 return Append(aKey, aValue);
168 V* bValue=iValues[index];
169 if (iTakeOwnershipValue)
171 // Since OWNED value is going to be replaced with aValue,
172 // destroy old value instance first:
176 iValues[index]=(V*)aValue;
181 K* KeyAt(TInt aIndex)
183 return iKeys[aIndex];
186 const V* ValueAt(TInt aIndex) const
188 return iValues[aIndex];
193 return iKeys.Count();
198 if ( iTakeOwnershipKey )
200 iKeys.ResetAndDestroy();
207 if ( iTakeOwnershipValue )
209 iValues.ResetAndDestroy();
217 TInt Insert(const K* aKey, const V* aValue)
219 TInt count=iKeys.Count();
221 TBool inserted=EFalse;
223 for(TInt i=0; i<count; i++)
225 if(*iKeys[i] >= *aKey)
227 err = iKeys.Insert(aKey, i);
230 err = iValues.Insert(aValue, i);
233 // inserted element of iKeys should be removed
247 err = iKeys.Append(aKey);
250 err = iValues.Append(aValue);
253 // last element of iKeys should be removed
254 TInt lastElementIndex = iKeys.Count() - 1;
255 iKeys.Remove(lastElementIndex);
263 TBool iTakeOwnershipKey;
264 TBool iTakeOwnershipValue;
265 RPointerArray<K> iKeys;
266 RPointerArray<V> iValues;
269 #endif // POINTERMAP_H