epoc32/include/e32hashtab.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100 (2010-03-31)
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@2
     1
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
williamr@2
     2
// All rights reserved.
williamr@2
     3
// This component and the accompanying materials are made available
williamr@4
     4
// under the terms of the License "Eclipse Public License v1.0"
williamr@2
     5
// which accompanies this distribution, and is available
williamr@4
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
williamr@2
     7
//
williamr@2
     8
// Initial Contributors:
williamr@2
     9
// Nokia Corporation - initial contribution.
williamr@2
    10
//
williamr@2
    11
// Contributors:
williamr@2
    12
//
williamr@2
    13
// Description:
williamr@2
    14
// e32/include/e32hashtab.h
williamr@2
    15
// 
williamr@2
    16
//
williamr@2
    17
williamr@2
    18
#ifndef __E32HASHTAB_H__
williamr@2
    19
#define __E32HASHTAB_H__
williamr@2
    20
#include <e32cmn.h>
williamr@2
    21
williamr@2
    22
/**
williamr@2
    23
@publishedAll
williamr@2
    24
@released
williamr@2
    25
williamr@2
    26
Defines a function type used by a THashFunction32 object. 
williamr@2
    27
williamr@2
    28
A function of this type implements an algorithm for producing a 32 bit hash
williamr@2
    29
value from a key.
williamr@2
    30
williamr@2
    31
@see THashFunction32
williamr@2
    32
*/
williamr@2
    33
typedef TUint32 (*TGeneralHashFunction32)(const TAny*);
williamr@2
    34
williamr@2
    35
williamr@2
    36
/**
williamr@2
    37
@publishedAll
williamr@2
    38
@released
williamr@2
    39
williamr@2
    40
A templated class which packages a function that calculates a 32 bit hash
williamr@2
    41
value from a key of templated type.
williamr@2
    42
williamr@2
    43
A THashFunction32<T> object is constructed and passed as a parameter to 
williamr@2
    44
member functions of the hash table classes RHashSet<T>, RPtrHashSet<T>,
williamr@2
    45
RHashMap<T,V> and RPtrHashMap<T,V>.
williamr@2
    46
williamr@2
    47
@see RHashSet
williamr@2
    48
@see RPtrHashSet
williamr@2
    49
@see RHashMap
williamr@2
    50
@see RPtrHashMap
williamr@2
    51
*/
williamr@2
    52
template <class T>
williamr@2
    53
class THashFunction32
williamr@2
    54
	{
williamr@2
    55
public:
williamr@2
    56
	inline THashFunction32( TUint32 (*aHashFunc)(const T&) )
williamr@2
    57
		{ iHashFunction = (TGeneralHashFunction32)aHashFunc; }
williamr@2
    58
	inline operator TGeneralHashFunction32() const
williamr@2
    59
		{ return iHashFunction; }
williamr@2
    60
	inline TUint32 Hash(const T& aKey) const
williamr@2
    61
		{ return (*iHashFunction)(&aKey); }
williamr@2
    62
private:
williamr@2
    63
	TGeneralHashFunction32 iHashFunction;
williamr@2
    64
	};
williamr@2
    65
williamr@2
    66
williamr@2
    67
/**
williamr@2
    68
@publishedAll
williamr@2
    69
@released
williamr@2
    70
williamr@2
    71
A set of common hashing functions for frequently occurring types.
williamr@2
    72
williamr@2
    73
@see RHashSet
williamr@2
    74
@see RPtrHashSet
williamr@2
    75
@see RHashMap
williamr@2
    76
@see RPtrHashMap
williamr@2
    77
*/
williamr@2
    78
class DefaultHash
williamr@2
    79
	{
williamr@2
    80
public:
williamr@2
    81
	IMPORT_C static TUint32 Integer(const TInt&);
williamr@2
    82
	IMPORT_C static TUint32 Des8(const TDesC8&);
williamr@2
    83
	IMPORT_C static TUint32 Des16(const TDesC16&);
williamr@2
    84
	IMPORT_C static TUint32 IntegerPtr(TInt* const &);
williamr@2
    85
	IMPORT_C static TUint32 Des8Ptr(TDesC8* const &);
williamr@2
    86
	IMPORT_C static TUint32 Des16Ptr(TDesC16* const &);
williamr@2
    87
	};
williamr@2
    88
williamr@2
    89
williamr@2
    90
williamr@2
    91
class THashTableIterBase;
williamr@2
    92
williamr@2
    93
/**
williamr@2
    94
@internalComponent
williamr@2
    95
williamr@2
    96
Base class used in the derivation of RHashSet<T>, RPtrHashSet<T>,
williamr@2
    97
RHashMap<K,V> and RPtrHashMap<K,V>.
williamr@2
    98
williamr@2
    99
This class provides a general hash table implementation using probe sequences
williamr@2
   100
generated by pseudo-double hashing.
williamr@2
   101
The class is internal and is not intended for use.
williamr@2
   102
*/
williamr@2
   103
class RHashTableBase
williamr@2
   104
	{
williamr@2
   105
public:
williamr@2
   106
	enum TDefaultSpecifier
williamr@2
   107
		{
williamr@2
   108
		EDefaultSpecifier_Normal,
williamr@2
   109
		};
williamr@2
   110
williamr@2
   111
protected:
williamr@2
   112
	template<class K, TDefaultSpecifier S>
williamr@2
   113
	class Defaults
williamr@2
   114
		{
williamr@2
   115
	public:
williamr@2
   116
		inline static TGeneralHashFunction32 Hash();
williamr@2
   117
		inline static TGeneralIdentityRelation Id();
williamr@2
   118
		};
williamr@2
   119
williamr@2
   120
protected:
williamr@2
   121
	enum TElementState
williamr@2
   122
		{
williamr@2
   123
		EEmpty=0,		// entry is vacant
williamr@2
   124
		EDeleted=1,		// entry has been deleted
williamr@2
   125
		EGen0=2,		// entry is occupied, generation number 0
williamr@2
   126
		EGen1=3,		// entry is occupied, generation number 1
williamr@2
   127
		EStateMask=3,
williamr@2
   128
		EOccupiedMask=2,
williamr@2
   129
		};
williamr@2
   130
williamr@2
   131
	struct SElement
williamr@2
   132
		{
williamr@2
   133
		inline void SetEmpty() {iHash=EEmpty;}
williamr@2
   134
		inline void SetDeleted() {iHash=EDeleted;}
williamr@2
   135
		inline TBool IsEmpty() const {return (iHash&EStateMask)==EEmpty;}
williamr@2
   136
		inline TBool IsDeleted() const {return (iHash&EStateMask)==EDeleted;}
williamr@2
   137
		inline TBool IsEmptyOrDeleted() const {return !(iHash&EOccupiedMask);}
williamr@2
   138
williamr@2
   139
		TUint32	iHash;			// bits 2-31 = 30 bit hash value, bits 0,1 = state
williamr@2
   140
		};
williamr@2
   141
williamr@2
   142
protected:
williamr@2
   143
	IMPORT_C RHashTableBase(TGeneralHashFunction32, TGeneralIdentityRelation, TInt aElementSize, TInt aKeyOffset);
williamr@2
   144
	IMPORT_C void Close();
williamr@2
   145
	IMPORT_C TAny* Find(const TAny* aKey, TInt aOffset=0) const;
williamr@2
   146
	IMPORT_C TAny* FindL(const TAny* aKey, TInt aOffset=0) const;
williamr@2
   147
	TInt Insert(const TAny* aKey, TAny*& aElement);
williamr@2
   148
	IMPORT_C TInt PtrInsert(const TAny* aKey, const TAny* aValue);
williamr@2
   149
	IMPORT_C void PtrInsertL(const TAny* aKey, const TAny* aValue);
williamr@2
   150
	IMPORT_C TInt ValueInsert(const TAny* aKey, TInt aKeySize, const TAny* aValue, TInt aValueOffset, TInt aValueSize);
williamr@2
   151
	IMPORT_C void ValueInsertL(const TAny* aKey, TInt aKeySize, const TAny* aValue, TInt aValueOffset, TInt aValueSize);
williamr@2
   152
	IMPORT_C TInt Remove(const TAny* aKey);
williamr@2
   153
	IMPORT_C TInt Count() const;
williamr@2
   154
	IMPORT_C TInt Reserve(TInt aCount);
williamr@2
   155
	IMPORT_C void ReserveL(TInt aCount);
williamr@2
   156
	IMPORT_C void ConsistencyCheck(TUint32* aDeleted=0, TUint32* aComparisons=0, TUint32 aChainLimit=0, TUint32* aChainInfo=0);
williamr@2
   157
private:
williamr@2
   158
	void SetThresholds();
williamr@2
   159
	TInt ExpandTable(TInt aNewIndexBits);
williamr@2
   160
	void ShrinkTable();
williamr@2
   161
	void ReformTable(TUint aNewIndexBits);
williamr@2
   162
	void VerifyReform();
williamr@2
   163
private:
williamr@2
   164
	inline SElement* Element(TInt aIndex)
williamr@2
   165
		{return (SElement*)(((TUint8*)iElements) + aIndex*iElementSize);}
williamr@2
   166
	inline const SElement* ElementC(TInt aIndex) const
williamr@2
   167
		{return (const SElement*)(((TUint8*)iElements) + aIndex*iElementSize);}
williamr@2
   168
	inline TAny* GetKey(const SElement* aElement) const
williamr@2
   169
		{return iKeyOffset ? ((TUint8*)aElement + iKeyOffset) : (TAny*)((TUint32*)aElement)[1];}
williamr@2
   170
private:
williamr@2
   171
	TGeneralHashFunction32 iHashFunc;	// generates the hash from a given key
williamr@2
   172
	TGeneralIdentityRelation iIdFunc;	// compare two keys for equality
williamr@2
   173
	TUint8 iIndexBits;					// number of bits used to index the table
williamr@2
   174
	TUint8 iGeneration;					// 2 or 3, generation number used when traversing entire table
williamr@2
   175
	TUint8 iKeyOffset;					// offset to key
williamr@2
   176
	TUint8 iPad0;
williamr@2
   177
	TAny* iElements;
williamr@2
   178
	TUint32 iCount;						// number of valid entries
williamr@2
   179
	TUint32 iEmptyCount;				// number of empty entries
williamr@2
   180
	TUint32 iLowerThreshold;			// shrink if count drops below this
williamr@2
   181
	TUint32 iUpperThreshold;			// expand if count rises above this
williamr@2
   182
	TUint32 iCleanThreshold;			// clean table if count of empty entries falls below this
williamr@2
   183
	TInt iElementSize;
williamr@2
   184
	TInt iPad1;							// expansion room
williamr@2
   185
	TInt iPad2;
williamr@2
   186
williamr@2
   187
	friend struct RHashTableBase::SElement;
williamr@2
   188
	friend class THashTableIterBase;
williamr@2
   189
	friend class HashTest;
williamr@2
   190
	};
williamr@2
   191
williamr@2
   192
williamr@2
   193
/**
williamr@2
   194
@internalComponent
williamr@2
   195
*/
williamr@2
   196
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TInt*, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   197
	{
williamr@2
   198
public:
williamr@2
   199
	inline static TGeneralHashFunction32 Hash();
williamr@2
   200
	inline static TGeneralIdentityRelation Id();
williamr@2
   201
	};
williamr@2
   202
williamr@2
   203
/**
williamr@2
   204
@internalComponent
williamr@2
   205
*/
williamr@2
   206
inline TGeneralHashFunction32 RHashTableBase::Defaults<TInt*, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   207
	{return (TGeneralHashFunction32)&DefaultHash::IntegerPtr;}
williamr@2
   208
williamr@2
   209
/**
williamr@2
   210
@internalComponent
williamr@2
   211
*/
williamr@2
   212
inline TGeneralIdentityRelation RHashTableBase::Defaults<TInt*, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   213
	{return (TGeneralIdentityRelation)&DefaultIdentity::IntegerPtr;}
williamr@2
   214
williamr@2
   215
/**
williamr@2
   216
@internalComponent
williamr@2
   217
*/
williamr@2
   218
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TInt32*, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   219
	{
williamr@2
   220
public:
williamr@2
   221
	inline static TGeneralHashFunction32 Hash();
williamr@2
   222
	inline static TGeneralIdentityRelation Id();
williamr@2
   223
	};
williamr@2
   224
williamr@2
   225
/**
williamr@2
   226
@internalComponent
williamr@2
   227
*/
williamr@2
   228
inline TGeneralHashFunction32 RHashTableBase::Defaults<TInt32*, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   229
	{return (TGeneralHashFunction32)&DefaultHash::IntegerPtr;}
williamr@2
   230
williamr@2
   231
/**
williamr@2
   232
@internalComponent
williamr@2
   233
*/
williamr@2
   234
inline TGeneralIdentityRelation RHashTableBase::Defaults<TInt32*, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   235
	{return (TGeneralIdentityRelation)&DefaultIdentity::IntegerPtr;}
williamr@2
   236
williamr@2
   237
/**
williamr@2
   238
@internalComponent
williamr@2
   239
*/
williamr@2
   240
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TUint*, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   241
	{
williamr@2
   242
public:
williamr@2
   243
	inline static TGeneralHashFunction32 Hash();
williamr@2
   244
	inline static TGeneralIdentityRelation Id();
williamr@2
   245
	};
williamr@2
   246
/**
williamr@2
   247
@internalComponent
williamr@2
   248
*/
williamr@2
   249
inline TGeneralHashFunction32 RHashTableBase::Defaults<TUint*, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   250
	{return (TGeneralHashFunction32)&DefaultHash::IntegerPtr;}
williamr@2
   251
williamr@2
   252
/**
williamr@2
   253
@internalComponent
williamr@2
   254
*/
williamr@2
   255
inline TGeneralIdentityRelation RHashTableBase::Defaults<TUint*, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   256
	{return (TGeneralIdentityRelation)&DefaultIdentity::IntegerPtr;}
williamr@2
   257
williamr@2
   258
williamr@2
   259
/**
williamr@2
   260
@internalComponent
williamr@2
   261
*/
williamr@2
   262
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TUint32*, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   263
	{
williamr@2
   264
public:
williamr@2
   265
	inline static TGeneralHashFunction32 Hash();
williamr@2
   266
	inline static TGeneralIdentityRelation Id();
williamr@2
   267
	};
williamr@2
   268
/**
williamr@2
   269
@internalComponent
williamr@2
   270
*/
williamr@2
   271
inline TGeneralHashFunction32 RHashTableBase::Defaults<TUint32*, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   272
	{return (TGeneralHashFunction32)&DefaultHash::IntegerPtr;}
williamr@2
   273
williamr@2
   274
/**
williamr@2
   275
@internalComponent
williamr@2
   276
*/
williamr@2
   277
inline TGeneralIdentityRelation RHashTableBase::Defaults<TUint32*, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   278
	{return (TGeneralIdentityRelation)&DefaultIdentity::IntegerPtr;}
williamr@2
   279
williamr@2
   280
/**
williamr@2
   281
@internalComponent
williamr@2
   282
*/
williamr@2
   283
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TDesC8*, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   284
	{
williamr@2
   285
public:
williamr@2
   286
	inline static TGeneralHashFunction32 Hash();
williamr@2
   287
	inline static TGeneralIdentityRelation Id();
williamr@2
   288
	};
williamr@2
   289
/**
williamr@2
   290
@internalComponent
williamr@2
   291
*/
williamr@2
   292
inline TGeneralHashFunction32 RHashTableBase::Defaults<TDesC8*, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   293
	{return (TGeneralHashFunction32)&DefaultHash::Des8Ptr;}
williamr@2
   294
williamr@2
   295
/**
williamr@2
   296
@internalComponent
williamr@2
   297
*/
williamr@2
   298
inline TGeneralIdentityRelation RHashTableBase::Defaults<TDesC8*, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   299
	{return (TGeneralIdentityRelation)&DefaultIdentity::Des8Ptr;}
williamr@2
   300
williamr@2
   301
/**
williamr@2
   302
@internalComponent
williamr@2
   303
*/
williamr@2
   304
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TDesC16*, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   305
	{
williamr@2
   306
public:
williamr@2
   307
	inline static TGeneralHashFunction32 Hash();
williamr@2
   308
	inline static TGeneralIdentityRelation Id();
williamr@2
   309
	};
williamr@2
   310
/**
williamr@2
   311
@internalComponent
williamr@2
   312
*/
williamr@2
   313
inline TGeneralHashFunction32 RHashTableBase::Defaults<TDesC16*, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   314
	{return (TGeneralHashFunction32)&DefaultHash::Des16Ptr;}
williamr@2
   315
williamr@2
   316
/**
williamr@2
   317
@internalComponent
williamr@2
   318
*/
williamr@2
   319
inline TGeneralIdentityRelation RHashTableBase::Defaults<TDesC16*, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   320
	{return (TGeneralIdentityRelation)&DefaultIdentity::Des16Ptr;}
williamr@2
   321
williamr@2
   322
/**
williamr@2
   323
@internalComponent
williamr@2
   324
*/
williamr@2
   325
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TInt, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   326
	{
williamr@2
   327
public:
williamr@2
   328
	inline static TGeneralHashFunction32 Hash();
williamr@2
   329
	inline static TGeneralIdentityRelation Id();
williamr@2
   330
	};
williamr@2
   331
williamr@2
   332
/**
williamr@2
   333
@internalComponent
williamr@2
   334
*/
williamr@2
   335
inline TGeneralHashFunction32 RHashTableBase::Defaults<TInt, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   336
	{return (TGeneralHashFunction32)&DefaultHash::Integer;}
williamr@2
   337
williamr@2
   338
/**
williamr@2
   339
@internalComponent
williamr@2
   340
*/
williamr@2
   341
inline TGeneralIdentityRelation RHashTableBase::Defaults<TInt, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   342
	{return (TGeneralIdentityRelation)&DefaultIdentity::Integer;}
williamr@2
   343
williamr@2
   344
/**
williamr@2
   345
@internalComponent
williamr@2
   346
*/
williamr@2
   347
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TInt32, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   348
	{
williamr@2
   349
public:
williamr@2
   350
	inline static TGeneralHashFunction32 Hash();
williamr@2
   351
	inline static TGeneralIdentityRelation Id();
williamr@2
   352
	};
williamr@2
   353
williamr@2
   354
/**
williamr@2
   355
@internalComponent
williamr@2
   356
*/
williamr@2
   357
inline TGeneralHashFunction32 RHashTableBase::Defaults<TInt32, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   358
	{return (TGeneralHashFunction32)&DefaultHash::Integer;}
williamr@2
   359
williamr@2
   360
/**
williamr@2
   361
@internalComponent
williamr@2
   362
*/
williamr@2
   363
inline TGeneralIdentityRelation RHashTableBase::Defaults<TInt32, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   364
	{return (TGeneralIdentityRelation)&DefaultIdentity::Integer;}
williamr@2
   365
williamr@2
   366
/**
williamr@2
   367
@internalComponent
williamr@2
   368
*/
williamr@2
   369
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TUint, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   370
	{
williamr@2
   371
public:
williamr@2
   372
	inline static TGeneralHashFunction32 Hash();
williamr@2
   373
	inline static TGeneralIdentityRelation Id();
williamr@2
   374
	};
williamr@2
   375
williamr@2
   376
/**
williamr@2
   377
@internalComponent
williamr@2
   378
*/
williamr@2
   379
inline TGeneralHashFunction32 RHashTableBase::Defaults<TUint, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   380
	{return (TGeneralHashFunction32)&DefaultHash::Integer;}
williamr@2
   381
williamr@2
   382
/**
williamr@2
   383
@internalComponent
williamr@2
   384
*/
williamr@2
   385
inline TGeneralIdentityRelation RHashTableBase::Defaults<TUint, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   386
	{return (TGeneralIdentityRelation)&DefaultIdentity::Integer;}
williamr@2
   387
williamr@2
   388
williamr@2
   389
/**
williamr@2
   390
@internalComponent
williamr@2
   391
*/
williamr@2
   392
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TUint32, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   393
	{
williamr@2
   394
public:
williamr@2
   395
	inline static TGeneralHashFunction32 Hash();
williamr@2
   396
	inline static TGeneralIdentityRelation Id();
williamr@2
   397
	};
williamr@2
   398
williamr@2
   399
/**
williamr@2
   400
@internalComponent
williamr@2
   401
*/
williamr@2
   402
inline TGeneralHashFunction32 RHashTableBase::Defaults<TUint32, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   403
	{return (TGeneralHashFunction32)&DefaultHash::Integer;}
williamr@2
   404
williamr@2
   405
/**
williamr@2
   406
@internalComponent
williamr@2
   407
*/
williamr@2
   408
inline TGeneralIdentityRelation RHashTableBase::Defaults<TUint32, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   409
	{return (TGeneralIdentityRelation)&DefaultIdentity::Integer;}
williamr@2
   410
williamr@2
   411
williamr@2
   412
/**
williamr@2
   413
@internalComponent
williamr@2
   414
*/
williamr@2
   415
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TDesC8, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   416
	{
williamr@2
   417
public:
williamr@2
   418
	inline static TGeneralHashFunction32 Hash();
williamr@2
   419
	inline static TGeneralIdentityRelation Id();
williamr@2
   420
	};
williamr@2
   421
williamr@2
   422
/**
williamr@2
   423
@internalComponent
williamr@2
   424
*/
williamr@2
   425
inline TGeneralHashFunction32 RHashTableBase::Defaults<TDesC8, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   426
	{return (TGeneralHashFunction32)&DefaultHash::Des8;}
williamr@2
   427
williamr@2
   428
/**
williamr@2
   429
@internalComponent
williamr@2
   430
*/
williamr@2
   431
inline TGeneralIdentityRelation RHashTableBase::Defaults<TDesC8, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   432
	{return (TGeneralIdentityRelation)&DefaultIdentity::Des8;}
williamr@2
   433
williamr@2
   434
williamr@2
   435
/**
williamr@2
   436
@internalComponent
williamr@2
   437
*/
williamr@2
   438
TEMPLATE_SPECIALIZATION class RHashTableBase::Defaults<TDesC16, RHashTableBase::EDefaultSpecifier_Normal>
williamr@2
   439
	{
williamr@2
   440
public:
williamr@2
   441
	inline static TGeneralHashFunction32 Hash();
williamr@2
   442
	inline static TGeneralIdentityRelation Id();
williamr@2
   443
	};
williamr@2
   444
williamr@2
   445
/**
williamr@2
   446
@internalComponent
williamr@2
   447
*/
williamr@2
   448
inline TGeneralHashFunction32 RHashTableBase::Defaults<TDesC16, RHashTableBase::EDefaultSpecifier_Normal>::Hash()
williamr@2
   449
	{return (TGeneralHashFunction32)&DefaultHash::Des16;}
williamr@2
   450
williamr@2
   451
/**
williamr@2
   452
@internalComponent
williamr@2
   453
*/
williamr@2
   454
inline TGeneralIdentityRelation RHashTableBase::Defaults<TDesC16, RHashTableBase::EDefaultSpecifier_Normal>::Id()
williamr@2
   455
	{return (TGeneralIdentityRelation)&DefaultIdentity::Des16;}
williamr@2
   456
williamr@2
   457
williamr@2
   458
williamr@2
   459
williamr@2
   460
/**
williamr@2
   461
@internalComponent
williamr@2
   462
williamr@2
   463
Base class used in the derivation of THashSetIter<T>, TPtrHashSetIter<T>,
williamr@2
   464
THashMapIter<K,V> and TPtrHashMapIter<K,V>.
williamr@2
   465
williamr@2
   466
This class provides iteration capability for the hash table classes derived
williamr@2
   467
from RHashTableBase.
williamr@2
   468
The class is internal and is not intended for use.
williamr@2
   469
*/
williamr@2
   470
class THashTableIterBase
williamr@2
   471
	{
williamr@2
   472
protected:
williamr@2
   473
	IMPORT_C THashTableIterBase(const RHashTableBase& aTable);
williamr@2
   474
	IMPORT_C void Reset();
williamr@2
   475
	IMPORT_C const TAny* Next(TInt aOffset=0);
williamr@2
   476
	IMPORT_C const TAny* Current(TInt aOffset=0) const;
williamr@2
   477
	IMPORT_C void RemoveCurrent();
williamr@2
   478
private:
williamr@2
   479
	const RHashTableBase& iTbl;
williamr@2
   480
	TInt iIndex;
williamr@2
   481
	TInt iPad1;							// expansion room
williamr@2
   482
	TInt iPad2;
williamr@2
   483
	};
williamr@2
   484
williamr@2
   485
williamr@2
   486
williamr@2
   487
template <class T> class THashSetIter;
williamr@2
   488
williamr@2
   489
/**
williamr@2
   490
@publishedAll
williamr@2
   491
@released
williamr@2
   492
williamr@2
   493
A templated class which implements an unordered extensional set of objects of
williamr@2
   494
type T using a probe-sequence hash table. The objects are copied into the set
williamr@2
   495
when they are added. A bitwise binary copy is used here, so the type T must
williamr@2
   496
not implement a nontrivial copy constructor.
williamr@2
   497
williamr@2
   498
*/
williamr@2
   499
template <class T>
williamr@2
   500
class RHashSet : public RHashTableBase
williamr@2
   501
	{
williamr@2
   502
private:
williamr@2
   503
	friend class THashSetIter<T>;
williamr@4
   504
	
williamr@2
   505
	struct SFullElement
williamr@2
   506
		{
williamr@2
   507
		TUint32 iHash;
williamr@2
   508
		T iT;
williamr@2
   509
		};
williamr@2
   510
williamr@2
   511
public:
williamr@2
   512
williamr@2
   513
/**
williamr@2
   514
A class which allows iteration over the elements of a RHashSet<T> class.
williamr@2
   515
williamr@2
   516
The set being iterated over may not be modified while an iteration is in progress
williamr@2
   517
or the iteration operations may malfunction or panic.
williamr@2
   518
williamr@2
   519
@see THashSetIter<T>
williamr@2
   520
*/
williamr@2
   521
	typedef THashSetIter<T> TIter;
williamr@2
   522
	
williamr@2
   523
/**
williamr@2
   524
Construct a set of objects of type T using a specified hash function and identity relation.
williamr@2
   525
The set is initially empty.
williamr@2
   526
williamr@2
   527
@param	aHash		The hash function used to hash the objects of type T.
williamr@2
   528
@param	aIdentity	The identity relation used to determine if two objects of type T
williamr@2
   529
					should be considered identical.
williamr@2
   530
*/
williamr@2
   531
	inline RHashSet(const THashFunction32<T>& aHash, const TIdentityRelation<T>& aIdentity)
williamr@2
   532
		:	RHashTableBase(aHash, aIdentity, sizeof(SFullElement), _FOFF(SFullElement,iT))
williamr@2
   533
		{}
williamr@2
   534
williamr@2
   535
williamr@2
   536
/**
williamr@2
   537
Construct a set of objects of type T using a default hash function and identity relation.
williamr@2
   538
The set is initially empty.
williamr@2
   539
*/
williamr@2
   540
	inline RHashSet()
williamr@2
   541
		:	RHashTableBase(Defaults<T,EDefaultSpecifier_Normal>::Hash(), Defaults<T,EDefaultSpecifier_Normal>::Id(), sizeof(SFullElement), _FOFF(SFullElement,iT))
williamr@2
   542
		{}
williamr@2
   543
williamr@2
   544
williamr@2
   545
/**
williamr@2
   546
Free all memory used by this set.
williamr@2
   547
Returns the set to the same state it had following construction.
williamr@2
   548
*/
williamr@2
   549
	inline void Close()
williamr@2
   550
		{ RHashTableBase::Close(); }
williamr@2
   551
williamr@2
   552
williamr@2
   553
/**
williamr@2
   554
Locate a specified element in the set.
williamr@2
   555
williamr@2
   556
@param	aKey	The object of type T to search for.
williamr@2
   557
@return			A pointer to the copy of the specified object in the set, if it
williamr@2
   558
				exists. The object may not be modified via this pointer.
williamr@2
   559
				NULL if the specified object is not a member of this set.
williamr@2
   560
*/
williamr@2
   561
	inline const T* Find(const T& aKey) const
williamr@2
   562
		{ return (const T*)RHashTableBase::Find(&aKey, _FOFF(SFullElement,iT)); }
williamr@2
   563
williamr@2
   564
williamr@2
   565
/**
williamr@2
   566
Locate a specified element in the set.
williamr@2
   567
williamr@2
   568
@param	aKey	The object of type T to search for.
williamr@2
   569
@return			A reference to the copy of the specified object in the set, if it
williamr@2
   570
				exists. The object may not be modified via this reference.
williamr@2
   571
@leave			KErrNotFound if the specified object is not a member of this set.
williamr@2
   572
*/
williamr@2
   573
	inline const T& FindL(const T& aKey) const
williamr@2
   574
		{ return *(const T*)RHashTableBase::FindL(&aKey, _FOFF(SFullElement,iT)); }
williamr@2
   575
williamr@2
   576
williamr@2
   577
/**
williamr@2
   578
Locate a specified element in the set.
williamr@2
   579
williamr@2
   580
@param	aKey	The object of type T to search for.
williamr@2
   581
@return			A pointer to the copy of the specified object in the set, if it
williamr@2
   582
				exists. The object may be modified via this pointer. Care should
williamr@2
   583
				be taken not to modify any parts of the object which are used by
williamr@2
   584
				either the hash function or the identity relation for this set.
williamr@2
   585
				If this is done the set may become inconsistent, resulting in
williamr@2
   586
				malfunctions and/or panics at a later time.
williamr@2
   587
				NULL if the specified object is not a member of this set.
williamr@2
   588
*/
williamr@2
   589
	inline T* Find(const T& aKey)
williamr@2
   590
		{ return (T*)RHashTableBase::Find(&aKey, _FOFF(SFullElement,iT)); }
williamr@2
   591
williamr@2
   592
williamr@2
   593
/**
williamr@2
   594
Locate a specified element in the set.
williamr@2
   595
williamr@2
   596
@param	aKey	The object of type T to search for.
williamr@2
   597
@return			A reference to the copy of the specified object in the set, if it
williamr@2
   598
				exists. The object may be modified via this reference. Care should
williamr@2
   599
				be taken not to modify any parts of the object which are used by
williamr@2
   600
				either the hash function or the identity relation for this set.
williamr@2
   601
				If this is done the set may become inconsistent, resulting in
williamr@2
   602
				malfunctions and/or panics at a later time.
williamr@2
   603
@leave			KErrNotFound if the specified object is not a member of this set.
williamr@2
   604
*/
williamr@2
   605
	inline T& FindL(const T& aKey)
williamr@2
   606
		{ return *(T*)RHashTableBase::FindL(&aKey, _FOFF(SFullElement,iT)); }
williamr@2
   607
williamr@2
   608
williamr@2
   609
/**
williamr@2
   610
Insert an element into the set.
williamr@2
   611
williamr@2
   612
If the specified object is not currently a member of the set, a copy of the
williamr@2
   613
object is added to the set and KErrNone is returned.
williamr@2
   614
If the specified object is currently a member of the set, the existing copy
williamr@2
   615
of the object is replaced by the provided object and KErrNone is
williamr@2
   616
returned.
williamr@2
   617
In both cases the object is copied bitwise into the set.
williamr@2
   618
williamr@2
   619
@param	aKey	The object of type T to add to the set.
williamr@2
   620
@return			KErrNone if the object was added successfully.
williamr@2
   621
				KErrNoMemory if memory could not be allocated to store
williamr@2
   622
					the copy of aKey.
williamr@2
   623
*/
williamr@2
   624
	inline TInt Insert(const T& aKey)
williamr@2
   625
		{ return RHashTableBase::ValueInsert(&aKey, sizeof(T), 0, 0, 0); }
williamr@2
   626
williamr@2
   627
williamr@2
   628
/**
williamr@2
   629
Insert an element into the set.
williamr@2
   630
williamr@2
   631
If the specified object is not currently a member of the set, a copy of the
williamr@2
   632
object is added to the set and KErrNone is returned.
williamr@2
   633
If the specified object is currently a member of the set, the existing copy
williamr@2
   634
of the object is replaced by the provided object and KErrNone is
williamr@2
   635
returned.
williamr@2
   636
In both cases the object is copied bitwise into the set.
williamr@2
   637
williamr@2
   638
@param	aKey	The object of type T to add to the set.
williamr@2
   639
@leave			KErrNoMemory if memory could not be allocated to store
williamr@2
   640
					the copy of aKey.
williamr@2
   641
*/
williamr@2
   642
	inline void InsertL(const T& aKey)
williamr@2
   643
		{ RHashTableBase::ValueInsertL(&aKey, sizeof(T), 0, 0, 0); }
williamr@2
   644
williamr@2
   645
williamr@2
   646
/**
williamr@2
   647
Remove an element from the set.
williamr@2
   648
williamr@2
   649
@param	aKey	The object to be removed.
williamr@2
   650
@return			KErrNone if the object was removed successfully.
williamr@2
   651
				KErrNotFound if the object was not present in the set.
williamr@2
   652
*/
williamr@2
   653
	inline TInt Remove(const T& aKey)
williamr@2
   654
		{ return RHashTableBase::Remove(&aKey); }
williamr@2
   655
williamr@2
   656
williamr@2
   657
/**
williamr@2
   658
Query the number of elements in the set.
williamr@2
   659
williamr@2
   660
@return	The number of elements currently in the set.
williamr@2
   661
*/
williamr@2
   662
	inline TInt Count() const
williamr@2
   663
		{ return RHashTableBase::Count(); }
williamr@2
   664
williamr@2
   665
williamr@2
   666
/**
williamr@2
   667
Expand the set to accommodate a specified number of elements.
williamr@2
   668
If the set already has enough space for the specified number of elements, no
williamr@2
   669
action is taken. Any elements already in the set are retained.
williamr@2
   670
williamr@2
   671
@param	aCount	The number of elements for which space should be allocated.
williamr@2
   672
@return	KErrNone if the operation completed successfully.
williamr@2
   673
@return	KErrNoMemory if sufficient memory could not be allocated.
williamr@2
   674
*/
williamr@2
   675
	inline TInt Reserve(TInt aCount)
williamr@2
   676
		{ return RHashTableBase::Reserve(aCount); }
williamr@2
   677
williamr@2
   678
williamr@2
   679
/**
williamr@2
   680
Expand the set to accommodate a specified number of elements.
williamr@2
   681
If the set already has enough space for the specified number of elements, no
williamr@2
   682
action is taken. Any elements already in the set are retained.
williamr@2
   683
williamr@2
   684
@param	aCount	The number of elements for which space should be allocated.
williamr@2
   685
@leave	KErrNoMemory if sufficient memory could not be allocated.
williamr@2
   686
*/
williamr@2
   687
	inline void ReserveL(TInt aCount)
williamr@2
   688
		{ RHashTableBase::ReserveL(aCount); }
williamr@2
   689
williamr@2
   690
	};
williamr@2
   691
williamr@2
   692
williamr@2
   693
/**
williamr@2
   694
@publishedAll
williamr@2
   695
@released
williamr@2
   696
williamr@2
   697
A templated class which allows iteration over the elements of a RHashSet<T>
williamr@2
   698
class.
williamr@2
   699
williamr@2
   700
The set being iterated over may not be modified while an iteration is in progress
williamr@2
   701
or the iteration operations may malfunction or panic.
williamr@2
   702
williamr@2
   703
@see RHashSet<T>
williamr@2
   704
*/
williamr@2
   705
template <class T>
williamr@2
   706
class THashSetIter : public THashTableIterBase
williamr@2
   707
	{
williamr@2
   708
private:
williamr@4
   709
	
williamr@2
   710
	struct SFullElement
williamr@2
   711
		{
williamr@2
   712
		TUint32 iHash;
williamr@2
   713
		T iT;
williamr@2
   714
		};
williamr@2
   715
williamr@2
   716
public:
williamr@2
   717
williamr@2
   718
/**
williamr@2
   719
Construct an iterator over the specified set.
williamr@2
   720
The iterator starts at conceptual position one before the beginning of the list
williamr@2
   721
being iterated.
williamr@2
   722
williamr@2
   723
@param	aSet	The set to be iterated over.
williamr@2
   724
*/
williamr@2
   725
	inline THashSetIter(const RHashSet<T>& aSet)
williamr@2
   726
		:	THashTableIterBase(aSet)
williamr@2
   727
		{}
williamr@2
   728
williamr@2
   729
williamr@2
   730
/**
williamr@2
   731
Reset the iterator to its initial state.
williamr@2
   732
williamr@2
   733
@param	aSet	The set to be iterated over.
williamr@2
   734
*/
williamr@2
   735
	inline void Reset()
williamr@2
   736
		{ THashTableIterBase::Reset(); }
williamr@2
   737
williamr@2
   738
williamr@2
   739
/**
williamr@2
   740
Return the current position of the iterator.
williamr@2
   741
williamr@2
   742
@return	A pointer to the set member corresponding to the current position of the
williamr@2
   743
		iterator.
williamr@2
   744
		NULL if the iterator has just been constructed or reset, or if it has
williamr@2
   745
		previously reached the end of an iteration.
williamr@2
   746
*/
williamr@2
   747
	inline const T* Current() const
williamr@2
   748
		{ return (const T*)THashTableIterBase::Current(_FOFF(SFullElement,iT)); }
williamr@2
   749
williamr@2
   750
williamr@2
   751
/**
williamr@2
   752
Steps the iterator to the next position.
williamr@2
   753
williamr@2
   754
@return	A pointer to the set member corresponding to the next position of the
williamr@2
   755
		iterator.
williamr@2
   756
		NULL if the iterator has exhausted all the available set elements.
williamr@2
   757
*/
williamr@2
   758
	inline const T* Next()
williamr@2
   759
		{ return (const T*)THashTableIterBase::Next(_FOFF(SFullElement,iT)); }
williamr@2
   760
williamr@2
   761
williamr@2
   762
/**
williamr@2
   763
Removes the element at the current iterator position from the hash table.
williamr@2
   764
If the iterator does not currently point to a valid element, no action is taken.
williamr@2
   765
Note that the iterator position is not altered so it no longer points to a valid
williamr@2
   766
element following the Remove(). It is illegal to call Current() on the iterator
williamr@2
   767
after calling Remove() - the only legal operations are Reset() and Next().
williamr@2
   768
williamr@2
   769
*/
williamr@2
   770
	inline void RemoveCurrent()
williamr@2
   771
		{ THashTableIterBase::RemoveCurrent(); }
williamr@2
   772
	};
williamr@2
   773
williamr@2
   774
williamr@2
   775
williamr@2
   776
template <class T> class TPtrHashSetIter;
williamr@2
   777
williamr@2
   778
/**
williamr@2
   779
@publishedAll
williamr@2
   780
@released
williamr@2
   781
williamr@2
   782
A templated class which implements an unordered extensional set of objects of
williamr@2
   783
type T using a probe-sequence hash table. The objects are not copied into the set
williamr@2
   784
when they are added; rather the set stores pointers to the contained objects.
williamr@2
   785
williamr@2
   786
*/
williamr@2
   787
template <class T>
williamr@2
   788
class RPtrHashSet : public RHashTableBase
williamr@2
   789
	{
williamr@2
   790
private:
williamr@2
   791
	friend class TPtrHashSetIter<T>;
williamr@4
   792
	
williamr@2
   793
	struct SFullElement
williamr@2
   794
		{
williamr@2
   795
		TUint32 iHash;
williamr@2
   796
		T* iT;
williamr@2
   797
		};
williamr@2
   798
williamr@2
   799
public:
williamr@2
   800
williamr@2
   801
/**
williamr@2
   802
A class which allows iteration over the elements of a RPtrHashSet<T> class.
williamr@2
   803
williamr@2
   804
The set being iterated over may not be modified while an iteration is in progress
williamr@2
   805
or the iteration operations may malfunction or panic.
williamr@2
   806
williamr@2
   807
@see TPtrHashSetIter<T>
williamr@2
   808
*/
williamr@2
   809
	typedef TPtrHashSetIter<T> TIter;
williamr@2
   810
williamr@2
   811
/**
williamr@2
   812
Construct a set of objects of type T using a specified hash function and identity relation.
williamr@2
   813
The set is initially empty.
williamr@2
   814
williamr@2
   815
@param	aHash		The hash function used to hash the objects of type T.
williamr@2
   816
@param	aIdentity	The identity relation used to determine if two objects of type T
williamr@2
   817
					should be considered identical.
williamr@2
   818
*/
williamr@2
   819
	inline RPtrHashSet(const THashFunction32<T>& aHash, const TIdentityRelation<T>& aIdentity)
williamr@2
   820
		:	RHashTableBase(aHash, aIdentity, sizeof(SFullElement), 0)
williamr@2
   821
		{}
williamr@2
   822
williamr@2
   823
williamr@2
   824
/**
williamr@2
   825
Construct a set of objects of type T using a default hash function and identity relation.
williamr@2
   826
The set is initially empty.
williamr@2
   827
*/
williamr@2
   828
	inline RPtrHashSet()
williamr@2
   829
		:	RHashTableBase(Defaults<T,EDefaultSpecifier_Normal>::Hash(), Defaults<T,EDefaultSpecifier_Normal>::Id(), sizeof(SFullElement), 0)
williamr@2
   830
		{}
williamr@2
   831
williamr@2
   832
williamr@2
   833
/**
williamr@2
   834
Free all memory used by this set.
williamr@2
   835
Returns the set to the same state it had following construction.
williamr@2
   836
*/
williamr@2
   837
	inline void Close()
williamr@2
   838
		{ RHashTableBase::Close(); }
williamr@2
   839
williamr@2
   840
williamr@2
   841
/**
williamr@2
   842
Locate a specified element in the set.
williamr@2
   843
williamr@2
   844
@param	aKey	The object of type T to search for.
williamr@2
   845
@return			A pointer to the specified object, if it is in the set.
williamr@2
   846
				The object may not be modified via this pointer.
williamr@2
   847
				NULL if the specified object is not a member of this set.
williamr@2
   848
*/
williamr@2
   849
	inline const T* Find(const T& aKey) const
williamr@2
   850
		{ return (const T*)RHashTableBase::Find(&aKey, -_FOFF(SFullElement,iT)); }
williamr@2
   851
williamr@2
   852
williamr@2
   853
/**
williamr@2
   854
Locate a specified element in the set.
williamr@2
   855
williamr@2
   856
@param	aKey	The object of type T to search for.
williamr@2
   857
@return			A reference to the specified object, if it is in the set.
williamr@2
   858
				The object may not be modified via this reference.
williamr@2
   859
@leave	KErrNotFound if the specified object is not a member of this set.
williamr@2
   860
*/
williamr@2
   861
	inline const T& FindL(const T& aKey) const
williamr@2
   862
		{ return *(const T*)RHashTableBase::FindL(&aKey, -_FOFF(SFullElement,iT)); }
williamr@2
   863
williamr@2
   864
williamr@2
   865
/**
williamr@2
   866
Locate a specified element in the set.
williamr@2
   867
williamr@2
   868
@param	aKey	The object of type T to search for.
williamr@2
   869
@return			A pointer to the specified object, if it is in the set.
williamr@2
   870
				The object may be modified via this pointer. Care should
williamr@2
   871
				be taken not to modify any parts of the object which are used by
williamr@2
   872
				either the hash function or the identity relation for this set.
williamr@2
   873
				If this is done the set may become inconsistent, resulting in
williamr@2
   874
				malfunctions and/or panics at a later time.
williamr@2
   875
				NULL if the specified object is not a member of this set.
williamr@2
   876
*/
williamr@2
   877
	inline T* Find(const T& aKey)
williamr@2
   878
		{ return (T*)RHashTableBase::Find(&aKey, -_FOFF(SFullElement,iT)); }
williamr@2
   879
williamr@2
   880
williamr@2
   881
/**
williamr@2
   882
Locate a specified element in the set.
williamr@2
   883
williamr@2
   884
@param	aKey	The object of type T to search for.
williamr@2
   885
@return			A reference to the specified object, if it is in the set.
williamr@2
   886
				The object may be modified via this reference. Care should
williamr@2
   887
				be taken not to modify any parts of the object which are used by
williamr@2
   888
				either the hash function or the identity relation for this set.
williamr@2
   889
				If this is done the set may become inconsistent, resulting in
williamr@2
   890
				malfunctions and/or panics at a later time.
williamr@2
   891
@leave	KErrNotFound if the specified object is not a member of this set.
williamr@2
   892
*/
williamr@2
   893
	inline T& FindL(const T& aKey)
williamr@2
   894
		{ return *(T*)RHashTableBase::FindL(&aKey, -_FOFF(SFullElement,iT)); }
williamr@2
   895
williamr@2
   896
williamr@2
   897
/**
williamr@2
   898
Insert an element into the set.
williamr@2
   899
williamr@2
   900
If the specified object is not currently a member of the set, a pointer to the
williamr@2
   901
object is added to the set and KErrNone is returned.
williamr@2
   902
If the specified object is currently a member of the set, the existing pointer
williamr@2
   903
to the object is replaced by the provided pointer and KErrNone is
williamr@2
   904
returned.
williamr@2
   905
In both cases only a pointer to the object is stored - the object is never copied.
williamr@2
   906
williamr@2
   907
@param	aKey	A pointer to the object of type T to add to the set.
williamr@2
   908
@return			KErrNone if the object was added successfully.
williamr@2
   909
				KErrNoMemory if memory could not be allocated to store
williamr@2
   910
					the pointer to the new object.
williamr@2
   911
*/
williamr@2
   912
	inline TInt Insert(const T* aKey)
williamr@2
   913
		{ return RHashTableBase::PtrInsert(aKey, 0); }
williamr@2
   914
williamr@2
   915
williamr@2
   916
/**
williamr@2
   917
Insert an element into the set.
williamr@2
   918
williamr@2
   919
If the specified object is not currently a member of the set, a pointer to the
williamr@2
   920
object is added to the set and KErrNone is returned.
williamr@2
   921
If the specified object is currently a member of the set, the existing pointer
williamr@2
   922
to the object is replaced by the provided pointer and KErrNone is
williamr@2
   923
returned.
williamr@2
   924
In both cases only a pointer to the object is stored - the object is never copied.
williamr@2
   925
williamr@2
   926
@param	aKey	A pointer to the object of type T to add to the set.
williamr@2
   927
@leave	KErrNoMemory if memory could not be allocated to store the pointer to the new object.
williamr@2
   928
*/
williamr@2
   929
	inline void InsertL(const T* aKey)
williamr@2
   930
		{ RHashTableBase::PtrInsertL(aKey, 0); }
williamr@2
   931
williamr@2
   932
williamr@2
   933
/**
williamr@2
   934
Remove an element from the set.
williamr@2
   935
williamr@2
   936
@param	aKey	A pointer to the object to be removed.
williamr@2
   937
@return			KErrNone if the object was removed successfully.
williamr@2
   938
				KErrNotFound if the object was not present in the set.
williamr@2
   939
*/
williamr@2
   940
	inline TInt Remove(const T* aKey)
williamr@2
   941
		{ return RHashTableBase::Remove(aKey); }
williamr@2
   942
williamr@2
   943
williamr@2
   944
/**
williamr@2
   945
Query the number of elements in the set.
williamr@2
   946
williamr@2
   947
@return	The number of elements currently in the set.
williamr@2
   948
*/
williamr@2
   949
	inline TInt Count() const
williamr@2
   950
		{ return RHashTableBase::Count(); }
williamr@2
   951
williamr@2
   952
williamr@2
   953
/**
williamr@2
   954
Expand the set to accommodate a specified number of elements.
williamr@2
   955
If the set already has enough space for the specified number of elements, no
williamr@2
   956
action is taken. Any elements already in the set are retained.
williamr@2
   957
williamr@2
   958
@param	aCount	The number of elements for which space should be allocated.
williamr@2
   959
@return	KErrNone if the operation completed successfully.
williamr@2
   960
@return	KErrNoMemory if sufficient memory could not be allocated.
williamr@2
   961
*/
williamr@2
   962
	inline TInt Reserve(TInt aCount)
williamr@2
   963
		{ return RHashTableBase::Reserve(aCount); }
williamr@2
   964
williamr@2
   965
williamr@2
   966
/**
williamr@2
   967
Expand the set to accommodate a specified number of elements.
williamr@2
   968
If the set already has enough space for the specified number of elements, no
williamr@2
   969
action is taken. Any elements already in the set are retained.
williamr@2
   970
williamr@2
   971
@param	aCount	The number of elements for which space should be allocated.
williamr@2
   972
@leave	KErrNoMemory if sufficient memory could not be allocated.
williamr@2
   973
*/
williamr@2
   974
	inline void ReserveL(TInt aCount)
williamr@2
   975
		{ RHashTableBase::ReserveL(aCount); }
williamr@2
   976
williamr@2
   977
williamr@2
   978
	void ResetAndDestroy();
williamr@2
   979
	};
williamr@2
   980
williamr@2
   981
williamr@2
   982
/**
williamr@2
   983
@publishedAll
williamr@2
   984
@released
williamr@2
   985
williamr@2
   986
A templated class which allows iteration over the elements of a RPtrHashSet<T>
williamr@2
   987
class.
williamr@2
   988
williamr@2
   989
The set being iterated over may not be modified while an iteration is in progress
williamr@2
   990
or the iteration operations may malfunction or panic.
williamr@2
   991
williamr@2
   992
@see RPtrHashSet<T>
williamr@2
   993
*/
williamr@2
   994
template <class T>
williamr@2
   995
class TPtrHashSetIter : public THashTableIterBase
williamr@2
   996
	{
williamr@2
   997
private:
williamr@4
   998
	
williamr@2
   999
	struct SFullElement		
williamr@2
  1000
		{
williamr@2
  1001
		TUint32 iHash;
williamr@2
  1002
		T* iT;
williamr@2
  1003
		};
williamr@2
  1004
williamr@2
  1005
public:
williamr@2
  1006
williamr@2
  1007
/**
williamr@2
  1008
Construct an iterator over the specified set.
williamr@2
  1009
The iterator starts at conceptual position one before the beginning of the list
williamr@2
  1010
being iterated.
williamr@2
  1011
williamr@2
  1012
@param	aSet	The set to be iterated over.
williamr@2
  1013
*/
williamr@2
  1014
	inline TPtrHashSetIter(const RPtrHashSet<T>& aSet)
williamr@2
  1015
		:	THashTableIterBase(aSet)
williamr@2
  1016
		{}
williamr@2
  1017
williamr@2
  1018
williamr@2
  1019
/**
williamr@2
  1020
Reset the iterator to its initial state.
williamr@2
  1021
williamr@2
  1022
@param	aSet	The set to be iterated over.
williamr@2
  1023
*/
williamr@2
  1024
	inline void Reset()
williamr@2
  1025
		{ THashTableIterBase::Reset(); }
williamr@2
  1026
williamr@2
  1027
williamr@2
  1028
/**
williamr@2
  1029
Return the current position of the iterator.
williamr@2
  1030
williamr@2
  1031
@return	A pointer to the set member corresponding to the current position of the
williamr@2
  1032
		iterator.
williamr@2
  1033
		NULL if the iterator has just been constructed or reset, or if it has
williamr@2
  1034
		previously reached the end of an iteration.
williamr@2
  1035
*/
williamr@2
  1036
	inline const T* Current() const
williamr@2
  1037
		{ return (const T*)THashTableIterBase::Current(-_FOFF(SFullElement,iT)); }
williamr@2
  1038
williamr@2
  1039
williamr@2
  1040
/**
williamr@2
  1041
Steps the iterator to the next position.
williamr@2
  1042
williamr@2
  1043
@return	A pointer to the set member corresponding to the next position of the
williamr@2
  1044
		iterator.
williamr@2
  1045
		NULL if the iterator has exhausted all the available set elements.
williamr@2
  1046
*/
williamr@2
  1047
	inline const T* Next()
williamr@2
  1048
		{ return (const T*)THashTableIterBase::Next(-_FOFF(SFullElement,iT)); }
williamr@2
  1049
williamr@2
  1050
williamr@2
  1051
/**
williamr@2
  1052
Removes the element at the current iterator position from the hash table.
williamr@2
  1053
If the iterator does not currently point to a valid element, no action is taken.
williamr@2
  1054
Note that the iterator position is not altered so it no longer points to a valid
williamr@2
  1055
element following the Remove(). It is illegal to call Current() on the iterator
williamr@2
  1056
after calling Remove() - the only legal operations are Reset() and Next().
williamr@2
  1057
williamr@2
  1058
*/
williamr@2
  1059
	inline void RemoveCurrent()
williamr@2
  1060
		{ THashTableIterBase::RemoveCurrent(); }
williamr@2
  1061
	};
williamr@2
  1062
williamr@2
  1063
williamr@2
  1064
williamr@2
  1065
template <class K, class V> class THashMapIter;
williamr@2
  1066
williamr@2
  1067
/**
williamr@2
  1068
@publishedAll
williamr@2
  1069
@released
williamr@2
  1070
williamr@2
  1071
A templated class which implements an associative array with key type K and value type V,
williamr@2
  1072
using a probe-sequence hash table. Both the key and value objects are copied into the
williamr@2
  1073
table when they are added. A bitwise binary copy is used here, so neither of the types
williamr@2
  1074
K and V may implement a nontrivial copy constructor.
williamr@2
  1075
williamr@2
  1076
*/
williamr@2
  1077
template <class K, class V>
williamr@2
  1078
class RHashMap : public RHashTableBase
williamr@2
  1079
	{
williamr@2
  1080
private:
williamr@2
  1081
	friend class THashMapIter<K,V>;
williamr@4
  1082
	
williamr@2
  1083
	struct SFullElement
williamr@2
  1084
		{
williamr@2
  1085
		TUint32 iHash;
williamr@2
  1086
		K iK;
williamr@2
  1087
		V iV;
williamr@2
  1088
		};
williamr@2
  1089
williamr@2
  1090
public:
williamr@2
  1091
williamr@2
  1092
/**
williamr@2
  1093
A class which allows iteration over the elements of a RHashMap<K,V> class.
williamr@2
  1094
williamr@2
  1095
The array being iterated over may not be modified while an iteration is in progress
williamr@2
  1096
or the iteration operations may malfunction or panic.
williamr@2
  1097
williamr@2
  1098
@see THashMapIter<K,V>
williamr@2
  1099
*/
williamr@2
  1100
	typedef THashMapIter<K,V> TIter;
williamr@2
  1101
williamr@2
  1102
/**
williamr@2
  1103
Construct an associative array of key-value pairs of type (K,V) using a
williamr@2
  1104
specified hash function and identity relation.
williamr@2
  1105
The array initially contains no key-value pairs.
williamr@2
  1106
williamr@2
  1107
@param	aHash		The hash function used to hash the key objects of type K.
williamr@2
  1108
@param	aIdentity	The identity relation used to determine if two key objects
williamr@2
  1109
					of type K should be considered identical.
williamr@2
  1110
*/
williamr@2
  1111
	inline RHashMap(const THashFunction32<K>& aHash, const TIdentityRelation<K>& aIdentity)
williamr@2
  1112
		:	RHashTableBase(aHash, aIdentity, sizeof(SFullElement), _FOFF(SFullElement,iK))
williamr@2
  1113
		{}
williamr@2
  1114
williamr@2
  1115
williamr@2
  1116
/**
williamr@2
  1117
Construct an associative array of key-value pairs of type (K,V) using a
williamr@2
  1118
default hash function and identity relation.
williamr@2
  1119
The array initially contains no key-value pairs.
williamr@2
  1120
*/
williamr@2
  1121
	inline RHashMap()
williamr@2
  1122
		:	RHashTableBase(Defaults<K,EDefaultSpecifier_Normal>::Hash(), Defaults<K,EDefaultSpecifier_Normal>::Id(), sizeof(SFullElement), _FOFF(SFullElement,iK))
williamr@2
  1123
		{}
williamr@2
  1124
williamr@2
  1125
williamr@2
  1126
/**
williamr@2
  1127
Free all memory used by this array.
williamr@2
  1128
Returns the array to the same state it had following construction.
williamr@2
  1129
*/
williamr@2
  1130
	inline void Close()
williamr@2
  1131
		{ RHashTableBase::Close(); }
williamr@2
  1132
williamr@2
  1133
williamr@2
  1134
/**
williamr@2
  1135
Look up a specified key in the associative array and return a pointer to the
williamr@2
  1136
corresponding value.
williamr@2
  1137
williamr@2
  1138
@param	aKey	The key object of type K to look up.
williamr@2
  1139
@return			A pointer to the copy of the corresponding value object in the
williamr@2
  1140
				array, if the specified key object was found.
williamr@2
  1141
				The value object may not be modified via this pointer.
williamr@2
  1142
				NULL if the specified key object was not found.
williamr@2
  1143
*/
williamr@2
  1144
	inline const V* Find(const K& aKey) const
williamr@2
  1145
		{ return (const V*)RHashTableBase::Find(&aKey, _FOFF(SFullElement,iV)); }
williamr@2
  1146
williamr@2
  1147
williamr@2
  1148
/**
williamr@2
  1149
Look up a specified key in the associative array and return a pointer to the
williamr@2
  1150
corresponding value.
williamr@2
  1151
williamr@2
  1152
@param	aKey	The key object of type K to look up.
williamr@2
  1153
@return			A reference to the copy of the corresponding value object in the
williamr@2
  1154
				array, if the specified key object was found.
williamr@2
  1155
				The value object may not be modified via this reference.
williamr@2
  1156
@leave	KErrNotFound if the specified key object was not found.
williamr@2
  1157
*/
williamr@2
  1158
	inline const V& FindL(const K& aKey) const
williamr@2
  1159
		{ return *(const V*)RHashTableBase::FindL(&aKey, _FOFF(SFullElement,iV)); }
williamr@2
  1160
williamr@2
  1161
williamr@2
  1162
/**
williamr@2
  1163
Look up a specified key in the associative array and return a pointer to the
williamr@2
  1164
corresponding value.
williamr@2
  1165
williamr@2
  1166
@param	aKey	The key object of type K to look up.
williamr@2
  1167
@return			A pointer to the copy of the corresponding value object in the
williamr@2
  1168
				array, if the specified key object was found.
williamr@2
  1169
				The value object may be modified via this pointer.
williamr@2
  1170
				NULL if the specified key object was not found.
williamr@2
  1171
*/
williamr@2
  1172
	inline V* Find(const K& aKey)
williamr@2
  1173
		{ return (V*)RHashTableBase::Find(&aKey, _FOFF(SFullElement,iV)); }
williamr@2
  1174
williamr@2
  1175
williamr@2
  1176
/**
williamr@2
  1177
Look up a specified key in the associative array and return a pointer to the
williamr@2
  1178
corresponding value.
williamr@2
  1179
williamr@2
  1180
@param	aKey	The key object of type K to look up.
williamr@2
  1181
@return			A reference to the copy of the corresponding value object in the
williamr@2
  1182
				array, if the specified key object was found.
williamr@2
  1183
				The value object may be modified via this reference.
williamr@2
  1184
@leave	KErrNotFound if the specified key object was not found.
williamr@2
  1185
*/
williamr@2
  1186
	inline V& FindL(const K& aKey)
williamr@2
  1187
		{ return *(V*)RHashTableBase::FindL(&aKey, _FOFF(SFullElement,iV)); }
williamr@2
  1188
williamr@2
  1189
williamr@2
  1190
/**
williamr@2
  1191
Insert a key-value pair into the array.
williamr@2
  1192
williamr@2
  1193
If the specified key object is not found in the array, a copy of the
williamr@2
  1194
key object along with a copy of the value object are added to the array
williamr@2
  1195
and KErrNone is returned.
williamr@2
  1196
If the specified key object is found in the array, the existing copies
williamr@2
  1197
of both the key and value objects are replaced by the provided objects
williamr@2
  1198
and KErrNone is returned.
williamr@2
  1199
In both cases the objects are copied bitwise into the array.
williamr@2
  1200
williamr@2
  1201
@param	aKey	The key object of type K to add to the array.
williamr@2
  1202
@param	aValue	The value object of type V to associate with aKey.
williamr@2
  1203
@return			KErrNone if the key-value pair was added successfully.
williamr@2
  1204
				KErrNoMemory if memory could not be allocated to store
williamr@2
  1205
					the copies of aKey and aValue.
williamr@2
  1206
*/
williamr@2
  1207
	inline TInt Insert(const K& aKey, const V& aValue)
williamr@2
  1208
		{ return RHashTableBase::ValueInsert(&aKey, sizeof(K), &aValue, _FOFF(SFullElement,iV), sizeof(V)); }
williamr@2
  1209
williamr@2
  1210
williamr@2
  1211
/**
williamr@2
  1212
Insert a key-value pair into the array.
williamr@2
  1213
williamr@2
  1214
If the specified key object is not found in the array, a copy of the
williamr@2
  1215
key object along with a copy of the value object are added to the array
williamr@2
  1216
and KErrNone is returned.
williamr@2
  1217
If the specified key object is found in the array, the existing copies
williamr@2
  1218
of both the key and value objects are replaced by the provided objects
williamr@2
  1219
and KErrNone is returned.
williamr@2
  1220
In both cases the objects are copied bitwise into the array.
williamr@2
  1221
williamr@2
  1222
@param	aKey	The key object of type K to add to the array.
williamr@2
  1223
@param	aValue	The value object of type V to associate with aKey.
williamr@2
  1224
@leave	KErrNoMemory if memory could not be allocated to store the copies of aKey and aValue.
williamr@2
  1225
*/
williamr@2
  1226
	inline void InsertL(const K& aKey, const V& aValue)
williamr@2
  1227
		{ RHashTableBase::ValueInsertL(&aKey, sizeof(K), &aValue, _FOFF(SFullElement,iV), sizeof(V)); }
williamr@2
  1228
williamr@2
  1229
williamr@2
  1230
/**
williamr@2
  1231
Remove a key-value pair from the array.
williamr@2
  1232
williamr@2
  1233
@param	aKey	The key to be removed.
williamr@2
  1234
@return			KErrNone if the key object and corresponding value object were
williamr@2
  1235
				removed successfully.
williamr@2
  1236
				KErrNotFound if the key object was not present in the array.
williamr@2
  1237
*/
williamr@2
  1238
	inline TInt Remove(const K& aKey)
williamr@2
  1239
		{ return RHashTableBase::Remove(&aKey); }
williamr@2
  1240
williamr@2
  1241
williamr@2
  1242
/**
williamr@2
  1243
Query the number of key-value pairs in the array.
williamr@2
  1244
williamr@2
  1245
@return	The number of key-value pairs currently in the array.
williamr@2
  1246
*/
williamr@2
  1247
	inline TInt Count() const
williamr@2
  1248
		{ return RHashTableBase::Count(); }
williamr@2
  1249
williamr@2
  1250
williamr@2
  1251
/**
williamr@2
  1252
Expand the array to accommodate a specified number of key-value pairs.
williamr@2
  1253
If the set already has enough space for the specified number of elements, no
williamr@2
  1254
action is taken. Any elements already in the set are retained.
williamr@2
  1255
williamr@2
  1256
@param	aCount	The number of key-value pairs for which space should be allocated.
williamr@2
  1257
@return	KErrNone if the operation completed successfully.
williamr@2
  1258
@return	KErrNoMemory if sufficient memory could not be allocated.
williamr@2
  1259
*/
williamr@2
  1260
	inline TInt Reserve(TInt aCount)
williamr@2
  1261
		{ return RHashTableBase::Reserve(aCount); }
williamr@2
  1262
williamr@2
  1263
williamr@2
  1264
/**
williamr@2
  1265
Expand the array to accommodate a specified number of key-value pairs.
williamr@2
  1266
If the set already has enough space for the specified number of elements, no
williamr@2
  1267
action is taken. Any elements already in the set are retained.
williamr@2
  1268
williamr@2
  1269
@param	aCount	The number of key-value pairs for which space should be allocated.
williamr@2
  1270
@leave	KErrNoMemory if sufficient memory could not be allocated.
williamr@2
  1271
*/
williamr@2
  1272
	inline void ReserveL(TInt aCount)
williamr@2
  1273
		{ RHashTableBase::ReserveL(aCount); }
williamr@2
  1274
williamr@2
  1275
	};
williamr@2
  1276
williamr@2
  1277
williamr@2
  1278
/**
williamr@2
  1279
@publishedAll
williamr@2
  1280
@released
williamr@2
  1281
williamr@2
  1282
A templated class which allows iteration over the elements of a RHashMap<K,V>
williamr@2
  1283
class.
williamr@2
  1284
williamr@2
  1285
The array being iterated over may not be modified while an iteration is in progress
williamr@2
  1286
or the iteration operations may malfunction or panic.
williamr@2
  1287
williamr@2
  1288
@see RHashMap<K,V>
williamr@2
  1289
*/
williamr@2
  1290
template <class K, class V>
williamr@2
  1291
class THashMapIter : public THashTableIterBase
williamr@2
  1292
	{
williamr@2
  1293
private:
williamr@4
  1294
	
williamr@2
  1295
	struct SFullElement
williamr@2
  1296
		{
williamr@2
  1297
		TUint32 iHash;
williamr@2
  1298
		K iK;
williamr@2
  1299
		V iV;
williamr@2
  1300
		};
williamr@2
  1301
williamr@2
  1302
public:
williamr@2
  1303
williamr@2
  1304
/**
williamr@2
  1305
Construct an iterator over the specified associative array.
williamr@2
  1306
The iterator starts at conceptual position one before the beginning of the list
williamr@2
  1307
being iterated.
williamr@2
  1308
williamr@2
  1309
@param	aMap	The array to be iterated over.
williamr@2
  1310
*/
williamr@2
  1311
	inline THashMapIter(const RHashMap<K,V>& aMap)
williamr@2
  1312
		:	THashTableIterBase(aMap)
williamr@2
  1313
		{}
williamr@2
  1314
williamr@2
  1315
williamr@2
  1316
/**
williamr@2
  1317
Reset the iterator to its initial state.
williamr@2
  1318
williamr@2
  1319
@param	aSet	The set to be iterated over.
williamr@2
  1320
*/
williamr@2
  1321
	inline void Reset()
williamr@2
  1322
		{ THashTableIterBase::Reset(); }
williamr@2
  1323
williamr@2
  1324
williamr@2
  1325
/**
williamr@2
  1326
Return the key corresponding to the current position of the iterator.
williamr@2
  1327
williamr@2
  1328
@return	A pointer to the key object corresponding to the current position of the
williamr@2
  1329
		iterator.
williamr@2
  1330
		NULL if the iterator has just been constructed or reset, or if it has
williamr@2
  1331
		previously reached the end of an iteration.
williamr@2
  1332
*/
williamr@2
  1333
	inline const K* CurrentKey() const
williamr@2
  1334
		{ return (const K*)THashTableIterBase::Current(_FOFF(SFullElement,iK)); }
williamr@2
  1335
williamr@2
  1336
williamr@2
  1337
/**
williamr@2
  1338
Steps the iterator to the next position and returns the corresponding key.
williamr@2
  1339
williamr@2
  1340
@return	A pointer to the key object corresponding to the next position of the
williamr@2
  1341
		iterator.
williamr@2
  1342
		NULL if the iterator has exhausted all the available key-value pairs.
williamr@2
  1343
*/
williamr@2
  1344
	inline const K* NextKey()
williamr@2
  1345
		{ return (const K*)THashTableIterBase::Next(_FOFF(SFullElement,iK)); }
williamr@2
  1346
williamr@2
  1347
williamr@2
  1348
/**
williamr@2
  1349
Return the value corresponding to the current position of the iterator.
williamr@2
  1350
williamr@2
  1351
@return	A pointer to the value object corresponding to the current position of the
williamr@2
  1352
		iterator.
williamr@2
  1353
		NULL if the iterator has just been constructed or reset, or if it has
williamr@2
  1354
		previously reached the end of an iteration.
williamr@2
  1355
*/
williamr@2
  1356
	inline V* CurrentValue() 
williamr@2
  1357
		{ return (V*)THashTableIterBase::Current(_FOFF(SFullElement,iV)); }
williamr@2
  1358
williamr@2
  1359
williamr@2
  1360
/**
williamr@2
  1361
Steps the iterator to the next position and returns the corresponding value.
williamr@2
  1362
williamr@2
  1363
@return	A pointer to the value object corresponding to the next position of the
williamr@2
  1364
		iterator.
williamr@2
  1365
		NULL if the iterator has exhausted all the available key-value pairs.
williamr@2
  1366
*/
williamr@2
  1367
	inline const V* NextValue()
williamr@2
  1368
		{ return (const V*)THashTableIterBase::Next(_FOFF(SFullElement,iV)); }
williamr@2
  1369
williamr@2
  1370
williamr@2
  1371
/**
williamr@2
  1372
Removes the element at the current iterator position from the hash table.
williamr@2
  1373
If the iterator does not currently point to a valid element, no action is taken.
williamr@2
  1374
Note that the iterator position is not altered so it no longer points to a valid
williamr@2
  1375
element following the Remove(). It is illegal to call either CurrentKey() or
williamr@2
  1376
CurrentValue() on the iterator after calling Remove() - the only legal
williamr@2
  1377
operations are Reset(), NextKey() or NextValue().
williamr@2
  1378
williamr@2
  1379
*/
williamr@2
  1380
	inline void RemoveCurrent()
williamr@2
  1381
		{ THashTableIterBase::RemoveCurrent(); }
williamr@2
  1382
	};
williamr@2
  1383
williamr@2
  1384
williamr@2
  1385
williamr@2
  1386
template <class K, class V> class TPtrHashMapIter;
williamr@2
  1387
williamr@2
  1388
/**
williamr@2
  1389
@publishedAll
williamr@2
  1390
@released
williamr@2
  1391
williamr@2
  1392
A templated class which implements an associative array with key type K and value type V,
williamr@2
  1393
using a probe-sequence hash table. Neither the key nor value objects are copied into the
williamr@2
  1394
table when they are added - only pointers are stored.
williamr@2
  1395
williamr@2
  1396
*/
williamr@2
  1397
template <class K, class V>
williamr@2
  1398
class RPtrHashMap : public RHashTableBase
williamr@2
  1399
	{
williamr@2
  1400
private:
williamr@2
  1401
	friend class TPtrHashMapIter<K,V>;
williamr@4
  1402
	
williamr@2
  1403
	struct SFullElement
williamr@2
  1404
		{
williamr@2
  1405
		TUint32 iHash;
williamr@2
  1406
		K* iK;
williamr@2
  1407
		V* iV;
williamr@2
  1408
		};
williamr@2
  1409
public:
williamr@2
  1410
williamr@2
  1411
/**
williamr@2
  1412
A class which allows iteration over the elements of a RPtrHashMap<K,V> class.
williamr@2
  1413
williamr@2
  1414
The array being iterated over may not be modified while an iteration is in progress
williamr@2
  1415
or the iteration operations may malfunction or panic.
williamr@2
  1416
williamr@2
  1417
@see TPtrHashMapIter<K,V>
williamr@2
  1418
*/
williamr@2
  1419
	typedef TPtrHashMapIter<K,V> TIter;
williamr@2
  1420
williamr@2
  1421
/**
williamr@2
  1422
Construct an associative array of key-value pairs of type (K,V) using a
williamr@2
  1423
specified hash function and identity relation.
williamr@2
  1424
The array initially contains no key-value pairs.
williamr@2
  1425
williamr@2
  1426
@param	aHash		The hash function used to hash the key objects of type K.
williamr@2
  1427
@param	aIdentity	The identity relation used to determine if two key objects
williamr@2
  1428
					of type K should be considered identical.
williamr@2
  1429
*/
williamr@2
  1430
	inline RPtrHashMap(const THashFunction32<K>& aHash, const TIdentityRelation<K>& aIdentity)
williamr@2
  1431
		:	RHashTableBase(aHash, aIdentity, sizeof(SFullElement), 0)
williamr@2
  1432
		{}
williamr@2
  1433
williamr@2
  1434
williamr@2
  1435
/**
williamr@2
  1436
Construct an associative array of key-value pairs of type (K,V) using a
williamr@2
  1437
default hash function and identity relation.
williamr@2
  1438
The array initially contains no key-value pairs.
williamr@2
  1439
*/
williamr@2
  1440
	inline RPtrHashMap()
williamr@2
  1441
		:	RHashTableBase(Defaults<K,EDefaultSpecifier_Normal>::Hash(), Defaults<K,EDefaultSpecifier_Normal>::Id(), sizeof(SFullElement), 0)
williamr@2
  1442
		{}
williamr@2
  1443
williamr@2
  1444
williamr@2
  1445
/**
williamr@2
  1446
Free all memory used by this array.
williamr@2
  1447
Returns the array to the same state it had following construction.
williamr@2
  1448
*/
williamr@2
  1449
	inline void Close()
williamr@2
  1450
		{ RHashTableBase::Close(); }
williamr@2
  1451
williamr@2
  1452
williamr@2
  1453
/**
williamr@2
  1454
Look up a specified key in the associative array and return a pointer to the
williamr@2
  1455
corresponding value.
williamr@2
  1456
williamr@2
  1457
@param	aKey	The key object of type K to look up.
williamr@2
  1458
@return			A pointer to corresponding value object if the specified key
williamr@2
  1459
				object was found. The value object may not be modified via
williamr@2
  1460
				this pointer.
williamr@2
  1461
				NULL if the specified key object was not found.
williamr@2
  1462
*/
williamr@2
  1463
	inline const V* Find(const K& aKey) const
williamr@2
  1464
		{ return (const V*)RHashTableBase::Find(&aKey, -_FOFF(SFullElement,iV)); }
williamr@2
  1465
williamr@2
  1466
williamr@2
  1467
/**
williamr@2
  1468
Look up a specified key in the associative array and return a pointer to the
williamr@2
  1469
corresponding value.
williamr@2
  1470
williamr@2
  1471
@param	aKey	The key object of type K to look up.
williamr@2
  1472
@return			A reference to corresponding value object if the specified key
williamr@2
  1473
				object was found. The value object may not be modified via
williamr@2
  1474
				this reference.
williamr@2
  1475
@leave	KErrNotFound if the specified key object was not found.
williamr@2
  1476
*/
williamr@2
  1477
	inline const V& FindL(const K& aKey) const
williamr@2
  1478
		{ return *(const V*)RHashTableBase::FindL(&aKey, -_FOFF(SFullElement,iV)); }
williamr@2
  1479
williamr@2
  1480
williamr@2
  1481
/**
williamr@2
  1482
Look up a specified key in the associative array and return a pointer to the
williamr@2
  1483
corresponding value.
williamr@2
  1484
williamr@2
  1485
@param	aKey	The key object of type K to look up.
williamr@2
  1486
@return			A pointer to corresponding value object if the specified key
williamr@2
  1487
				object was found. The value object may be modified via
williamr@2
  1488
				this pointer.
williamr@2
  1489
				NULL if the specified key object was not found.
williamr@2
  1490
*/
williamr@2
  1491
	inline V* Find(const K& aKey)
williamr@2
  1492
		{ return (V*)RHashTableBase::Find(&aKey, -_FOFF(SFullElement,iV)); }
williamr@2
  1493
williamr@2
  1494
williamr@2
  1495
/**
williamr@2
  1496
Look up a specified key in the associative array and return a pointer to the
williamr@2
  1497
corresponding value.
williamr@2
  1498
williamr@2
  1499
@param	aKey	The key object of type K to look up.
williamr@2
  1500
@return			A reference to corresponding value object if the specified key
williamr@2
  1501
				object was found. The value object may be modified via
williamr@2
  1502
				this reference.
williamr@2
  1503
@leave	KErrNotFound if the specified key object was not found.
williamr@2
  1504
*/
williamr@2
  1505
	inline V& FindL(const K& aKey)
williamr@2
  1506
		{ return *(V*)RHashTableBase::FindL(&aKey, -_FOFF(SFullElement,iV)); }
williamr@2
  1507
williamr@2
  1508
williamr@2
  1509
/**
williamr@2
  1510
Insert a key-value pair into the array.
williamr@2
  1511
williamr@2
  1512
If the specified key object is not found in the array, a pointer to the
williamr@2
  1513
key object along with a pointer to the value object are added to the array
williamr@2
  1514
and KErrNone is returned.
williamr@2
  1515
If the specified key object is found in the array, the existing pointers
williamr@2
  1516
to both the key and value objects are replaced by the provided pointers
williamr@2
  1517
and KErrNone is returned.
williamr@2
  1518
In both cases only pointers are stored in the array - the objects themselves
williamr@2
  1519
are not copied.
williamr@2
  1520
williamr@2
  1521
@param	aKey	A pointer to the key object of type K to add to the array.
williamr@2
  1522
@param	aValue	A pointer to the value object of type V to associate with aKey.
williamr@2
  1523
@return			KErrNone if the key-value pair was added successfully.
williamr@2
  1524
				KErrNoMemory if memory could not be allocated to store
williamr@2
  1525
					the pointers aKey and aValue.
williamr@2
  1526
*/
williamr@2
  1527
	inline TInt Insert(const K* aKey, const V* aValue)
williamr@2
  1528
		{ return RHashTableBase::PtrInsert(aKey, aValue); }
williamr@2
  1529
williamr@2
  1530
williamr@2
  1531
/**
williamr@2
  1532
Insert a key-value pair into the array.
williamr@2
  1533
williamr@2
  1534
If the specified key object is not found in the array, a pointer to the
williamr@2
  1535
key object along with a pointer to the value object are added to the array
williamr@2
  1536
and KErrNone is returned.
williamr@2
  1537
If the specified key object is found in the array, the existing pointers
williamr@2
  1538
to both the key and value objects are replaced by the provided pointers
williamr@2
  1539
and KErrNone is returned.
williamr@2
  1540
In both cases only pointers are stored in the array - the objects themselves
williamr@2
  1541
are not copied.
williamr@2
  1542
williamr@2
  1543
@param	aKey	A pointer to the key object of type K to add to the array.
williamr@2
  1544
@param	aValue	A pointer to the value object of type V to associate with aKey.
williamr@2
  1545
@leave	KErrNoMemory if memory could not be allocated to store the pointers aKey and aValue.
williamr@2
  1546
*/
williamr@2
  1547
	inline void InsertL(const K* aKey, const V* aValue)
williamr@2
  1548
		{ RHashTableBase::PtrInsertL(aKey, aValue); }
williamr@2
  1549
williamr@2
  1550
williamr@2
  1551
/**
williamr@2
  1552
Remove a key-value pair from the array.
williamr@2
  1553
williamr@2
  1554
@param	aKey	A pointer to the key to be removed.
williamr@2
  1555
@return			KErrNone if the pointers to the key object and corresponding
williamr@2
  1556
				value object were removed successfully.
williamr@2
  1557
				KErrNotFound if the key object was not present in the array.
williamr@2
  1558
*/
williamr@2
  1559
	inline TInt Remove(const K* aKey)
williamr@2
  1560
		{ return RHashTableBase::Remove(aKey); }
williamr@2
  1561
williamr@2
  1562
williamr@2
  1563
/**
williamr@2
  1564
Query the number of key-value pairs in the array.
williamr@2
  1565
williamr@2
  1566
@return	The number of key-value pairs currently in the array.
williamr@2
  1567
*/
williamr@2
  1568
	inline TInt Count() const
williamr@2
  1569
		{ return RHashTableBase::Count(); }
williamr@2
  1570
williamr@2
  1571
williamr@2
  1572
/**
williamr@2
  1573
Expand the array to accommodate a specified number of key-value pairs.
williamr@2
  1574
If the set already has enough space for the specified number of elements, no
williamr@2
  1575
action is taken. Any elements already in the set are retained.
williamr@2
  1576
williamr@2
  1577
@param	aCount	The number of key-value pairs for which space should be allocated.
williamr@2
  1578
@return	KErrNone if the operation completed successfully.
williamr@2
  1579
@return	KErrNoMemory if sufficient memory could not be allocated.
williamr@2
  1580
*/
williamr@2
  1581
	inline TInt Reserve(TInt aCount)
williamr@2
  1582
		{ return RHashTableBase::Reserve(aCount); }
williamr@2
  1583
williamr@2
  1584
williamr@2
  1585
/**
williamr@2
  1586
Expand the array to accommodate a specified number of key-value pairs.
williamr@2
  1587
If the set already has enough space for the specified number of elements, no
williamr@2
  1588
action is taken. Any elements already in the set are retained.
williamr@2
  1589
williamr@2
  1590
@param	aCount	The number of key-value pairs for which space should be allocated.
williamr@2
  1591
@leave	KErrNoMemory if sufficient memory could not be allocated.
williamr@2
  1592
*/
williamr@2
  1593
	inline void ReserveL(TInt aCount)
williamr@2
  1594
		{ RHashTableBase::ReserveL(aCount); }
williamr@2
  1595
williamr@2
  1596
williamr@2
  1597
	void ResetAndDestroy();
williamr@2
  1598
	};
williamr@2
  1599
williamr@2
  1600
williamr@2
  1601
/**
williamr@2
  1602
@publishedAll
williamr@2
  1603
@released
williamr@2
  1604
williamr@2
  1605
A templated class which allows iteration over the elements of a RPtrHashMap<K,V>
williamr@2
  1606
class.
williamr@2
  1607
williamr@2
  1608
The array being iterated over may not be modified while an iteration is in progress
williamr@2
  1609
or the iteration operations may malfunction or panic.
williamr@2
  1610
williamr@2
  1611
@see RPtrHashMap<K,V>
williamr@2
  1612
*/
williamr@2
  1613
template <class K, class V>
williamr@2
  1614
class TPtrHashMapIter : public THashTableIterBase
williamr@2
  1615
	{
williamr@2
  1616
private:
williamr@4
  1617
	
williamr@2
  1618
	struct SFullElement
williamr@2
  1619
		{
williamr@2
  1620
		TUint32 iHash;
williamr@2
  1621
		K* iK;
williamr@2
  1622
		V* iV;
williamr@2
  1623
		};
williamr@2
  1624
public:
williamr@2
  1625
williamr@2
  1626
/**
williamr@2
  1627
Construct an iterator over the specified associative array.
williamr@2
  1628
The iterator starts at conceptual position one before the beginning of the list
williamr@2
  1629
being iterated.
williamr@2
  1630
williamr@2
  1631
@param	aMap	The array to be iterated over.
williamr@2
  1632
*/
williamr@2
  1633
	inline TPtrHashMapIter(const RPtrHashMap<K,V>& aMap)
williamr@2
  1634
		:	THashTableIterBase(aMap)
williamr@2
  1635
		{}
williamr@2
  1636
williamr@2
  1637
williamr@2
  1638
/**
williamr@2
  1639
Reset the iterator to its initial state.
williamr@2
  1640
williamr@2
  1641
@param	aSet	The set to be iterated over.
williamr@2
  1642
*/
williamr@2
  1643
	inline void Reset()
williamr@2
  1644
		{ THashTableIterBase::Reset(); }
williamr@2
  1645
williamr@2
  1646
williamr@2
  1647
/**
williamr@2
  1648
Return the key corresponding to the current position of the iterator.
williamr@2
  1649
williamr@2
  1650
@return	A pointer to the key object corresponding to the current position of the
williamr@2
  1651
		iterator.
williamr@2
  1652
		NULL if the iterator has just been constructed or reset, or if it has
williamr@2
  1653
		previously reached the end of an iteration.
williamr@2
  1654
*/
williamr@2
  1655
	inline const K* CurrentKey() const
williamr@2
  1656
		{ return (const K*)THashTableIterBase::Current(-_FOFF(SFullElement,iK)); }
williamr@2
  1657
williamr@2
  1658
williamr@2
  1659
/**
williamr@2
  1660
Steps the iterator to the next position and returns the corresponding key.
williamr@2
  1661
williamr@2
  1662
@return	A pointer to the key object corresponding to the next position of the
williamr@2
  1663
		iterator.
williamr@2
  1664
		NULL if the iterator has exhausted all the available key-value pairs.
williamr@2
  1665
*/
williamr@2
  1666
	inline const K* NextKey()
williamr@2
  1667
		{ return (const K*)THashTableIterBase::Next(-_FOFF(SFullElement,iK)); }
williamr@2
  1668
williamr@2
  1669
williamr@2
  1670
/**
williamr@2
  1671
Return the value corresponding to the current position of the iterator.
williamr@2
  1672
williamr@2
  1673
@return	A pointer to the value object corresponding to the current position of the
williamr@2
  1674
		iterator.
williamr@2
  1675
		NULL if the iterator has just been constructed or reset, or if it has
williamr@2
  1676
		previously reached the end of an iteration.
williamr@2
  1677
*/
williamr@2
  1678
	inline const V* CurrentValue() const
williamr@2
  1679
		{ return (const V*)THashTableIterBase::Current(-_FOFF(SFullElement,iV)); }
williamr@2
  1680
williamr@2
  1681
williamr@2
  1682
/**
williamr@2
  1683
Steps the iterator to the next position and returns the corresponding value.
williamr@2
  1684
williamr@2
  1685
@return	A pointer to the value object corresponding to the next position of the
williamr@2
  1686
		iterator.
williamr@2
  1687
		NULL if the iterator has exhausted all the available key-value pairs.
williamr@2
  1688
*/
williamr@2
  1689
	inline const V* NextValue()
williamr@2
  1690
		{ return (const V*)THashTableIterBase::Next(-_FOFF(SFullElement,iV)); }
williamr@2
  1691
williamr@2
  1692
		
williamr@2
  1693
/**
williamr@2
  1694
Removes the element at the current iterator position from the hash table.
williamr@2
  1695
If the iterator does not currently point to a valid element, no action is taken.
williamr@2
  1696
Note that the iterator position is not altered so it no longer points to a valid
williamr@2
  1697
element following the Remove(). It is illegal to call either CurrentKey() or
williamr@2
  1698
CurrentValue() on the iterator after calling Remove() - the only legal
williamr@2
  1699
operations are Reset(), NextKey() or NextValue().
williamr@2
  1700
williamr@2
  1701
*/
williamr@2
  1702
	inline void RemoveCurrent()
williamr@2
  1703
		{ THashTableIterBase::RemoveCurrent(); }
williamr@2
  1704
	};
williamr@2
  1705
williamr@2
  1706
williamr@2
  1707
williamr@2
  1708
/**
williamr@2
  1709
Deletes all the objects of type T to which pointers are stored in this set.
williamr@2
  1710
Then frees all the memory used by the set and returns the set to the same state
williamr@2
  1711
as immediately following construction.
williamr@2
  1712
*/
williamr@2
  1713
template <class T>
williamr@2
  1714
void RPtrHashSet<T>::ResetAndDestroy()
williamr@2
  1715
	{
williamr@2
  1716
	TPtrHashSetIter<T> iter(*this);
williamr@2
  1717
	T* p;
williamr@2
  1718
	do	{
williamr@2
  1719
		p = (T*)iter.Next();
williamr@2
  1720
		delete p;
williamr@2
  1721
		} while(p);
williamr@2
  1722
	Close();
williamr@2
  1723
	}
williamr@2
  1724
williamr@2
  1725
williamr@2
  1726
/**
williamr@2
  1727
Deletes all the key objects of type K and corresponding value objects of type V
williamr@2
  1728
to which pointers are stored in this array.
williamr@2
  1729
Then frees all the memory used by the array and returns the array to the same
williamr@2
  1730
state as immediately following construction.
williamr@2
  1731
*/
williamr@2
  1732
template <class K, class V>
williamr@2
  1733
void RPtrHashMap<K,V>::ResetAndDestroy()
williamr@2
  1734
	{
williamr@2
  1735
	TPtrHashMapIter<K,V> iter(*this);
williamr@2
  1736
	K* p;
williamr@2
  1737
	V* q;
williamr@2
  1738
	do	{
williamr@2
  1739
		p = (K*)iter.NextKey();
williamr@2
  1740
		q = (V*)iter.CurrentValue();
williamr@2
  1741
		delete p;
williamr@2
  1742
		delete q;
williamr@2
  1743
		} while(p);
williamr@2
  1744
	Close();
williamr@2
  1745
	}
williamr@2
  1746
williamr@2
  1747
williamr@2
  1748
#endif