2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
19
* $Id: RefHash3KeysIdPool.c 679340 2008-07-24 10:28:29Z borisk $
23
// ---------------------------------------------------------------------------
25
// ---------------------------------------------------------------------------
26
#if defined(XERCES_TMPLSINC)
27
#include <xercesc/util/RefHash3KeysIdPool.hpp>
30
#include <xercesc/util/NullPointerException.hpp>
34
XERCES_CPP_NAMESPACE_BEGIN
36
// ---------------------------------------------------------------------------
37
// RefHash3KeysIdPool: Constructors and Destructor
38
// ---------------------------------------------------------------------------
39
template <class TVal, class THasher>
40
RefHash3KeysIdPool<TVal, THasher>::RefHash3KeysIdPool(
41
const XMLSize_t modulus,
42
const XMLSize_t initSize,
43
MemoryManager* const manager)
45
: fMemoryManager(manager)
48
, fHashModulus(modulus)
50
, fIdPtrsCount(initSize)
55
// Allocate the initial id pointers array. We don't have to zero them
56
// out since the fIdCounter value tells us which ones are valid. The
57
// zeroth element is never used (and represents an invalid pool id.)
61
fIdPtrs = (TVal**) fMemoryManager->allocate(fIdPtrsCount * sizeof(TVal*)); //new TVal*[fIdPtrsCount];
65
template <class TVal, class THasher>
66
RefHash3KeysIdPool<TVal, THasher>::RefHash3KeysIdPool(
67
const XMLSize_t modulus,
68
const THasher& hasher,
69
const XMLSize_t initSize,
70
MemoryManager* const manager)
72
: fMemoryManager(manager)
75
, fHashModulus(modulus)
78
, fIdPtrsCount(initSize)
83
// Allocate the initial id pointers array. We don't have to zero them
84
// out since the fIdCounter value tells us which ones are valid. The
85
// zeroth element is never used (and represents an invalid pool id.)
89
fIdPtrs = (TVal**) fMemoryManager->allocate(fIdPtrsCount * sizeof(TVal*)); //new TVal*[fIdPtrsCount];
93
template <class TVal, class THasher>
94
RefHash3KeysIdPool<TVal, THasher>::RefHash3KeysIdPool(
95
const XMLSize_t modulus,
96
const bool adoptElems,
97
const XMLSize_t initSize,
98
MemoryManager* const manager)
100
: fMemoryManager(manager)
101
, fAdoptedElems(adoptElems)
103
, fHashModulus(modulus)
105
, fIdPtrsCount(initSize)
111
// Allocate the initial id pointers array. We don't have to zero them
112
// out since the fIdCounter value tells us which ones are valid. The
113
// zeroth element is never used (and represents an invalid pool id.)
117
fIdPtrs = (TVal**) fMemoryManager->allocate(fIdPtrsCount * sizeof(TVal*)); //new TVal*[fIdPtrsCount];
121
template <class TVal, class THasher>
122
RefHash3KeysIdPool<TVal, THasher>::RefHash3KeysIdPool(
123
const XMLSize_t modulus,
124
const bool adoptElems,
125
const THasher& hasher,
126
const XMLSize_t initSize,
127
MemoryManager* const manager)
129
: fMemoryManager(manager)
130
, fAdoptedElems(adoptElems)
132
, fHashModulus(modulus)
135
, fIdPtrsCount(initSize)
140
// Allocate the initial id pointers array. We don't have to zero them
141
// out since the fIdCounter value tells us which ones are valid. The
142
// zeroth element is never used (and represents an invalid pool id.)
146
fIdPtrs = (TVal**) fMemoryManager->allocate(fIdPtrsCount * sizeof(TVal*)); //new TVal*[fIdPtrsCount];
150
template <class TVal, class THasher>
151
void RefHash3KeysIdPool<TVal, THasher>::initialize(const XMLSize_t modulus)
154
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::HshTbl_ZeroModulus, fMemoryManager);
156
// Allocate the bucket list and zero them
157
fBucketList = (RefHash3KeysTableBucketElem<TVal>**) fMemoryManager->allocate
159
fHashModulus * sizeof(RefHash3KeysTableBucketElem<TVal>*)
160
); //new RefHash3KeysTableBucketElem<TVal>*[fHashModulus];
161
memset(fBucketList, 0, sizeof(fBucketList[0]) * fHashModulus);
164
template <class TVal, class THasher>
165
RefHash3KeysIdPool<TVal, THasher>::~RefHash3KeysIdPool()
169
// Then delete the bucket list & hasher & id pointers list
170
fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
172
fMemoryManager->deallocate(fBucketList); //delete [] fBucketList;
177
// ---------------------------------------------------------------------------
178
// RefHash3KeysIdPool: Element management
179
// ---------------------------------------------------------------------------
180
template <class TVal, class THasher>
181
bool RefHash3KeysIdPool<TVal, THasher>::isEmpty() const
183
// Just check the bucket list for non-empty elements
184
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
186
if (fBucketList[buckInd] != 0)
192
template <class TVal, class THasher>
193
bool RefHash3KeysIdPool<TVal, THasher>::
194
containsKey(const void* const key1, const int key2, const int key3) const
197
const RefHash3KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, key3, hashVal);
198
return (findIt != 0);
201
template <class TVal, class THasher>
202
void RefHash3KeysIdPool<TVal, THasher>::removeAll()
204
if (fIdCounter == 0) return;
206
// Clean up the buckets first
207
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
209
// Get the bucket list head for this entry
210
RefHash3KeysTableBucketElem<TVal>* curElem = fBucketList[buckInd];
211
RefHash3KeysTableBucketElem<TVal>* nextElem;
214
// Save the next element before we hose this one
215
nextElem = curElem->fNext;
217
// If we adopted the data, then delete it too
218
// (Note: the userdata hash table instance has data type of void *.
219
// This will generate compiler warnings here on some platforms, but they
220
// can be ignored since fAdoptedElements is false.
222
delete curElem->fData;
224
// Then delete the current element and move forward
226
// destructor is empty...
227
// curElem->~RefHash3KeysTableBucketElem();
228
fMemoryManager->deallocate(curElem);
232
// Clean out this entry
233
fBucketList[buckInd] = 0;
236
// Reset the id counter
241
// ---------------------------------------------------------------------------
242
// RefHash3KeysIdPool: Getters
243
// ---------------------------------------------------------------------------
244
template <class TVal, class THasher>
246
RefHash3KeysIdPool<TVal, THasher>::getByKey(const void* const key1, const int key2, const int key3)
249
RefHash3KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, key3, hashVal);
252
return findIt->fData;
255
template <class TVal, class THasher>
257
RefHash3KeysIdPool<TVal, THasher>::getByKey(const void* const key1, const int key2, const int key3) const
260
const RefHash3KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, key3, hashVal);
263
return findIt->fData;
266
template <class TVal, class THasher>
268
RefHash3KeysIdPool<TVal, THasher>::getById(const unsigned int elemId)
270
// If its either zero or beyond our current id, its an error
271
if (!elemId || (elemId > fIdCounter))
272
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_InvalidId, fMemoryManager);
274
return fIdPtrs[elemId];
277
template <class TVal, class THasher>
279
RefHash3KeysIdPool<TVal, THasher>::getById(const unsigned int elemId) const
281
// If its either zero or beyond our current id, its an error
282
if (!elemId || (elemId > fIdCounter))
283
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_InvalidId, fMemoryManager);
285
return fIdPtrs[elemId];
288
template <class TVal, class THasher>
289
MemoryManager* RefHash3KeysIdPool<TVal, THasher>::getMemoryManager() const
291
return fMemoryManager;
294
template <class TVal, class THasher>
295
XMLSize_t RefHash3KeysIdPool<TVal, THasher>::getHashModulus() const
300
// ---------------------------------------------------------------------------
301
// RefHash3KeysIdPool: Putters
302
// ---------------------------------------------------------------------------
303
template <class TVal, class THasher>
305
RefHash3KeysIdPool<TVal, THasher>::put(void* key1, int key2, int key3, TVal* const valueToAdopt)
307
// First see if the key exists already
310
RefHash3KeysTableBucketElem<TVal>* newBucket = findBucketElem(key1, key2, key3, hashVal);
313
// If so,then update its value. If not, then we need to add it to
318
retId = newBucket->fData->getId();
320
delete newBucket->fData;
321
newBucket->fData = valueToAdopt;
322
newBucket->fKey1 = key1;
323
newBucket->fKey2 = key2;
324
newBucket->fKey3 = key3;
328
// Revisit: the gcc compiler 2.95.x is generating an
329
// internal compiler error message. So we use the default
330
// memory manager for now.
331
#if defined (XML_GCC_VERSION) && (XML_GCC_VERSION < 29600)
332
newBucket = new RefHash3KeysTableBucketElem<TVal>(key1, key2, key3, valueToAdopt, fBucketList[hashVal]);
335
new (fMemoryManager->allocate(sizeof(RefHash3KeysTableBucketElem<TVal>)))
336
RefHash3KeysTableBucketElem<TVal>(key1, key2, key3, valueToAdopt, fBucketList[hashVal]);
338
fBucketList[hashVal] = newBucket;
341
// Give this new one the next available id and add to the pointer list.
342
// Expand the list if that is now required.
344
if (fIdCounter + 1 == fIdPtrsCount)
346
// Create a new count 1.5 times larger and allocate a new array
347
XMLSize_t newCount = (XMLSize_t)(fIdPtrsCount * 1.5);
348
TVal** newArray = (TVal**) fMemoryManager->allocate
350
newCount * sizeof(TVal*)
351
); //new TVal*[newCount];
353
// Copy over the old contents to the new array
354
memcpy(newArray, fIdPtrs, fIdPtrsCount * sizeof(TVal*));
356
// Ok, toss the old array and store the new data
357
fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
359
fIdPtrsCount = newCount;
361
retId = ++fIdCounter;
364
fIdPtrs[retId] = valueToAdopt;
366
// Set the id on the passed element
367
valueToAdopt->setId(retId);
369
// Return the id that we gave to this element
373
// ---------------------------------------------------------------------------
374
// RefHash3KeysIdPool: Private methods
375
// ---------------------------------------------------------------------------
376
template <class TVal, class THasher>
377
inline RefHash3KeysTableBucketElem<TVal>* RefHash3KeysIdPool<TVal, THasher>::
378
findBucketElem(const void* const key1, const int key2, const int key3, XMLSize_t& hashVal)
381
hashVal = fHasher.getHashVal(key1, fHashModulus);
382
assert(hashVal < fHashModulus);
384
// Search that bucket for the key
385
RefHash3KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
388
if((key2==curElem->fKey2) && (key3==curElem->fKey3) && (fHasher.equals(key1, curElem->fKey1)))
391
curElem = curElem->fNext;
396
template <class TVal, class THasher>
397
inline const RefHash3KeysTableBucketElem<TVal>* RefHash3KeysIdPool<TVal, THasher>::
398
findBucketElem(const void* const key1, const int key2, const int key3, XMLSize_t& hashVal) const
401
hashVal = fHasher.getHashVal(key1, fHashModulus);
402
assert(hashVal < fHashModulus);
404
// Search that bucket for the key
405
const RefHash3KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
408
if((key2==curElem->fKey2) && (key3==curElem->fKey3) && (fHasher.equals(key1, curElem->fKey1)))
411
curElem = curElem->fNext;
417
// ---------------------------------------------------------------------------
418
// RefHash3KeysIdPoolEnumerator: Constructors and Destructor
419
// ---------------------------------------------------------------------------
420
template <class TVal, class THasher>
421
RefHash3KeysIdPoolEnumerator<TVal, THasher>::
422
RefHash3KeysIdPoolEnumerator(RefHash3KeysIdPool<TVal, THasher>* const toEnum
424
, MemoryManager* const manager)
425
: fAdoptedElems(adopt), fCurIndex(0), fToEnum(toEnum), fMemoryManager(manager)
428
ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, fMemoryManager);
434
template <class TVal, class THasher>
435
RefHash3KeysIdPoolEnumerator<TVal, THasher>::~RefHash3KeysIdPoolEnumerator()
441
template <class TVal, class THasher>
442
RefHash3KeysIdPoolEnumerator<TVal, THasher>::
443
RefHash3KeysIdPoolEnumerator(const RefHash3KeysIdPoolEnumerator<TVal, THasher>& toCopy) :
444
XMLEnumerator<TVal>(toCopy)
446
, fAdoptedElems(toCopy.fAdoptedElems)
447
, fCurIndex(toCopy.fCurIndex)
448
, fToEnum(toCopy.fToEnum)
449
, fCurElem(toCopy.fCurElem)
450
, fCurHash(toCopy.fCurHash)
451
, fMemoryManager(toCopy.fMemoryManager)
455
// ---------------------------------------------------------------------------
456
// RefHash3KeysIdPoolEnumerator: Enum interface
457
// ---------------------------------------------------------------------------
458
template <class TVal, class THasher>
459
bool RefHash3KeysIdPoolEnumerator<TVal, THasher>::hasMoreElements() const
461
// If our index is zero or past the end, then we are done
462
if (!fCurIndex || (fCurIndex > fToEnum->fIdCounter))
467
template <class TVal, class THasher>
468
TVal& RefHash3KeysIdPoolEnumerator<TVal, THasher>::nextElement()
470
// If our index is zero or past the end, then we are done
471
if (!fCurIndex || (fCurIndex > fToEnum->fIdCounter))
472
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
474
// Return the current element and bump the index
475
return *fToEnum->fIdPtrs[fCurIndex++];
478
template <class TVal, class THasher>
479
void RefHash3KeysIdPoolEnumerator<TVal, THasher>::Reset()
482
// Find the next available bucket element in the pool. We use the id
483
// array since its very easy to enumerator through by just maintaining
484
// an index. If the id counter is zero, then its empty and we leave the
485
// current index to zero.
487
fCurIndex = fToEnum->fIdCounter ? 1:0;
491
template <class TVal, class THasher>
492
XMLSize_t RefHash3KeysIdPoolEnumerator<TVal, THasher>::size() const
494
return fToEnum->fIdCounter;
497
template <class TVal, class THasher>
498
void RefHash3KeysIdPoolEnumerator<TVal, THasher>::resetKey()
500
fCurHash = (XMLSize_t)-1;
505
template <class TVal, class THasher>
506
bool RefHash3KeysIdPoolEnumerator<TVal, THasher>::hasMoreKeys() const
509
// If our current has is at the max and there are no more elements
510
// in the current bucket, then no more elements.
512
if (!fCurElem && (fCurHash == fToEnum->fHashModulus))
518
template <class TVal, class THasher>
519
void RefHash3KeysIdPoolEnumerator<TVal, THasher>::nextElementKey(void*& retKey1, int& retKey2, int& retKey3)
521
// Make sure we have an element to return
523
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
526
// Save the current element, then move up to the next one for the
529
RefHash3KeysTableBucketElem<TVal>* saveElem = fCurElem;
532
retKey1 = saveElem->fKey1;
533
retKey2 = saveElem->fKey2;
534
retKey3 = saveElem->fKey3;
539
template <class TVal, class THasher>
540
void RefHash3KeysIdPoolEnumerator<TVal, THasher>::findNext()
543
// If there is a current element, move to its next element. If this
544
// hits the end of the bucket, the next block will handle the rest.
547
fCurElem = fCurElem->fNext;
550
// If the current element is null, then we have to move up to the
551
// next hash value. If that is the hash modulus, then we cannot
557
if (fCurHash == fToEnum->fHashModulus)
560
// Else find the next non-empty bucket
561
while (fToEnum->fBucketList[fCurHash]==0)
563
// Bump to the next hash value. If we max out return
565
if (fCurHash == fToEnum->fHashModulus)
568
fCurElem = fToEnum->fBucketList[fCurHash];
572
XERCES_CPP_NAMESPACE_END