You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by am...@apache.org on 2008/04/17 15:40:26 UTC
svn commit: r649094 - in /xerces/c/trunk/src/xercesc/util: NameIdPool.c
NameIdPool.hpp
Author: amassari
Date: Thu Apr 17 06:40:21 2008
New Revision: 649094
URL: http://svn.apache.org/viewvc?rev=649094&view=rev
Log:
Make NameIdPool reuse RefHashTableOf so that it supports rehashing
Modified:
xerces/c/trunk/src/xercesc/util/NameIdPool.c
xerces/c/trunk/src/xercesc/util/NameIdPool.hpp
Modified: xerces/c/trunk/src/xercesc/util/NameIdPool.c
URL: http://svn.apache.org/viewvc/xerces/c/trunk/src/xercesc/util/NameIdPool.c?rev=649094&r1=649093&r2=649094&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/NameIdPool.c (original)
+++ xerces/c/trunk/src/xercesc/util/NameIdPool.c Thu Apr 17 06:40:21 2008
@@ -36,23 +36,6 @@
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
-// NameIdPoolBucketElem: Constructors and Destructor
-// ---------------------------------------------------------------------------
-template <class TElem> NameIdPoolBucketElem<TElem>::
-NameIdPoolBucketElem(TElem* const value
- , NameIdPoolBucketElem<TElem>* const next) :
- fData(value)
- , fNext(next)
-{
-}
-
-template <class TElem> NameIdPoolBucketElem<TElem>::~NameIdPoolBucketElem()
-{
- // Nothing to do
-}
-
-
-// ---------------------------------------------------------------------------
// NameIdPool: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
@@ -64,18 +47,11 @@
, fIdPtrs(0)
, fIdPtrsCount(initSize)
, fIdCounter(0)
- , fHashModulus(hashModulus)
{
- if (!fHashModulus)
+ if (!hashModulus)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_ZeroModulus, fMemoryManager);
- // Allocate the bucket list and zero them
- fBucketList = (NameIdPoolBucketElem<TElem>**) fMemoryManager->allocate
- (
- fHashModulus * sizeof(NameIdPoolBucketElem<TElem>*)
- ); //new NameIdPoolBucketElem<TElem>*[fHashModulus];
- memset(fBucketList, 0, sizeof(fBucketList[0]) * fHashModulus);
-
+ fBucketList = new (fMemoryManager) RefHashTableOf<TElem>(hashModulus, fMemoryManager);
//
// Allocate the initial id pointers array. We don't have to zero them
// out since the fIdCounter value tells us which ones are valid. The
@@ -98,9 +74,7 @@
//
fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
- // Remove all elements then delete the bucket list
- removeAll();
- fMemoryManager->deallocate(fBucketList); //delete [] fBucketList;
+ delete fBucketList;
}
@@ -110,9 +84,7 @@
template <class TElem> bool
NameIdPool<TElem>::containsKey(const XMLCh* const key) const
{
- unsigned int hashVal;
- const NameIdPoolBucketElem<TElem>* findIt = findBucketElem(key, hashVal);
- return (findIt != 0);
+ return fBucketList->containsKey(key);
}
@@ -120,27 +92,7 @@
{
if (fIdCounter == 0) return;
- // Clean up the buckets first
- for (unsigned int buckInd = 0; buckInd < fHashModulus; buckInd++)
- {
- NameIdPoolBucketElem<TElem>* curElem = fBucketList[buckInd];
- NameIdPoolBucketElem<TElem>* nextElem;
- while (curElem)
- {
- // Save the next element before we hose this one
- nextElem = curElem->fNext;
-
- delete curElem->fData;
- // destructor is empty...
- // curElem->~NameIdPoolBucketElem();
- fMemoryManager->deallocate(curElem);
-
- curElem = nextElem;
- }
-
- // Empty out the bucket
- fBucketList[buckInd] = 0;
- }
+ fBucketList->removeAll();
// Reset the id counter
fIdCounter = 0;
@@ -153,21 +105,13 @@
template <class TElem> TElem*
NameIdPool<TElem>::getByKey(const XMLCh* const key)
{
- unsigned int hashVal;
- NameIdPoolBucketElem<TElem>* findIt = findBucketElem(key, hashVal);
- if (!findIt)
- return 0;
- return findIt->fData;
+ return fBucketList->get(key);
}
template <class TElem> const TElem*
NameIdPool<TElem>::getByKey(const XMLCh* const key) const
{
- unsigned int hashVal;
- const NameIdPoolBucketElem<TElem>* findIt = findBucketElem(key, hashVal);
- if (!findIt)
- return 0;
- return findIt->fData;
+ return fBucketList->get(key);
}
template <class TElem> TElem*
@@ -203,8 +147,7 @@
unsigned int NameIdPool<TElem>::put(TElem* const elemToAdopt)
{
// First see if the key exists already. If so, its an error
- unsigned int hashVal;
- if (findBucketElem(elemToAdopt->getKey(), hashVal))
+ if(fBucketList->containsKey(elemToAdopt->getKey()))
{
ThrowXMLwithMemMgr1
(
@@ -215,11 +158,7 @@
);
}
- // Create a new bucket element and add it to the appropriate list
- NameIdPoolBucketElem<TElem>* newBucket =
- new (fMemoryManager->allocate(sizeof(NameIdPoolBucketElem<TElem>)))
- NameIdPoolBucketElem<TElem>(elemToAdopt,fBucketList[hashVal]);
- fBucketList[hashVal] = newBucket;
+ fBucketList->put((void*)elemToAdopt->getKey(), elemToAdopt);
//
// Give this new one the next available id and add to the pointer list.
@@ -254,52 +193,6 @@
// ---------------------------------------------------------------------------
-// NameIdPool: Private methods
-// ---------------------------------------------------------------------------
-template <class TElem>
-NameIdPoolBucketElem<TElem>* NameIdPool<TElem>::
-findBucketElem(const XMLCh* const key, unsigned int& hashVal)
-{
- // Hash the key
- hashVal = XMLString::hash(key, fHashModulus, fMemoryManager);
-
- assert(hashVal < fHashModulus);
-
- // Search that bucket for the key
- NameIdPoolBucketElem<TElem>* curElem = fBucketList[hashVal];
- while (curElem)
- {
- if (XMLString::equals(key, curElem->fData->getKey()))
- return curElem;
- curElem = curElem->fNext;
- }
- return 0;
-}
-
-template <class TElem>
-const NameIdPoolBucketElem<TElem>* NameIdPool<TElem>::
-findBucketElem(const XMLCh* const key, unsigned int& hashVal) const
-{
- // Hash the key
- hashVal = XMLString::hash(key, fHashModulus, fMemoryManager);
-
- assert(hashVal < fHashModulus);
-
- // Search that bucket for the key
- const NameIdPoolBucketElem<TElem>* curElem = fBucketList[hashVal];
- while (curElem)
- {
- if (XMLString::equals(key, curElem->fData->getKey()))
- return curElem;
-
- curElem = curElem->fNext;
- }
- return 0;
-}
-
-
-
-// ---------------------------------------------------------------------------
// NameIdPoolEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem> NameIdPoolEnumerator<TElem>::
@@ -311,7 +204,7 @@
, fToEnum(toEnum)
, fMemoryManager(manager)
{
- Reset();
+ Reset();
}
template <class TElem> NameIdPoolEnumerator<TElem>::
Modified: xerces/c/trunk/src/xercesc/util/NameIdPool.hpp
URL: http://svn.apache.org/viewvc/xerces/c/trunk/src/xercesc/util/NameIdPool.hpp?rev=649094&r1=649093&r2=649094&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/NameIdPool.hpp (original)
+++ xerces/c/trunk/src/xercesc/util/NameIdPool.hpp Thu Apr 17 06:40:21 2008
@@ -25,6 +25,7 @@
#include <xercesc/util/XMemory.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
+#include <xercesc/util/RefHashTableOf.hpp>
XERCES_CPP_NAMESPACE_BEGIN
@@ -57,30 +58,6 @@
//
// All elements are assumed to be owned by the pool!
//
-// We have to have a bucket element structure to use to maintain the linked
-// lists for each bucket. Because some of the compilers we have to support
-// are totally brain dead, it cannot be a nested class as it should be.
-//
-template <class TElem> struct NameIdPoolBucketElem
-{
-public :
- NameIdPoolBucketElem
- (
- TElem* const value
- , NameIdPoolBucketElem<TElem>* const next
- );
- ~NameIdPoolBucketElem();
-
- TElem* fData;
- NameIdPoolBucketElem<TElem>* fNext;
-private:
- // -----------------------------------------------------------------------
- // Unimplemented constructors and operators
- // -----------------------------------------------------------------------
- NameIdPoolBucketElem(const NameIdPoolBucketElem<TElem>&);
- NameIdPoolBucketElem<TElem>& operator=(const NameIdPoolBucketElem<TElem>&);
-};
-
template <class TElem> class NameIdPool : public XMemory
{
@@ -137,28 +114,11 @@
NameIdPool(const NameIdPool<TElem>&);
NameIdPool<TElem>& operator=(const NameIdPool<TElem>&);
-
- // -----------------------------------------------------------------------
- // Private helper methods
- // -----------------------------------------------------------------------
- NameIdPoolBucketElem<TElem>* findBucketElem
- (
- const XMLCh* const key
- , unsigned int& hashVal
- );
- const NameIdPoolBucketElem<TElem>* findBucketElem
- (
- const XMLCh* const key
- , unsigned int& hashVal
- ) const;
-
-
// -----------------------------------------------------------------------
// Data members
//
// fBucketList
- // This is the array that contains the heads of all of the list
- // buckets, one for each possible hash value.
+ // This is the hash table that contains the values.
//
// fIdPtrs
// fIdPtrsCount
@@ -174,16 +134,12 @@
// element. So the first element is 1, the next is 2, etc... This
// means that this value is set to the top index of the fIdPtrs array.
//
- // fHashModulus
- // This is the modulus to use in this pool. The fBucketList array
- // is of this size. It should be a prime number.
// -----------------------------------------------------------------------
MemoryManager* fMemoryManager;
- NameIdPoolBucketElem<TElem>** fBucketList;
+ RefHashTableOf<TElem>* fBucketList;
TElem** fIdPtrs;
unsigned int fIdPtrsCount;
unsigned int fIdCounter;
- unsigned int fHashModulus;
};
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org