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