sl@0
|
1 |
// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32/euser/us_htab.cpp
|
sl@0
|
15 |
//
|
sl@0
|
16 |
//
|
sl@0
|
17 |
|
sl@0
|
18 |
#include "us_std.h"
|
sl@0
|
19 |
#include <e32hashtab.h>
|
sl@0
|
20 |
|
sl@0
|
21 |
const TUint KDefaultIndexBits = 4;
|
sl@0
|
22 |
const TUint KMaxIndexBits = 28;
|
sl@0
|
23 |
|
sl@0
|
24 |
extern TUint32 DefaultIntegerHash(const TAny*);
|
sl@0
|
25 |
extern TUint32 DefaultStringHash(const TUint8*, TInt);
|
sl@0
|
26 |
extern TUint32 DefaultWStringHash(const TUint16*, TInt);
|
sl@0
|
27 |
|
sl@0
|
28 |
#define _DEBUG_HASH_TABLE
|
sl@0
|
29 |
#ifndef _DEBUG
|
sl@0
|
30 |
#undef _DEBUG_HASH_TABLE
|
sl@0
|
31 |
#endif
|
sl@0
|
32 |
|
sl@0
|
33 |
#define __PANIC(x) Panic(x)
|
sl@0
|
34 |
|
sl@0
|
35 |
EXPORT_C RHashTableBase::RHashTableBase(TGeneralHashFunction32 aHash, TGeneralIdentityRelation aId, TInt aElementSize, TInt aKeyOffset)
|
sl@0
|
36 |
: iHashFunc(aHash),
|
sl@0
|
37 |
iIdFunc(aId),
|
sl@0
|
38 |
iIndexBits(TUint8(KDefaultIndexBits)),
|
sl@0
|
39 |
iGeneration(EGen0),
|
sl@0
|
40 |
iPad0(0),
|
sl@0
|
41 |
iElements(0),
|
sl@0
|
42 |
iCount(0),
|
sl@0
|
43 |
iPad1(0),
|
sl@0
|
44 |
iPad2(0)
|
sl@0
|
45 |
{
|
sl@0
|
46 |
__ASSERT_ALWAYS(aHash!=NULL, __PANIC(EHashTableNoHashFunc));
|
sl@0
|
47 |
__ASSERT_ALWAYS(aId!=NULL, __PANIC(EHashTableNoIdentityRelation));
|
sl@0
|
48 |
__ASSERT_ALWAYS(aElementSize>0, __PANIC(EHashTableBadElementSize));
|
sl@0
|
49 |
__ASSERT_ALWAYS(aKeyOffset==0 || TUint(aKeyOffset-4)<(TUint)Min(252,aElementSize-4), __PANIC(EHashTableBadKeyOffset));
|
sl@0
|
50 |
iElementSize = aElementSize;
|
sl@0
|
51 |
iKeyOffset = (TUint8)aKeyOffset; // 0 means ptr at offset 4
|
sl@0
|
52 |
iEmptyCount = 0;
|
sl@0
|
53 |
SetThresholds();
|
sl@0
|
54 |
}
|
sl@0
|
55 |
|
sl@0
|
56 |
void RHashTableBase::SetThresholds()
|
sl@0
|
57 |
{
|
sl@0
|
58 |
TUint32 max = 1u << iIndexBits;
|
sl@0
|
59 |
if (iIndexBits == KMaxIndexBits)
|
sl@0
|
60 |
iUpperThreshold = KMaxTUint;
|
sl@0
|
61 |
else
|
sl@0
|
62 |
iUpperThreshold = (max>>1) + (max>>2); // 3/4 of max
|
sl@0
|
63 |
if (iIndexBits == KDefaultIndexBits)
|
sl@0
|
64 |
iLowerThreshold = 0;
|
sl@0
|
65 |
else
|
sl@0
|
66 |
iLowerThreshold = max >> 2; // 1/4 of max
|
sl@0
|
67 |
|
sl@0
|
68 |
// clean table if <1/8 of entries empty
|
sl@0
|
69 |
iCleanThreshold = max>>3;
|
sl@0
|
70 |
}
|
sl@0
|
71 |
|
sl@0
|
72 |
EXPORT_C void RHashTableBase::Close()
|
sl@0
|
73 |
{
|
sl@0
|
74 |
User::Free(iElements);
|
sl@0
|
75 |
new (this) RHashTableBase(iHashFunc, iIdFunc, iElementSize, iKeyOffset);
|
sl@0
|
76 |
}
|
sl@0
|
77 |
|
sl@0
|
78 |
EXPORT_C TInt RHashTableBase::Count() const
|
sl@0
|
79 |
{
|
sl@0
|
80 |
return (TInt)iCount;
|
sl@0
|
81 |
}
|
sl@0
|
82 |
|
sl@0
|
83 |
EXPORT_C TAny* RHashTableBase::Find(const TAny* aKey, TInt aOffset) const
|
sl@0
|
84 |
{
|
sl@0
|
85 |
if (!iElements)
|
sl@0
|
86 |
return NULL;
|
sl@0
|
87 |
TUint32 hash = (*iHashFunc)(aKey);
|
sl@0
|
88 |
TUint32 ix = hash >> (32 - iIndexBits); // top bits of hash used as initial index
|
sl@0
|
89 |
hash = (hash &~ EStateMask) | iGeneration;
|
sl@0
|
90 |
TUint32 mask = (1u << iIndexBits) - 1; // iIndexBits 1's
|
sl@0
|
91 |
TUint32 step = (hash >> 1) & mask; // iIndexBits-1 LSBs of hash followed by 1
|
sl@0
|
92 |
FOREVER
|
sl@0
|
93 |
{
|
sl@0
|
94 |
const SElement* e = ElementC(ix);
|
sl@0
|
95 |
if (e->iHash==hash && (*iIdFunc)(aKey, GetKey(e)))
|
sl@0
|
96 |
{
|
sl@0
|
97 |
if (aOffset >= 0)
|
sl@0
|
98 |
return ((TUint8*)e) + aOffset;
|
sl@0
|
99 |
return *(TAny**)((TUint8*)e - aOffset);
|
sl@0
|
100 |
}
|
sl@0
|
101 |
if (e->IsEmpty())
|
sl@0
|
102 |
break;
|
sl@0
|
103 |
ix = (ix + step) & mask;
|
sl@0
|
104 |
}
|
sl@0
|
105 |
return NULL;
|
sl@0
|
106 |
}
|
sl@0
|
107 |
|
sl@0
|
108 |
EXPORT_C TAny* RHashTableBase::FindL(const TAny* aKey, TInt aOffset) const
|
sl@0
|
109 |
{
|
sl@0
|
110 |
TAny* p = Find(aKey, aOffset);
|
sl@0
|
111 |
if (!p)
|
sl@0
|
112 |
User::Leave(KErrNotFound);
|
sl@0
|
113 |
return p;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
TInt RHashTableBase::Insert(const TAny* aKey, TAny*& aElement)
|
sl@0
|
117 |
{
|
sl@0
|
118 |
TInt r = KErrNone;
|
sl@0
|
119 |
TUint32 max = 1u << iIndexBits;
|
sl@0
|
120 |
if (!iElements)
|
sl@0
|
121 |
{
|
sl@0
|
122 |
iElements = User::AllocZ(max * iElementSize);
|
sl@0
|
123 |
if (!iElements)
|
sl@0
|
124 |
return KErrNoMemory;
|
sl@0
|
125 |
iEmptyCount = max;
|
sl@0
|
126 |
}
|
sl@0
|
127 |
else if (iCount > iUpperThreshold)
|
sl@0
|
128 |
{
|
sl@0
|
129 |
r = ExpandTable(iIndexBits+1);
|
sl@0
|
130 |
if (iEmptyCount>1)
|
sl@0
|
131 |
r = KErrNone; // doesn't matter if expand fails unless there is only one empty slot left
|
sl@0
|
132 |
max = 1u << iIndexBits;
|
sl@0
|
133 |
}
|
sl@0
|
134 |
else if (iEmptyCount < iCleanThreshold)
|
sl@0
|
135 |
ReformTable(iIndexBits);
|
sl@0
|
136 |
|
sl@0
|
137 |
TUint32 hash = (*iHashFunc)(aKey);
|
sl@0
|
138 |
TUint32 ix = hash >> (32 - iIndexBits);
|
sl@0
|
139 |
TUint32 mask = max - 1;
|
sl@0
|
140 |
hash = (hash &~ EStateMask) | iGeneration;
|
sl@0
|
141 |
TUint32 step = (hash >> 1) & mask; // iIndexBits-1 LSBs of hash followed by 1
|
sl@0
|
142 |
SElement* e = 0;
|
sl@0
|
143 |
SElement* d = 0;
|
sl@0
|
144 |
FOREVER
|
sl@0
|
145 |
{
|
sl@0
|
146 |
e = Element(ix);
|
sl@0
|
147 |
if (e->IsEmpty())
|
sl@0
|
148 |
break;
|
sl@0
|
149 |
if (e->IsDeleted())
|
sl@0
|
150 |
{
|
sl@0
|
151 |
if (!d)
|
sl@0
|
152 |
d = e;
|
sl@0
|
153 |
}
|
sl@0
|
154 |
else if (e->iHash==hash && (*iIdFunc)(aKey, GetKey(e)))
|
sl@0
|
155 |
{
|
sl@0
|
156 |
aElement = e;
|
sl@0
|
157 |
return KErrNone; // duplicate so always succeed
|
sl@0
|
158 |
}
|
sl@0
|
159 |
ix = (ix + step) & mask;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
if (d)
|
sl@0
|
162 |
e = d; // if we can reuse a deleted slot, always succeed
|
sl@0
|
163 |
else
|
sl@0
|
164 |
{
|
sl@0
|
165 |
if (r!=KErrNone)
|
sl@0
|
166 |
return r; // new slot needed - if we failed to expand, fail the request here
|
sl@0
|
167 |
--iEmptyCount;
|
sl@0
|
168 |
}
|
sl@0
|
169 |
e->iHash = hash;
|
sl@0
|
170 |
aElement = e;
|
sl@0
|
171 |
++iCount;
|
sl@0
|
172 |
return KErrNone;
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
EXPORT_C TInt RHashTableBase::PtrInsert(const TAny* aKey, const TAny* aValue)
|
sl@0
|
176 |
{
|
sl@0
|
177 |
const TAny** e;
|
sl@0
|
178 |
TInt r = Insert(aKey, (TAny*&)e);
|
sl@0
|
179 |
if (r==KErrNone)
|
sl@0
|
180 |
{
|
sl@0
|
181 |
e[1] = aKey;
|
sl@0
|
182 |
if (iElementSize>=12)
|
sl@0
|
183 |
e[2] = aValue;
|
sl@0
|
184 |
}
|
sl@0
|
185 |
return r;
|
sl@0
|
186 |
}
|
sl@0
|
187 |
|
sl@0
|
188 |
EXPORT_C void RHashTableBase::PtrInsertL(const TAny* aKey, const TAny* aValue)
|
sl@0
|
189 |
{
|
sl@0
|
190 |
const TAny** e;
|
sl@0
|
191 |
User::LeaveIfError(Insert(aKey, (TAny*&)e));
|
sl@0
|
192 |
e[1] = aKey;
|
sl@0
|
193 |
if (iElementSize>=12)
|
sl@0
|
194 |
e[2] = aValue;
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
EXPORT_C TInt RHashTableBase::ValueInsert(const TAny* aKey, TInt aKeySize, const TAny* aValue, TInt aValueOffset, TInt aValueSize)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
TUint8* e;
|
sl@0
|
200 |
TInt r = Insert(aKey, (TAny*&)e);
|
sl@0
|
201 |
if (r==KErrNone)
|
sl@0
|
202 |
{
|
sl@0
|
203 |
memcpy(e+iKeyOffset, aKey, aKeySize);
|
sl@0
|
204 |
if (aValue)
|
sl@0
|
205 |
memcpy(e+aValueOffset, aValue, aValueSize);
|
sl@0
|
206 |
}
|
sl@0
|
207 |
return r;
|
sl@0
|
208 |
}
|
sl@0
|
209 |
|
sl@0
|
210 |
EXPORT_C void RHashTableBase::ValueInsertL(const TAny* aKey, TInt aKeySize, const TAny* aValue, TInt aValueOffset, TInt aValueSize)
|
sl@0
|
211 |
{
|
sl@0
|
212 |
TUint8* e;
|
sl@0
|
213 |
User::LeaveIfError(Insert(aKey, (TAny*&)e));
|
sl@0
|
214 |
memcpy(e+iKeyOffset, aKey, aKeySize);
|
sl@0
|
215 |
if (aValue)
|
sl@0
|
216 |
memcpy(e+aValueOffset, aValue, aValueSize);
|
sl@0
|
217 |
}
|
sl@0
|
218 |
|
sl@0
|
219 |
EXPORT_C TInt RHashTableBase::Remove(const TAny* aKey)
|
sl@0
|
220 |
{
|
sl@0
|
221 |
SElement* e = (SElement*)Find(aKey);
|
sl@0
|
222 |
if (!e)
|
sl@0
|
223 |
return KErrNotFound;
|
sl@0
|
224 |
e->SetDeleted();
|
sl@0
|
225 |
if (--iCount == 0)
|
sl@0
|
226 |
{
|
sl@0
|
227 |
Close();
|
sl@0
|
228 |
return KErrNone;
|
sl@0
|
229 |
}
|
sl@0
|
230 |
if (iCount < iLowerThreshold)
|
sl@0
|
231 |
ShrinkTable();
|
sl@0
|
232 |
return KErrNone;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
void RHashTableBase::ReformTable(TUint aNewIndexBits)
|
sl@0
|
236 |
{
|
sl@0
|
237 |
if (!iElements)
|
sl@0
|
238 |
return;
|
sl@0
|
239 |
TUint32 max = 1u << iIndexBits;
|
sl@0
|
240 |
TUint32 newmax = 1u << aNewIndexBits;
|
sl@0
|
241 |
TUint32 newmask = newmax - 1;
|
sl@0
|
242 |
TUint32 ix = 0;
|
sl@0
|
243 |
TUint32 newsh = 32 - aNewIndexBits;
|
sl@0
|
244 |
iGeneration ^= 1; // change generation so we know which entries have been updated
|
sl@0
|
245 |
for (; ix < max; ++ix)
|
sl@0
|
246 |
{
|
sl@0
|
247 |
SElement* e = Element(ix);
|
sl@0
|
248 |
if (e->IsEmpty())
|
sl@0
|
249 |
continue; // skip empty entries
|
sl@0
|
250 |
if (e->IsDeleted())
|
sl@0
|
251 |
{
|
sl@0
|
252 |
e->SetEmpty(); // mark deleted entries as empty
|
sl@0
|
253 |
continue;
|
sl@0
|
254 |
}
|
sl@0
|
255 |
if ((e->iHash & EStateMask) == iGeneration) // entry has been processed so leave it alone
|
sl@0
|
256 |
continue;
|
sl@0
|
257 |
TUint32 pos = e->iHash >> newsh;
|
sl@0
|
258 |
if (pos == ix)
|
sl@0
|
259 |
{
|
sl@0
|
260 |
e->iHash ^= 1; // entry is in first position for its hash so leave it there
|
sl@0
|
261 |
continue;
|
sl@0
|
262 |
}
|
sl@0
|
263 |
TUint32 step = (e->iHash >> 1) & newmask;
|
sl@0
|
264 |
FOREVER
|
sl@0
|
265 |
{
|
sl@0
|
266 |
SElement* d = Element(pos);
|
sl@0
|
267 |
if (d->IsEmptyOrDeleted())
|
sl@0
|
268 |
{
|
sl@0
|
269 |
memcpy(d, e, iElementSize);
|
sl@0
|
270 |
d->iHash &= ~EStateMask;
|
sl@0
|
271 |
d->iHash |= iGeneration; // mark it as processed
|
sl@0
|
272 |
e->SetEmpty(); // remove old entry
|
sl@0
|
273 |
break;
|
sl@0
|
274 |
}
|
sl@0
|
275 |
if ((d->iHash & EStateMask) != iGeneration)
|
sl@0
|
276 |
{
|
sl@0
|
277 |
if (pos == ix)
|
sl@0
|
278 |
{
|
sl@0
|
279 |
e->iHash ^= 1; // entry is already in correct position so leave it there
|
sl@0
|
280 |
break;
|
sl@0
|
281 |
}
|
sl@0
|
282 |
if ((d->iHash >> newsh) == pos)
|
sl@0
|
283 |
{
|
sl@0
|
284 |
// candidate for replacement is in correct position so leave it and look elsewhere
|
sl@0
|
285 |
d->iHash ^= 1;
|
sl@0
|
286 |
}
|
sl@0
|
287 |
else
|
sl@0
|
288 |
{
|
sl@0
|
289 |
Mem::Swap(d, e, iElementSize); // switch entries
|
sl@0
|
290 |
d->iHash ^= 1; // mark entry as processed
|
sl@0
|
291 |
--ix; // process current position again
|
sl@0
|
292 |
break;
|
sl@0
|
293 |
}
|
sl@0
|
294 |
}
|
sl@0
|
295 |
pos = (pos + step) & newmask;
|
sl@0
|
296 |
}
|
sl@0
|
297 |
}
|
sl@0
|
298 |
iIndexBits = (TUint8)aNewIndexBits;
|
sl@0
|
299 |
iEmptyCount = newmax - iCount;
|
sl@0
|
300 |
SetThresholds();
|
sl@0
|
301 |
#ifdef _DEBUG_HASH_TABLE
|
sl@0
|
302 |
VerifyReform();
|
sl@0
|
303 |
#endif
|
sl@0
|
304 |
}
|
sl@0
|
305 |
|
sl@0
|
306 |
#ifdef _DEBUG_HASH_TABLE
|
sl@0
|
307 |
void RHashTableBase::VerifyReform()
|
sl@0
|
308 |
{
|
sl@0
|
309 |
TUint32 dcount;
|
sl@0
|
310 |
ConsistencyCheck(&dcount);
|
sl@0
|
311 |
__ASSERT_ALWAYS(dcount==0, __PANIC(EHashTableDeletedEntryAfterReform));
|
sl@0
|
312 |
}
|
sl@0
|
313 |
#endif
|
sl@0
|
314 |
|
sl@0
|
315 |
EXPORT_C void RHashTableBase::ConsistencyCheck(TUint32* aDeleted, TUint32* aComparisons, TUint32 aChainLimit, TUint32* aChainInfo)
|
sl@0
|
316 |
{
|
sl@0
|
317 |
#ifdef _DEBUG_HASH_TABLE
|
sl@0
|
318 |
TUint32 count = 0;
|
sl@0
|
319 |
TUint32 dcount = 0;
|
sl@0
|
320 |
TUint32 ecount = 0;
|
sl@0
|
321 |
TUint32 max = 1u << iIndexBits;
|
sl@0
|
322 |
TUint32 mask = max - 1;
|
sl@0
|
323 |
TUint32 sh = 32 - iIndexBits;
|
sl@0
|
324 |
TUint32 ix = 0;
|
sl@0
|
325 |
TUint32 cmp = 0;
|
sl@0
|
326 |
if (aChainInfo)
|
sl@0
|
327 |
memclr(aChainInfo, aChainLimit*sizeof(TUint32));
|
sl@0
|
328 |
if (iElements)
|
sl@0
|
329 |
{
|
sl@0
|
330 |
for (ix = 0; ix < max; ++ix)
|
sl@0
|
331 |
{
|
sl@0
|
332 |
SElement* e = Element(ix);
|
sl@0
|
333 |
if (e->IsEmpty())
|
sl@0
|
334 |
{
|
sl@0
|
335 |
++ecount;
|
sl@0
|
336 |
continue;
|
sl@0
|
337 |
}
|
sl@0
|
338 |
if (e->IsDeleted())
|
sl@0
|
339 |
{
|
sl@0
|
340 |
++dcount;
|
sl@0
|
341 |
continue;
|
sl@0
|
342 |
}
|
sl@0
|
343 |
++count;
|
sl@0
|
344 |
__ASSERT_ALWAYS((e->iHash & EStateMask) == iGeneration, __PANIC(EHashTableBadGeneration));
|
sl@0
|
345 |
TUint32 hash = (*iHashFunc)(GetKey(e));
|
sl@0
|
346 |
hash = (hash &~ EStateMask) | iGeneration;
|
sl@0
|
347 |
__ASSERT_ALWAYS(e->iHash == hash, __PANIC(EHashTableBadHash));
|
sl@0
|
348 |
|
sl@0
|
349 |
TUint32 pos = hash >> sh;
|
sl@0
|
350 |
TUint32 step = (hash >> 1) & mask;
|
sl@0
|
351 |
SElement* f = 0;
|
sl@0
|
352 |
TUint32 cl = 0;
|
sl@0
|
353 |
FOREVER
|
sl@0
|
354 |
{
|
sl@0
|
355 |
f = Element(pos);
|
sl@0
|
356 |
if (f->IsEmpty())
|
sl@0
|
357 |
{
|
sl@0
|
358 |
f = 0;
|
sl@0
|
359 |
break;
|
sl@0
|
360 |
}
|
sl@0
|
361 |
++cl;
|
sl@0
|
362 |
if (!f->IsDeleted() && f->iHash==hash)
|
sl@0
|
363 |
{
|
sl@0
|
364 |
++cmp;
|
sl@0
|
365 |
if (e==f || (*iIdFunc)(GetKey(e), GetKey(f)))
|
sl@0
|
366 |
break;
|
sl@0
|
367 |
}
|
sl@0
|
368 |
pos = (pos + step) & mask;
|
sl@0
|
369 |
}
|
sl@0
|
370 |
__ASSERT_ALWAYS(e==f, __PANIC(EHashTableEntryLost));
|
sl@0
|
371 |
if (aChainInfo && cl<aChainLimit)
|
sl@0
|
372 |
++aChainInfo[cl];
|
sl@0
|
373 |
}
|
sl@0
|
374 |
}
|
sl@0
|
375 |
if (aDeleted)
|
sl@0
|
376 |
*aDeleted = dcount;
|
sl@0
|
377 |
if (aComparisons)
|
sl@0
|
378 |
*aComparisons = cmp;
|
sl@0
|
379 |
__ASSERT_ALWAYS(iCount==count, __PANIC(EHashTableCountWrong));
|
sl@0
|
380 |
__ASSERT_ALWAYS(iEmptyCount==ecount, __PANIC(EHashTableEmptyCountWrong));
|
sl@0
|
381 |
#else
|
sl@0
|
382 |
if (aDeleted)
|
sl@0
|
383 |
*aDeleted = KMaxTUint;
|
sl@0
|
384 |
if (aComparisons)
|
sl@0
|
385 |
*aComparisons = KMaxTUint;
|
sl@0
|
386 |
if (aChainInfo)
|
sl@0
|
387 |
memclr(aChainInfo, aChainLimit*sizeof(TUint32));
|
sl@0
|
388 |
#endif
|
sl@0
|
389 |
}
|
sl@0
|
390 |
|
sl@0
|
391 |
void RHashTableBase::ShrinkTable()
|
sl@0
|
392 |
{
|
sl@0
|
393 |
ReformTable(iIndexBits - 1);
|
sl@0
|
394 |
TUint32 max = 1u << iIndexBits;
|
sl@0
|
395 |
iElements = User::ReAlloc(iElements, max * iElementSize);
|
sl@0
|
396 |
}
|
sl@0
|
397 |
|
sl@0
|
398 |
TInt RHashTableBase::ExpandTable(TInt aNewIndexBits)
|
sl@0
|
399 |
{
|
sl@0
|
400 |
TUint32 newmax = 1u << aNewIndexBits;
|
sl@0
|
401 |
if (!iElements)
|
sl@0
|
402 |
{
|
sl@0
|
403 |
iElements = User::AllocZ(newmax * iElementSize);
|
sl@0
|
404 |
if (!iElements)
|
sl@0
|
405 |
return KErrNoMemory;
|
sl@0
|
406 |
iIndexBits = (TUint8)aNewIndexBits;
|
sl@0
|
407 |
iEmptyCount = newmax;
|
sl@0
|
408 |
SetThresholds();
|
sl@0
|
409 |
return KErrNone;
|
sl@0
|
410 |
}
|
sl@0
|
411 |
TUint32 max = 1u << iIndexBits;
|
sl@0
|
412 |
TAny* p = User::ReAlloc(iElements, newmax * iElementSize);
|
sl@0
|
413 |
if (!p)
|
sl@0
|
414 |
return KErrNoMemory;
|
sl@0
|
415 |
iElements = p;
|
sl@0
|
416 |
memclr(Element(max), (newmax-max)*iElementSize);
|
sl@0
|
417 |
ReformTable(aNewIndexBits);
|
sl@0
|
418 |
return KErrNone;
|
sl@0
|
419 |
}
|
sl@0
|
420 |
|
sl@0
|
421 |
EXPORT_C TInt RHashTableBase::Reserve(TInt aCount)
|
sl@0
|
422 |
{
|
sl@0
|
423 |
__ASSERT_ALWAYS((TUint)aCount<0x40000000u, __PANIC(EHashTableBadReserveCount));
|
sl@0
|
424 |
TInt new_ixb = iIndexBits;
|
sl@0
|
425 |
TUint grow_threshold = iUpperThreshold;
|
sl@0
|
426 |
while (TUint(aCount) > grow_threshold)
|
sl@0
|
427 |
{
|
sl@0
|
428 |
grow_threshold <<= 1;
|
sl@0
|
429 |
++new_ixb;
|
sl@0
|
430 |
}
|
sl@0
|
431 |
// Expand the table if it isn't large enough to fit aCount elements in it
|
sl@0
|
432 |
// or if the table hasn't yet been created, create it with ExpandTable
|
sl@0
|
433 |
if (new_ixb > TInt(iIndexBits) || !iElements)
|
sl@0
|
434 |
{
|
sl@0
|
435 |
return ExpandTable(new_ixb);
|
sl@0
|
436 |
}
|
sl@0
|
437 |
return KErrNone;
|
sl@0
|
438 |
}
|
sl@0
|
439 |
|
sl@0
|
440 |
EXPORT_C void RHashTableBase::ReserveL(TInt aCount)
|
sl@0
|
441 |
{
|
sl@0
|
442 |
User::LeaveIfError(Reserve(aCount));
|
sl@0
|
443 |
}
|
sl@0
|
444 |
|
sl@0
|
445 |
EXPORT_C THashTableIterBase::THashTableIterBase(const RHashTableBase& aTable)
|
sl@0
|
446 |
: iTbl(aTable), iIndex(-1), iPad1(0), iPad2(0)
|
sl@0
|
447 |
{
|
sl@0
|
448 |
}
|
sl@0
|
449 |
|
sl@0
|
450 |
EXPORT_C void THashTableIterBase::Reset()
|
sl@0
|
451 |
{
|
sl@0
|
452 |
iIndex = -1;
|
sl@0
|
453 |
}
|
sl@0
|
454 |
|
sl@0
|
455 |
EXPORT_C const TAny* THashTableIterBase::Next(TInt aOffset)
|
sl@0
|
456 |
{
|
sl@0
|
457 |
TInt max = 1 << iTbl.iIndexBits;
|
sl@0
|
458 |
if (!iTbl.iElements)
|
sl@0
|
459 |
return NULL;
|
sl@0
|
460 |
__ASSERT_DEBUG(iIndex>=-1 && iIndex<=max, __PANIC(EHashTableIterNextBadIndex));
|
sl@0
|
461 |
if (iIndex < max)
|
sl@0
|
462 |
++iIndex;
|
sl@0
|
463 |
for(; iIndex < max; ++iIndex)
|
sl@0
|
464 |
{
|
sl@0
|
465 |
const RHashTableBase::SElement* e = iTbl.ElementC(iIndex);
|
sl@0
|
466 |
if (!e->IsEmptyOrDeleted())
|
sl@0
|
467 |
{
|
sl@0
|
468 |
if (aOffset >= 0)
|
sl@0
|
469 |
return (TUint8*)e + aOffset;
|
sl@0
|
470 |
return *(const TAny**)((TUint8*)e - aOffset);
|
sl@0
|
471 |
}
|
sl@0
|
472 |
}
|
sl@0
|
473 |
return NULL;
|
sl@0
|
474 |
}
|
sl@0
|
475 |
|
sl@0
|
476 |
EXPORT_C const TAny* THashTableIterBase::Current(TInt aOffset) const
|
sl@0
|
477 |
{
|
sl@0
|
478 |
TInt max = 1 << iTbl.iIndexBits;
|
sl@0
|
479 |
if (!iTbl.iElements || iIndex<0 || iIndex>=max)
|
sl@0
|
480 |
return NULL;
|
sl@0
|
481 |
const RHashTableBase::SElement* e = iTbl.ElementC(iIndex);
|
sl@0
|
482 |
__ASSERT_DEBUG(!e->IsEmptyOrDeleted(), __PANIC(EHashTableIterCurrentBadIndex));
|
sl@0
|
483 |
if (aOffset >= 0)
|
sl@0
|
484 |
return (TUint8*)e + aOffset;
|
sl@0
|
485 |
return *(const TAny**)((TUint8*)e - aOffset);
|
sl@0
|
486 |
}
|
sl@0
|
487 |
|
sl@0
|
488 |
EXPORT_C void THashTableIterBase::RemoveCurrent()
|
sl@0
|
489 |
{
|
sl@0
|
490 |
TInt max = 1 << iTbl.iIndexBits;
|
sl@0
|
491 |
if (!iTbl.iElements || iIndex<0 || iIndex>=max)
|
sl@0
|
492 |
return;
|
sl@0
|
493 |
RHashTableBase& tbl = (RHashTableBase&)iTbl;
|
sl@0
|
494 |
RHashTableBase::SElement* e = tbl.Element(iIndex);
|
sl@0
|
495 |
__ASSERT_DEBUG(!e->IsEmptyOrDeleted(), __PANIC(EHashTableIterCurrentBadIndex));
|
sl@0
|
496 |
|
sl@0
|
497 |
// mark entry as deleted but don't shrink the array since that will mess up the iteration
|
sl@0
|
498 |
e->SetDeleted();
|
sl@0
|
499 |
if (--tbl.iCount == 0)
|
sl@0
|
500 |
{
|
sl@0
|
501 |
memclr(tbl.iElements, max * tbl.iElementSize);
|
sl@0
|
502 |
tbl.iEmptyCount = max;
|
sl@0
|
503 |
tbl.iGeneration = RHashTableBase::EGen0;
|
sl@0
|
504 |
}
|
sl@0
|
505 |
}
|
sl@0
|
506 |
|
sl@0
|
507 |
/**
|
sl@0
|
508 |
@publishedAll
|
sl@0
|
509 |
@released
|
sl@0
|
510 |
|
sl@0
|
511 |
Calculate a 32 bit hash from an 8 bit descriptor.
|
sl@0
|
512 |
|
sl@0
|
513 |
@param aDes The descriptor to be hashed.
|
sl@0
|
514 |
@return The calculated 32 bit hash value.
|
sl@0
|
515 |
*/
|
sl@0
|
516 |
EXPORT_C TUint32 DefaultHash::Des8(const TDesC8& aDes)
|
sl@0
|
517 |
{
|
sl@0
|
518 |
return DefaultStringHash(aDes.Ptr(), aDes.Length());
|
sl@0
|
519 |
}
|
sl@0
|
520 |
|
sl@0
|
521 |
|
sl@0
|
522 |
/**
|
sl@0
|
523 |
@publishedAll
|
sl@0
|
524 |
@released
|
sl@0
|
525 |
|
sl@0
|
526 |
Calculate a 32 bit hash from a 16 bit descriptor.
|
sl@0
|
527 |
|
sl@0
|
528 |
@param aDes The descriptor to be hashed.
|
sl@0
|
529 |
@return The calculated 32 bit hash value.
|
sl@0
|
530 |
*/
|
sl@0
|
531 |
EXPORT_C TUint32 DefaultHash::Des16(const TDesC16& aDes)
|
sl@0
|
532 |
{
|
sl@0
|
533 |
return DefaultWStringHash(aDes.Ptr(), aDes.Size());
|
sl@0
|
534 |
}
|
sl@0
|
535 |
|
sl@0
|
536 |
|
sl@0
|
537 |
/**
|
sl@0
|
538 |
@publishedAll
|
sl@0
|
539 |
@released
|
sl@0
|
540 |
|
sl@0
|
541 |
Calculate a 32 bit hash from a TInt pointer.
|
sl@0
|
542 |
|
sl@0
|
543 |
@param aPtr The TInt pointer to be hashed.
|
sl@0
|
544 |
@return The calculated 32 bit hash value.
|
sl@0
|
545 |
*/
|
sl@0
|
546 |
EXPORT_C TUint32 DefaultHash::IntegerPtr(TInt* const& aPtr)
|
sl@0
|
547 |
{
|
sl@0
|
548 |
return Integer((TInt)aPtr);
|
sl@0
|
549 |
}
|
sl@0
|
550 |
|
sl@0
|
551 |
/**
|
sl@0
|
552 |
@publishedAll
|
sl@0
|
553 |
@released
|
sl@0
|
554 |
|
sl@0
|
555 |
Calculate a 32 bit hash from a TDesC8 pointer.
|
sl@0
|
556 |
|
sl@0
|
557 |
@param aPtr The TDesC8 pointer to be hashed.
|
sl@0
|
558 |
@return The calculated 32 bit hash value.
|
sl@0
|
559 |
*/
|
sl@0
|
560 |
EXPORT_C TUint32 DefaultHash::Des8Ptr(TDesC8* const& aPtr)
|
sl@0
|
561 |
{
|
sl@0
|
562 |
return Integer((TInt)aPtr);
|
sl@0
|
563 |
}
|
sl@0
|
564 |
|
sl@0
|
565 |
/**
|
sl@0
|
566 |
@publishedAll
|
sl@0
|
567 |
@released
|
sl@0
|
568 |
|
sl@0
|
569 |
Calculate a 32 bit hash from a TDesC16 pointer.
|
sl@0
|
570 |
|
sl@0
|
571 |
@param aPtr The TDesC16 pointer to be hashed.
|
sl@0
|
572 |
@return The calculated 32 bit hash value.
|
sl@0
|
573 |
*/
|
sl@0
|
574 |
EXPORT_C TUint32 DefaultHash::Des16Ptr(TDesC16* const& aPtr)
|
sl@0
|
575 |
{
|
sl@0
|
576 |
return Integer((TInt)aPtr);
|
sl@0
|
577 |
}
|
sl@0
|
578 |
|
sl@0
|
579 |
/**
|
sl@0
|
580 |
@publishedAll
|
sl@0
|
581 |
@released
|
sl@0
|
582 |
|
sl@0
|
583 |
Compare two integers for equality.
|
sl@0
|
584 |
|
sl@0
|
585 |
@param aA The first integer to be compared
|
sl@0
|
586 |
@param aB The second integer to be compared
|
sl@0
|
587 |
@return ETrue if the arguments are equal, EFalse otherwise.
|
sl@0
|
588 |
*/
|
sl@0
|
589 |
EXPORT_C TBool DefaultIdentity::Integer(const TInt& aA, const TInt& aB)
|
sl@0
|
590 |
{
|
sl@0
|
591 |
return aA == aB;
|
sl@0
|
592 |
}
|
sl@0
|
593 |
|
sl@0
|
594 |
|
sl@0
|
595 |
/**
|
sl@0
|
596 |
@publishedAll
|
sl@0
|
597 |
@released
|
sl@0
|
598 |
|
sl@0
|
599 |
Compare two 8 bit descriptors for exact binary equality.
|
sl@0
|
600 |
|
sl@0
|
601 |
@param aA The first integer to be compared
|
sl@0
|
602 |
@param aB The second integer to be compared
|
sl@0
|
603 |
@return ETrue if the arguments are identical, EFalse otherwise.
|
sl@0
|
604 |
*/
|
sl@0
|
605 |
EXPORT_C TBool DefaultIdentity::Des8(const TDesC8& aA, const TDesC8& aB)
|
sl@0
|
606 |
{
|
sl@0
|
607 |
return aA == aB;
|
sl@0
|
608 |
}
|
sl@0
|
609 |
|
sl@0
|
610 |
|
sl@0
|
611 |
/**
|
sl@0
|
612 |
@publishedAll
|
sl@0
|
613 |
@released
|
sl@0
|
614 |
|
sl@0
|
615 |
Compare two 16 bit descriptors for exact binary equality.
|
sl@0
|
616 |
|
sl@0
|
617 |
@param aA The first integer to be compared
|
sl@0
|
618 |
@param aB The second integer to be compared
|
sl@0
|
619 |
@return ETrue if the arguments are identical, EFalse otherwise.
|
sl@0
|
620 |
*/
|
sl@0
|
621 |
EXPORT_C TBool DefaultIdentity::Des16(const TDesC16& aA, const TDesC16& aB)
|
sl@0
|
622 |
{
|
sl@0
|
623 |
return aA == aB;
|
sl@0
|
624 |
}
|
sl@0
|
625 |
|
sl@0
|
626 |
/**
|
sl@0
|
627 |
@publishedAll
|
sl@0
|
628 |
@released
|
sl@0
|
629 |
|
sl@0
|
630 |
Compare two TInt pointers for equality.
|
sl@0
|
631 |
|
sl@0
|
632 |
@param aA The first pointer to be compared
|
sl@0
|
633 |
@param aB The second pointer to be compared
|
sl@0
|
634 |
@return ETrue if the arguments are equal, EFalse otherwise.
|
sl@0
|
635 |
*/
|
sl@0
|
636 |
EXPORT_C TBool DefaultIdentity::IntegerPtr(TInt* const& aA,TInt* const& aB)
|
sl@0
|
637 |
{
|
sl@0
|
638 |
return aA == aB;
|
sl@0
|
639 |
}
|
sl@0
|
640 |
|
sl@0
|
641 |
/**
|
sl@0
|
642 |
@publishedAll
|
sl@0
|
643 |
@released
|
sl@0
|
644 |
|
sl@0
|
645 |
Compare two TDesC8 pointers for equality.
|
sl@0
|
646 |
|
sl@0
|
647 |
@param aA The first pointer to be compared
|
sl@0
|
648 |
@param aB The second pointer to be compared
|
sl@0
|
649 |
@return ETrue if the arguments are equal, EFalse otherwise.
|
sl@0
|
650 |
*/
|
sl@0
|
651 |
EXPORT_C TBool DefaultIdentity::Des8Ptr(TDesC8* const& aA,TDesC8* const& aB)
|
sl@0
|
652 |
{
|
sl@0
|
653 |
return aA == aB;
|
sl@0
|
654 |
}
|
sl@0
|
655 |
|
sl@0
|
656 |
/**
|
sl@0
|
657 |
@publishedAll
|
sl@0
|
658 |
@released
|
sl@0
|
659 |
|
sl@0
|
660 |
Compare two TDesC16 pointers for equality.
|
sl@0
|
661 |
|
sl@0
|
662 |
@param aA The first pointer to be compared
|
sl@0
|
663 |
@param aB The second pointer to be compared
|
sl@0
|
664 |
@return ETrue if the arguments are equal, EFalse otherwise.
|
sl@0
|
665 |
*/
|
sl@0
|
666 |
EXPORT_C TBool DefaultIdentity::Des16Ptr(TDesC16* const& aA,TDesC16* const& aB)
|
sl@0
|
667 |
{
|
sl@0
|
668 |
return aA == aB;
|
sl@0
|
669 |
}
|