You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by jp...@locus.apache.org on 2000/06/28 00:11:13 UTC
cvs commit: xml-xerces/c/src/util RefHashTableOf.c RefHashTableOf.hpp
jpolast 00/06/27 15:11:12
Modified: c/src/util RefHashTableOf.c RefHashTableOf.hpp
Log:
added more general functionality to hashtables.
able to specify which hasher to use.
default: HashXMLCh [hashes XMLCh* strings]
future todo: make hasher class references static so only
one instance of a hasher is ever created.
Revision Changes Path
1.4 +61 -37 xml-xerces/c/src/util/RefHashTableOf.c
Index: RefHashTableOf.c
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/util/RefHashTableOf.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RefHashTableOf.c 2000/03/02 19:54:44 1.3
+++ RefHashTableOf.c 2000/06/27 22:11:12 1.4
@@ -56,6 +56,14 @@
/**
* $Log: RefHashTableOf.c,v $
+ * Revision 1.4 2000/06/27 22:11:12 jpolast
+ * added more general functionality to hashtables.
+ * able to specify which hasher to use.
+ * default: HashXMLCh [hashes XMLCh* strings]
+ *
+ * future todo: make hasher class references static so only
+ * one instance of a hasher is ever created.
+ *
* Revision 1.3 2000/03/02 19:54:44 roddey
* This checkin includes many changes done while waiting for the
* 1.1.0 code to be finished. I can't list them all here, but a list is
@@ -84,15 +92,35 @@
// ---------------------------------------------------------------------------
// RefHashTableOf: Constructors and Destructor
// ---------------------------------------------------------------------------
-template <class TVal> RefHashTableOf<TVal>::
-RefHashTableOf( const unsigned int modulus
- , const bool adoptElems) :
-
- fAdoptedElems(adoptElems)
- , fBucketList(0)
- , fHashModulus(modulus)
+template <class TVal> RefHashTableOf<TVal>::RefHashTableOf(const unsigned int modulus, const bool adoptElems)
+ : fAdoptedElems(adoptElems), fBucketList(0), fHashModulus(modulus)
+{
+ initialize(modulus);
+
+ // create default hasher
+ fHash = new HashXMLCh();
+}
+
+template <class TVal> RefHashTableOf<TVal>::RefHashTableOf(const unsigned int modulus, const bool adoptElems, const HashBase* hash)
+ : fAdoptedElems(adoptElems), fBucketList(0), fHashModulus(modulus)
{
- if (modulus == 0)
+ initialize(modulus);
+ // set hasher
+ fHash = hash;
+}
+
+template <class TVal> RefHashTableOf<TVal>::RefHashTableOf(const unsigned int modulus)
+ : fAdoptedElems(true), fBucketList(0), fHashModulus(modulus)
+{
+ initialize(modulus);
+
+ // create default hasher
+ fHash = new HashXMLCh();
+}
+
+template <class TVal> void RefHashTableOf<TVal>::initialize(const unsigned int modulus)
+{
+ if (modulus == 0)
ThrowXML(IllegalArgumentException, XMLExcepts::HshTbl_ZeroModulus);
// Allocate the bucket list and zero them
@@ -105,8 +133,9 @@
{
removeAll();
- // Then delete the bucket list
+ // Then delete the bucket list & hasher
delete [] fBucketList;
+ delete fHash;
}
@@ -125,7 +154,7 @@
}
template <class TVal> bool RefHashTableOf<TVal>::
-containsKey(const XMLCh* const key) const
+containsKey(const void* const key) const
{
unsigned int hashVal;
const RefHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
@@ -133,7 +162,7 @@
}
template <class TVal> void RefHashTableOf<TVal>::
-removeKey(const XMLCh* const key)
+removeKey(const void* const key)
{
unsigned int hashVal;
removeBucketElem(key, hashVal);
@@ -170,7 +199,7 @@
// ---------------------------------------------------------------------------
// RefHashTableOf: Getters
// ---------------------------------------------------------------------------
-template <class TVal> TVal* RefHashTableOf<TVal>::get(const XMLCh* const key)
+template <class TVal> TVal* RefHashTableOf<TVal>::get(const void* const key)
{
unsigned int hashVal;
RefHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
@@ -180,7 +209,7 @@
}
template <class TVal> const TVal* RefHashTableOf<TVal>::
-get(const XMLCh* const key) const
+get(const void* const key) const
{
unsigned int hashVal;
const RefHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
@@ -193,15 +222,11 @@
// ---------------------------------------------------------------------------
// RefHashTableOf: Putters
// ---------------------------------------------------------------------------
-template <class TVal> void RefHashTableOf<TVal>::put(TVal* const valueToAdopt)
+template <class TVal> void RefHashTableOf<TVal>::put(void* key, TVal* const valueToAdopt)
{
// First see if the key exists already
unsigned int hashVal;
- RefHashTableBucketElem<TVal>* newBucket = findBucketElem
- (
- valueToAdopt->getKey()
- , hashVal
- );
+ RefHashTableBucketElem<TVal>* newBucket = findBucketElem(key, hashVal);
//
// If so,then update its value. If not, then we need to add it to
@@ -212,24 +237,31 @@
if (fAdoptedElems)
delete newBucket->fData;
newBucket->fData = valueToAdopt;
+ newBucket->fKey = key;
}
else
{
- newBucket = new RefHashTableBucketElem<TVal>(valueToAdopt, fBucketList[hashVal]);
+ newBucket = new RefHashTableBucketElem<TVal>(key, valueToAdopt, fBucketList[hashVal]);
fBucketList[hashVal] = newBucket;
}
}
+// this function is deprecated in favor of put(key,value). use at your own risk.
+template <class TVal> void RefHashTableOf<TVal>::put(TVal* const valueToAdopt)
+{
+ put((void*)(valueToAdopt->getKey()), valueToAdopt);
+}
+
// ---------------------------------------------------------------------------
// RefHashTableOf: Private methods
// ---------------------------------------------------------------------------
template <class TVal> RefHashTableBucketElem<TVal>* RefHashTableOf<TVal>::
-findBucketElem(const XMLCh* const key, unsigned int& hashVal)
+findBucketElem(const void* const key, unsigned int& hashVal)
{
// Hash the key
- hashVal = XMLString::hash(key, fHashModulus);
+ hashVal = fHash->getHashVal(key, fHashModulus);
if (hashVal > fHashModulus)
ThrowXML(RuntimeException, XMLExcepts::HshTbl_BadHashFromKey);
@@ -237,7 +269,7 @@
RefHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
- if (!XMLString::compareString(key, curElem->fData->getKey()))
+ if (fHash->equals(key, curElem->fKey))
return curElem;
curElem = curElem->fNext;
@@ -246,10 +278,10 @@
}
template <class TVal> const RefHashTableBucketElem<TVal>* RefHashTableOf<TVal>::
-findBucketElem(const XMLCh* const key, unsigned int& hashVal) const
+findBucketElem(const void* const key, unsigned int& hashVal) const
{
// Hash the key
- hashVal = XMLString::hash(key, fHashModulus);
+ hashVal = fHash->getHashVal(key, fHashModulus);
if (hashVal > fHashModulus)
ThrowXML(RuntimeException, XMLExcepts::HshTbl_BadHashFromKey);
@@ -257,7 +289,7 @@
const RefHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
- if (!XMLString::compareString(key, curElem->fData->getKey()))
+ if (fHash->equals(key, curElem->fKey))
return curElem;
curElem = curElem->fNext;
@@ -267,10 +299,10 @@
template <class TVal> void RefHashTableOf<TVal>::
-removeBucketElem(const XMLCh* const key, unsigned int& hashVal)
+removeBucketElem(const void* const key, unsigned int& hashVal)
{
// Hash the key
- hashVal = XMLString::hash(key, fHashModulus);
+ hashVal = fHash->getHashVal(key, fHashModulus);
if (hashVal > fHashModulus)
ThrowXML(RuntimeException, XMLExcepts::HshTbl_BadHashFromKey);
@@ -283,7 +315,7 @@
while (curElem)
{
- if (!XMLString::compareString(key, curElem->fData->getKey()))
+ if (fHash->equals(key, curElem->fKey))
{
if (!lastElem)
{
@@ -322,12 +354,8 @@
// RefHashTableOfEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal> RefHashTableOfEnumerator<TVal>::
-RefHashTableOfEnumerator( RefHashTableOf<TVal>* const toEnum
- , const bool adopt) :
- fAdopted(adopt)
- , fCurElem(0)
- , fCurHash((unsigned int)-1)
- , fToEnum(toEnum)
+RefHashTableOfEnumerator(RefHashTableOf<TVal>* const toEnum, const bool adopt)
+ : fAdopted(adopt), fCurElem(0), fCurHash((unsigned int)-1), fToEnum(toEnum)
{
//
// Find the next available bucket element in the hash table. If it
@@ -349,8 +377,7 @@
// ---------------------------------------------------------------------------
// RefHashTableOfEnumerator: Enum interface
// ---------------------------------------------------------------------------
-template <class TVal> bool RefHashTableOfEnumerator<TVal>::
-hasMoreElements() const
+template <class TVal> bool RefHashTableOfEnumerator<TVal>::hasMoreElements() const
{
//
// If our current has is at the max and there are no more elements
1.6 +35 -31 xml-xerces/c/src/util/RefHashTableOf.hpp
Index: RefHashTableOf.hpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/util/RefHashTableOf.hpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- RefHashTableOf.hpp 2000/03/02 19:54:44 1.5
+++ RefHashTableOf.hpp 2000/06/27 22:11:12 1.6
@@ -56,6 +56,14 @@
/*
* $Log: RefHashTableOf.hpp,v $
+ * Revision 1.6 2000/06/27 22:11:12 jpolast
+ * added more general functionality to hashtables.
+ * able to specify which hasher to use.
+ * default: HashXMLCh [hashes XMLCh* strings]
+ *
+ * future todo: make hasher class references static so only
+ * one instance of a hasher is ever created.
+ *
* Revision 1.5 2000/03/02 19:54:44 roddey
* This checkin includes many changes done while waiting for the
* 1.1.0 code to be finished. I can't list them all here, but a list is
@@ -86,12 +94,15 @@
#include <util/XercesDefs.hpp>
#include <util/KeyValuePair.hpp>
+#include <util/HashBase.hpp>
#include <util/IllegalArgumentException.hpp>
#include <util/NoSuchElementException.hpp>
#include <util/RuntimeException.hpp>
#include <util/XMLExceptMsgs.hpp>
#include <util/XMLEnumerator.hpp>
#include <util/XMLString.hpp>
+#include <util/HashBase.hpp>
+#include <util/HashXMLCh.hpp>
//
@@ -108,29 +119,31 @@
//
template <class TVal> struct RefHashTableBucketElem
{
- RefHashTableBucketElem(TVal* const value, RefHashTableBucketElem<TVal>* next) :
-
- fData(value)
- , fNext(next)
+ RefHashTableBucketElem(void* key, TVal* const value, RefHashTableBucketElem<TVal>* next)
+ : fData(value), fNext(next), fKey(key)
{
}
TVal* fData;
RefHashTableBucketElem<TVal>* fNext;
+ void* fKey;
};
template <class TVal> class RefHashTableOf
{
-public :
+public:
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
- RefHashTableOf
- (
- const unsigned int modulus
- , const bool adoptElems = true
- );
+ // backwards compatability - default hasher is HashXMLCh
+ RefHashTableOf(const unsigned int modulus);
+ // backwards compatability - default hasher is HashXMLCh
+ RefHashTableOf(const unsigned int modulus, const bool adoptElems);
+ // if a hash function is passed in, it will be deleted when the hashtable is deleted.
+ // use a new instance of the hasher class for each hashtable, otherwise one hashtable
+ // may delete the hasher of a different hashtable if both use the same hasher.
+ RefHashTableOf(const unsigned int modulus, const bool adoptElems, const HashBase* hash);
~RefHashTableOf();
@@ -138,22 +151,23 @@
// Element management
// -----------------------------------------------------------------------
bool isEmpty() const;
- bool containsKey(const XMLCh* const key) const;
- void removeKey(const XMLCh* const key);
+ bool containsKey(const void* const key) const;
+ void removeKey(const void* const key);
void removeAll();
// -----------------------------------------------------------------------
// Getters
// -----------------------------------------------------------------------
- TVal* get(const XMLCh* const key);
- const TVal* get(const XMLCh* const key) const;
+ TVal* get(const void* const key);
+ const TVal* get(const void* const key) const;
// -----------------------------------------------------------------------
// Putters
// -----------------------------------------------------------------------
- void put(TVal* const valueToAdopt);
+ void put(void* key, TVal* const valueToAdopt);
+ void put(TVal* const valueToAdopt); // deprecated. use at your own risk. instead use put(key, value)
private :
@@ -167,17 +181,10 @@
// -----------------------------------------------------------------------
// Private methods
// -----------------------------------------------------------------------
- RefHashTableBucketElem<TVal>* findBucketElem
- (
- const XMLCh* const key,
- unsigned int& hashVal
- );
- const RefHashTableBucketElem<TVal>* findBucketElem
- (
- const XMLCh* const key
- , unsigned int& hashVal
- ) const;
- void removeBucketElem(const XMLCh* const key, unsigned int& hashVal);
+ RefHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal);
+ const RefHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal) const;
+ void removeBucketElem(const void* const key, unsigned int& hashVal);
+ void initialize(const unsigned int modulus);
// -----------------------------------------------------------------------
@@ -195,10 +202,14 @@
// fHashModulus
// The modulus used for this hash table, to hash the keys. This is
// also the number of elements in the bucket list.
+ //
+ // fHash
+ // The hasher for the key data type.
// -----------------------------------------------------------------------
bool fAdoptedElems;
RefHashTableBucketElem<TVal>** fBucketList;
unsigned int fHashModulus;
+ HashBase* fHash;
};
@@ -213,11 +224,7 @@
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
- RefHashTableOfEnumerator
- (
- RefHashTableOf<TVal>* const toEnum
- , const bool adopt = false
- );
+ RefHashTableOfEnumerator(RefHashTableOf<TVal>* const toEnum, const bool adopt = false);
~RefHashTableOfEnumerator();