Fixed Xerces compilation

This commit is contained in:
pelya
2010-11-02 11:55:32 +02:00
parent 570f3a2a9a
commit e60151b10f
23 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,344 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/BaseRefVectorOf.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// BaseRefVectorOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
BaseRefVectorOf<TElem>::BaseRefVectorOf( const XMLSize_t maxElems
, const bool adoptElems
, MemoryManager* const manager) :
fAdoptedElems(adoptElems)
, fCurCount(0)
, fMaxCount(maxElems)
, fElemList(0)
, fMemoryManager(manager)
{
// Allocate and initialize the array
fElemList = (TElem**) fMemoryManager->allocate(maxElems * sizeof(TElem*));//new TElem*[maxElems];
for (XMLSize_t index = 0; index < maxElems; index++)
fElemList[index] = 0;
}
//implemented so code will link
template <class TElem> BaseRefVectorOf<TElem>::~BaseRefVectorOf()
{
}
// ---------------------------------------------------------------------------
// BaseRefVectorOf: Element management
// ---------------------------------------------------------------------------
template <class TElem> void BaseRefVectorOf<TElem>::addElement(TElem* const toAdd)
{
ensureExtraCapacity(1);
fElemList[fCurCount] = toAdd;
fCurCount++;
}
template <class TElem> void
BaseRefVectorOf<TElem>::setElementAt(TElem* const toSet, const XMLSize_t setAt)
{
if (setAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
if (fAdoptedElems)
delete fElemList[setAt];
fElemList[setAt] = toSet;
}
template <class TElem> void BaseRefVectorOf<TElem>::
insertElementAt(TElem* const toInsert, const XMLSize_t insertAt)
{
if (insertAt == fCurCount)
{
addElement(toInsert);
return;
}
if (insertAt > fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
ensureExtraCapacity(1);
// Make room for the newbie
for (XMLSize_t index = fCurCount; index > insertAt; index--)
fElemList[index] = fElemList[index-1];
// And stick it in and bump the count
fElemList[insertAt] = toInsert;
fCurCount++;
}
template <class TElem> TElem* BaseRefVectorOf<TElem>::
orphanElementAt(const XMLSize_t orphanAt)
{
if (orphanAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
// Get the element we are going to orphan
TElem* retVal = fElemList[orphanAt];
// Optimize if its the last element
if (orphanAt == fCurCount-1)
{
fElemList[orphanAt] = 0;
fCurCount--;
return retVal;
}
// Copy down every element above orphan point
for (XMLSize_t index = orphanAt; index < fCurCount-1; index++)
fElemList[index] = fElemList[index+1];
// Keep unused elements zero for sanity's sake
fElemList[fCurCount-1] = 0;
// And bump down count
fCurCount--;
return retVal;
}
template <class TElem> void BaseRefVectorOf<TElem>::removeAllElements()
{
for (XMLSize_t index = 0; index < fCurCount; index++)
{
if (fAdoptedElems)
delete fElemList[index];
// Keep unused elements zero for sanity's sake
fElemList[index] = 0;
}
fCurCount = 0;
}
template <class TElem> void BaseRefVectorOf<TElem>::
removeElementAt(const XMLSize_t removeAt)
{
if (removeAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
if (fAdoptedElems)
delete fElemList[removeAt];
// Optimize if its the last element
if (removeAt == fCurCount-1)
{
fElemList[removeAt] = 0;
fCurCount--;
return;
}
// Copy down every element above remove point
for (XMLSize_t index = removeAt; index < fCurCount-1; index++)
fElemList[index] = fElemList[index+1];
// Keep unused elements zero for sanity's sake
fElemList[fCurCount-1] = 0;
// And bump down count
fCurCount--;
}
template <class TElem> void BaseRefVectorOf<TElem>::removeLastElement()
{
if (!fCurCount)
return;
fCurCount--;
if (fAdoptedElems)
delete fElemList[fCurCount];
}
template <class TElem>
bool BaseRefVectorOf<TElem>::containsElement(const TElem* const toCheck) {
for (XMLSize_t i = 0; i < fCurCount; i++) {
if (fElemList[i] == toCheck) {
return true;
}
}
return false;
}
//
// cleanup():
// similar to destructor
// called to cleanup the memory, in case destructor cannot be called
//
template <class TElem> void BaseRefVectorOf<TElem>::cleanup()
{
if (fAdoptedElems)
{
for (XMLSize_t index = 0; index < fCurCount; index++)
delete fElemList[index];
}
fMemoryManager->deallocate(fElemList);//delete [] fElemList;
}
//
// reinitialize():
// similar to constructor
// called to re-construct the fElemList from scratch again
//
template <class TElem> void BaseRefVectorOf<TElem>::reinitialize()
{
// reinitialize the array
if (fElemList)
cleanup();
fElemList = (TElem**) fMemoryManager->allocate(fMaxCount * sizeof(TElem*));//new TElem*[fMaxCount];
for (XMLSize_t index = 0; index < fMaxCount; index++)
fElemList[index] = 0;
}
template <class TElem>
MemoryManager* BaseRefVectorOf<TElem>::getMemoryManager() const
{
return fMemoryManager;
}
// ---------------------------------------------------------------------------
// BaseRefVectorOf: Getter methods
// ---------------------------------------------------------------------------
template <class TElem> XMLSize_t BaseRefVectorOf<TElem>::curCapacity() const
{
return fMaxCount;
}
template <class TElem> const TElem* BaseRefVectorOf<TElem>::
elementAt(const XMLSize_t getAt) const
{
if (getAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
return fElemList[getAt];
}
template <class TElem> TElem*
BaseRefVectorOf<TElem>::elementAt(const XMLSize_t getAt)
{
if (getAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
return fElemList[getAt];
}
template <class TElem> XMLSize_t BaseRefVectorOf<TElem>::size() const
{
return fCurCount;
}
// ---------------------------------------------------------------------------
// BaseRefVectorOf: Miscellaneous
// ---------------------------------------------------------------------------
template <class TElem> void BaseRefVectorOf<TElem>::
ensureExtraCapacity(const XMLSize_t length)
{
XMLSize_t newMax = fCurCount + length;
if (newMax <= fMaxCount)
return;
// Choose how much bigger based on the current size.
// This will grow half as much again.
if (newMax < fMaxCount + fMaxCount/2)
newMax = fMaxCount + fMaxCount/2;
// Allocate the new array and copy over the existing stuff
TElem** newList = (TElem**) fMemoryManager->allocate
(
newMax * sizeof(TElem*)
);//new TElem*[newMax];
XMLSize_t index = 0;
for (; index < fCurCount; index++)
newList[index] = fElemList[index];
// Zero out the rest of them
for (; index < newMax; index++)
newList[index] = 0;
// Clean up the old array and update our members
fMemoryManager->deallocate(fElemList);//delete [] fElemList;
fElemList = newList;
fMaxCount = newMax;
}
// ---------------------------------------------------------------------------
// AbstractBaseRefVectorEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem> BaseRefVectorEnumerator<TElem>::
BaseRefVectorEnumerator( BaseRefVectorOf<TElem>* const toEnum
, const bool adopt) :
fAdopted(adopt)
, fCurIndex(0)
, fToEnum(toEnum)
{
}
template <class TElem> BaseRefVectorEnumerator<TElem>::~BaseRefVectorEnumerator()
{
if (fAdopted)
delete fToEnum;
}
template <class TElem> BaseRefVectorEnumerator<TElem>::
BaseRefVectorEnumerator(const BaseRefVectorEnumerator<TElem>& toCopy) :
XMLEnumerator<TElem>(toCopy)
, XMemory(toCopy)
, fAdopted(toCopy.fAdopted)
, fCurIndex(toCopy.fCurIndex)
, fToEnum(toCopy.fToEnum)
{
}
// ---------------------------------------------------------------------------
// RefBaseRefVectorEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TElem> bool BaseRefVectorEnumerator<TElem>::hasMoreElements() const
{
if (fCurIndex >= fToEnum->size())
return false;
return true;
}
template <class TElem> TElem& BaseRefVectorEnumerator<TElem>::nextElement()
{
return *(fToEnum->elementAt(fCurIndex++));
}
template <class TElem> void BaseRefVectorEnumerator<TElem>::Reset()
{
fCurIndex = 0;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,107 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: CountedPointer.c 471747 2006-11-06 14:31:56Z amassari $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/CountedPointer.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// CountedPointerTo: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class T> CountedPointerTo<T>::
CountedPointerTo(const CountedPointerTo<T>& toCopy) :
fPtr(toCopy.fPtr)
{
if (fPtr)
fPtr->addRef();
}
template <class T> CountedPointerTo<T>::CountedPointerTo(T* p) :
fPtr(p)
{
if (fPtr)
fPtr->addRef();
}
template <class T> CountedPointerTo<T>::~CountedPointerTo()
{
if (fPtr)
fPtr->removeRef();
}
// ---------------------------------------------------------------------------
// CountedPointerTo: Operators
// ---------------------------------------------------------------------------
template <class T> CountedPointerTo<T>&
CountedPointerTo<T>::operator=(const CountedPointerTo<T>& other)
{
if (this == &other)
return *this;
if (other.fPtr)
other.fPtr->addRef();
if (fPtr)
fPtr->removeRef();
fPtr = other.fPtr;
return *this;
}
template <class T> CountedPointerTo<T>::operator T*()
{
return fPtr;
}
template <class T> const T* CountedPointerTo<T>::operator->() const
{
return fPtr;
}
template <class T> T* CountedPointerTo<T>::operator->()
{
return fPtr;
}
template <class T> const T& CountedPointerTo<T>::operator*() const
{
if (!fPtr)
ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, 0);
return *fPtr;
}
template <class T> T& CountedPointerTo<T>::operator*()
{
if (!fPtr)
ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, 0);
return *fPtr;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: FlagJanitor.c 471747 2006-11-06 14:31:56Z amassari $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/FlagJanitor.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// Constructors and Destructor
// ---------------------------------------------------------------------------
template <class T> FlagJanitor<T>::FlagJanitor(T* const valPtr, const T newVal)
: fValPtr(valPtr)
{
// Store the pointer, save the org value, and store the new value
if (fValPtr)
{
fOldVal = *fValPtr;
*fValPtr = newVal;
}
}
template <class T> FlagJanitor<T>::~FlagJanitor()
{
// Restore the old value
if (fValPtr)
*fValPtr = fOldVal;
}
// ---------------------------------------------------------------------------
// Value management methods
// ---------------------------------------------------------------------------
template <class T> void FlagJanitor<T>::release()
{
fValPtr = 0;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,591 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: Hash2KeysSetOf.c 883368 2009-11-23 15:28:19Z amassari $
*/
// ---------------------------------------------------------------------------
// Include
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/Hash2KeysSetOf.hpp>
#endif
#include <xercesc/util/Janitor.hpp>
#include <xercesc/util/NullPointerException.hpp>
#include <assert.h>
#include <new>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// Hash2KeysSetOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class THasher>
Hash2KeysSetOf<THasher>::Hash2KeysSetOf(
const XMLSize_t modulus,
MemoryManager* const manager)
: fMemoryManager(manager)
, fBucketList(0)
, fHashModulus(modulus)
, fCount(0)
, fAvailable(0)
{
initialize(modulus);
}
template <class THasher>
Hash2KeysSetOf<THasher>::Hash2KeysSetOf(
const XMLSize_t modulus,
const THasher& hasher,
MemoryManager* const manager)
: fMemoryManager(manager)
, fBucketList(0)
, fHashModulus(modulus)
, fCount(0)
, fAvailable(0)
, fHasher (hasher)
{
initialize(modulus);
}
template <class THasher>
void Hash2KeysSetOf<THasher>::initialize(const XMLSize_t modulus)
{
if (modulus == 0)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::HshTbl_ZeroModulus, fMemoryManager);
// Allocate the bucket list and zero them
fBucketList = (Hash2KeysSetBucketElem**) fMemoryManager->allocate
(
fHashModulus * sizeof(Hash2KeysSetBucketElem*)
); //new Hash2KeysSetBucketElem*[fHashModulus];
memset(fBucketList, 0, sizeof(fBucketList[0]) * fHashModulus);
}
template <class THasher>
Hash2KeysSetOf<THasher>::~Hash2KeysSetOf()
{
Hash2KeysSetBucketElem* nextElem;
if(!isEmpty())
{
// Clean up the buckets first
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
{
// Get the bucket list head for this entry
Hash2KeysSetBucketElem* curElem = fBucketList[buckInd];
while (curElem)
{
// Save the next element before we hose this one
nextElem = curElem->fNext;
fMemoryManager->deallocate(curElem);
curElem = nextElem;
}
// Clean out this entry
fBucketList[buckInd] = 0;
}
}
// Then delete the list of available blocks
Hash2KeysSetBucketElem* curElem = fAvailable;
while (curElem)
{
// Save the next element before we hose this one
nextElem = curElem->fNext;
fMemoryManager->deallocate(curElem);
curElem = nextElem;
}
fAvailable = 0;
// Then delete the bucket list & hasher
fMemoryManager->deallocate(fBucketList); //delete [] fBucketList;
fBucketList = 0;
}
// ---------------------------------------------------------------------------
// Hash2KeysSetOf: Element management
// ---------------------------------------------------------------------------
template <class THasher>
bool Hash2KeysSetOf<THasher>::isEmpty() const
{
return (fCount==0);
}
template <class THasher>
bool Hash2KeysSetOf<THasher>::containsKey(const void* const key1, const int key2) const
{
XMLSize_t hashVal;
const Hash2KeysSetBucketElem* findIt = findBucketElem(key1, key2, hashVal);
return (findIt != 0);
}
template <class THasher>
void Hash2KeysSetOf<THasher>::removeKey(const void* const key1, const int key2)
{
// Hash the key
XMLSize_t hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
//
// Search the given bucket for this key. Keep up with the previous
// element so we can patch around it.
//
Hash2KeysSetBucketElem* curElem = fBucketList[hashVal];
Hash2KeysSetBucketElem* lastElem = 0;
while (curElem)
{
if((key2==curElem->fKey2) && (fHasher.equals(key1, curElem->fKey1)))
{
if (!lastElem)
{
// It was the first in the bucket
fBucketList[hashVal] = curElem->fNext;
}
else
{
// Patch around the current element
lastElem->fNext = curElem->fNext;
}
// Move the current element to the list of available blocks
curElem->fNext=fAvailable;
fAvailable=curElem;
fCount--;
return;
}
// Move both pointers upwards
lastElem = curElem;
curElem = curElem->fNext;
}
// We never found that key
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists, fMemoryManager);
}
template <class THasher>
void Hash2KeysSetOf<THasher>::
removeKey(const void* const key1)
{
// Hash the key
XMLSize_t hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
//
// Search the given bucket for this key. Keep up with the previous
// element so we can patch around it.
//
Hash2KeysSetBucketElem* curElem = fBucketList[hashVal];
Hash2KeysSetBucketElem* lastElem = 0;
while (curElem)
{
if(fHasher.equals(key1, curElem->fKey1))
{
if (!lastElem)
{
// It was the first in the bucket
fBucketList[hashVal] = curElem->fNext;
}
else
{
// Patch around the current element
lastElem->fNext = curElem->fNext;
}
Hash2KeysSetBucketElem* toBeDeleted=curElem;
curElem = curElem->fNext;
// Move the current element to the list of available blocks
toBeDeleted->fNext=fAvailable;
fAvailable=toBeDeleted;
fCount--;
}
else
{
// Move both pointers upwards
lastElem = curElem;
curElem = curElem->fNext;
}
}
}
template <class THasher>
void Hash2KeysSetOf<THasher>::removeAll()
{
if(isEmpty())
return;
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
{
if(fBucketList[buckInd]!=0)
{
// Advance to the end of the chain, and connect it to the list of
// available blocks
Hash2KeysSetBucketElem* curElem = fBucketList[buckInd];
while (curElem->fNext)
curElem = curElem->fNext;
curElem->fNext=fAvailable;
fAvailable=fBucketList[buckInd];
fBucketList[buckInd] = 0;
}
}
fCount=0;
}
// ---------------------------------------------------------------------------
// Hash2KeysSetOf: Getters
// ---------------------------------------------------------------------------
template <class THasher>
MemoryManager* Hash2KeysSetOf<THasher>::getMemoryManager() const
{
return fMemoryManager;
}
template <class THasher>
XMLSize_t Hash2KeysSetOf<THasher>::getHashModulus() const
{
return fHashModulus;
}
// ---------------------------------------------------------------------------
// Hash2KeysSetOf: Putters
// ---------------------------------------------------------------------------
template <class THasher>
void Hash2KeysSetOf<THasher>::put(const void* key1, int key2)
{
// Apply 4 load factor to find threshold.
XMLSize_t threshold = fHashModulus * 4;
// If we've grown too big, expand the table and rehash.
if (fCount >= threshold)
rehash();
// First see if the key exists already
XMLSize_t hashVal;
Hash2KeysSetBucketElem* newBucket = findBucketElem(key1, key2, hashVal);
//
// If so,then update its value. If not, then we need to add it to
// the right bucket
//
if (newBucket)
{
newBucket->fKey1 = key1;
newBucket->fKey2 = key2;
}
else
{
if(fAvailable==0)
newBucket = (Hash2KeysSetBucketElem*)fMemoryManager->allocate(sizeof(Hash2KeysSetBucketElem));
else
{
newBucket = fAvailable;
fAvailable = fAvailable->fNext;
}
newBucket->fKey1 = key1;
newBucket->fKey2 = key2;
newBucket->fNext = fBucketList[hashVal];
fBucketList[hashVal] = newBucket;
fCount++;
}
}
template <class THasher>
bool Hash2KeysSetOf<THasher>::putIfNotPresent(const void* key1, int key2)
{
// First see if the key exists already
XMLSize_t hashVal;
Hash2KeysSetBucketElem* newBucket = findBucketElem(key1, key2, hashVal);
//
// If so,then update its value. If not, then we need to add it to
// the right bucket
//
if (newBucket)
return false;
// Apply 4 load factor to find threshold.
XMLSize_t threshold = fHashModulus * 4;
// If we've grown too big, expand the table and rehash.
if (fCount >= threshold)
rehash();
if(fAvailable==0)
newBucket = (Hash2KeysSetBucketElem*)fMemoryManager->allocate(sizeof(Hash2KeysSetBucketElem));
else
{
newBucket = fAvailable;
fAvailable = fAvailable->fNext;
}
newBucket->fKey1 = key1;
newBucket->fKey2 = key2;
newBucket->fNext = fBucketList[hashVal];
fBucketList[hashVal] = newBucket;
fCount++;
return true;
}
// ---------------------------------------------------------------------------
// Hash2KeysSetOf: Private methods
// ---------------------------------------------------------------------------
template <class THasher>
inline Hash2KeysSetBucketElem* Hash2KeysSetOf<THasher>::
findBucketElem(const void* const key1, const int key2, XMLSize_t& hashVal)
{
// Hash the key
hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
// Search that bucket for the key
Hash2KeysSetBucketElem* curElem = fBucketList[hashVal];
while (curElem)
{
if((key2==curElem->fKey2) && (fHasher.equals(key1, curElem->fKey1)))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
template <class THasher>
inline const Hash2KeysSetBucketElem* Hash2KeysSetOf<THasher>::
findBucketElem(const void* const key1, const int key2, XMLSize_t& hashVal) const
{
// Hash the key
hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
// Search that bucket for the key
const Hash2KeysSetBucketElem* curElem = fBucketList[hashVal];
while (curElem)
{
if((key2==curElem->fKey2) && (fHasher.equals(key1, curElem->fKey1)))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
template <class THasher>
void Hash2KeysSetOf<THasher>::
rehash()
{
const XMLSize_t newMod = (fHashModulus * 8)+1;
Hash2KeysSetBucketElem** newBucketList =
(Hash2KeysSetBucketElem**) fMemoryManager->allocate
(
newMod * sizeof(Hash2KeysSetBucketElem*)
);//new Hash2KeysSetBucketElem*[fHashModulus];
// Make sure the new bucket list is destroyed if an
// exception is thrown.
ArrayJanitor<Hash2KeysSetBucketElem*> guard(newBucketList, fMemoryManager);
memset(newBucketList, 0, newMod * sizeof(newBucketList[0]));
// Rehash all existing entries.
for (XMLSize_t index = 0; index < fHashModulus; index++)
{
// Get the bucket list head for this entry
Hash2KeysSetBucketElem* curElem = fBucketList[index];
while (curElem)
{
// Save the next element before we detach this one
Hash2KeysSetBucketElem* nextElem = curElem->fNext;
const XMLSize_t hashVal = fHasher.getHashVal(curElem->fKey1, newMod);
assert(hashVal < newMod);
Hash2KeysSetBucketElem* newHeadElem = newBucketList[hashVal];
// Insert at the start of this bucket's list.
curElem->fNext = newHeadElem;
newBucketList[hashVal] = curElem;
curElem = nextElem;
}
}
Hash2KeysSetBucketElem** const oldBucketList = fBucketList;
// Everything is OK at this point, so update the
// member variables.
fBucketList = guard.release();
fHashModulus = newMod;
// Delete the old bucket list.
fMemoryManager->deallocate(oldBucketList);//delete[] oldBucketList;
}
// ---------------------------------------------------------------------------
// Hash2KeysSetOfEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class THasher>
Hash2KeysSetOfEnumerator<THasher>::
Hash2KeysSetOfEnumerator(Hash2KeysSetOf<THasher>* const toEnum
, const bool adopt
, MemoryManager* const manager)
: fAdopted(adopt), fCurElem(0), fCurHash((XMLSize_t)-1), fToEnum(toEnum)
, fMemoryManager(manager)
, fLockPrimaryKey(0)
{
if (!toEnum)
ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, fMemoryManager);
//
// Find the next available bucket element in the hash table. If it
// comes back zero, that just means the table is empty.
//
// Note that the -1 in the current hash tells it to start
// from the beginning.
//
findNext();
}
template <class THasher>
Hash2KeysSetOfEnumerator<THasher>::~Hash2KeysSetOfEnumerator()
{
if (fAdopted)
delete fToEnum;
}
// ---------------------------------------------------------------------------
// Hash2KeysSetOfEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class THasher>
bool Hash2KeysSetOfEnumerator<THasher>::hasMoreElements() const
{
//
// If our current has is at the max and there are no more elements
// in the current bucket, then no more elements.
//
if (!fCurElem && (fCurHash == fToEnum->fHashModulus))
return false;
return true;
}
template <class THasher>
void Hash2KeysSetOfEnumerator<THasher>::nextElementKey(const void*& retKey1, int& retKey2)
{
// Make sure we have an element to return
if (!hasMoreElements())
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
//
// Save the current element, then move up to the next one for the
// next time around.
//
Hash2KeysSetBucketElem* saveElem = fCurElem;
findNext();
retKey1 = saveElem->fKey1;
retKey2 = saveElem->fKey2;
return;
}
template <class THasher>
void Hash2KeysSetOfEnumerator<THasher>::Reset()
{
if(fLockPrimaryKey)
fCurHash=fToEnum->fHasher.getHashVal(fLockPrimaryKey, fToEnum->fHashModulus);
else
fCurHash = (XMLSize_t)-1;
fCurElem = 0;
findNext();
}
template <class THasher>
void Hash2KeysSetOfEnumerator<THasher>::setPrimaryKey(const void* key)
{
fLockPrimaryKey=key;
Reset();
}
// ---------------------------------------------------------------------------
// Hash2KeysSetOfEnumerator: Private helper methods
// ---------------------------------------------------------------------------
template <class THasher>
void Hash2KeysSetOfEnumerator<THasher>::findNext()
{
// Code to execute if we have to return only values with the primary key
if(fLockPrimaryKey)
{
if(!fCurElem)
fCurElem = fToEnum->fBucketList[fCurHash];
else
fCurElem = fCurElem->fNext;
while (fCurElem && (!fToEnum->fHasher.equals(fLockPrimaryKey, fCurElem->fKey1)))
fCurElem = fCurElem->fNext;
// if we didn't found it, make so hasMoreElements() returns false
if(!fCurElem)
fCurHash = fToEnum->fHashModulus;
return;
}
//
// If there is a current element, move to its next element. If this
// hits the end of the bucket, the next block will handle the rest.
//
if (fCurElem)
fCurElem = fCurElem->fNext;
//
// If the current element is null, then we have to move up to the
// next hash value. If that is the hash modulus, then we cannot
// go further.
//
if (!fCurElem)
{
fCurHash++;
if (fCurHash == fToEnum->fHashModulus)
return;
// Else find the next non-empty bucket
while (fToEnum->fBucketList[fCurHash]==0)
{
// Bump to the next hash value. If we max out return
fCurHash++;
if (fCurHash == fToEnum->fHashModulus)
return;
}
fCurElem = fToEnum->fBucketList[fCurHash];
}
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,248 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: Janitor.c 669844 2008-06-20 10:11:44Z borisk $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/Janitor.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// Janitor: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class T> Janitor<T>::Janitor(T* const toDelete) :
fData(toDelete)
{
}
template <class T> Janitor<T>::~Janitor()
{
reset();
}
// ---------------------------------------------------------------------------
// Janitor: Public, non-virtual methods
// ---------------------------------------------------------------------------
template <class T> void
Janitor<T>::orphan()
{
release();
}
template <class T> T&
Janitor<T>::operator*() const
{
return *fData;
}
template <class T> T*
Janitor<T>::operator->() const
{
return fData;
}
template <class T> T*
Janitor<T>::get() const
{
return fData;
}
template <class T> T*
Janitor<T>::release()
{
T* p = fData;
fData = 0;
return p;
}
template <class T> void Janitor<T>::reset(T* p)
{
if (fData)
delete fData;
fData = p;
}
template <class T> bool Janitor<T>::isDataNull()
{
return (fData == 0);
}
// -----------------------------------------------------------------------
// ArrayJanitor: Constructors and Destructor
// -----------------------------------------------------------------------
template <class T> ArrayJanitor<T>::ArrayJanitor(T* const toDelete) :
fData(toDelete)
, fMemoryManager(0)
{
}
template <class T>
ArrayJanitor<T>::ArrayJanitor(T* const toDelete,
MemoryManager* const manager) :
fData(toDelete)
, fMemoryManager(manager)
{
}
template <class T> ArrayJanitor<T>::~ArrayJanitor()
{
reset();
}
// -----------------------------------------------------------------------
// ArrayJanitor: Public, non-virtual methods
// -----------------------------------------------------------------------
template <class T> void
ArrayJanitor<T>::orphan()
{
release();
}
// Look, Ma! No hands! Don't call this with null data!
template <class T> T&
ArrayJanitor<T>::operator[](int index) const
{
// TODO: Add appropriate exception
return fData[index];
}
template <class T> T*
ArrayJanitor<T>::get() const
{
return fData;
}
template <class T> T*
ArrayJanitor<T>::release()
{
T* p = fData;
fData = 0;
return p;
}
template <class T> void
ArrayJanitor<T>::reset(T* p)
{
if (fData) {
if (fMemoryManager)
fMemoryManager->deallocate((void*)fData);
else
delete [] fData;
}
fData = p;
fMemoryManager = 0;
}
template <class T> void
ArrayJanitor<T>::reset(T* p, MemoryManager* const manager)
{
if (fData) {
if (fMemoryManager)
fMemoryManager->deallocate((void*)fData);
else
delete [] fData;
}
fData = p;
fMemoryManager = manager;
}
//
// JanitorMemFunCall
//
template <class T>
JanitorMemFunCall<T>::JanitorMemFunCall(
T* object,
MFPT toCall) :
fObject(object),
fToCall(toCall)
{
}
template <class T>
JanitorMemFunCall<T>::~JanitorMemFunCall()
{
reset ();
}
template <class T>
T& JanitorMemFunCall<T>::operator*() const
{
return *fObject;
}
template <class T>
T* JanitorMemFunCall<T>::operator->() const
{
return fObject;
}
template <class T>
T* JanitorMemFunCall<T>::get() const
{
return fObject;
}
template <class T>
T* JanitorMemFunCall<T>::release()
{
T* p = fObject;
fObject = 0;
return p;
}
template <class T>
void JanitorMemFunCall<T>::reset(T* p)
{
if (fObject != 0 && fToCall != 0)
(fObject->*fToCall)();
fObject = p;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* $Id: KeyRefPair.c 471747 2006-11-06 14:31:56Z amassari $
*/
// ---------------------------------------------------------------------------
// Include
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/KeyRefPair.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// KeyRefPair: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TKey, class TValue> KeyRefPair<TKey,TValue>::KeyRefPair()
{
}
template <class TKey, class TValue> KeyRefPair<TKey,TValue>::
KeyRefPair(TKey* key, TValue* value) :
fKey(key)
, fValue(value)
{
}
template <class TKey, class TValue> KeyRefPair<TKey,TValue>::
KeyRefPair(const KeyRefPair<TKey,TValue>* toCopy) :
fKey(toCopy->fKey)
, fValue(toCopy->fValue)
{
}
template <class TKey, class TValue> KeyRefPair<TKey,TValue>::
KeyRefPair(const KeyRefPair<TKey,TValue>& toCopy) :
fKey(toCopy.fKey)
, fValue(toCopy.fValue)
{
}
template <class TKey, class TValue> KeyRefPair<TKey,TValue>::~KeyRefPair()
{
}
// ---------------------------------------------------------------------------
// KeyRefPair: Getters
// ---------------------------------------------------------------------------
template <class TKey, class TValue> const TKey*
KeyRefPair<TKey,TValue>::getKey() const
{
return fKey;
}
template <class TKey, class TValue> TKey* KeyRefPair<TKey,TValue>::getKey()
{
return fKey;
}
template <class TKey, class TValue> const TValue*
KeyRefPair<TKey,TValue>::getValue() const
{
return fValue;
}
template <class TKey, class TValue> TValue* KeyRefPair<TKey,TValue>::getValue()
{
return fValue;
}
// ---------------------------------------------------------------------------
// KeyRefPair: Setters
// ---------------------------------------------------------------------------
template <class TKey, class TValue> TKey*
KeyRefPair<TKey,TValue>::setKey(TKey* newKey)
{
fKey = newKey;
return fKey;
}
template <class TKey, class TValue> TValue*
KeyRefPair<TKey,TValue>::setValue(TValue* newValue)
{
fValue = newValue;
return fValue;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,104 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: KeyValuePair.c 471747 2006-11-06 14:31:56Z amassari $
*/
// ---------------------------------------------------------------------------
// Include
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/KeyValuePair.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// KeyValuePair: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TKey, class TValue> KeyValuePair<TKey,TValue>::KeyValuePair()
{
}
template <class TKey, class TValue> KeyValuePair<TKey,TValue>::
KeyValuePair(const TKey& key, const TValue& value) :
fKey(key)
, fValue(value)
{
}
template <class TKey, class TValue> KeyValuePair<TKey,TValue>::
KeyValuePair(const KeyValuePair<TKey,TValue>& toCopy) :
fKey(toCopy.fKey)
, fValue(toCopy.fValue)
{
}
template <class TKey, class TValue> KeyValuePair<TKey,TValue>::~KeyValuePair()
{
}
// ---------------------------------------------------------------------------
// KeyValuePair: Getters
// ---------------------------------------------------------------------------
template <class TKey, class TValue> const TKey&
KeyValuePair<TKey,TValue>::getKey() const
{
return fKey;
}
template <class TKey, class TValue> TKey& KeyValuePair<TKey,TValue>::getKey()
{
return fKey;
}
template <class TKey, class TValue> const TValue&
KeyValuePair<TKey,TValue>::getValue() const
{
return fValue;
}
template <class TKey, class TValue> TValue& KeyValuePair<TKey,TValue>::getValue()
{
return fValue;
}
// ---------------------------------------------------------------------------
// KeyValuePair: Setters
// ---------------------------------------------------------------------------
template <class TKey, class TValue> TKey&
KeyValuePair<TKey,TValue>::setKey(const TKey& newKey)
{
fKey = newKey;
return fKey;
}
template <class TKey, class TValue> TValue&
KeyValuePair<TKey,TValue>::setValue(const TValue& newValue)
{
fValue = newValue;
return fValue;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,275 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: LogicalPath.c 932887 2010-04-11 13:04:59Z borisk $
*/
#if !defined(XERCESC_INCLUDE_GUARD_WEAVEPATH_CPP)
#define XERCESC_INCLUDE_GUARD_WEAVEPATH_CPP
/***
*
* Previously, each <OS>PlatformUtils.cpp has its onw copy of the
* method weavePaths(), and almost of them implemented the same logic,
* with few platform specific difference, and unfortunately that
* implementation was wrong.
*
* The only platform specific issue is slash character.
* On all platforms other than Windows, chForwardSlash and chBackSlash
* are considered slash, while on Windows, two additional characters,
* chYenSign and chWonSign are slash as well.
*
* The idea is to maintain a SINGLE copy of this method rather than
* each <OS>PlatformUtils.cpp has its own copy, we introduce a new
* method, XMLPlatformUtils::isAnySlash(), to replace the direct checking
* code ( if ( c == chForwardSlash || c == chBackSlash).
*
* With this approach, we might have a performance hit since isAnySlash()
* is so frequently used in this implementation, so we intend to make it
* inline. Then we face a complier issue.
*
* There are two compilation units involved, one is PlatformUtils.cpp and
* the other <OS>PlatformUtils.cpp. When PlatformUtils.cp get compiled,
* the weavePath(), remove**Slash() have dependency upon isAnySlash() which
* is in <OS>PlatformUtils.cpp (and what is worse, it is inlined), so we have
* undefined/unresolved symbol: isAnySlash() on AIX/xlc_r, Solaris/cc and
* Linux/gcc, while MSVC and HP/aCC are fine with this.
*
* That means we can not place these new methods in PlatformUtils.cpp with
* inlined XMLPlatformUtils::isAnySlash() in <OS>PlatformUtils.cpp.
*
* The solution to this is <os>PlatformUtils.cpp will include this file so that
* we have only one copy of these methods while get compiled in <os>PlatformUtils
* inlined isAnySlash().
*
***/
XMLCh* XMLPlatformUtils::weavePaths(const XMLCh* const basePath
, const XMLCh* const relativePath
, MemoryManager* const manager)
{
// Create a buffer as large as both parts and empty it
XMLCh* tmpBuf = (XMLCh*) manager->allocate
(
(XMLString::stringLen(basePath)
+ XMLString::stringLen(relativePath) + 2) * sizeof(XMLCh)
);//new XMLCh[XMLString::stringLen(basePath) + XMLString::stringLen(relativePath) + 2];
*tmpBuf = 0;
//
// If we have no base path, then just take the relative path as is.
//
if ((!basePath) || (!*basePath))
{
XMLString::copyString(tmpBuf, relativePath);
return tmpBuf;
}
//
// Remove anything after the last slash
//
const XMLCh* basePtr = basePath + (XMLString::stringLen(basePath) - 1);
while ((basePtr >= basePath) && ((isAnySlash(*basePtr) == false)))
{
basePtr--;
}
// There is no relevant base path, so just take the relative part
if (basePtr < basePath)
{
XMLString::copyString(tmpBuf, relativePath);
return tmpBuf;
}
//
// 1. concatenate the base and relative
// 2. remove all occurrences of "/./"
// 3. remove all occurrences of segment/../ where segment is not ../
//
XMLString::subString(tmpBuf, basePath, 0, (basePtr - basePath + 1), manager);
tmpBuf[basePtr - basePath + 1] = 0;
XMLString::catString(tmpBuf, relativePath);
removeDotSlash(tmpBuf, manager);
removeDotDotSlash(tmpBuf, manager);
return tmpBuf;
}
//
// Remove all occurrences of './' when it is part of '/./'
//
// Since it could be '.\' or other combination on windows ( eg, '.'+chYanSign)
// we can't make use of patterMatch().
//
//
void XMLPlatformUtils::removeDotSlash(XMLCh* const path
, MemoryManager* const manager)
{
if ((!path) || (!*path))
return;
XMLCh* srcPtr = XMLString::replicate(path, manager);
int srcLen = XMLString::stringLen(srcPtr);
ArrayJanitor<XMLCh> janName(srcPtr, manager);
XMLCh* tarPtr = path;
while (*srcPtr)
{
if ( 3 <= srcLen )
{
if ( (isAnySlash(*srcPtr)) &&
(chPeriod == *(srcPtr+1)) &&
(isAnySlash(*(srcPtr+2))) )
{
// "\.\x" seen
// skip the first two, and start from the 3rd,
// since "\x" could be another "\."
srcPtr+=2;
srcLen-=2;
}
else
{
*tarPtr++ = *srcPtr++; // eat the current char
srcLen--;
}
}
else if ( 1 == srcLen )
{
*tarPtr++ = *srcPtr++;
}
else if ( 2 == srcLen)
{
*tarPtr++ = *srcPtr++;
*tarPtr++ = *srcPtr++;
}
}
*tarPtr = 0;
return;
}
//
// Remove all occurrences of '/segment/../' when segment is not '..'
//
// Cases with extra /../ is left to the underlying file system.
//
void XMLPlatformUtils::removeDotDotSlash(XMLCh* const path
, MemoryManager* const manager)
{
int pathLen = XMLString::stringLen(path);
XMLCh* tmp1 = (XMLCh*) manager->allocate
(
(pathLen+1) * sizeof(XMLCh)
);//new XMLCh [pathLen+1];
ArrayJanitor<XMLCh> tmp1Name(tmp1, manager);
XMLCh* tmp2 = (XMLCh*) manager->allocate
(
(pathLen+1) * sizeof(XMLCh)
);//new XMLCh [pathLen+1];
ArrayJanitor<XMLCh> tmp2Name(tmp2, manager);
// remove all "<segment>/../" where "<segment>" is a complete
// path segment not equal to ".."
int index = -1;
int segIndex = -1;
int offset = 1;
while ((index = searchSlashDotDotSlash(&(path[offset]))) != -1)
{
// Undo offset
index += offset;
// Find start of <segment> within substring ending at found point.
XMLString::subString(tmp1, path, 0, index-1, manager);
segIndex = index - 1;
while ((segIndex >= 0) && (!isAnySlash(tmp1[segIndex])))
{
segIndex--;
}
// Ensure <segment> exists and != ".."
if (segIndex >= 0 &&
(path[segIndex+1] != chPeriod ||
path[segIndex+2] != chPeriod ||
segIndex + 3 != index))
{
XMLString::subString(tmp1, path, 0, segIndex, manager);
XMLString::subString(tmp2, path, index+3, XMLString::stringLen(path), manager);
path[0] = 0;
XMLString::catString(path, tmp1);
XMLString::catString(path, tmp2);
offset = (segIndex == 0 ? 1 : segIndex);
}
else
{
offset += 4;
}
}// while
}
int XMLPlatformUtils::searchSlashDotDotSlash(XMLCh* const srcPath)
{
if ((!srcPath) || (!*srcPath))
return -1;
XMLCh* srcPtr = srcPath;
int srcLen = XMLString::stringLen(srcPath);
int retVal = -1;
while (*srcPtr)
{
if ( 4 <= srcLen )
{
if ( (isAnySlash(*srcPtr)) &&
(chPeriod == *(srcPtr+1)) &&
(chPeriod == *(srcPtr+2)) &&
(isAnySlash(*(srcPtr+3))) )
{
retVal = (srcPtr - srcPath);
break;
}
else
{
srcPtr++;
srcLen--;
}
}
else
{
break;
}
} // while
return retVal;
}
#endif

View File

@@ -0,0 +1,284 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: NameIdPool.c 883368 2009-11-23 15:28:19Z amassari $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/NameIdPool.hpp>
#endif
#include <xercesc/util/IllegalArgumentException.hpp>
#include <xercesc/util/NoSuchElementException.hpp>
#include <xercesc/util/RuntimeException.hpp>
#include <new>
#include <assert.h>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// NameIdPool: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
NameIdPool<TElem>::NameIdPool( const XMLSize_t hashModulus
, const XMLSize_t initSize
, MemoryManager* const manager) :
fMemoryManager(manager)
, fIdPtrs(0)
, fIdPtrsCount(initSize)
, fIdCounter(0)
, fBucketList(hashModulus, manager)
{
if (!hashModulus)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_ZeroModulus, 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
// zeroth element is never used (and represents an invalid pool id.)
//
if (!fIdPtrsCount)
fIdPtrsCount = 256;
fIdPtrs = (TElem**) fMemoryManager->allocate
(
fIdPtrsCount * sizeof(TElem*)
);
fIdPtrs[0] = 0;
}
template <class TElem> NameIdPool<TElem>::~NameIdPool()
{
//
// Delete the id pointers list. The stuff it points to will be cleaned
// up when we clean the bucket lists.
//
fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
}
// ---------------------------------------------------------------------------
// NameIdPool: Element management
// ---------------------------------------------------------------------------
template <class TElem>
inline bool NameIdPool<TElem>::
containsKey(const XMLCh* const key) const
{
if (fIdCounter == 0) return false;
return fBucketList.containsKey(key);
}
template <class TElem> void NameIdPool<TElem>::removeAll()
{
if (fIdCounter == 0) return;
fBucketList.removeAll();
// Reset the id counter
fIdCounter = 0;
}
// ---------------------------------------------------------------------------
// NameIdPool: Getters
// ---------------------------------------------------------------------------
template <class TElem>
inline TElem* NameIdPool<TElem>::
getByKey(const XMLCh* const key)
{
if (fIdCounter == 0) return 0;
return fBucketList.get(key);
}
template <class TElem>
inline const TElem* NameIdPool<TElem>::
getByKey(const XMLCh* const key) const
{
if (fIdCounter == 0) return 0;
return fBucketList.get(key);
}
template <class TElem>
inline TElem* NameIdPool<TElem>::
getById(const XMLSize_t elemId)
{
// If its either zero or beyond our current id, its an error
if (!elemId || (elemId > fIdCounter))
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_InvalidId, fMemoryManager);
return fIdPtrs[elemId];
}
template <class TElem>
inline const TElem* NameIdPool<TElem>::
getById(const XMLSize_t elemId) const
{
// If its either zero or beyond our current id, its an error
if (!elemId || (elemId > fIdCounter))
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_InvalidId, fMemoryManager);
return fIdPtrs[elemId];
}
template <class TElem>
inline MemoryManager* NameIdPool<TElem>::getMemoryManager() const
{
return fMemoryManager;
}
// ---------------------------------------------------------------------------
// NameIdPool: Setters
// ---------------------------------------------------------------------------
template <class TElem>
XMLSize_t NameIdPool<TElem>::put(TElem* const elemToAdopt)
{
// First see if the key exists already. If so, its an error
if(containsKey(elemToAdopt->getKey()))
{
ThrowXMLwithMemMgr1
(
IllegalArgumentException
, XMLExcepts::Pool_ElemAlreadyExists
, elemToAdopt->getKey()
, fMemoryManager
);
}
fBucketList.put((void*)elemToAdopt->getKey(), elemToAdopt);
//
// Give this new one the next available id and add to the pointer list.
// Expand the list if that is now required.
//
if (fIdCounter + 1 == fIdPtrsCount)
{
// Create a new count 1.5 times larger and allocate a new array
XMLSize_t newCount = (XMLSize_t)(fIdPtrsCount * 1.5);
TElem** newArray = (TElem**) fMemoryManager->allocate
(
newCount * sizeof(TElem*)
); //new TElem*[newCount];
// Copy over the old contents to the new array
memcpy(newArray, fIdPtrs, fIdPtrsCount * sizeof(TElem*));
// Ok, toss the old array and store the new data
fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
fIdPtrs = newArray;
fIdPtrsCount = newCount;
}
const XMLSize_t retId = ++fIdCounter;
fIdPtrs[retId] = elemToAdopt;
// Set the id on the passed element
elemToAdopt->setId(retId);
// Return the id that we gave to this element
return retId;
}
// ---------------------------------------------------------------------------
// NameIdPoolEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem> NameIdPoolEnumerator<TElem>::
NameIdPoolEnumerator(NameIdPool<TElem>* const toEnum
, MemoryManager* const manager) :
XMLEnumerator<TElem>()
, fCurIndex(0)
, fToEnum(toEnum)
, fMemoryManager(manager)
{
Reset();
}
template <class TElem> NameIdPoolEnumerator<TElem>::
NameIdPoolEnumerator(const NameIdPoolEnumerator<TElem>& toCopy) :
XMLEnumerator<TElem>(toCopy)
, XMemory(toCopy)
, fCurIndex(toCopy.fCurIndex)
, fToEnum(toCopy.fToEnum)
, fMemoryManager(toCopy.fMemoryManager)
{
}
template <class TElem> NameIdPoolEnumerator<TElem>::~NameIdPoolEnumerator()
{
// We don't own the pool being enumerated, so no cleanup required
}
// ---------------------------------------------------------------------------
// NameIdPoolEnumerator: Public operators
// ---------------------------------------------------------------------------
template <class TElem> NameIdPoolEnumerator<TElem>& NameIdPoolEnumerator<TElem>::
operator=(const NameIdPoolEnumerator<TElem>& toAssign)
{
if (this == &toAssign)
return *this;
fMemoryManager = toAssign.fMemoryManager;
fCurIndex = toAssign.fCurIndex;
fToEnum = toAssign.fToEnum;
return *this;
}
// ---------------------------------------------------------------------------
// NameIdPoolEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TElem> bool NameIdPoolEnumerator<TElem>::
hasMoreElements() const
{
// If our index is zero or past the end, then we are done
if (!fCurIndex || (fCurIndex > fToEnum->fIdCounter))
return false;
return true;
}
template <class TElem> TElem& NameIdPoolEnumerator<TElem>::nextElement()
{
// If our index is zero or past the end, then we are done
if (!fCurIndex || (fCurIndex > fToEnum->fIdCounter))
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
// Return the current element and bump the index
return *fToEnum->fIdPtrs[fCurIndex++];
}
template <class TElem> void NameIdPoolEnumerator<TElem>::Reset()
{
//
// Find the next available bucket element in the pool. We use the id
// array since its very easy to enumerator through by just maintaining
// an index. If the id counter is zero, then its empty and we leave the
// current index to zero.
//
fCurIndex = fToEnum->fIdCounter ? 1:0;
}
template <class TElem> XMLSize_t NameIdPoolEnumerator<TElem>::size() const
{
return fToEnum->fIdCounter;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,269 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: RefArrayOf.c 932887 2010-04-11 13:04:59Z borisk $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/RefArrayOf.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// RefArrayOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
RefArrayOf<TElem>::RefArrayOf(const XMLSize_t size,
MemoryManager* const manager) :
fSize(size)
, fArray(0)
, fMemoryManager(manager)
{
fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
for (XMLSize_t index = 0; index < fSize; index++)
fArray[index] = 0;
}
template <class TElem>
RefArrayOf<TElem>::RefArrayOf(TElem* values[],
const XMLSize_t size,
MemoryManager* const manager) :
fSize(size)
, fArray(0)
, fMemoryManager(manager)
{
fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
for (XMLSize_t index = 0; index < fSize; index++)
fArray[index] = values[index];
}
template <class TElem> RefArrayOf<TElem>::
RefArrayOf(const RefArrayOf<TElem>& source) :
fSize(source.fSize)
, fArray(0)
, fMemoryManager(source.fMemoryManager)
{
fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
for (XMLSize_t index = 0; index < fSize; index++)
fArray[index] = source.fArray[index];
}
template <class TElem> RefArrayOf<TElem>::~RefArrayOf()
{
fMemoryManager->deallocate(fArray);//delete [] fArray;
}
// ---------------------------------------------------------------------------
// RefArrayOf: Public operators
// ---------------------------------------------------------------------------
template <class TElem> TElem*& RefArrayOf<TElem>::
operator[](const XMLSize_t index)
{
if (index >= fSize)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
return fArray[index];
}
template <class TElem> const TElem* RefArrayOf<TElem>::
operator[](const XMLSize_t index) const
{
if (index >= fSize)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
return fArray[index];
}
template <class TElem> RefArrayOf<TElem>& RefArrayOf<TElem>::
operator=(const RefArrayOf<TElem>& toAssign)
{
if (this == &toAssign)
return *this;
// Reallocate if not the same size
if (toAssign.fSize != fSize)
{
fMemoryManager->deallocate(fArray);//delete [] fArray;
fSize = toAssign.fSize;
fArray = (TElem**) fMemoryManager->allocate(fSize * sizeof(TElem*));//new TElem*[fSize];
}
// Copy over the source elements
for (XMLSize_t index = 0; index < fSize; index++)
fArray[index] = toAssign.fArray[index];
return *this;
}
template <class TElem> bool RefArrayOf<TElem>::
operator==(const RefArrayOf<TElem>& toCompare) const
{
if (this == &toCompare)
return true;
if (fSize != toCompare.fSize)
return false;
for (XMLSize_t index = 0; index < fSize; index++)
{
if (fArray[index] != toCompare.fArray[index])
return false;
}
return true;
}
template <class TElem> bool RefArrayOf<TElem>::
operator!=(const RefArrayOf<TElem>& toCompare) const
{
return !operator==(toCompare);
}
// ---------------------------------------------------------------------------
// RefArrayOf: Copy operations
// ---------------------------------------------------------------------------
template <class TElem> XMLSize_t RefArrayOf<TElem>::
copyFrom(const RefArrayOf<TElem>& srcArray)
{
//
// Copy over as many of the source elements as will fit into
// this array.
//
const XMLSize_t count = fSize < srcArray.fSize ?
fSize : srcArray.fSize;
for (XMLSize_t index = 0; index < fSize; index++)
fArray[index] = srcArray.fArray[index];
return count;
}
// ---------------------------------------------------------------------------
// RefArrayOf: Getter methods
// ---------------------------------------------------------------------------
template <class TElem> XMLSize_t RefArrayOf<TElem>::length() const
{
return fSize;
}
template <class TElem> TElem** RefArrayOf<TElem>::rawData() const
{
return fArray;
}
// ---------------------------------------------------------------------------
// RefArrayOf: Element management methods
// ---------------------------------------------------------------------------
template <class TElem> void RefArrayOf<TElem>::deleteAt(const XMLSize_t index)
{
if (index >= fSize)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
delete fArray[index];
fArray[index] = 0;
}
template <class TElem> void RefArrayOf<TElem>::deleteAllElements()
{
for (XMLSize_t index = 0; index < fSize; index++)
{
delete fArray[index];
fArray[index] = 0;
}
}
template <class TElem> void RefArrayOf<TElem>::resize(const XMLSize_t newSize)
{
if (newSize == fSize)
return;
if (newSize < fSize)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Array_BadNewSize, fMemoryManager);
// Allocate the new array
TElem** newArray = (TElem**) fMemoryManager->allocate
(
newSize * sizeof(TElem*)
);//new TElem*[newSize];
// Copy the existing values
XMLSize_t index = 0;
for (; index < fSize; index++)
newArray[index] = fArray[index];
for (; index < newSize; index++)
newArray[index] = 0;
// Delete the old array and update our members
fMemoryManager->deallocate(fArray);//delete [] fArray;
fArray = newArray;
fSize = newSize;
}
// ---------------------------------------------------------------------------
// RefArrayEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem> RefArrayEnumerator<TElem>::
RefArrayEnumerator( RefArrayOf<TElem>* const toEnum
, const bool adopt) :
fAdopted(adopt)
, fCurIndex(0)
, fToEnum(toEnum)
{
}
template <class TElem> RefArrayEnumerator<TElem>::~RefArrayEnumerator()
{
if (fAdopted)
delete fToEnum;
}
// ---------------------------------------------------------------------------
// RefArrayEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TElem> bool RefArrayEnumerator<TElem>::hasMoreElements() const
{
if (fCurIndex >= fToEnum->length())
return false;
return true;
}
template <class TElem> TElem& RefArrayEnumerator<TElem>::nextElement()
{
return *(*fToEnum)[fCurIndex++];
}
template <class TElem> void RefArrayEnumerator<TElem>::Reset()
{
fCurIndex = 0;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,122 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include "RefArrayVectorOf.hpp"
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// RefArrayVectorOf: Constructor and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
RefArrayVectorOf<TElem>::RefArrayVectorOf( const XMLSize_t maxElems
, const bool adoptElems
, MemoryManager* const manager)
: BaseRefVectorOf<TElem>(maxElems, adoptElems, manager)
{
}
template <class TElem> RefArrayVectorOf<TElem>::~RefArrayVectorOf()
{
if (this->fAdoptedElems)
{
for (XMLSize_t index = 0; index < this->fCurCount; index++)
this->fMemoryManager->deallocate(this->fElemList[index]);//delete[] fElemList[index];
}
this->fMemoryManager->deallocate(this->fElemList);//delete [] fElemList;
}
template <class TElem> void
RefArrayVectorOf<TElem>::setElementAt(TElem* const toSet, const XMLSize_t setAt)
{
if (setAt >= this->fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, this->fMemoryManager);
if (this->fAdoptedElems)
this->fMemoryManager->deallocate(this->fElemList[setAt]);
this->fElemList[setAt] = toSet;
}
template <class TElem> void RefArrayVectorOf<TElem>::removeAllElements()
{
for (XMLSize_t index = 0; index < this->fCurCount; index++)
{
if (this->fAdoptedElems)
this->fMemoryManager->deallocate(this->fElemList[index]);
// Keep unused elements zero for sanity's sake
this->fElemList[index] = 0;
}
this->fCurCount = 0;
}
template <class TElem> void RefArrayVectorOf<TElem>::
removeElementAt(const XMLSize_t removeAt)
{
if (removeAt >= this->fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, this->fMemoryManager);
if (this->fAdoptedElems)
this->fMemoryManager->deallocate(this->fElemList[removeAt]);
// Optimize if its the last element
if (removeAt == this->fCurCount-1)
{
this->fElemList[removeAt] = 0;
this->fCurCount--;
return;
}
// Copy down every element above remove point
for (XMLSize_t index = removeAt; index < this->fCurCount-1; index++)
this->fElemList[index] = this->fElemList[index+1];
// Keep unused elements zero for sanity's sake
this->fElemList[this->fCurCount-1] = 0;
// And bump down count
this->fCurCount--;
}
template <class TElem> void RefArrayVectorOf<TElem>::removeLastElement()
{
if (!this->fCurCount)
return;
this->fCurCount--;
if (this->fAdoptedElems)
this->fMemoryManager->deallocate(this->fElemList[this->fCurCount]);
}
template <class TElem> void RefArrayVectorOf<TElem>::cleanup()
{
if (this->fAdoptedElems)
{
for (XMLSize_t index = 0; index < this->fCurCount; index++)
this->fMemoryManager->deallocate(this->fElemList[index]);
}
this->fMemoryManager->deallocate(this->fElemList);//delete [] fElemList;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,692 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: RefHash2KeysTableOf.c 679340 2008-07-24 10:28:29Z borisk $
*/
// ---------------------------------------------------------------------------
// Include
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/RefHash2KeysTableOf.hpp>
#endif
#include <xercesc/util/Janitor.hpp>
#include <xercesc/util/NullPointerException.hpp>
#include <assert.h>
#include <new>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// RefHash2KeysTableOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
RefHash2KeysTableOf<TVal, THasher>::RefHash2KeysTableOf(
const XMLSize_t modulus,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(true)
, fBucketList(0)
, fHashModulus(modulus)
, fCount(0)
{
initialize(modulus);
}
template <class TVal, class THasher>
RefHash2KeysTableOf<TVal, THasher>::RefHash2KeysTableOf(
const XMLSize_t modulus,
const THasher& hasher,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(true)
, fBucketList(0)
, fHashModulus(modulus)
, fCount(0)
, fHasher (hasher)
{
initialize(modulus);
}
template <class TVal, class THasher>
RefHash2KeysTableOf<TVal, THasher>::RefHash2KeysTableOf(
const XMLSize_t modulus,
const bool adoptElems,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(adoptElems)
, fBucketList(0)
, fHashModulus(modulus)
, fCount(0)
{
initialize(modulus);
}
template <class TVal, class THasher>
RefHash2KeysTableOf<TVal, THasher>::RefHash2KeysTableOf(
const XMLSize_t modulus,
const bool adoptElems,
const THasher& hasher,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(adoptElems)
, fBucketList(0)
, fHashModulus(modulus)
, fCount(0)
, fHasher (hasher)
{
initialize(modulus);
}
template <class TVal, class THasher>
void RefHash2KeysTableOf<TVal, THasher>::initialize(const XMLSize_t modulus)
{
if (modulus == 0)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::HshTbl_ZeroModulus, fMemoryManager);
// Allocate the bucket list and zero them
fBucketList = (RefHash2KeysTableBucketElem<TVal>**) fMemoryManager->allocate
(
fHashModulus * sizeof(RefHash2KeysTableBucketElem<TVal>*)
); //new RefHash2KeysTableBucketElem<TVal>*[fHashModulus];
memset(fBucketList, 0, sizeof(fBucketList[0]) * fHashModulus);
}
template <class TVal, class THasher>
RefHash2KeysTableOf<TVal, THasher>::~RefHash2KeysTableOf()
{
removeAll();
// Then delete the bucket list & hasher
fMemoryManager->deallocate(fBucketList); //delete [] fBucketList;
fBucketList = 0;
}
// ---------------------------------------------------------------------------
// RefHash2KeysTableOf: Element management
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
bool RefHash2KeysTableOf<TVal, THasher>::isEmpty() const
{
return (fCount==0);
}
template <class TVal, class THasher>
bool RefHash2KeysTableOf<TVal, THasher>::
containsKey(const void* const key1, const int key2) const
{
XMLSize_t hashVal;
const RefHash2KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, hashVal);
return (findIt != 0);
}
template <class TVal, class THasher>
void RefHash2KeysTableOf<TVal, THasher>::
removeKey(const void* const key1, const int key2)
{
// Hash the key
XMLSize_t hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
//
// Search the given bucket for this key. Keep up with the previous
// element so we can patch around it.
//
RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
RefHash2KeysTableBucketElem<TVal>* lastElem = 0;
while (curElem)
{
if((key2==curElem->fKey2) && (fHasher.equals(key1, curElem->fKey1)))
{
if (!lastElem)
{
// It was the first in the bucket
fBucketList[hashVal] = curElem->fNext;
}
else
{
// Patch around the current element
lastElem->fNext = curElem->fNext;
}
// If we adopted the elements, then delete the data
if (fAdoptedElems)
delete curElem->fData;
// Delete the current element
// delete curElem;
// destructor is empty...
// curElem->~RefHash2KeysTableBucketElem();
fMemoryManager->deallocate(curElem);
fCount--;
return;
}
// Move both pointers upwards
lastElem = curElem;
curElem = curElem->fNext;
}
// We never found that key
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists, fMemoryManager);
}
template <class TVal, class THasher>
void RefHash2KeysTableOf<TVal, THasher>::
removeKey(const void* const key1)
{
// Hash the key
XMLSize_t hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
//
// Search the given bucket for this key. Keep up with the previous
// element so we can patch around it.
//
RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
RefHash2KeysTableBucketElem<TVal>* lastElem = 0;
while (curElem)
{
if(fHasher.equals(key1, curElem->fKey1))
{
if (!lastElem)
{
// It was the first in the bucket
fBucketList[hashVal] = curElem->fNext;
}
else
{
// Patch around the current element
lastElem->fNext = curElem->fNext;
}
// If we adopted the elements, then delete the data
if (fAdoptedElems)
delete curElem->fData;
RefHash2KeysTableBucketElem<TVal>* toBeDeleted=curElem;
curElem = curElem->fNext;
// Delete the current element
// delete curElem;
// destructor is empty...
// curElem->~RefHash2KeysTableBucketElem();
fMemoryManager->deallocate(toBeDeleted);
fCount--;
}
else
{
// Move both pointers upwards
lastElem = curElem;
curElem = curElem->fNext;
}
}
}
template <class TVal, class THasher>
void RefHash2KeysTableOf<TVal, THasher>::removeAll()
{
if(isEmpty())
return;
// Clean up the buckets first
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
{
// Get the bucket list head for this entry
RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[buckInd];
RefHash2KeysTableBucketElem<TVal>* nextElem;
while (curElem)
{
// Save the next element before we hose this one
nextElem = curElem->fNext;
// If we adopted the data, then delete it too
// (Note: the userdata hash table instance has data type of void *.
// This will generate compiler warnings here on some platforms, but they
// can be ignored since fAdoptedElements is false.
if (fAdoptedElems)
delete curElem->fData;
// Then delete the current element and move forward
// destructor is empty...
// curElem->~RefHash2KeysTableBucketElem();
fMemoryManager->deallocate(curElem);
curElem = nextElem;
}
// Clean out this entry
fBucketList[buckInd] = 0;
}
fCount=0;
}
// this function transfer the data from key1 to key2
template <class TVal, class THasher>
void RefHash2KeysTableOf<TVal, THasher>::transferElement(const void* const key1, void* key2)
{
// Hash the key
XMLSize_t hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
//
// Search the given bucket for this key. Keep up with the previous
// element so we can patch around it.
//
RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
RefHash2KeysTableBucketElem<TVal>* lastElem = 0;
while (curElem)
{
// if this element has the same primary key, remove it and add it using the new primary key
if(fHasher.equals(key1, curElem->fKey1))
{
if (!lastElem)
{
// It was the first in the bucket
fBucketList[hashVal] = curElem->fNext;
}
else
{
// Patch around the current element
lastElem->fNext = curElem->fNext;
}
// this code comes from put(), but it doesn't update fCount
XMLSize_t hashVal2;
RefHash2KeysTableBucketElem<TVal>* newBucket = findBucketElem(key2, curElem->fKey2, hashVal2);
if (newBucket)
{
if (fAdoptedElems)
delete newBucket->fData;
newBucket->fData = curElem->fData;
newBucket->fKey1 = key2;
newBucket->fKey2 = curElem->fKey2;
}
else
{
newBucket =
new (fMemoryManager->allocate(sizeof(RefHash2KeysTableBucketElem<TVal>)))
RefHash2KeysTableBucketElem<TVal>(key2, curElem->fKey2, curElem->fData, fBucketList[hashVal2]);
fBucketList[hashVal2] = newBucket;
}
RefHash2KeysTableBucketElem<TVal>* elemToDelete = curElem;
// Update just curElem; lastElem must stay the same
curElem = curElem->fNext;
// Delete the current element
// delete elemToDelete;
// destructor is empty...
// curElem->~RefHash2KeysTableBucketElem();
fMemoryManager->deallocate(elemToDelete);
}
else
{
// Move both pointers upwards
lastElem = curElem;
curElem = curElem->fNext;
}
}
}
// ---------------------------------------------------------------------------
// RefHash2KeysTableOf: Getters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
TVal* RefHash2KeysTableOf<TVal, THasher>::get(const void* const key1, const int key2)
{
XMLSize_t hashVal;
RefHash2KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, hashVal);
if (!findIt)
return 0;
return findIt->fData;
}
template <class TVal, class THasher>
const TVal* RefHash2KeysTableOf<TVal, THasher>::
get(const void* const key1, const int key2) const
{
XMLSize_t hashVal;
const RefHash2KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, hashVal);
if (!findIt)
return 0;
return findIt->fData;
}
template <class TVal, class THasher>
MemoryManager* RefHash2KeysTableOf<TVal, THasher>::getMemoryManager() const
{
return fMemoryManager;
}
template <class TVal, class THasher>
XMLSize_t RefHash2KeysTableOf<TVal, THasher>::getHashModulus() const
{
return fHashModulus;
}
// ---------------------------------------------------------------------------
// RefHash2KeysTableOf: Putters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
void RefHash2KeysTableOf<TVal, THasher>::put(void* key1, int key2, TVal* const valueToAdopt)
{
// Apply 4 load factor to find threshold.
XMLSize_t threshold = fHashModulus * 4;
// If we've grown too big, expand the table and rehash.
if (fCount >= threshold)
rehash();
// First see if the key exists already
XMLSize_t hashVal;
RefHash2KeysTableBucketElem<TVal>* newBucket = findBucketElem(key1, key2, hashVal);
//
// If so,then update its value. If not, then we need to add it to
// the right bucket
//
if (newBucket)
{
if (fAdoptedElems)
delete newBucket->fData;
newBucket->fData = valueToAdopt;
newBucket->fKey1 = key1;
newBucket->fKey2 = key2;
}
else
{
newBucket =
new (fMemoryManager->allocate(sizeof(RefHash2KeysTableBucketElem<TVal>)))
RefHash2KeysTableBucketElem<TVal>(key1, key2, valueToAdopt, fBucketList[hashVal]);
fBucketList[hashVal] = newBucket;
fCount++;
}
}
// ---------------------------------------------------------------------------
// RefHash2KeysTableOf: Private methods
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
inline RefHash2KeysTableBucketElem<TVal>* RefHash2KeysTableOf<TVal, THasher>::
findBucketElem(const void* const key1, const int key2, XMLSize_t& hashVal)
{
// Hash the key
hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
// Search that bucket for the key
RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
if((key2==curElem->fKey2) && (fHasher.equals(key1, curElem->fKey1)))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
template <class TVal, class THasher>
inline const RefHash2KeysTableBucketElem<TVal>* RefHash2KeysTableOf<TVal, THasher>::
findBucketElem(const void* const key1, const int key2, XMLSize_t& hashVal) const
{
// Hash the key
hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
// Search that bucket for the key
const RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
if((key2==curElem->fKey2) && (fHasher.equals(key1, curElem->fKey1)))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
template <class TVal, class THasher>
void RefHash2KeysTableOf<TVal, THasher>::
rehash()
{
const XMLSize_t newMod = (fHashModulus * 8)+1;
RefHash2KeysTableBucketElem<TVal>** newBucketList =
(RefHash2KeysTableBucketElem<TVal>**) fMemoryManager->allocate
(
newMod * sizeof(RefHash2KeysTableBucketElem<TVal>*)
);//new RefHash2KeysTableBucketElem<TVal>*[fHashModulus];
// Make sure the new bucket list is destroyed if an
// exception is thrown.
ArrayJanitor<RefHash2KeysTableBucketElem<TVal>*> guard(newBucketList, fMemoryManager);
memset(newBucketList, 0, newMod * sizeof(newBucketList[0]));
// Rehash all existing entries.
for (XMLSize_t index = 0; index < fHashModulus; index++)
{
// Get the bucket list head for this entry
RefHash2KeysTableBucketElem<TVal>* curElem = fBucketList[index];
while (curElem)
{
// Save the next element before we detach this one
RefHash2KeysTableBucketElem<TVal>* nextElem = curElem->fNext;
const XMLSize_t hashVal = fHasher.getHashVal(curElem->fKey1, newMod);
assert(hashVal < newMod);
RefHash2KeysTableBucketElem<TVal>* newHeadElem = newBucketList[hashVal];
// Insert at the start of this bucket's list.
curElem->fNext = newHeadElem;
newBucketList[hashVal] = curElem;
curElem = nextElem;
}
}
RefHash2KeysTableBucketElem<TVal>** const oldBucketList = fBucketList;
// Everything is OK at this point, so update the
// member variables.
fBucketList = guard.release();
fHashModulus = newMod;
// Delete the old bucket list.
fMemoryManager->deallocate(oldBucketList);//delete[] oldBucketList;
}
// ---------------------------------------------------------------------------
// RefHash2KeysTableOfEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
RefHash2KeysTableOfEnumerator<TVal, THasher>::
RefHash2KeysTableOfEnumerator(RefHash2KeysTableOf<TVal, THasher>* const toEnum
, const bool adopt
, MemoryManager* const manager)
: fAdopted(adopt), fCurElem(0), fCurHash((XMLSize_t)-1), fToEnum(toEnum)
, fMemoryManager(manager)
, fLockPrimaryKey(0)
{
if (!toEnum)
ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, fMemoryManager);
//
// Find the next available bucket element in the hash table. If it
// comes back zero, that just means the table is empty.
//
// Note that the -1 in the current hash tells it to start
// from the beginning.
//
findNext();
}
template <class TVal, class THasher>
RefHash2KeysTableOfEnumerator<TVal, THasher>::~RefHash2KeysTableOfEnumerator()
{
if (fAdopted)
delete fToEnum;
}
// ---------------------------------------------------------------------------
// RefHash2KeysTableOfEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
bool RefHash2KeysTableOfEnumerator<TVal, THasher>::hasMoreElements() const
{
//
// If our current has is at the max and there are no more elements
// in the current bucket, then no more elements.
//
if (!fCurElem && (fCurHash == fToEnum->fHashModulus))
return false;
return true;
}
template <class TVal, class THasher>
TVal& RefHash2KeysTableOfEnumerator<TVal, THasher>::nextElement()
{
// Make sure we have an element to return
if (!hasMoreElements())
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
//
// Save the current element, then move up to the next one for the
// next time around.
//
RefHash2KeysTableBucketElem<TVal>* saveElem = fCurElem;
findNext();
return *saveElem->fData;
}
template <class TVal, class THasher>
void RefHash2KeysTableOfEnumerator<TVal, THasher>::nextElementKey(void*& retKey1, int& retKey2)
{
// Make sure we have an element to return
if (!hasMoreElements())
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
//
// Save the current element, then move up to the next one for the
// next time around.
//
RefHash2KeysTableBucketElem<TVal>* saveElem = fCurElem;
findNext();
retKey1 = saveElem->fKey1;
retKey2 = saveElem->fKey2;
return;
}
template <class TVal, class THasher>
void RefHash2KeysTableOfEnumerator<TVal, THasher>::Reset()
{
if(fLockPrimaryKey)
fCurHash=fToEnum->fHasher.getHashVal(fLockPrimaryKey, fToEnum->fHashModulus);
else
fCurHash = (XMLSize_t)-1;
fCurElem = 0;
findNext();
}
template <class TVal, class THasher>
void RefHash2KeysTableOfEnumerator<TVal, THasher>::setPrimaryKey(const void* key)
{
fLockPrimaryKey=key;
Reset();
}
// ---------------------------------------------------------------------------
// RefHash2KeysTableOfEnumerator: Private helper methods
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
void RefHash2KeysTableOfEnumerator<TVal, THasher>::findNext()
{
// Code to execute if we have to return only values with the primary key
if(fLockPrimaryKey)
{
if(!fCurElem)
fCurElem = fToEnum->fBucketList[fCurHash];
else
fCurElem = fCurElem->fNext;
while (fCurElem && (!fToEnum->fHasher.equals(fLockPrimaryKey, fCurElem->fKey1)))
fCurElem = fCurElem->fNext;
// if we didn't found it, make so hasMoreElements() returns false
if(!fCurElem)
fCurHash = fToEnum->fHashModulus;
return;
}
//
// If there is a current element, move to its next element. If this
// hits the end of the bucket, the next block will handle the rest.
//
if (fCurElem)
fCurElem = fCurElem->fNext;
//
// If the current element is null, then we have to move up to the
// next hash value. If that is the hash modulus, then we cannot
// go further.
//
if (!fCurElem)
{
fCurHash++;
if (fCurHash == fToEnum->fHashModulus)
return;
// Else find the next non-empty bucket
while (fToEnum->fBucketList[fCurHash]==0)
{
// Bump to the next hash value. If we max out return
fCurHash++;
if (fCurHash == fToEnum->fHashModulus)
return;
}
fCurElem = fToEnum->fBucketList[fCurHash];
}
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,572 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: RefHash3KeysIdPool.c 883368 2009-11-23 15:28:19Z amassari $
*/
// ---------------------------------------------------------------------------
// Include
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/RefHash3KeysIdPool.hpp>
#endif
#include <xercesc/util/NullPointerException.hpp>
#include <assert.h>
#include <new>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// RefHash3KeysIdPool: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
RefHash3KeysIdPool<TVal, THasher>::RefHash3KeysIdPool(
const XMLSize_t modulus,
const XMLSize_t initSize,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(true)
, fBucketList(0)
, fHashModulus(modulus)
, fIdPtrs(0)
, fIdPtrsCount(initSize)
, fIdCounter(0)
{
initialize(modulus);
// 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
// zeroth element is never used (and represents an invalid pool id.)
//
if (!fIdPtrsCount)
fIdPtrsCount = 256;
fIdPtrs = (TVal**) fMemoryManager->allocate(fIdPtrsCount * sizeof(TVal*)); //new TVal*[fIdPtrsCount];
fIdPtrs[0] = 0;
}
template <class TVal, class THasher>
RefHash3KeysIdPool<TVal, THasher>::RefHash3KeysIdPool(
const XMLSize_t modulus,
const THasher& hasher,
const XMLSize_t initSize,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(true)
, fBucketList(0)
, fHashModulus(modulus)
, fIdPtrs(0)
, fIdPtrsCount(initSize)
, fIdCounter(0)
, fHasher(hasher)
{
initialize(modulus);
// 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
// zeroth element is never used (and represents an invalid pool id.)
//
if (!fIdPtrsCount)
fIdPtrsCount = 256;
fIdPtrs = (TVal**) fMemoryManager->allocate(fIdPtrsCount * sizeof(TVal*)); //new TVal*[fIdPtrsCount];
fIdPtrs[0] = 0;
}
template <class TVal, class THasher>
RefHash3KeysIdPool<TVal, THasher>::RefHash3KeysIdPool(
const XMLSize_t modulus,
const bool adoptElems,
const XMLSize_t initSize,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(adoptElems)
, fBucketList(0)
, fHashModulus(modulus)
, fIdPtrs(0)
, fIdPtrsCount(initSize)
, fIdCounter(0)
{
initialize(modulus);
// 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
// zeroth element is never used (and represents an invalid pool id.)
//
if (!fIdPtrsCount)
fIdPtrsCount = 256;
fIdPtrs = (TVal**) fMemoryManager->allocate(fIdPtrsCount * sizeof(TVal*)); //new TVal*[fIdPtrsCount];
fIdPtrs[0] = 0;
}
template <class TVal, class THasher>
RefHash3KeysIdPool<TVal, THasher>::RefHash3KeysIdPool(
const XMLSize_t modulus,
const bool adoptElems,
const THasher& hasher,
const XMLSize_t initSize,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(adoptElems)
, fBucketList(0)
, fHashModulus(modulus)
, fIdPtrs(0)
, fIdPtrsCount(initSize)
, fIdCounter(0)
, fHasher(hasher)
{
initialize(modulus);
// 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
// zeroth element is never used (and represents an invalid pool id.)
//
if (!fIdPtrsCount)
fIdPtrsCount = 256;
fIdPtrs = (TVal**) fMemoryManager->allocate(fIdPtrsCount * sizeof(TVal*)); //new TVal*[fIdPtrsCount];
fIdPtrs[0] = 0;
}
template <class TVal, class THasher>
void RefHash3KeysIdPool<TVal, THasher>::initialize(const XMLSize_t modulus)
{
if (modulus == 0)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::HshTbl_ZeroModulus, fMemoryManager);
// Allocate the bucket list and zero them
fBucketList = (RefHash3KeysTableBucketElem<TVal>**) fMemoryManager->allocate
(
fHashModulus * sizeof(RefHash3KeysTableBucketElem<TVal>*)
); //new RefHash3KeysTableBucketElem<TVal>*[fHashModulus];
memset(fBucketList, 0, sizeof(fBucketList[0]) * fHashModulus);
}
template <class TVal, class THasher>
RefHash3KeysIdPool<TVal, THasher>::~RefHash3KeysIdPool()
{
removeAll();
// Then delete the bucket list & hasher & id pointers list
fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
fIdPtrs = 0;
fMemoryManager->deallocate(fBucketList); //delete [] fBucketList;
fBucketList = 0;
}
// ---------------------------------------------------------------------------
// RefHash3KeysIdPool: Element management
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
bool RefHash3KeysIdPool<TVal, THasher>::isEmpty() const
{
// Just check the bucket list for non-empty elements
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
{
if (fBucketList[buckInd] != 0)
return false;
}
return true;
}
template <class TVal, class THasher>
bool RefHash3KeysIdPool<TVal, THasher>::
containsKey(const void* const key1, const int key2, const int key3) const
{
XMLSize_t hashVal;
const RefHash3KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, key3, hashVal);
return (findIt != 0);
}
template <class TVal, class THasher>
void RefHash3KeysIdPool<TVal, THasher>::removeAll()
{
if (fIdCounter == 0) return;
// Clean up the buckets first
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
{
// Get the bucket list head for this entry
RefHash3KeysTableBucketElem<TVal>* curElem = fBucketList[buckInd];
RefHash3KeysTableBucketElem<TVal>* nextElem;
while (curElem)
{
// Save the next element before we hose this one
nextElem = curElem->fNext;
// If we adopted the data, then delete it too
// (Note: the userdata hash table instance has data type of void *.
// This will generate compiler warnings here on some platforms, but they
// can be ignored since fAdoptedElements is false.
if (fAdoptedElems)
delete curElem->fData;
// Then delete the current element and move forward
// delete curElem;
// destructor is empty...
// curElem->~RefHash3KeysTableBucketElem();
fMemoryManager->deallocate(curElem);
curElem = nextElem;
}
// Clean out this entry
fBucketList[buckInd] = 0;
}
// Reset the id counter
fIdCounter = 0;
}
// ---------------------------------------------------------------------------
// RefHash3KeysIdPool: Getters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
TVal*
RefHash3KeysIdPool<TVal, THasher>::getByKey(const void* const key1, const int key2, const int key3)
{
XMLSize_t hashVal;
RefHash3KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, key3, hashVal);
if (!findIt)
return 0;
return findIt->fData;
}
template <class TVal, class THasher>
const TVal*
RefHash3KeysIdPool<TVal, THasher>::getByKey(const void* const key1, const int key2, const int key3) const
{
XMLSize_t hashVal;
const RefHash3KeysTableBucketElem<TVal>* findIt = findBucketElem(key1, key2, key3, hashVal);
if (!findIt)
return 0;
return findIt->fData;
}
template <class TVal, class THasher>
TVal*
RefHash3KeysIdPool<TVal, THasher>::getById(const unsigned int elemId)
{
// If its either zero or beyond our current id, its an error
if (!elemId || (elemId > fIdCounter))
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_InvalidId, fMemoryManager);
return fIdPtrs[elemId];
}
template <class TVal, class THasher>
const TVal*
RefHash3KeysIdPool<TVal, THasher>::getById(const unsigned int elemId) const
{
// If its either zero or beyond our current id, its an error
if (!elemId || (elemId > fIdCounter))
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Pool_InvalidId, fMemoryManager);
return fIdPtrs[elemId];
}
template <class TVal, class THasher>
MemoryManager* RefHash3KeysIdPool<TVal, THasher>::getMemoryManager() const
{
return fMemoryManager;
}
template <class TVal, class THasher>
XMLSize_t RefHash3KeysIdPool<TVal, THasher>::getHashModulus() const
{
return fHashModulus;
}
// ---------------------------------------------------------------------------
// RefHash3KeysIdPool: Putters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
XMLSize_t
RefHash3KeysIdPool<TVal, THasher>::put(void* key1, int key2, int key3, TVal* const valueToAdopt)
{
// First see if the key exists already
XMLSize_t hashVal;
XMLSize_t retId;
RefHash3KeysTableBucketElem<TVal>* newBucket = findBucketElem(key1, key2, key3, hashVal);
//
// If so,then update its value. If not, then we need to add it to
// the right bucket
//
if (newBucket)
{
retId = newBucket->fData->getId();
if (fAdoptedElems)
delete newBucket->fData;
newBucket->fData = valueToAdopt;
newBucket->fKey1 = key1;
newBucket->fKey2 = key2;
newBucket->fKey3 = key3;
}
else
{
// Revisit: the gcc compiler 2.95.x is generating an
// internal compiler error message. So we use the default
// memory manager for now.
#if defined (XML_GCC_VERSION) && (XML_GCC_VERSION < 29600)
newBucket = new RefHash3KeysTableBucketElem<TVal>(key1, key2, key3, valueToAdopt, fBucketList[hashVal]);
#else
newBucket =
new (fMemoryManager->allocate(sizeof(RefHash3KeysTableBucketElem<TVal>)))
RefHash3KeysTableBucketElem<TVal>(key1, key2, key3, valueToAdopt, fBucketList[hashVal]);
#endif
fBucketList[hashVal] = newBucket;
//
// Give this new one the next available id and add to the pointer list.
// Expand the list if that is now required.
//
if (fIdCounter + 1 == fIdPtrsCount)
{
// Create a new count 1.5 times larger and allocate a new array
XMLSize_t newCount = (XMLSize_t)(fIdPtrsCount * 1.5);
TVal** newArray = (TVal**) fMemoryManager->allocate
(
newCount * sizeof(TVal*)
); //new TVal*[newCount];
// Copy over the old contents to the new array
memcpy(newArray, fIdPtrs, fIdPtrsCount * sizeof(TVal*));
// Ok, toss the old array and store the new data
fMemoryManager->deallocate(fIdPtrs); //delete [] fIdPtrs;
fIdPtrs = newArray;
fIdPtrsCount = newCount;
}
retId = ++fIdCounter;
}
fIdPtrs[retId] = valueToAdopt;
// Set the id on the passed element
valueToAdopt->setId(retId);
// Return the id that we gave to this element
return retId;
}
// ---------------------------------------------------------------------------
// RefHash3KeysIdPool: Private methods
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
inline RefHash3KeysTableBucketElem<TVal>* RefHash3KeysIdPool<TVal, THasher>::
findBucketElem(const void* const key1, const int key2, const int key3, XMLSize_t& hashVal)
{
// Hash the key
hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
// Search that bucket for the key
RefHash3KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
if((key2==curElem->fKey2) && (key3==curElem->fKey3) && (fHasher.equals(key1, curElem->fKey1)))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
template <class TVal, class THasher>
inline const RefHash3KeysTableBucketElem<TVal>* RefHash3KeysIdPool<TVal, THasher>::
findBucketElem(const void* const key1, const int key2, const int key3, XMLSize_t& hashVal) const
{
// Hash the key
hashVal = fHasher.getHashVal(key1, fHashModulus);
assert(hashVal < fHashModulus);
// Search that bucket for the key
const RefHash3KeysTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
if((key2==curElem->fKey2) && (key3==curElem->fKey3) && (fHasher.equals(key1, curElem->fKey1)))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
// ---------------------------------------------------------------------------
// RefHash3KeysIdPoolEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
RefHash3KeysIdPoolEnumerator<TVal, THasher>::
RefHash3KeysIdPoolEnumerator(RefHash3KeysIdPool<TVal, THasher>* const toEnum
, const bool adopt
, MemoryManager* const manager)
: fAdoptedElems(adopt), fCurIndex(0), fToEnum(toEnum), fMemoryManager(manager)
{
if (!toEnum)
ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, fMemoryManager);
Reset();
resetKey();
}
template <class TVal, class THasher>
RefHash3KeysIdPoolEnumerator<TVal, THasher>::~RefHash3KeysIdPoolEnumerator()
{
if (fAdoptedElems)
delete fToEnum;
}
template <class TVal, class THasher>
RefHash3KeysIdPoolEnumerator<TVal, THasher>::
RefHash3KeysIdPoolEnumerator(const RefHash3KeysIdPoolEnumerator<TVal, THasher>& toCopy) :
XMLEnumerator<TVal>(toCopy)
, XMemory(toCopy)
, fAdoptedElems(toCopy.fAdoptedElems)
, fCurIndex(toCopy.fCurIndex)
, fToEnum(toCopy.fToEnum)
, fCurElem(toCopy.fCurElem)
, fCurHash(toCopy.fCurHash)
, fMemoryManager(toCopy.fMemoryManager)
{
}
// ---------------------------------------------------------------------------
// RefHash3KeysIdPoolEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
bool RefHash3KeysIdPoolEnumerator<TVal, THasher>::hasMoreElements() const
{
// If our index is zero or past the end, then we are done
if (!fCurIndex || (fCurIndex > fToEnum->fIdCounter))
return false;
return true;
}
template <class TVal, class THasher>
TVal& RefHash3KeysIdPoolEnumerator<TVal, THasher>::nextElement()
{
// If our index is zero or past the end, then we are done
if (!fCurIndex || (fCurIndex > fToEnum->fIdCounter))
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
// Return the current element and bump the index
return *fToEnum->fIdPtrs[fCurIndex++];
}
template <class TVal, class THasher>
void RefHash3KeysIdPoolEnumerator<TVal, THasher>::Reset()
{
//
// Find the next available bucket element in the pool. We use the id
// array since its very easy to enumerator through by just maintaining
// an index. If the id counter is zero, then its empty and we leave the
// current index to zero.
//
fCurIndex = fToEnum->fIdCounter ? 1:0;
}
template <class TVal, class THasher>
XMLSize_t RefHash3KeysIdPoolEnumerator<TVal, THasher>::size() const
{
return fToEnum->fIdCounter;
}
template <class TVal, class THasher>
void RefHash3KeysIdPoolEnumerator<TVal, THasher>::resetKey()
{
fCurHash = (XMLSize_t)-1;
fCurElem = 0;
findNext();
}
template <class TVal, class THasher>
bool RefHash3KeysIdPoolEnumerator<TVal, THasher>::hasMoreKeys() const
{
//
// If our current has is at the max and there are no more elements
// in the current bucket, then no more elements.
//
if (!fCurElem && (fCurHash == fToEnum->fHashModulus))
return false;
return true;
}
template <class TVal, class THasher>
void RefHash3KeysIdPoolEnumerator<TVal, THasher>::nextElementKey(void*& retKey1, int& retKey2, int& retKey3)
{
// Make sure we have an element to return
if (!hasMoreKeys())
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
//
// Save the current element, then move up to the next one for the
// next time around.
//
RefHash3KeysTableBucketElem<TVal>* saveElem = fCurElem;
findNext();
retKey1 = saveElem->fKey1;
retKey2 = saveElem->fKey2;
retKey3 = saveElem->fKey3;
return;
}
template <class TVal, class THasher>
void RefHash3KeysIdPoolEnumerator<TVal, THasher>::findNext()
{
//
// If there is a current element, move to its next element. If this
// hits the end of the bucket, the next block will handle the rest.
//
if (fCurElem)
fCurElem = fCurElem->fNext;
//
// If the current element is null, then we have to move up to the
// next hash value. If that is the hash modulus, then we cannot
// go further.
//
if (!fCurElem)
{
fCurHash++;
if (fCurHash == fToEnum->fHashModulus)
return;
// Else find the next non-empty bucket
while (fToEnum->fBucketList[fCurHash]==0)
{
// Bump to the next hash value. If we max out return
fCurHash++;
if (fCurHash == fToEnum->fHashModulus)
return;
}
fCurElem = fToEnum->fBucketList[fCurHash];
}
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,663 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: RefHashTableOf.c 678409 2008-07-21 13:08:10Z borisk $
*/
// ---------------------------------------------------------------------------
// Include
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/RefHashTableOf.hpp>
#endif
#include <xercesc/util/Janitor.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/NullPointerException.hpp>
#include <new>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// RefHashTableOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
RefHashTableOf<TVal, THasher>::RefHashTableOf(
const XMLSize_t modulus,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(true)
, fBucketList(0)
, fHashModulus(modulus)
, fInitialModulus(modulus)
, fCount(0)
{
initialize(modulus);
}
template <class TVal, class THasher>
RefHashTableOf<TVal, THasher>::RefHashTableOf(
const XMLSize_t modulus,
const THasher& hasher,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(true)
, fBucketList(0)
, fHashModulus(modulus)
, fInitialModulus(modulus)
, fCount(0)
, fHasher (hasher)
{
initialize(modulus);
}
template <class TVal, class THasher>
RefHashTableOf<TVal, THasher>::RefHashTableOf(
const XMLSize_t modulus,
const bool adoptElems,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(adoptElems)
, fBucketList(0)
, fHashModulus(modulus)
, fInitialModulus(modulus)
, fCount(0)
{
initialize(modulus);
}
template <class TVal, class THasher>
RefHashTableOf<TVal, THasher>::RefHashTableOf(
const XMLSize_t modulus,
const bool adoptElems,
const THasher& hasher,
MemoryManager* const manager)
: fMemoryManager(manager)
, fAdoptedElems(adoptElems)
, fBucketList(0)
, fHashModulus(modulus)
, fInitialModulus(modulus)
, fCount(0)
, fHasher (hasher)
{
initialize(modulus);
}
template <class TVal, class THasher>
void RefHashTableOf<TVal, THasher>::initialize(const XMLSize_t modulus)
{
if (modulus == 0)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::HshTbl_ZeroModulus, fMemoryManager);
// Allocate the bucket list and zero them
fBucketList = (RefHashTableBucketElem<TVal>**) fMemoryManager->allocate
(
fHashModulus * sizeof(RefHashTableBucketElem<TVal>*)
);
for (XMLSize_t index = 0; index < fHashModulus; index++)
fBucketList[index] = 0;
}
template <class TVal, class THasher>
RefHashTableOf<TVal, THasher>::~RefHashTableOf()
{
cleanup();
}
// ---------------------------------------------------------------------------
// RefHashTableOf: Element management
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
inline bool RefHashTableOf<TVal, THasher>::isEmpty() const
{
return fCount==0;
}
template <class TVal, class THasher>
inline bool RefHashTableOf<TVal, THasher>::containsKey(const void* const key) const
{
XMLSize_t hashVal;
const RefHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
return (findIt != 0);
}
template <class TVal, class THasher>
void RefHashTableOf<TVal, THasher>::
removeKey(const void* const key)
{
// Hash the key
XMLSize_t hashVal = fHasher.getHashVal(key, fHashModulus);
//
// Search the given bucket for this key. Keep up with the previous
// element so we can patch around it.
//
RefHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
RefHashTableBucketElem<TVal>* lastElem = 0;
while (curElem)
{
if (fHasher.equals(key, curElem->fKey))
{
if (!lastElem)
{
// It was the first in the bucket
fBucketList[hashVal] = curElem->fNext;
}
else
{
// Patch around the current element
lastElem->fNext = curElem->fNext;
}
// If we adopted the data, then delete it too
// (Note: the userdata hash table instance has data type of void *.
// This will generate compiler warnings here on some platforms, but they
// can be ignored since fAdoptedElements is false.
if (fAdoptedElems)
delete curElem->fData;
// Then delete the current element and move forward
// delete curElem;
// destructor doesn't do anything...
fMemoryManager->deallocate(curElem);
fCount--;
return;
}
// Move both pointers upwards
lastElem = curElem;
curElem = curElem->fNext;
}
// We never found that key
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists, fMemoryManager);
}
template <class TVal, class THasher>
void RefHashTableOf<TVal, THasher>::removeAll()
{
if(isEmpty())
return;
// Clean up the buckets first
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
{
// Get the bucket list head for this entry
RefHashTableBucketElem<TVal>* curElem = fBucketList[buckInd];
RefHashTableBucketElem<TVal>* nextElem;
while (curElem)
{
// Save the next element before we hose this one
nextElem = curElem->fNext;
// If we adopted the data, then delete it too
// (Note: the userdata hash table instance has data type of void *.
// This will generate compiler warnings here on some platforms, but they
// can be ignored since fAdoptedElements is false.
if (fAdoptedElems)
delete curElem->fData;
// Then delete the current element and move forward
// delete curElem;
// destructor doesn't do anything...
// curElem->~RefHashTableBucketElem();
fMemoryManager->deallocate(curElem);
curElem = nextElem;
}
// Clean out this entry
fBucketList[buckInd] = 0;
}
fCount = 0;
}
// This method returns the data associated with a key. The key entry is deleted. The caller
// now owns the returned data (case of hashtable adopting the data).
// This function is called by transferElement so that the undeleted data can be transferred
// to a new key which will own that data.
template <class TVal, class THasher> TVal* RefHashTableOf<TVal, THasher>::
orphanKey(const void* const key)
{
// Hash the key
TVal* retVal = 0;
XMLSize_t hashVal = fHasher.getHashVal(key, fHashModulus);
//
// Search the given bucket for this key. Keep up with the previous
// element so we can patch around it.
//
RefHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
RefHashTableBucketElem<TVal>* lastElem = 0;
while (curElem)
{
if (fHasher.equals(key, curElem->fKey))
{
if (!lastElem)
{
// It was the first in the bucket
fBucketList[hashVal] = curElem->fNext;
}
else
{
// Patch around the current element
lastElem->fNext = curElem->fNext;
}
retVal = curElem->fData;
// Delete the current element
// delete curElem;
// destructor doesn't do anything...
// curElem->~RefHashTableBucketElem();
fMemoryManager->deallocate(curElem);
break;
}
// Move both pointers upwards
lastElem = curElem;
curElem = curElem->fNext;
}
// We never found that key
if (!retVal)
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists, fMemoryManager);
return retVal;
}
//
// cleanup():
// similar to destructor
// called to cleanup the memory, in case destructor cannot be called
//
template <class TVal, class THasher>
void RefHashTableOf<TVal, THasher>::cleanup()
{
removeAll();
// Then delete the bucket list & hasher
fMemoryManager->deallocate(fBucketList);
fBucketList = 0;
}
//
// reinitialize():
// similar to constructor
// called to re-construct the fElemList from scratch again
//
template <class TVal, class THasher>
void RefHashTableOf<TVal, THasher>::reinitialize(const THasher& hasher)
{
if (fBucketList)
cleanup();
fHasher = hasher;
fHashModulus = fInitialModulus;
initialize(fHashModulus);
}
// this function transfer the data from key1 to key2
// this is equivalent to calling
// 1. get(key1) to retrieve the data,
// 2. removeKey(key1),
// 3. and then put(key2, data)
// except that the data is not deleted in "removeKey" even it is adopted so that it
// can be transferred to key2.
// whatever key2 has originally will be purged (if adopted)
template <class TVal, class THasher>
inline void RefHashTableOf<TVal, THasher>::transferElement(const void* const key1, void* key2)
{
put(key2, orphanKey(key1));
}
// ---------------------------------------------------------------------------
// RefHashTableOf: Getters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
inline TVal* RefHashTableOf<TVal, THasher>::get(const void* const key)
{
XMLSize_t hashVal;
RefHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
return findIt ? findIt->fData : 0;
}
template <class TVal, class THasher>
inline const TVal* RefHashTableOf<TVal, THasher>::
get(const void* const key) const
{
XMLSize_t hashVal;
const RefHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
return findIt ? findIt->fData : 0;
}
template <class TVal, class THasher>
inline MemoryManager* RefHashTableOf<TVal, THasher>::getMemoryManager() const
{
return fMemoryManager;
}
template <class TVal, class THasher>
inline XMLSize_t RefHashTableOf<TVal, THasher>::getHashModulus() const
{
return fHashModulus;
}
template <class TVal, class THasher>
inline XMLSize_t RefHashTableOf<TVal, THasher>::getCount() const
{
return fCount;
}
// ---------------------------------------------------------------------------
// RefHashTableOf: Getters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
inline void RefHashTableOf<TVal, THasher>::setAdoptElements(const bool aValue)
{
fAdoptedElems = aValue;
}
// ---------------------------------------------------------------------------
// RefHashTableOf: Putters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
void RefHashTableOf<TVal, THasher>::put(void* key, TVal* const valueToAdopt)
{
// Apply 0.75 load factor to find threshold.
XMLSize_t threshold = fHashModulus * 3 / 4;
// If we've grown too big, expand the table and rehash.
if (fCount >= threshold)
rehash();
// First see if the key exists already
XMLSize_t hashVal;
RefHashTableBucketElem<TVal>* newBucket = findBucketElem(key, hashVal);
//
// If so,then update its value. If not, then we need to add it to
// the right bucket
//
if (newBucket)
{
if (fAdoptedElems)
delete newBucket->fData;
newBucket->fData = valueToAdopt;
newBucket->fKey = key;
}
else
{
newBucket =
new (fMemoryManager->allocate(sizeof(RefHashTableBucketElem<TVal>)))
RefHashTableBucketElem<TVal>(key, valueToAdopt, fBucketList[hashVal]);
fBucketList[hashVal] = newBucket;
fCount++;
}
}
// ---------------------------------------------------------------------------
// RefHashTableOf: Private methods
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
void RefHashTableOf<TVal, THasher>::rehash()
{
const XMLSize_t newMod = (fHashModulus * 2) + 1;
RefHashTableBucketElem<TVal>** newBucketList =
(RefHashTableBucketElem<TVal>**) fMemoryManager->allocate
(
newMod * sizeof(RefHashTableBucketElem<TVal>*)
);
// Make sure the new bucket list is destroyed if an
// exception is thrown.
ArrayJanitor<RefHashTableBucketElem<TVal>*> guard(newBucketList, fMemoryManager);
memset(newBucketList, 0, newMod * sizeof(newBucketList[0]));
// Rehash all existing entries.
for (XMLSize_t index = 0; index < fHashModulus; index++)
{
// Get the bucket list head for this entry
RefHashTableBucketElem<TVal>* curElem = fBucketList[index];
while (curElem)
{
// Save the next element before we detach this one
RefHashTableBucketElem<TVal>* const nextElem = curElem->fNext;
const XMLSize_t hashVal = fHasher.getHashVal(curElem->fKey, newMod);
RefHashTableBucketElem<TVal>* const newHeadElem = newBucketList[hashVal];
// Insert at the start of this bucket's list.
curElem->fNext = newHeadElem;
newBucketList[hashVal] = curElem;
curElem = nextElem;
}
}
RefHashTableBucketElem<TVal>** const oldBucketList = fBucketList;
// Everything is OK at this point, so update the
// member variables.
fBucketList = guard.release();
fHashModulus = newMod;
// Delete the old bucket list.
fMemoryManager->deallocate(oldBucketList);//delete[] oldBucketList;
}
template <class TVal, class THasher>
inline RefHashTableBucketElem<TVal>* RefHashTableOf<TVal, THasher>::
findBucketElem(const void* const key, XMLSize_t& hashVal)
{
// Hash the key
hashVal = fHasher.getHashVal(key, fHashModulus);
// Search that bucket for the key
RefHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
if (fHasher.equals(key, curElem->fKey))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
template <class TVal, class THasher>
inline const RefHashTableBucketElem<TVal>* RefHashTableOf<TVal, THasher>::
findBucketElem(const void* const key, XMLSize_t& hashVal) const
{
// Hash the key
hashVal = fHasher.getHashVal(key, fHashModulus);
// Search that bucket for the key
const RefHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
if (fHasher.equals(key, curElem->fKey))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
// ---------------------------------------------------------------------------
// RefHashTableOfEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal, class THasher> RefHashTableOfEnumerator<TVal, THasher>::
RefHashTableOfEnumerator(RefHashTableOf<TVal, THasher>* const toEnum
, const bool adopt
, MemoryManager* const manager)
: fAdopted(adopt), fCurElem(0), fCurHash((XMLSize_t)-1), fToEnum(toEnum)
, fMemoryManager(manager)
{
if (!toEnum)
ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, fMemoryManager);
//
// Find the next available bucket element in the hash table. If it
// comes back zero, that just means the table is empty.
//
// Note that the -1 in the current hash tells it to start
// from the beginning.
//
findNext();
}
template <class TVal, class THasher>
RefHashTableOfEnumerator<TVal, THasher>::~RefHashTableOfEnumerator()
{
if (fAdopted)
delete fToEnum;
}
template <class TVal, class THasher> RefHashTableOfEnumerator<TVal, THasher>::
RefHashTableOfEnumerator(const RefHashTableOfEnumerator<TVal, THasher>& toCopy) :
XMLEnumerator<TVal>(toCopy)
, XMemory(toCopy)
, fAdopted(toCopy.fAdopted)
, fCurElem(toCopy.fCurElem)
, fCurHash(toCopy.fCurHash)
, fToEnum(toCopy.fToEnum)
, fMemoryManager(toCopy.fMemoryManager)
{
}
// ---------------------------------------------------------------------------
// RefHashTableOfEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
bool RefHashTableOfEnumerator<TVal, THasher>::hasMoreElements() const
{
//
// If our current has is at the max and there are no more elements
// in the current bucket, then no more elements.
//
if (!fCurElem && (fCurHash == fToEnum->fHashModulus))
return false;
return true;
}
template <class TVal, class THasher>
TVal& RefHashTableOfEnumerator<TVal, THasher>::nextElement()
{
// Make sure we have an element to return
if (!hasMoreElements())
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
//
// Save the current element, then move up to the next one for the
// next time around.
//
RefHashTableBucketElem<TVal>* saveElem = fCurElem;
findNext();
return *saveElem->fData;
}
template <class TVal, class THasher>
void* RefHashTableOfEnumerator<TVal, THasher>::nextElementKey()
{
// Make sure we have an element to return
if (!hasMoreElements())
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
//
// Save the current element, then move up to the next one for the
// next time around.
//
RefHashTableBucketElem<TVal>* saveElem = fCurElem;
findNext();
return saveElem->fKey;
}
template <class TVal, class THasher>
void RefHashTableOfEnumerator<TVal, THasher>::Reset()
{
fCurHash = (XMLSize_t)-1;
fCurElem = 0;
findNext();
}
// ---------------------------------------------------------------------------
// RefHashTableOfEnumerator: Private helper methods
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
void RefHashTableOfEnumerator<TVal, THasher>::findNext()
{
//
// If there is a current element, move to its next element. If this
// hits the end of the bucket, the next block will handle the rest.
//
if (fCurElem)
fCurElem = fCurElem->fNext;
//
// If the current element is null, then we have to move up to the
// next hash value. If that is the hash modulus, then we cannot
// go further.
//
if (!fCurElem)
{
fCurHash++;
if (fCurHash == fToEnum->fHashModulus)
return;
// Else find the next non-empty bucket
while (fToEnum->fBucketList[fCurHash]==0)
{
// Bump to the next hash value. If we max out return
fCurHash++;
if (fCurHash == fToEnum->fHashModulus)
return;
}
fCurElem = fToEnum->fBucketList[fCurHash];
}
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,160 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: RefStackOf.c 676911 2008-07-15 13:27:32Z amassari $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/RefStackOf.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// RefStackOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
RefStackOf<TElem>::RefStackOf(const XMLSize_t initElems,
const bool adoptElems,
MemoryManager* const manager) :
fVector(initElems, adoptElems, manager)
{
}
template <class TElem> RefStackOf<TElem>::~RefStackOf()
{
}
// ---------------------------------------------------------------------------
// RefStackOf: Element management methods
// ---------------------------------------------------------------------------
template <class TElem> const TElem* RefStackOf<TElem>::
elementAt(const XMLSize_t index) const
{
if (index >= fVector.size())
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Stack_BadIndex, fVector.getMemoryManager());
return fVector.elementAt(index);
}
template <class TElem> TElem* RefStackOf<TElem>::popAt(const XMLSize_t index)
{
if (index >= fVector.size())
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Stack_BadIndex, fVector.getMemoryManager());
// Orphan off the element from the slot in the vector
return fVector.orphanElementAt(index);
}
template <class TElem> void RefStackOf<TElem>::push(TElem* const toPush)
{
fVector.addElement(toPush);
}
template <class TElem> const TElem* RefStackOf<TElem>::peek() const
{
const XMLSize_t curSize = fVector.size();
if (curSize == 0)
ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::Stack_EmptyStack, fVector.getMemoryManager());
return fVector.elementAt(curSize-1);
}
template <class TElem> TElem* RefStackOf<TElem>::pop()
{
const XMLSize_t curSize = fVector.size();
if (curSize == 0)
ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::Stack_EmptyStack, fVector.getMemoryManager());
// Orphan off the element from the last slot in the vector
return fVector.orphanElementAt(curSize-1);
}
template <class TElem> void RefStackOf<TElem>::removeAllElements()
{
fVector.removeAllElements();
}
// ---------------------------------------------------------------------------
// RefStackOf: Getter methods
// ---------------------------------------------------------------------------
template <class TElem> bool RefStackOf<TElem>::empty()
{
return (fVector.size() == 0);
}
template <class TElem> XMLSize_t RefStackOf<TElem>::curCapacity()
{
return fVector.curCapacity();
}
template <class TElem> XMLSize_t RefStackOf<TElem>::size()
{
return fVector.size();
}
// ---------------------------------------------------------------------------
// RefStackEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem> RefStackEnumerator<TElem>::
RefStackEnumerator( RefStackOf<TElem>* const toEnum
, const bool adopt) :
fAdopted(adopt)
, fCurIndex(0)
, fToEnum(toEnum)
, fVector(&toEnum->fVector)
{
}
template <class TElem> RefStackEnumerator<TElem>::~RefStackEnumerator()
{
if (fAdopted)
delete fToEnum;
}
// ---------------------------------------------------------------------------
// RefStackEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TElem> bool RefStackEnumerator<TElem>::hasMoreElements() const
{
if (fCurIndex >= fVector->size())
return false;
return true;
}
template <class TElem> TElem& RefStackEnumerator<TElem>::nextElement()
{
return *fVector->elementAt(fCurIndex++);
}
template <class TElem> void RefStackEnumerator<TElem>::Reset()
{
fCurIndex = 0;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: RefVectorOf.c 676911 2008-07-15 13:27:32Z amassari $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/RefVectorOf.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// RefVectorOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
RefVectorOf<TElem>::RefVectorOf(const XMLSize_t maxElems,
const bool adoptElems,
MemoryManager* const manager)
: BaseRefVectorOf<TElem>(maxElems, adoptElems, manager)
{
}
template <class TElem> RefVectorOf<TElem>::~RefVectorOf()
{
if (this->fAdoptedElems)
{
for (XMLSize_t index = 0; index < this->fCurCount; index++)
delete this->fElemList[index];
}
this->fMemoryManager->deallocate(this->fElemList);//delete [] this->fElemList;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/TransENameMap.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// ENameMapFor: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TType>
ENameMapFor<TType>::ENameMapFor(const XMLCh* const encodingName) :
ENameMap(encodingName)
{
}
template <class TType> ENameMapFor<TType>::~ENameMapFor()
{
}
// ---------------------------------------------------------------------------
// ENameMapFor: Implementation of virtual factory method
// ---------------------------------------------------------------------------
template <class TType> XMLTranscoder*
ENameMapFor<TType>::makeNew(const XMLSize_t blockSize,
MemoryManager* const manager) const
{
return new (manager) TType(getKey(), blockSize, manager);
}
// ---------------------------------------------------------------------------
// ENameMapFor: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TType> EEndianNameMapFor<TType>::EEndianNameMapFor(const XMLCh* const encodingName, const bool swapped) :
ENameMap(encodingName)
, fSwapped(swapped)
{
}
template <class TType> EEndianNameMapFor<TType>::~EEndianNameMapFor()
{
}
// ---------------------------------------------------------------------------
// ENameMapFor: Implementation of virtual factory method
// ---------------------------------------------------------------------------
template <class TType> XMLTranscoder*
EEndianNameMapFor<TType>::makeNew(const XMLSize_t blockSize,
MemoryManager* const manager) const
{
return new (manager) TType(getKey(), blockSize, fSwapped, manager);
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,252 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: ValueArrayOf.c 932887 2010-04-11 13:04:59Z borisk $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/ValueArrayOf.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// ValueArrayOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
ValueArrayOf<TElem>::ValueArrayOf(const XMLSize_t size,
MemoryManager* const manager) :
fSize(size)
, fArray(0)
, fMemoryManager(manager)
{
fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize];
}
template <class TElem>
ValueArrayOf<TElem>::ValueArrayOf( const TElem* values
, const XMLSize_t size
, MemoryManager* const manager) :
fSize(size)
, fArray(0)
, fMemoryManager(manager)
{
fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize];
for (XMLSize_t index = 0; index < fSize; index++)
fArray[index] = values[index];
}
template <class TElem>
ValueArrayOf<TElem>::ValueArrayOf(const ValueArrayOf<TElem>& source) :
XMemory(source)
, fSize(source.fSize)
, fArray(0)
, fMemoryManager(source.fMemoryManager)
{
fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize];
for (XMLSize_t index = 0; index < fSize; index++)
fArray[index] = source.fArray[index];
}
template <class TElem> ValueArrayOf<TElem>::~ValueArrayOf()
{
fMemoryManager->deallocate(fArray); //delete [] fArray;
}
// ---------------------------------------------------------------------------
// ValueArrayOf: Public operators
// ---------------------------------------------------------------------------
template <class TElem> TElem& ValueArrayOf<TElem>::
operator[](const XMLSize_t index)
{
if (index >= fSize)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
return fArray[index];
}
template <class TElem> const TElem& ValueArrayOf<TElem>::
operator[](const XMLSize_t index) const
{
if (index >= fSize)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
return fArray[index];
}
template <class TElem> ValueArrayOf<TElem>& ValueArrayOf<TElem>::
operator=(const ValueArrayOf<TElem>& toAssign)
{
if (this == &toAssign)
return *this;
// Reallocate if not the same size
if (toAssign.fSize != fSize)
{
fMemoryManager->deallocate(fArray); //delete [] fArray;
fSize = toAssign.fSize;
fArray = (TElem*) fMemoryManager->allocate(fSize * sizeof(TElem)); //new TElem[fSize];
}
// Copy over the source elements
for (XMLSize_t index = 0; index < fSize; index++)
fArray[index] = toAssign.fArray[index];
return *this;
}
template <class TElem> bool ValueArrayOf<TElem>::
operator==(const ValueArrayOf<TElem>& toCompare) const
{
if (this == &toCompare)
return true;
if (fSize != toCompare.fSize)
return false;
for (XMLSize_t index = 0; index < fSize; index++)
{
if (fArray[index] != toCompare.fArray[index])
return false;
}
return true;
}
template <class TElem> bool ValueArrayOf<TElem>::
operator!=(const ValueArrayOf<TElem>& toCompare) const
{
return !operator==(toCompare);
}
// ---------------------------------------------------------------------------
// ValueArrayOf: Copy operations
// ---------------------------------------------------------------------------
template <class TElem> XMLSize_t ValueArrayOf<TElem>::
copyFrom(const ValueArrayOf<TElem>& srcArray)
{
//
// Copy over as many of the source elements as will fit into
// this array.
//
const XMLSize_t count = fSize < srcArray.fSize ?
fSize : srcArray.fSize;
for (XMLSize_t index = 0; index < count; index++)
fArray[index] = srcArray.fArray[index];
return count;
}
// ---------------------------------------------------------------------------
// ValueArrayOf: Getter methods
// ---------------------------------------------------------------------------
template <class TElem> XMLSize_t ValueArrayOf<TElem>::
length() const
{
return fSize;
}
template <class TElem> TElem* ValueArrayOf<TElem>::
rawData() const
{
return fArray;
}
// ---------------------------------------------------------------------------
// ValueArrayOf: Miscellaneous methods
// ---------------------------------------------------------------------------
template <class TElem> void ValueArrayOf<TElem>::
resize(const XMLSize_t newSize)
{
if (newSize == fSize)
return;
if (newSize < fSize)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Array_BadNewSize, fMemoryManager);
// Allocate the new array
TElem* newArray = (TElem*) fMemoryManager->allocate
(
newSize * sizeof(TElem)
); //new TElem[newSize];
// Copy the existing values
XMLSize_t index = 0;
for (; index < fSize; index++)
newArray[index] = fArray[index];
for (; index < newSize; index++)
newArray[index] = TElem(0);
// Delete the old array and update our members
fMemoryManager->deallocate(fArray); //delete [] fArray;
fArray = newArray;
fSize = newSize;
}
// ---------------------------------------------------------------------------
// ValueArrayEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem> ValueArrayEnumerator<TElem>::
ValueArrayEnumerator(ValueArrayOf<TElem>* const toEnum, const bool adopt) :
fAdopted(adopt)
, fCurIndex(0)
, fToEnum(toEnum)
{
}
template <class TElem> ValueArrayEnumerator<TElem>::~ValueArrayEnumerator()
{
if (fAdopted)
delete fToEnum;
}
// ---------------------------------------------------------------------------
// ValueArrayEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TElem> bool ValueArrayEnumerator<TElem>::hasMoreElements() const
{
if (fCurIndex >= fToEnum->length())
return false;
return true;
}
template <class TElem> TElem& ValueArrayEnumerator<TElem>::nextElement()
{
return (*fToEnum)[fCurIndex++];
}
template <class TElem> void ValueArrayEnumerator<TElem>::Reset()
{
fCurIndex = 0;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,489 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: ValueHashTableOf.c 679340 2008-07-24 10:28:29Z borisk $
*/
// ---------------------------------------------------------------------------
// Include
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/ValueHashTableOf.hpp>
#endif
#include <xercesc/util/NullPointerException.hpp>
#include <xercesc/util/Janitor.hpp>
#include <assert.h>
#include <new>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// ValueHashTableOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
ValueHashTableOf<TVal, THasher>::ValueHashTableOf( const XMLSize_t modulus
, const THasher& hasher
, MemoryManager* const manager)
: fMemoryManager(manager)
, fBucketList(0)
, fHashModulus(modulus)
, fInitialModulus(modulus)
, fCount(0)
, fHasher(hasher)
{
initialize(modulus);
}
template <class TVal, class THasher>
ValueHashTableOf<TVal, THasher>::ValueHashTableOf( const XMLSize_t modulus
, MemoryManager* const manager)
: fMemoryManager(manager)
, fBucketList(0)
, fHashModulus(modulus)
, fInitialModulus(modulus)
, fCount(0)
, fHasher()
{
initialize(modulus);
}
template <class TVal, class THasher>
void ValueHashTableOf<TVal, THasher>::initialize(const XMLSize_t modulus)
{
if (modulus == 0)
ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::HshTbl_ZeroModulus, fMemoryManager);
// Allocate the bucket list and zero them
fBucketList = (ValueHashTableBucketElem<TVal>**) fMemoryManager->allocate
(
fHashModulus * sizeof(ValueHashTableBucketElem<TVal>*)
); //new ValueHashTableBucketElem<TVal>*[fHashModulus];
memset(fBucketList, 0, sizeof(fBucketList[0]) * fHashModulus);
}
template <class TVal, class THasher>
ValueHashTableOf<TVal, THasher>::~ValueHashTableOf()
{
removeAll();
// Then delete the bucket list & hasher
fMemoryManager->deallocate(fBucketList); //delete [] fBucketList;
}
// ---------------------------------------------------------------------------
// ValueHashTableOf: Element management
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
bool ValueHashTableOf<TVal, THasher>::isEmpty() const
{
return fCount==0;
}
template <class TVal, class THasher>
bool ValueHashTableOf<TVal, THasher>::
containsKey(const void* const key) const
{
XMLSize_t hashVal;
const ValueHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
return (findIt != 0);
}
template <class TVal, class THasher>
void ValueHashTableOf<TVal, THasher>::
removeKey(const void* const key)
{
XMLSize_t hashVal;
removeBucketElem(key, hashVal);
}
template <class TVal, class THasher>
void ValueHashTableOf<TVal, THasher>::removeAll()
{
if(isEmpty())
return;
// Clean up the buckets first
for (XMLSize_t buckInd = 0; buckInd < fHashModulus; buckInd++)
{
// Get the bucket list head for this entry
ValueHashTableBucketElem<TVal>* curElem = fBucketList[buckInd];
ValueHashTableBucketElem<TVal>* nextElem;
while (curElem)
{
// Save the next element before we hose this one
nextElem = curElem->fNext;
// delete the current element and move forward
// destructor is empty...
// curElem->~ValueHashTableBucketElem();
fMemoryManager->deallocate(curElem);
curElem = nextElem;
}
// Clean out this entry
fBucketList[buckInd] = 0;
}
fCount = 0;
}
// ---------------------------------------------------------------------------
// ValueHashTableOf: Getters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
TVal& ValueHashTableOf<TVal, THasher>::get(const void* const key, MemoryManager* const manager)
{
XMLSize_t hashVal;
ValueHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
if (!findIt)
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists, manager);
return findIt->fData;
}
template <class TVal, class THasher>
const TVal& ValueHashTableOf<TVal, THasher>::
get(const void* const key) const
{
XMLSize_t hashVal;
const ValueHashTableBucketElem<TVal>* findIt = findBucketElem(key, hashVal);
if (!findIt)
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists, fMemoryManager);
return findIt->fData;
}
// ---------------------------------------------------------------------------
// ValueHashTableOf: Putters
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
void ValueHashTableOf<TVal, THasher>::put(void* key, const TVal& valueToAdopt)
{
// Apply 0.75 load factor to find threshold.
XMLSize_t threshold = fHashModulus * 3 / 4;
// If we've grown too big, expand the table and rehash.
if (fCount >= threshold)
rehash();
// First see if the key exists already
XMLSize_t hashVal;
ValueHashTableBucketElem<TVal>* newBucket = findBucketElem(key, hashVal);
//
// If so,then update its value. If not, then we need to add it to
// the right bucket
//
if (newBucket)
{
newBucket->fData = valueToAdopt;
newBucket->fKey = key;
}
else
{
newBucket =
new (fMemoryManager->allocate(sizeof(ValueHashTableBucketElem<TVal>)))
ValueHashTableBucketElem<TVal>(key, valueToAdopt, fBucketList[hashVal]);
fBucketList[hashVal] = newBucket;
fCount++;
}
}
// ---------------------------------------------------------------------------
// ValueHashTableOf: Private methods
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
void ValueHashTableOf<TVal, THasher>::rehash()
{
const XMLSize_t newMod = (fHashModulus * 2) + 1;
ValueHashTableBucketElem<TVal>** newBucketList =
(ValueHashTableBucketElem<TVal>**) fMemoryManager->allocate
(
newMod * sizeof(ValueHashTableBucketElem<TVal>*)
);//new RefHashTableBucketElem<TVal>*[newMod];
// Make sure the new bucket list is destroyed if an
// exception is thrown.
ArrayJanitor<ValueHashTableBucketElem<TVal>*> guard(newBucketList, fMemoryManager);
memset(newBucketList, 0, newMod * sizeof(newBucketList[0]));
// Rehash all existing entries.
for (XMLSize_t index = 0; index < fHashModulus; index++)
{
// Get the bucket list head for this entry
ValueHashTableBucketElem<TVal>* curElem = fBucketList[index];
while (curElem)
{
// Save the next element before we detach this one
ValueHashTableBucketElem<TVal>* const nextElem = curElem->fNext;
const XMLSize_t hashVal = fHasher.getHashVal(curElem->fKey, newMod);
assert(hashVal < newMod);
ValueHashTableBucketElem<TVal>* const newHeadElem = newBucketList[hashVal];
// Insert at the start of this bucket's list.
curElem->fNext = newHeadElem;
newBucketList[hashVal] = curElem;
curElem = nextElem;
}
}
ValueHashTableBucketElem<TVal>** const oldBucketList = fBucketList;
// Everything is OK at this point, so update the
// member variables.
fBucketList = guard.release();
fHashModulus = newMod;
// Delete the old bucket list.
fMemoryManager->deallocate(oldBucketList);//delete[] oldBucketList;
}
template <class TVal, class THasher>
inline ValueHashTableBucketElem<TVal>* ValueHashTableOf<TVal, THasher>::
findBucketElem(const void* const key, XMLSize_t& hashVal)
{
// Hash the key
hashVal = fHasher.getHashVal(key, fHashModulus);
assert(hashVal < fHashModulus);
// Search that bucket for the key
ValueHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
if (fHasher.equals(key, curElem->fKey))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
template <class TVal, class THasher>
inline const ValueHashTableBucketElem<TVal>* ValueHashTableOf<TVal, THasher>::
findBucketElem(const void* const key, XMLSize_t& hashVal) const
{
// Hash the key
hashVal = fHasher.getHashVal(key, fHashModulus);
assert(hashVal < fHashModulus);
// Search that bucket for the key
const ValueHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
while (curElem)
{
if (fHasher.equals(key, curElem->fKey))
return curElem;
curElem = curElem->fNext;
}
return 0;
}
template <class TVal, class THasher>
void ValueHashTableOf<TVal, THasher>::
removeBucketElem(const void* const key, XMLSize_t& hashVal)
{
// Hash the key
hashVal = fHasher.getHashVal(key, fHashModulus);
assert(hashVal < fHashModulus);
//
// Search the given bucket for this key. Keep up with the previous
// element so we can patch around it.
//
ValueHashTableBucketElem<TVal>* curElem = fBucketList[hashVal];
ValueHashTableBucketElem<TVal>* lastElem = 0;
while (curElem)
{
if (fHasher.equals(key, curElem->fKey))
{
if (!lastElem)
{
// It was the first in the bucket
fBucketList[hashVal] = curElem->fNext;
}
else
{
// Patch around the current element
lastElem->fNext = curElem->fNext;
}
// Delete the current element
// delete curElem;
// destructor is empty...
// curElem->~ValueHashTableBucketElem();
fMemoryManager->deallocate(curElem);
fCount--;
return;
}
// Move both pointers upwards
lastElem = curElem;
curElem = curElem->fNext;
}
// We never found that key
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists, fMemoryManager);
}
// ---------------------------------------------------------------------------
// ValueHashTableOfEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
ValueHashTableOfEnumerator<TVal, THasher>::
ValueHashTableOfEnumerator(ValueHashTableOf<TVal, THasher>* const toEnum
, const bool adopt
, MemoryManager* const manager)
: fAdopted(adopt), fCurElem(0), fCurHash((XMLSize_t)-1), fToEnum(toEnum), fMemoryManager(manager)
{
if (!toEnum)
ThrowXMLwithMemMgr(NullPointerException, XMLExcepts::CPtr_PointerIsZero, manager);
//
// Find the next available bucket element in the hash table. If it
// comes back zero, that just means the table is empty.
//
// Note that the -1 in the current hash tells it to start from the
// beginning.
//
findNext();
}
template <class TVal, class THasher>
ValueHashTableOfEnumerator<TVal, THasher>::~ValueHashTableOfEnumerator()
{
if (fAdopted)
delete fToEnum;
}
// ---------------------------------------------------------------------------
// ValueHashTableOfEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
bool ValueHashTableOfEnumerator<TVal, THasher>::hasMoreElements() const
{
//
// If our current has is at the max and there are no more elements
// in the current bucket, then no more elements.
//
if (!fCurElem && (fCurHash == fToEnum->fHashModulus))
return false;
return true;
}
template <class TVal, class THasher>
TVal& ValueHashTableOfEnumerator<TVal, THasher>::nextElement()
{
// Make sure we have an element to return
if (!hasMoreElements())
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
//
// Save the current element, then move up to the next one for the
// next time around.
//
ValueHashTableBucketElem<TVal>* saveElem = fCurElem;
findNext();
return saveElem->fData;
}
template <class TVal, class THasher>
void* ValueHashTableOfEnumerator<TVal, THasher>::nextElementKey()
{
// Make sure we have an element to return
if (!hasMoreElements())
ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::Enum_NoMoreElements, fMemoryManager);
//
// Save the current element, then move up to the next one for the
// next time around.
//
ValueHashTableBucketElem<TVal>* saveElem = fCurElem;
findNext();
return saveElem->fKey;
}
template <class TVal, class THasher>
void ValueHashTableOfEnumerator<TVal, THasher>::Reset()
{
fCurHash = (XMLSize_t)-1;
fCurElem = 0;
findNext();
}
// ---------------------------------------------------------------------------
// ValueHashTableOfEnumerator: Private helper methods
// ---------------------------------------------------------------------------
template <class TVal, class THasher>
void ValueHashTableOfEnumerator<TVal, THasher>::findNext()
{
//
// If there is a current element, move to its next element. If this
// hits the end of the bucket, the next block will handle the rest.
//
if (fCurElem)
fCurElem = fCurElem->fNext;
//
// If the current element is null, then we have to move up to the
// next hash value. If that is the hash modulus, then we cannot
// go further.
//
if (!fCurElem)
{
if (++fCurHash == fToEnum->fHashModulus)
return;
// Else find the next non-empty bucket
while (fToEnum->fBucketList[fCurHash]==0)
{
// Bump to the next hash value. If we max out return
if (++fCurHash == fToEnum->fHashModulus)
return;
}
fCurElem = fToEnum->fBucketList[fCurHash];
}
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,146 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: ValueStackOf.c 676911 2008-07-15 13:27:32Z amassari $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/ValueStackOf.hpp>
#endif
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// ValueStackOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
ValueStackOf<TElem>::ValueStackOf(const XMLSize_t fInitCapacity,
MemoryManager* const manager,
const bool toCallDestructor) :
fVector(fInitCapacity, manager, toCallDestructor)
{
}
template <class TElem> ValueStackOf<TElem>::~ValueStackOf()
{
}
// ---------------------------------------------------------------------------
// ValueStackOf: Element management methods
// ---------------------------------------------------------------------------
template <class TElem> void ValueStackOf<TElem>::push(const TElem& toPush)
{
fVector.addElement(toPush);
}
template <class TElem> const TElem& ValueStackOf<TElem>::peek() const
{
const XMLSize_t curSize = fVector.size();
if (curSize == 0)
ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::Stack_EmptyStack, fVector.getMemoryManager());
return fVector.elementAt(curSize-1);
}
template <class TElem> TElem ValueStackOf<TElem>::pop()
{
const XMLSize_t curSize = fVector.size();
if (curSize == 0)
ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::Stack_EmptyStack, fVector.getMemoryManager());
TElem retVal = fVector.elementAt(curSize-1);
fVector.removeElementAt(curSize-1);
return retVal;
}
template <class TElem> void ValueStackOf<TElem>::removeAllElements()
{
fVector.removeAllElements();
}
// ---------------------------------------------------------------------------
// ValueStackOf: Getter methods
// ---------------------------------------------------------------------------
template <class TElem> bool ValueStackOf<TElem>::empty()
{
return (fVector.size() == 0);
}
template <class TElem> XMLSize_t ValueStackOf<TElem>::curCapacity()
{
return fVector.curCapacity();
}
template <class TElem> XMLSize_t ValueStackOf<TElem>::size()
{
return fVector.size();
}
// ---------------------------------------------------------------------------
// ValueStackEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem> ValueStackEnumerator<TElem>::
ValueStackEnumerator( ValueStackOf<TElem>* const toEnum
, const bool adopt) :
fAdopted(adopt)
, fCurIndex(0)
, fToEnum(toEnum)
, fVector(&toEnum->fVector)
{
}
template <class TElem> ValueStackEnumerator<TElem>::~ValueStackEnumerator()
{
if (fAdopted)
delete fToEnum;
}
// ---------------------------------------------------------------------------
// ValueStackEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TElem> bool ValueStackEnumerator<TElem>::hasMoreElements() const
{
if (fCurIndex >= fVector->size())
return false;
return true;
}
template <class TElem> TElem& ValueStackEnumerator<TElem>::nextElement()
{
return fVector->elementAt(fCurIndex++);
}
template <class TElem> void ValueStackEnumerator<TElem>::Reset()
{
fCurIndex = 0;
}
XERCES_CPP_NAMESPACE_END

View File

@@ -0,0 +1,295 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: ValueVectorOf.c 676911 2008-07-15 13:27:32Z amassari $
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#if defined(XERCES_TMPLSINC)
#include <xercesc/util/ValueVectorOf.hpp>
#endif
#include <string.h>
XERCES_CPP_NAMESPACE_BEGIN
// ---------------------------------------------------------------------------
// ValueVectorOf: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem>
ValueVectorOf<TElem>::ValueVectorOf(const XMLSize_t maxElems,
MemoryManager* const manager,
const bool toCallDestructor) :
fCallDestructor(toCallDestructor)
, fCurCount(0)
, fMaxCount(maxElems)
, fElemList(0)
, fMemoryManager(manager)
{
fElemList = (TElem*) fMemoryManager->allocate
(
fMaxCount * sizeof(TElem)
); //new TElem[fMaxCount];
memset(fElemList, 0, fMaxCount * sizeof(TElem));
}
template <class TElem>
ValueVectorOf<TElem>::ValueVectorOf(const ValueVectorOf<TElem>& toCopy) :
XMemory(toCopy)
, fCallDestructor(toCopy.fCallDestructor)
, fCurCount(toCopy.fCurCount)
, fMaxCount(toCopy.fMaxCount)
, fElemList(0)
, fMemoryManager(toCopy.fMemoryManager)
{
fElemList = (TElem*) fMemoryManager->allocate
(
fMaxCount * sizeof(TElem)
); //new TElem[fMaxCount];
memset(fElemList, 0, fMaxCount * sizeof(TElem));
for (XMLSize_t index = 0; index < fCurCount; index++)
fElemList[index] = toCopy.fElemList[index];
}
template <class TElem> ValueVectorOf<TElem>::~ValueVectorOf()
{
if (fCallDestructor) {
for (XMLSize_t index=fMaxCount; index > 0; index--)
fElemList[index-1].~TElem();
}
fMemoryManager->deallocate(fElemList); //delete [] fElemList;
}
// ---------------------------------------------------------------------------
// ValueVectorOf: Operators
// ---------------------------------------------------------------------------
template <class TElem> ValueVectorOf<TElem>&
ValueVectorOf<TElem>::operator=(const ValueVectorOf<TElem>& toAssign)
{
if (this == &toAssign)
return *this;
// Reallocate if required
if (fMaxCount < toAssign.fCurCount)
{
fMemoryManager->deallocate(fElemList); //delete [] fElemList;
fElemList = (TElem*) fMemoryManager->allocate
(
toAssign.fMaxCount * sizeof(TElem)
); //new TElem[toAssign.fMaxCount];
fMaxCount = toAssign.fMaxCount;
}
fCurCount = toAssign.fCurCount;
for (XMLSize_t index = 0; index < fCurCount; index++)
fElemList[index] = toAssign.fElemList[index];
return *this;
}
// ---------------------------------------------------------------------------
// ValueVectorOf: Element management
// ---------------------------------------------------------------------------
template <class TElem> void ValueVectorOf<TElem>::addElement(const TElem& toAdd)
{
ensureExtraCapacity(1);
fElemList[fCurCount++] = toAdd;
}
template <class TElem> void ValueVectorOf<TElem>::
setElementAt(const TElem& toSet, const XMLSize_t setAt)
{
if (setAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
fElemList[setAt] = toSet;
}
template <class TElem> void ValueVectorOf<TElem>::
insertElementAt(const TElem& toInsert, const XMLSize_t insertAt)
{
if (insertAt == fCurCount)
{
addElement(toInsert);
return;
}
if (insertAt > fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
// Make room for the newbie
ensureExtraCapacity(1);
for (XMLSize_t index = fCurCount; index > insertAt; index--)
fElemList[index] = fElemList[index-1];
// And stick it in and bump the count
fElemList[insertAt] = toInsert;
fCurCount++;
}
template <class TElem> void ValueVectorOf<TElem>::
removeElementAt(const XMLSize_t removeAt)
{
if (removeAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
// Copy down every element above remove point
for (XMLSize_t index = removeAt; index < fCurCount-1; index++)
fElemList[index] = fElemList[index+1];
// And bump down count
fCurCount--;
}
template <class TElem> void ValueVectorOf<TElem>::removeAllElements()
{
fCurCount = 0;
}
template <class TElem>
bool ValueVectorOf<TElem>::containsElement(const TElem& toCheck,
const XMLSize_t startIndex) {
for (XMLSize_t i = startIndex; i < fCurCount; i++) {
if (fElemList[i] == toCheck) {
return true;
}
}
return false;
}
// ---------------------------------------------------------------------------
// ValueVectorOf: Getter methods
// ---------------------------------------------------------------------------
template <class TElem> const TElem& ValueVectorOf<TElem>::
elementAt(const XMLSize_t getAt) const
{
if (getAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
return fElemList[getAt];
}
template <class TElem> TElem& ValueVectorOf<TElem>::
elementAt(const XMLSize_t getAt)
{
if (getAt >= fCurCount)
ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Vector_BadIndex, fMemoryManager);
return fElemList[getAt];
}
template <class TElem> XMLSize_t ValueVectorOf<TElem>::curCapacity() const
{
return fMaxCount;
}
template <class TElem> XMLSize_t ValueVectorOf<TElem>::size() const
{
return fCurCount;
}
template <class TElem>
MemoryManager* ValueVectorOf<TElem>::getMemoryManager() const
{
return fMemoryManager;
}
// ---------------------------------------------------------------------------
// ValueVectorOf: Miscellaneous
// ---------------------------------------------------------------------------
template <class TElem> void ValueVectorOf<TElem>::
ensureExtraCapacity(const XMLSize_t length)
{
XMLSize_t newMax = fCurCount + length;
if (newMax > fMaxCount)
{
// Avoid too many reallocations by expanding by a percentage
XMLSize_t minNewMax = (XMLSize_t)((double)fCurCount * 1.25);
if (newMax < minNewMax)
newMax = minNewMax;
TElem* newList = (TElem*) fMemoryManager->allocate
(
newMax * sizeof(TElem)
); //new TElem[newMax];
for (XMLSize_t index = 0; index < fCurCount; index++)
newList[index] = fElemList[index];
fMemoryManager->deallocate(fElemList); //delete [] fElemList;
fElemList = newList;
fMaxCount = newMax;
}
}
template <class TElem> const TElem* ValueVectorOf<TElem>::rawData() const
{
return fElemList;
}
// ---------------------------------------------------------------------------
// ValueVectorEnumerator: Constructors and Destructor
// ---------------------------------------------------------------------------
template <class TElem> ValueVectorEnumerator<TElem>::
ValueVectorEnumerator( ValueVectorOf<TElem>* const toEnum
, const bool adopt) :
fAdopted(adopt)
, fCurIndex(0)
, fToEnum(toEnum)
{
}
template <class TElem> ValueVectorEnumerator<TElem>::~ValueVectorEnumerator()
{
if (fAdopted)
delete fToEnum;
}
// ---------------------------------------------------------------------------
// ValueVectorEnumerator: Enum interface
// ---------------------------------------------------------------------------
template <class TElem> bool
ValueVectorEnumerator<TElem>::hasMoreElements() const
{
if (fCurIndex >= fToEnum->size())
return false;
return true;
}
template <class TElem> TElem& ValueVectorEnumerator<TElem>::nextElement()
{
return fToEnum->elementAt(fCurIndex++);
}
template <class TElem> void ValueVectorEnumerator<TElem>::Reset()
{
fCurIndex = 0;
}
XERCES_CPP_NAMESPACE_END