sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32/include/e32hashtab.h sl@0: // sl@0: // sl@0: sl@0: #ifndef __E32HASHTAB_H__ sl@0: #define __E32HASHTAB_H__ sl@0: #include sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: Defines a function type used by a THashFunction32 object. sl@0: sl@0: A function of this type implements an algorithm for producing a 32 bit hash sl@0: value from a key. sl@0: sl@0: @see THashFunction32 sl@0: */ sl@0: typedef TUint32 (*TGeneralHashFunction32)(const TAny*); sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which packages a function that calculates a 32 bit hash sl@0: value from a key of templated type. sl@0: sl@0: A THashFunction32 object is constructed and passed as a parameter to sl@0: member functions of the hash table classes RHashSet, RPtrHashSet, sl@0: RHashMap and RPtrHashMap. sl@0: sl@0: @see RHashSet sl@0: @see RPtrHashSet sl@0: @see RHashMap sl@0: @see RPtrHashMap sl@0: */ sl@0: template sl@0: class THashFunction32 sl@0: { sl@0: public: sl@0: inline THashFunction32( TUint32 (*aHashFunc)(const T&) ) sl@0: { iHashFunction = (TGeneralHashFunction32)aHashFunc; } sl@0: inline operator TGeneralHashFunction32() const sl@0: { return iHashFunction; } sl@0: inline TUint32 Hash(const T& aKey) const sl@0: { return (*iHashFunction)(&aKey); } sl@0: private: sl@0: TGeneralHashFunction32 iHashFunction; sl@0: }; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A set of common hashing functions for frequently occurring types. sl@0: sl@0: @see RHashSet sl@0: @see RPtrHashSet sl@0: @see RHashMap sl@0: @see RPtrHashMap sl@0: */ sl@0: class DefaultHash sl@0: { sl@0: public: sl@0: IMPORT_C static TUint32 Integer(const TInt&); sl@0: IMPORT_C static TUint32 Des8(const TDesC8&); sl@0: IMPORT_C static TUint32 Des16(const TDesC16&); sl@0: IMPORT_C static TUint32 IntegerPtr(TInt* const &); sl@0: IMPORT_C static TUint32 Des8Ptr(TDesC8* const &); sl@0: IMPORT_C static TUint32 Des16Ptr(TDesC16* const &); sl@0: }; sl@0: sl@0: sl@0: sl@0: class THashTableIterBase; sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: Base class used in the derivation of RHashSet, RPtrHashSet, sl@0: RHashMap and RPtrHashMap. sl@0: sl@0: This class provides a general hash table implementation using probe sequences sl@0: generated by pseudo-double hashing. sl@0: The class is internal and is not intended for use. sl@0: */ sl@0: class RHashTableBase sl@0: { sl@0: public: sl@0: enum TDefaultSpecifier sl@0: { sl@0: EDefaultSpecifier_Normal, sl@0: }; sl@0: sl@0: protected: sl@0: template sl@0: class Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: protected: sl@0: enum TElementState sl@0: { sl@0: EEmpty=0, // entry is vacant sl@0: EDeleted=1, // entry has been deleted sl@0: EGen0=2, // entry is occupied, generation number 0 sl@0: EGen1=3, // entry is occupied, generation number 1 sl@0: EStateMask=3, sl@0: EOccupiedMask=2, sl@0: }; sl@0: sl@0: struct SElement sl@0: { sl@0: inline void SetEmpty() {iHash=EEmpty;} sl@0: inline void SetDeleted() {iHash=EDeleted;} sl@0: inline TBool IsEmpty() const {return (iHash&EStateMask)==EEmpty;} sl@0: inline TBool IsDeleted() const {return (iHash&EStateMask)==EDeleted;} sl@0: inline TBool IsEmptyOrDeleted() const {return !(iHash&EOccupiedMask);} sl@0: sl@0: TUint32 iHash; // bits 2-31 = 30 bit hash value, bits 0,1 = state sl@0: }; sl@0: sl@0: protected: sl@0: IMPORT_C RHashTableBase(TGeneralHashFunction32, TGeneralIdentityRelation, TInt aElementSize, TInt aKeyOffset); sl@0: IMPORT_C void Close(); sl@0: IMPORT_C TAny* Find(const TAny* aKey, TInt aOffset=0) const; sl@0: IMPORT_C TAny* FindL(const TAny* aKey, TInt aOffset=0) const; sl@0: TInt Insert(const TAny* aKey, TAny*& aElement); sl@0: IMPORT_C TInt PtrInsert(const TAny* aKey, const TAny* aValue); sl@0: IMPORT_C void PtrInsertL(const TAny* aKey, const TAny* aValue); sl@0: IMPORT_C TInt ValueInsert(const TAny* aKey, TInt aKeySize, const TAny* aValue, TInt aValueOffset, TInt aValueSize); sl@0: IMPORT_C void ValueInsertL(const TAny* aKey, TInt aKeySize, const TAny* aValue, TInt aValueOffset, TInt aValueSize); sl@0: IMPORT_C TInt Remove(const TAny* aKey); sl@0: IMPORT_C TInt Count() const; sl@0: IMPORT_C TInt Reserve(TInt aCount); sl@0: IMPORT_C void ReserveL(TInt aCount); sl@0: IMPORT_C void ConsistencyCheck(TUint32* aDeleted=0, TUint32* aComparisons=0, TUint32 aChainLimit=0, TUint32* aChainInfo=0); sl@0: private: sl@0: void SetThresholds(); sl@0: TInt ExpandTable(TInt aNewIndexBits); sl@0: void ShrinkTable(); sl@0: void ReformTable(TUint aNewIndexBits); sl@0: void VerifyReform(); sl@0: private: sl@0: inline SElement* Element(TInt aIndex) sl@0: {return (SElement*)(((TUint8*)iElements) + aIndex*iElementSize);} sl@0: inline const SElement* ElementC(TInt aIndex) const sl@0: {return (const SElement*)(((TUint8*)iElements) + aIndex*iElementSize);} sl@0: inline TAny* GetKey(const SElement* aElement) const sl@0: {return iKeyOffset ? ((TUint8*)aElement + iKeyOffset) : (TAny*)((TUint32*)aElement)[1];} sl@0: private: sl@0: TGeneralHashFunction32 iHashFunc; // generates the hash from a given key sl@0: TGeneralIdentityRelation iIdFunc; // compare two keys for equality sl@0: TUint8 iIndexBits; // number of bits used to index the table sl@0: TUint8 iGeneration; // 2 or 3, generation number used when traversing entire table sl@0: TUint8 iKeyOffset; // offset to key sl@0: TUint8 iPad0; sl@0: TAny* iElements; sl@0: TUint32 iCount; // number of valid entries sl@0: TUint32 iEmptyCount; // number of empty entries sl@0: TUint32 iLowerThreshold; // shrink if count drops below this sl@0: TUint32 iUpperThreshold; // expand if count rises above this sl@0: TUint32 iCleanThreshold; // clean table if count of empty entries falls below this sl@0: TInt iElementSize; sl@0: TInt iPad1; // expansion room sl@0: TInt iPad2; sl@0: sl@0: friend struct RHashTableBase::SElement; sl@0: friend class THashTableIterBase; sl@0: friend class HashTest; sl@0: }; sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::IntegerPtr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::IntegerPtr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::IntegerPtr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::IntegerPtr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::IntegerPtr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::IntegerPtr;} sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::IntegerPtr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::IntegerPtr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::Des8Ptr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::Des8Ptr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::Des16Ptr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::Des16Ptr;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::Integer;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::Integer;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::Integer;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::Integer;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::Integer;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::Integer;} sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::Integer;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::Integer;} sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::Des8;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::Des8;} sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults sl@0: { sl@0: public: sl@0: inline static TGeneralHashFunction32 Hash(); sl@0: inline static TGeneralIdentityRelation Id(); sl@0: }; sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralHashFunction32 RHashTableBase::Defaults::Hash() sl@0: {return (TGeneralHashFunction32)&DefaultHash::Des16;} sl@0: sl@0: /** sl@0: @internalComponent sl@0: */ sl@0: inline TGeneralIdentityRelation RHashTableBase::Defaults::Id() sl@0: {return (TGeneralIdentityRelation)&DefaultIdentity::Des16;} sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: @internalComponent sl@0: sl@0: Base class used in the derivation of THashSetIter, TPtrHashSetIter, sl@0: THashMapIter and TPtrHashMapIter. sl@0: sl@0: This class provides iteration capability for the hash table classes derived sl@0: from RHashTableBase. sl@0: The class is internal and is not intended for use. sl@0: */ sl@0: class THashTableIterBase sl@0: { sl@0: protected: sl@0: IMPORT_C THashTableIterBase(const RHashTableBase& aTable); sl@0: IMPORT_C void Reset(); sl@0: IMPORT_C const TAny* Next(TInt aOffset=0); sl@0: IMPORT_C const TAny* Current(TInt aOffset=0) const; sl@0: IMPORT_C void RemoveCurrent(); sl@0: private: sl@0: const RHashTableBase& iTbl; sl@0: TInt iIndex; sl@0: TInt iPad1; // expansion room sl@0: TInt iPad2; sl@0: }; sl@0: sl@0: sl@0: sl@0: template class THashSetIter; sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which implements an unordered extensional set of objects of sl@0: type T using a probe-sequence hash table. The objects are copied into the set sl@0: when they are added. A bitwise binary copy is used here, so the type T must sl@0: not implement a nontrivial copy constructor. sl@0: sl@0: */ sl@0: template sl@0: class RHashSet : public RHashTableBase sl@0: { sl@0: private: sl@0: friend class THashSetIter; sl@0: sl@0: struct SFullElement sl@0: { sl@0: TUint32 iHash; sl@0: T iT; sl@0: }; sl@0: sl@0: public: sl@0: sl@0: /** sl@0: A class which allows iteration over the elements of a RHashSet class. sl@0: sl@0: The set being iterated over may not be modified while an iteration is in progress sl@0: or the iteration operations may malfunction or panic. sl@0: sl@0: @see THashSetIter sl@0: */ sl@0: typedef THashSetIter TIter; sl@0: sl@0: /** sl@0: Construct a set of objects of type T using a specified hash function and identity relation. sl@0: The set is initially empty. sl@0: sl@0: @param aHash The hash function used to hash the objects of type T. sl@0: @param aIdentity The identity relation used to determine if two objects of type T sl@0: should be considered identical. sl@0: */ sl@0: inline RHashSet(const THashFunction32& aHash, const TIdentityRelation& aIdentity) sl@0: : RHashTableBase(aHash, aIdentity, sizeof(SFullElement), _FOFF(SFullElement,iT)) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Construct a set of objects of type T using a default hash function and identity relation. sl@0: The set is initially empty. sl@0: */ sl@0: inline RHashSet() sl@0: : RHashTableBase(Defaults::Hash(), Defaults::Id(), sizeof(SFullElement), _FOFF(SFullElement,iT)) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Free all memory used by this set. sl@0: Returns the set to the same state it had following construction. sl@0: */ sl@0: inline void Close() sl@0: { RHashTableBase::Close(); } sl@0: sl@0: sl@0: /** sl@0: Locate a specified element in the set. sl@0: sl@0: @param aKey The object of type T to search for. sl@0: @return A pointer to the copy of the specified object in the set, if it sl@0: exists. The object may not be modified via this pointer. sl@0: NULL if the specified object is not a member of this set. sl@0: */ sl@0: inline const T* Find(const T& aKey) const sl@0: { return (const T*)RHashTableBase::Find(&aKey, _FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Locate a specified element in the set. sl@0: sl@0: @param aKey The object of type T to search for. sl@0: @return A reference to the copy of the specified object in the set, if it sl@0: exists. The object may not be modified via this reference. sl@0: @leave KErrNotFound if the specified object is not a member of this set. sl@0: */ sl@0: inline const T& FindL(const T& aKey) const sl@0: { return *(const T*)RHashTableBase::FindL(&aKey, _FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Locate a specified element in the set. sl@0: sl@0: @param aKey The object of type T to search for. sl@0: @return A pointer to the copy of the specified object in the set, if it sl@0: exists. The object may be modified via this pointer. Care should sl@0: be taken not to modify any parts of the object which are used by sl@0: either the hash function or the identity relation for this set. sl@0: If this is done the set may become inconsistent, resulting in sl@0: malfunctions and/or panics at a later time. sl@0: NULL if the specified object is not a member of this set. sl@0: */ sl@0: inline T* Find(const T& aKey) sl@0: { return (T*)RHashTableBase::Find(&aKey, _FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Locate a specified element in the set. sl@0: sl@0: @param aKey The object of type T to search for. sl@0: @return A reference to the copy of the specified object in the set, if it sl@0: exists. The object may be modified via this reference. Care should sl@0: be taken not to modify any parts of the object which are used by sl@0: either the hash function or the identity relation for this set. sl@0: If this is done the set may become inconsistent, resulting in sl@0: malfunctions and/or panics at a later time. sl@0: @leave KErrNotFound if the specified object is not a member of this set. sl@0: */ sl@0: inline T& FindL(const T& aKey) sl@0: { return *(T*)RHashTableBase::FindL(&aKey, _FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Insert an element into the set. sl@0: sl@0: If the specified object is not currently a member of the set, a copy of the sl@0: object is added to the set and KErrNone is returned. sl@0: If the specified object is currently a member of the set, the existing copy sl@0: of the object is replaced by the provided object and KErrNone is sl@0: returned. sl@0: In both cases the object is copied bitwise into the set. sl@0: sl@0: @param aKey The object of type T to add to the set. sl@0: @return KErrNone if the object was added successfully. sl@0: KErrNoMemory if memory could not be allocated to store sl@0: the copy of aKey. sl@0: */ sl@0: inline TInt Insert(const T& aKey) sl@0: { return RHashTableBase::ValueInsert(&aKey, sizeof(T), 0, 0, 0); } sl@0: sl@0: sl@0: /** sl@0: Insert an element into the set. sl@0: sl@0: If the specified object is not currently a member of the set, a copy of the sl@0: object is added to the set and KErrNone is returned. sl@0: If the specified object is currently a member of the set, the existing copy sl@0: of the object is replaced by the provided object and KErrNone is sl@0: returned. sl@0: In both cases the object is copied bitwise into the set. sl@0: sl@0: @param aKey The object of type T to add to the set. sl@0: @leave KErrNoMemory if memory could not be allocated to store sl@0: the copy of aKey. sl@0: */ sl@0: inline void InsertL(const T& aKey) sl@0: { RHashTableBase::ValueInsertL(&aKey, sizeof(T), 0, 0, 0); } sl@0: sl@0: sl@0: /** sl@0: Remove an element from the set. sl@0: sl@0: @param aKey The object to be removed. sl@0: @return KErrNone if the object was removed successfully. sl@0: KErrNotFound if the object was not present in the set. sl@0: */ sl@0: inline TInt Remove(const T& aKey) sl@0: { return RHashTableBase::Remove(&aKey); } sl@0: sl@0: sl@0: /** sl@0: Query the number of elements in the set. sl@0: sl@0: @return The number of elements currently in the set. sl@0: */ sl@0: inline TInt Count() const sl@0: { return RHashTableBase::Count(); } sl@0: sl@0: sl@0: /** sl@0: Expand the set to accommodate a specified number of elements. sl@0: If the set already has enough space for the specified number of elements, no sl@0: action is taken. Any elements already in the set are retained. sl@0: sl@0: @param aCount The number of elements for which space should be allocated. sl@0: @return KErrNone if the operation completed successfully. sl@0: @return KErrNoMemory if sufficient memory could not be allocated. sl@0: */ sl@0: inline TInt Reserve(TInt aCount) sl@0: { return RHashTableBase::Reserve(aCount); } sl@0: sl@0: sl@0: /** sl@0: Expand the set to accommodate a specified number of elements. sl@0: If the set already has enough space for the specified number of elements, no sl@0: action is taken. Any elements already in the set are retained. sl@0: sl@0: @param aCount The number of elements for which space should be allocated. sl@0: @leave KErrNoMemory if sufficient memory could not be allocated. sl@0: */ sl@0: inline void ReserveL(TInt aCount) sl@0: { RHashTableBase::ReserveL(aCount); } sl@0: sl@0: }; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which allows iteration over the elements of a RHashSet sl@0: class. sl@0: sl@0: The set being iterated over may not be modified while an iteration is in progress sl@0: or the iteration operations may malfunction or panic. sl@0: sl@0: @see RHashSet sl@0: */ sl@0: template sl@0: class THashSetIter : public THashTableIterBase sl@0: { sl@0: private: sl@0: sl@0: struct SFullElement sl@0: { sl@0: TUint32 iHash; sl@0: T iT; sl@0: }; sl@0: sl@0: public: sl@0: sl@0: /** sl@0: Construct an iterator over the specified set. sl@0: The iterator starts at conceptual position one before the beginning of the list sl@0: being iterated. sl@0: sl@0: @param aSet The set to be iterated over. sl@0: */ sl@0: inline THashSetIter(const RHashSet& aSet) sl@0: : THashTableIterBase(aSet) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Reset the iterator to its initial state. sl@0: sl@0: @param aSet The set to be iterated over. sl@0: */ sl@0: inline void Reset() sl@0: { THashTableIterBase::Reset(); } sl@0: sl@0: sl@0: /** sl@0: Return the current position of the iterator. sl@0: sl@0: @return A pointer to the set member corresponding to the current position of the sl@0: iterator. sl@0: NULL if the iterator has just been constructed or reset, or if it has sl@0: previously reached the end of an iteration. sl@0: */ sl@0: inline const T* Current() const sl@0: { return (const T*)THashTableIterBase::Current(_FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Steps the iterator to the next position. sl@0: sl@0: @return A pointer to the set member corresponding to the next position of the sl@0: iterator. sl@0: NULL if the iterator has exhausted all the available set elements. sl@0: */ sl@0: inline const T* Next() sl@0: { return (const T*)THashTableIterBase::Next(_FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Removes the element at the current iterator position from the hash table. sl@0: If the iterator does not currently point to a valid element, no action is taken. sl@0: Note that the iterator position is not altered so it no longer points to a valid sl@0: element following the Remove(). It is illegal to call Current() on the iterator sl@0: after calling Remove() - the only legal operations are Reset() and Next(). sl@0: sl@0: */ sl@0: inline void RemoveCurrent() sl@0: { THashTableIterBase::RemoveCurrent(); } sl@0: }; sl@0: sl@0: sl@0: sl@0: template class TPtrHashSetIter; sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which implements an unordered extensional set of objects of sl@0: type T using a probe-sequence hash table. The objects are not copied into the set sl@0: when they are added; rather the set stores pointers to the contained objects. sl@0: sl@0: */ sl@0: template sl@0: class RPtrHashSet : public RHashTableBase sl@0: { sl@0: private: sl@0: friend class TPtrHashSetIter; sl@0: sl@0: struct SFullElement sl@0: { sl@0: TUint32 iHash; sl@0: T* iT; sl@0: }; sl@0: sl@0: public: sl@0: sl@0: /** sl@0: A class which allows iteration over the elements of a RPtrHashSet class. sl@0: sl@0: The set being iterated over may not be modified while an iteration is in progress sl@0: or the iteration operations may malfunction or panic. sl@0: sl@0: @see TPtrHashSetIter sl@0: */ sl@0: typedef TPtrHashSetIter TIter; sl@0: sl@0: /** sl@0: Construct a set of objects of type T using a specified hash function and identity relation. sl@0: The set is initially empty. sl@0: sl@0: @param aHash The hash function used to hash the objects of type T. sl@0: @param aIdentity The identity relation used to determine if two objects of type T sl@0: should be considered identical. sl@0: */ sl@0: inline RPtrHashSet(const THashFunction32& aHash, const TIdentityRelation& aIdentity) sl@0: : RHashTableBase(aHash, aIdentity, sizeof(SFullElement), 0) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Construct a set of objects of type T using a default hash function and identity relation. sl@0: The set is initially empty. sl@0: */ sl@0: inline RPtrHashSet() sl@0: : RHashTableBase(Defaults::Hash(), Defaults::Id(), sizeof(SFullElement), 0) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Free all memory used by this set. sl@0: Returns the set to the same state it had following construction. sl@0: */ sl@0: inline void Close() sl@0: { RHashTableBase::Close(); } sl@0: sl@0: sl@0: /** sl@0: Locate a specified element in the set. sl@0: sl@0: @param aKey The object of type T to search for. sl@0: @return A pointer to the specified object, if it is in the set. sl@0: The object may not be modified via this pointer. sl@0: NULL if the specified object is not a member of this set. sl@0: */ sl@0: inline const T* Find(const T& aKey) const sl@0: { return (const T*)RHashTableBase::Find(&aKey, -_FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Locate a specified element in the set. sl@0: sl@0: @param aKey The object of type T to search for. sl@0: @return A reference to the specified object, if it is in the set. sl@0: The object may not be modified via this reference. sl@0: @leave KErrNotFound if the specified object is not a member of this set. sl@0: */ sl@0: inline const T& FindL(const T& aKey) const sl@0: { return *(const T*)RHashTableBase::FindL(&aKey, -_FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Locate a specified element in the set. sl@0: sl@0: @param aKey The object of type T to search for. sl@0: @return A pointer to the specified object, if it is in the set. sl@0: The object may be modified via this pointer. Care should sl@0: be taken not to modify any parts of the object which are used by sl@0: either the hash function or the identity relation for this set. sl@0: If this is done the set may become inconsistent, resulting in sl@0: malfunctions and/or panics at a later time. sl@0: NULL if the specified object is not a member of this set. sl@0: */ sl@0: inline T* Find(const T& aKey) sl@0: { return (T*)RHashTableBase::Find(&aKey, -_FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Locate a specified element in the set. sl@0: sl@0: @param aKey The object of type T to search for. sl@0: @return A reference to the specified object, if it is in the set. sl@0: The object may be modified via this reference. Care should sl@0: be taken not to modify any parts of the object which are used by sl@0: either the hash function or the identity relation for this set. sl@0: If this is done the set may become inconsistent, resulting in sl@0: malfunctions and/or panics at a later time. sl@0: @leave KErrNotFound if the specified object is not a member of this set. sl@0: */ sl@0: inline T& FindL(const T& aKey) sl@0: { return *(T*)RHashTableBase::FindL(&aKey, -_FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Insert an element into the set. sl@0: sl@0: If the specified object is not currently a member of the set, a pointer to the sl@0: object is added to the set and KErrNone is returned. sl@0: If the specified object is currently a member of the set, the existing pointer sl@0: to the object is replaced by the provided pointer and KErrNone is sl@0: returned. sl@0: In both cases only a pointer to the object is stored - the object is never copied. sl@0: sl@0: @param aKey A pointer to the object of type T to add to the set. sl@0: @return KErrNone if the object was added successfully. sl@0: KErrNoMemory if memory could not be allocated to store sl@0: the pointer to the new object. sl@0: */ sl@0: inline TInt Insert(const T* aKey) sl@0: { return RHashTableBase::PtrInsert(aKey, 0); } sl@0: sl@0: sl@0: /** sl@0: Insert an element into the set. sl@0: sl@0: If the specified object is not currently a member of the set, a pointer to the sl@0: object is added to the set and KErrNone is returned. sl@0: If the specified object is currently a member of the set, the existing pointer sl@0: to the object is replaced by the provided pointer and KErrNone is sl@0: returned. sl@0: In both cases only a pointer to the object is stored - the object is never copied. sl@0: sl@0: @param aKey A pointer to the object of type T to add to the set. sl@0: @leave KErrNoMemory if memory could not be allocated to store the pointer to the new object. sl@0: */ sl@0: inline void InsertL(const T* aKey) sl@0: { RHashTableBase::PtrInsertL(aKey, 0); } sl@0: sl@0: sl@0: /** sl@0: Remove an element from the set. sl@0: sl@0: @param aKey A pointer to the object to be removed. sl@0: @return KErrNone if the object was removed successfully. sl@0: KErrNotFound if the object was not present in the set. sl@0: */ sl@0: inline TInt Remove(const T* aKey) sl@0: { return RHashTableBase::Remove(aKey); } sl@0: sl@0: sl@0: /** sl@0: Query the number of elements in the set. sl@0: sl@0: @return The number of elements currently in the set. sl@0: */ sl@0: inline TInt Count() const sl@0: { return RHashTableBase::Count(); } sl@0: sl@0: sl@0: /** sl@0: Expand the set to accommodate a specified number of elements. sl@0: If the set already has enough space for the specified number of elements, no sl@0: action is taken. Any elements already in the set are retained. sl@0: sl@0: @param aCount The number of elements for which space should be allocated. sl@0: @return KErrNone if the operation completed successfully. sl@0: @return KErrNoMemory if sufficient memory could not be allocated. sl@0: */ sl@0: inline TInt Reserve(TInt aCount) sl@0: { return RHashTableBase::Reserve(aCount); } sl@0: sl@0: sl@0: /** sl@0: Expand the set to accommodate a specified number of elements. sl@0: If the set already has enough space for the specified number of elements, no sl@0: action is taken. Any elements already in the set are retained. sl@0: sl@0: @param aCount The number of elements for which space should be allocated. sl@0: @leave KErrNoMemory if sufficient memory could not be allocated. sl@0: */ sl@0: inline void ReserveL(TInt aCount) sl@0: { RHashTableBase::ReserveL(aCount); } sl@0: sl@0: sl@0: void ResetAndDestroy(); sl@0: }; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which allows iteration over the elements of a RPtrHashSet sl@0: class. sl@0: sl@0: The set being iterated over may not be modified while an iteration is in progress sl@0: or the iteration operations may malfunction or panic. sl@0: sl@0: @see RPtrHashSet sl@0: */ sl@0: template sl@0: class TPtrHashSetIter : public THashTableIterBase sl@0: { sl@0: private: sl@0: sl@0: struct SFullElement sl@0: { sl@0: TUint32 iHash; sl@0: T* iT; sl@0: }; sl@0: sl@0: public: sl@0: sl@0: /** sl@0: Construct an iterator over the specified set. sl@0: The iterator starts at conceptual position one before the beginning of the list sl@0: being iterated. sl@0: sl@0: @param aSet The set to be iterated over. sl@0: */ sl@0: inline TPtrHashSetIter(const RPtrHashSet& aSet) sl@0: : THashTableIterBase(aSet) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Reset the iterator to its initial state. sl@0: sl@0: @param aSet The set to be iterated over. sl@0: */ sl@0: inline void Reset() sl@0: { THashTableIterBase::Reset(); } sl@0: sl@0: sl@0: /** sl@0: Return the current position of the iterator. sl@0: sl@0: @return A pointer to the set member corresponding to the current position of the sl@0: iterator. sl@0: NULL if the iterator has just been constructed or reset, or if it has sl@0: previously reached the end of an iteration. sl@0: */ sl@0: inline const T* Current() const sl@0: { return (const T*)THashTableIterBase::Current(-_FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Steps the iterator to the next position. sl@0: sl@0: @return A pointer to the set member corresponding to the next position of the sl@0: iterator. sl@0: NULL if the iterator has exhausted all the available set elements. sl@0: */ sl@0: inline const T* Next() sl@0: { return (const T*)THashTableIterBase::Next(-_FOFF(SFullElement,iT)); } sl@0: sl@0: sl@0: /** sl@0: Removes the element at the current iterator position from the hash table. sl@0: If the iterator does not currently point to a valid element, no action is taken. sl@0: Note that the iterator position is not altered so it no longer points to a valid sl@0: element following the Remove(). It is illegal to call Current() on the iterator sl@0: after calling Remove() - the only legal operations are Reset() and Next(). sl@0: sl@0: */ sl@0: inline void RemoveCurrent() sl@0: { THashTableIterBase::RemoveCurrent(); } sl@0: }; sl@0: sl@0: sl@0: sl@0: template class THashMapIter; sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which implements an associative array with key type K and value type V, sl@0: using a probe-sequence hash table. Both the key and value objects are copied into the sl@0: table when they are added. A bitwise binary copy is used here, so neither of the types sl@0: K and V may implement a nontrivial copy constructor. sl@0: sl@0: */ sl@0: template sl@0: class RHashMap : public RHashTableBase sl@0: { sl@0: private: sl@0: friend class THashMapIter; sl@0: sl@0: struct SFullElement sl@0: { sl@0: TUint32 iHash; sl@0: K iK; sl@0: V iV; sl@0: }; sl@0: sl@0: public: sl@0: sl@0: /** sl@0: A class which allows iteration over the elements of a RHashMap class. sl@0: sl@0: The array being iterated over may not be modified while an iteration is in progress sl@0: or the iteration operations may malfunction or panic. sl@0: sl@0: @see THashMapIter sl@0: */ sl@0: typedef THashMapIter TIter; sl@0: sl@0: /** sl@0: Construct an associative array of key-value pairs of type (K,V) using a sl@0: specified hash function and identity relation. sl@0: The array initially contains no key-value pairs. sl@0: sl@0: @param aHash The hash function used to hash the key objects of type K. sl@0: @param aIdentity The identity relation used to determine if two key objects sl@0: of type K should be considered identical. sl@0: */ sl@0: inline RHashMap(const THashFunction32& aHash, const TIdentityRelation& aIdentity) sl@0: : RHashTableBase(aHash, aIdentity, sizeof(SFullElement), _FOFF(SFullElement,iK)) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Construct an associative array of key-value pairs of type (K,V) using a sl@0: default hash function and identity relation. sl@0: The array initially contains no key-value pairs. sl@0: */ sl@0: inline RHashMap() sl@0: : RHashTableBase(Defaults::Hash(), Defaults::Id(), sizeof(SFullElement), _FOFF(SFullElement,iK)) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Free all memory used by this array. sl@0: Returns the array to the same state it had following construction. sl@0: */ sl@0: inline void Close() sl@0: { RHashTableBase::Close(); } sl@0: sl@0: sl@0: /** sl@0: Look up a specified key in the associative array and return a pointer to the sl@0: corresponding value. sl@0: sl@0: @param aKey The key object of type K to look up. sl@0: @return A pointer to the copy of the corresponding value object in the sl@0: array, if the specified key object was found. sl@0: The value object may not be modified via this pointer. sl@0: NULL if the specified key object was not found. sl@0: */ sl@0: inline const V* Find(const K& aKey) const sl@0: { return (const V*)RHashTableBase::Find(&aKey, _FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Look up a specified key in the associative array and return a pointer to the sl@0: corresponding value. sl@0: sl@0: @param aKey The key object of type K to look up. sl@0: @return A reference to the copy of the corresponding value object in the sl@0: array, if the specified key object was found. sl@0: The value object may not be modified via this reference. sl@0: @leave KErrNotFound if the specified key object was not found. sl@0: */ sl@0: inline const V& FindL(const K& aKey) const sl@0: { return *(const V*)RHashTableBase::FindL(&aKey, _FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Look up a specified key in the associative array and return a pointer to the sl@0: corresponding value. sl@0: sl@0: @param aKey The key object of type K to look up. sl@0: @return A pointer to the copy of the corresponding value object in the sl@0: array, if the specified key object was found. sl@0: The value object may be modified via this pointer. sl@0: NULL if the specified key object was not found. sl@0: */ sl@0: inline V* Find(const K& aKey) sl@0: { return (V*)RHashTableBase::Find(&aKey, _FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Look up a specified key in the associative array and return a pointer to the sl@0: corresponding value. sl@0: sl@0: @param aKey The key object of type K to look up. sl@0: @return A reference to the copy of the corresponding value object in the sl@0: array, if the specified key object was found. sl@0: The value object may be modified via this reference. sl@0: @leave KErrNotFound if the specified key object was not found. sl@0: */ sl@0: inline V& FindL(const K& aKey) sl@0: { return *(V*)RHashTableBase::FindL(&aKey, _FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Insert a key-value pair into the array. sl@0: sl@0: If the specified key object is not found in the array, a copy of the sl@0: key object along with a copy of the value object are added to the array sl@0: and KErrNone is returned. sl@0: If the specified key object is found in the array, the existing copies sl@0: of both the key and value objects are replaced by the provided objects sl@0: and KErrNone is returned. sl@0: In both cases the objects are copied bitwise into the array. sl@0: sl@0: @param aKey The key object of type K to add to the array. sl@0: @param aValue The value object of type V to associate with aKey. sl@0: @return KErrNone if the key-value pair was added successfully. sl@0: KErrNoMemory if memory could not be allocated to store sl@0: the copies of aKey and aValue. sl@0: */ sl@0: inline TInt Insert(const K& aKey, const V& aValue) sl@0: { return RHashTableBase::ValueInsert(&aKey, sizeof(K), &aValue, _FOFF(SFullElement,iV), sizeof(V)); } sl@0: sl@0: sl@0: /** sl@0: Insert a key-value pair into the array. sl@0: sl@0: If the specified key object is not found in the array, a copy of the sl@0: key object along with a copy of the value object are added to the array sl@0: and KErrNone is returned. sl@0: If the specified key object is found in the array, the existing copies sl@0: of both the key and value objects are replaced by the provided objects sl@0: and KErrNone is returned. sl@0: In both cases the objects are copied bitwise into the array. sl@0: sl@0: @param aKey The key object of type K to add to the array. sl@0: @param aValue The value object of type V to associate with aKey. sl@0: @leave KErrNoMemory if memory could not be allocated to store the copies of aKey and aValue. sl@0: */ sl@0: inline void InsertL(const K& aKey, const V& aValue) sl@0: { RHashTableBase::ValueInsertL(&aKey, sizeof(K), &aValue, _FOFF(SFullElement,iV), sizeof(V)); } sl@0: sl@0: sl@0: /** sl@0: Remove a key-value pair from the array. sl@0: sl@0: @param aKey The key to be removed. sl@0: @return KErrNone if the key object and corresponding value object were sl@0: removed successfully. sl@0: KErrNotFound if the key object was not present in the array. sl@0: */ sl@0: inline TInt Remove(const K& aKey) sl@0: { return RHashTableBase::Remove(&aKey); } sl@0: sl@0: sl@0: /** sl@0: Query the number of key-value pairs in the array. sl@0: sl@0: @return The number of key-value pairs currently in the array. sl@0: */ sl@0: inline TInt Count() const sl@0: { return RHashTableBase::Count(); } sl@0: sl@0: sl@0: /** sl@0: Expand the array to accommodate a specified number of key-value pairs. sl@0: If the set already has enough space for the specified number of elements, no sl@0: action is taken. Any elements already in the set are retained. sl@0: sl@0: @param aCount The number of key-value pairs for which space should be allocated. sl@0: @return KErrNone if the operation completed successfully. sl@0: @return KErrNoMemory if sufficient memory could not be allocated. sl@0: */ sl@0: inline TInt Reserve(TInt aCount) sl@0: { return RHashTableBase::Reserve(aCount); } sl@0: sl@0: sl@0: /** sl@0: Expand the array to accommodate a specified number of key-value pairs. sl@0: If the set already has enough space for the specified number of elements, no sl@0: action is taken. Any elements already in the set are retained. sl@0: sl@0: @param aCount The number of key-value pairs for which space should be allocated. sl@0: @leave KErrNoMemory if sufficient memory could not be allocated. sl@0: */ sl@0: inline void ReserveL(TInt aCount) sl@0: { RHashTableBase::ReserveL(aCount); } sl@0: sl@0: }; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which allows iteration over the elements of a RHashMap sl@0: class. sl@0: sl@0: The array being iterated over may not be modified while an iteration is in progress sl@0: or the iteration operations may malfunction or panic. sl@0: sl@0: @see RHashMap sl@0: */ sl@0: template sl@0: class THashMapIter : public THashTableIterBase sl@0: { sl@0: private: sl@0: sl@0: struct SFullElement sl@0: { sl@0: TUint32 iHash; sl@0: K iK; sl@0: V iV; sl@0: }; sl@0: sl@0: public: sl@0: sl@0: /** sl@0: Construct an iterator over the specified associative array. sl@0: The iterator starts at conceptual position one before the beginning of the list sl@0: being iterated. sl@0: sl@0: @param aMap The array to be iterated over. sl@0: */ sl@0: inline THashMapIter(const RHashMap& aMap) sl@0: : THashTableIterBase(aMap) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Reset the iterator to its initial state. sl@0: sl@0: @param aSet The set to be iterated over. sl@0: */ sl@0: inline void Reset() sl@0: { THashTableIterBase::Reset(); } sl@0: sl@0: sl@0: /** sl@0: Return the key corresponding to the current position of the iterator. sl@0: sl@0: @return A pointer to the key object corresponding to the current position of the sl@0: iterator. sl@0: NULL if the iterator has just been constructed or reset, or if it has sl@0: previously reached the end of an iteration. sl@0: */ sl@0: inline const K* CurrentKey() const sl@0: { return (const K*)THashTableIterBase::Current(_FOFF(SFullElement,iK)); } sl@0: sl@0: sl@0: /** sl@0: Steps the iterator to the next position and returns the corresponding key. sl@0: sl@0: @return A pointer to the key object corresponding to the next position of the sl@0: iterator. sl@0: NULL if the iterator has exhausted all the available key-value pairs. sl@0: */ sl@0: inline const K* NextKey() sl@0: { return (const K*)THashTableIterBase::Next(_FOFF(SFullElement,iK)); } sl@0: sl@0: sl@0: /** sl@0: Return the value corresponding to the current position of the iterator. sl@0: sl@0: @return A pointer to the value object corresponding to the current position of the sl@0: iterator. sl@0: NULL if the iterator has just been constructed or reset, or if it has sl@0: previously reached the end of an iteration. sl@0: */ sl@0: inline V* CurrentValue() sl@0: { return (V*)THashTableIterBase::Current(_FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Steps the iterator to the next position and returns the corresponding value. sl@0: sl@0: @return A pointer to the value object corresponding to the next position of the sl@0: iterator. sl@0: NULL if the iterator has exhausted all the available key-value pairs. sl@0: */ sl@0: inline const V* NextValue() sl@0: { return (const V*)THashTableIterBase::Next(_FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Removes the element at the current iterator position from the hash table. sl@0: If the iterator does not currently point to a valid element, no action is taken. sl@0: Note that the iterator position is not altered so it no longer points to a valid sl@0: element following the Remove(). It is illegal to call either CurrentKey() or sl@0: CurrentValue() on the iterator after calling Remove() - the only legal sl@0: operations are Reset(), NextKey() or NextValue(). sl@0: sl@0: */ sl@0: inline void RemoveCurrent() sl@0: { THashTableIterBase::RemoveCurrent(); } sl@0: }; sl@0: sl@0: sl@0: sl@0: template class TPtrHashMapIter; sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which implements an associative array with key type K and value type V, sl@0: using a probe-sequence hash table. Neither the key nor value objects are copied into the sl@0: table when they are added - only pointers are stored. sl@0: sl@0: */ sl@0: template sl@0: class RPtrHashMap : public RHashTableBase sl@0: { sl@0: private: sl@0: friend class TPtrHashMapIter; sl@0: sl@0: struct SFullElement sl@0: { sl@0: TUint32 iHash; sl@0: K* iK; sl@0: V* iV; sl@0: }; sl@0: public: sl@0: sl@0: /** sl@0: A class which allows iteration over the elements of a RPtrHashMap class. sl@0: sl@0: The array being iterated over may not be modified while an iteration is in progress sl@0: or the iteration operations may malfunction or panic. sl@0: sl@0: @see TPtrHashMapIter sl@0: */ sl@0: typedef TPtrHashMapIter TIter; sl@0: sl@0: /** sl@0: Construct an associative array of key-value pairs of type (K,V) using a sl@0: specified hash function and identity relation. sl@0: The array initially contains no key-value pairs. sl@0: sl@0: @param aHash The hash function used to hash the key objects of type K. sl@0: @param aIdentity The identity relation used to determine if two key objects sl@0: of type K should be considered identical. sl@0: */ sl@0: inline RPtrHashMap(const THashFunction32& aHash, const TIdentityRelation& aIdentity) sl@0: : RHashTableBase(aHash, aIdentity, sizeof(SFullElement), 0) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Construct an associative array of key-value pairs of type (K,V) using a sl@0: default hash function and identity relation. sl@0: The array initially contains no key-value pairs. sl@0: */ sl@0: inline RPtrHashMap() sl@0: : RHashTableBase(Defaults::Hash(), Defaults::Id(), sizeof(SFullElement), 0) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Free all memory used by this array. sl@0: Returns the array to the same state it had following construction. sl@0: */ sl@0: inline void Close() sl@0: { RHashTableBase::Close(); } sl@0: sl@0: sl@0: /** sl@0: Look up a specified key in the associative array and return a pointer to the sl@0: corresponding value. sl@0: sl@0: @param aKey The key object of type K to look up. sl@0: @return A pointer to corresponding value object if the specified key sl@0: object was found. The value object may not be modified via sl@0: this pointer. sl@0: NULL if the specified key object was not found. sl@0: */ sl@0: inline const V* Find(const K& aKey) const sl@0: { return (const V*)RHashTableBase::Find(&aKey, -_FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Look up a specified key in the associative array and return a pointer to the sl@0: corresponding value. sl@0: sl@0: @param aKey The key object of type K to look up. sl@0: @return A reference to corresponding value object if the specified key sl@0: object was found. The value object may not be modified via sl@0: this reference. sl@0: @leave KErrNotFound if the specified key object was not found. sl@0: */ sl@0: inline const V& FindL(const K& aKey) const sl@0: { return *(const V*)RHashTableBase::FindL(&aKey, -_FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Look up a specified key in the associative array and return a pointer to the sl@0: corresponding value. sl@0: sl@0: @param aKey The key object of type K to look up. sl@0: @return A pointer to corresponding value object if the specified key sl@0: object was found. The value object may be modified via sl@0: this pointer. sl@0: NULL if the specified key object was not found. sl@0: */ sl@0: inline V* Find(const K& aKey) sl@0: { return (V*)RHashTableBase::Find(&aKey, -_FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Look up a specified key in the associative array and return a pointer to the sl@0: corresponding value. sl@0: sl@0: @param aKey The key object of type K to look up. sl@0: @return A reference to corresponding value object if the specified key sl@0: object was found. The value object may be modified via sl@0: this reference. sl@0: @leave KErrNotFound if the specified key object was not found. sl@0: */ sl@0: inline V& FindL(const K& aKey) sl@0: { return *(V*)RHashTableBase::FindL(&aKey, -_FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Insert a key-value pair into the array. sl@0: sl@0: If the specified key object is not found in the array, a pointer to the sl@0: key object along with a pointer to the value object are added to the array sl@0: and KErrNone is returned. sl@0: If the specified key object is found in the array, the existing pointers sl@0: to both the key and value objects are replaced by the provided pointers sl@0: and KErrNone is returned. sl@0: In both cases only pointers are stored in the array - the objects themselves sl@0: are not copied. sl@0: sl@0: @param aKey A pointer to the key object of type K to add to the array. sl@0: @param aValue A pointer to the value object of type V to associate with aKey. sl@0: @return KErrNone if the key-value pair was added successfully. sl@0: KErrNoMemory if memory could not be allocated to store sl@0: the pointers aKey and aValue. sl@0: */ sl@0: inline TInt Insert(const K* aKey, const V* aValue) sl@0: { return RHashTableBase::PtrInsert(aKey, aValue); } sl@0: sl@0: sl@0: /** sl@0: Insert a key-value pair into the array. sl@0: sl@0: If the specified key object is not found in the array, a pointer to the sl@0: key object along with a pointer to the value object are added to the array sl@0: and KErrNone is returned. sl@0: If the specified key object is found in the array, the existing pointers sl@0: to both the key and value objects are replaced by the provided pointers sl@0: and KErrNone is returned. sl@0: In both cases only pointers are stored in the array - the objects themselves sl@0: are not copied. sl@0: sl@0: @param aKey A pointer to the key object of type K to add to the array. sl@0: @param aValue A pointer to the value object of type V to associate with aKey. sl@0: @leave KErrNoMemory if memory could not be allocated to store the pointers aKey and aValue. sl@0: */ sl@0: inline void InsertL(const K* aKey, const V* aValue) sl@0: { RHashTableBase::PtrInsertL(aKey, aValue); } sl@0: sl@0: sl@0: /** sl@0: Remove a key-value pair from the array. sl@0: sl@0: @param aKey A pointer to the key to be removed. sl@0: @return KErrNone if the pointers to the key object and corresponding sl@0: value object were removed successfully. sl@0: KErrNotFound if the key object was not present in the array. sl@0: */ sl@0: inline TInt Remove(const K* aKey) sl@0: { return RHashTableBase::Remove(aKey); } sl@0: sl@0: sl@0: /** sl@0: Query the number of key-value pairs in the array. sl@0: sl@0: @return The number of key-value pairs currently in the array. sl@0: */ sl@0: inline TInt Count() const sl@0: { return RHashTableBase::Count(); } sl@0: sl@0: sl@0: /** sl@0: Expand the array to accommodate a specified number of key-value pairs. sl@0: If the set already has enough space for the specified number of elements, no sl@0: action is taken. Any elements already in the set are retained. sl@0: sl@0: @param aCount The number of key-value pairs for which space should be allocated. sl@0: @return KErrNone if the operation completed successfully. sl@0: @return KErrNoMemory if sufficient memory could not be allocated. sl@0: */ sl@0: inline TInt Reserve(TInt aCount) sl@0: { return RHashTableBase::Reserve(aCount); } sl@0: sl@0: sl@0: /** sl@0: Expand the array to accommodate a specified number of key-value pairs. sl@0: If the set already has enough space for the specified number of elements, no sl@0: action is taken. Any elements already in the set are retained. sl@0: sl@0: @param aCount The number of key-value pairs for which space should be allocated. sl@0: @leave KErrNoMemory if sufficient memory could not be allocated. sl@0: */ sl@0: inline void ReserveL(TInt aCount) sl@0: { RHashTableBase::ReserveL(aCount); } sl@0: sl@0: sl@0: void ResetAndDestroy(); sl@0: }; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A templated class which allows iteration over the elements of a RPtrHashMap sl@0: class. sl@0: sl@0: The array being iterated over may not be modified while an iteration is in progress sl@0: or the iteration operations may malfunction or panic. sl@0: sl@0: @see RPtrHashMap sl@0: */ sl@0: template sl@0: class TPtrHashMapIter : public THashTableIterBase sl@0: { sl@0: private: sl@0: sl@0: struct SFullElement sl@0: { sl@0: TUint32 iHash; sl@0: K* iK; sl@0: V* iV; sl@0: }; sl@0: public: sl@0: sl@0: /** sl@0: Construct an iterator over the specified associative array. sl@0: The iterator starts at conceptual position one before the beginning of the list sl@0: being iterated. sl@0: sl@0: @param aMap The array to be iterated over. sl@0: */ sl@0: inline TPtrHashMapIter(const RPtrHashMap& aMap) sl@0: : THashTableIterBase(aMap) sl@0: {} sl@0: sl@0: sl@0: /** sl@0: Reset the iterator to its initial state. sl@0: sl@0: @param aSet The set to be iterated over. sl@0: */ sl@0: inline void Reset() sl@0: { THashTableIterBase::Reset(); } sl@0: sl@0: sl@0: /** sl@0: Return the key corresponding to the current position of the iterator. sl@0: sl@0: @return A pointer to the key object corresponding to the current position of the sl@0: iterator. sl@0: NULL if the iterator has just been constructed or reset, or if it has sl@0: previously reached the end of an iteration. sl@0: */ sl@0: inline const K* CurrentKey() const sl@0: { return (const K*)THashTableIterBase::Current(-_FOFF(SFullElement,iK)); } sl@0: sl@0: sl@0: /** sl@0: Steps the iterator to the next position and returns the corresponding key. sl@0: sl@0: @return A pointer to the key object corresponding to the next position of the sl@0: iterator. sl@0: NULL if the iterator has exhausted all the available key-value pairs. sl@0: */ sl@0: inline const K* NextKey() sl@0: { return (const K*)THashTableIterBase::Next(-_FOFF(SFullElement,iK)); } sl@0: sl@0: sl@0: /** sl@0: Return the value corresponding to the current position of the iterator. sl@0: sl@0: @return A pointer to the value object corresponding to the current position of the sl@0: iterator. sl@0: NULL if the iterator has just been constructed or reset, or if it has sl@0: previously reached the end of an iteration. sl@0: */ sl@0: inline const V* CurrentValue() const sl@0: { return (const V*)THashTableIterBase::Current(-_FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Steps the iterator to the next position and returns the corresponding value. sl@0: sl@0: @return A pointer to the value object corresponding to the next position of the sl@0: iterator. sl@0: NULL if the iterator has exhausted all the available key-value pairs. sl@0: */ sl@0: inline const V* NextValue() sl@0: { return (const V*)THashTableIterBase::Next(-_FOFF(SFullElement,iV)); } sl@0: sl@0: sl@0: /** sl@0: Removes the element at the current iterator position from the hash table. sl@0: If the iterator does not currently point to a valid element, no action is taken. sl@0: Note that the iterator position is not altered so it no longer points to a valid sl@0: element following the Remove(). It is illegal to call either CurrentKey() or sl@0: CurrentValue() on the iterator after calling Remove() - the only legal sl@0: operations are Reset(), NextKey() or NextValue(). sl@0: sl@0: */ sl@0: inline void RemoveCurrent() sl@0: { THashTableIterBase::RemoveCurrent(); } sl@0: }; sl@0: sl@0: sl@0: sl@0: /** sl@0: Deletes all the objects of type T to which pointers are stored in this set. sl@0: Then frees all the memory used by the set and returns the set to the same state sl@0: as immediately following construction. sl@0: */ sl@0: template sl@0: void RPtrHashSet::ResetAndDestroy() sl@0: { sl@0: TPtrHashSetIter iter(*this); sl@0: T* p; sl@0: do { sl@0: p = (T*)iter.Next(); sl@0: delete p; sl@0: } while(p); sl@0: Close(); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Deletes all the key objects of type K and corresponding value objects of type V sl@0: to which pointers are stored in this array. sl@0: Then frees all the memory used by the array and returns the array to the same sl@0: state as immediately following construction. sl@0: */ sl@0: template sl@0: void RPtrHashMap::ResetAndDestroy() sl@0: { sl@0: TPtrHashMapIter iter(*this); sl@0: K* p; sl@0: V* q; sl@0: do { sl@0: p = (K*)iter.NextKey(); sl@0: q = (V*)iter.CurrentValue(); sl@0: delete p; sl@0: delete q; sl@0: } while(p); sl@0: Close(); sl@0: } sl@0: sl@0: sl@0: #endif