You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by kn...@apache.org on 2002/07/11 20:49:53 UTC

cvs commit: xml-xerces/c/src/xercesc/util RefHashTableOf.hpp RefHashTableOf.c

knoaman     2002/07/11 11:49:53

  Modified:    c/src/xercesc/util RefHashTableOf.hpp RefHashTableOf.c
  Log:
  Add setAdoptElements method.
  Rename removeBucketElemSafe to orphanKey.
  
  Revision  Changes    Path
  1.4       +20 -12    xml-xerces/c/src/xercesc/util/RefHashTableOf.hpp
  
  Index: RefHashTableOf.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/xercesc/util/RefHashTableOf.hpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RefHashTableOf.hpp	4 Jul 2002 15:24:57 -0000	1.3
  +++ RefHashTableOf.hpp	11 Jul 2002 18:49:53 -0000	1.4
  @@ -56,6 +56,10 @@
   
   /*
    * $Log$
  + * Revision 1.4  2002/07/11 18:49:53  knoaman
  + * Add setAdoptElements method.
  + * Rename removeBucketElemSafe to orphanKey.
  + *
    * Revision 1.3  2002/07/04 15:24:57  tng
    * DOM L3: add transferElement and removeBucketElemSafe for use in DOMDocument::renameNode.
    *
  @@ -143,7 +147,7 @@
   
       TVal*                           fData;
       RefHashTableBucketElem<TVal>*   fNext;
  -    void*                           fKey;
  +	void*							fKey;
   };
   
   
  @@ -153,13 +157,13 @@
       // -----------------------------------------------------------------------
       //  Constructors and Destructor
       // -----------------------------------------------------------------------
  -    // backwards compatability - default hasher is HashXMLCh
  +	// backwards compatability - default hasher is HashXMLCh
       RefHashTableOf(const unsigned int modulus);
  -    // backwards compatability - default hasher is HashXMLCh
  +	// backwards compatability - default hasher is HashXMLCh
       RefHashTableOf(const unsigned int modulus, const bool adoptElems);
  -    // if a hash function is passed in, it will be deleted when the hashtable is deleted.
  -    // use a new instance of the hasher class for each hashtable, otherwise one hashtable
  -    // may delete the hasher of a different hashtable if both use the same hasher.
  +	// if a hash function is passed in, it will be deleted when the hashtable is deleted.
  +	// use a new instance of the hasher class for each hashtable, otherwise one hashtable
  +	// may delete the hasher of a different hashtable if both use the same hasher.
       RefHashTableOf(const unsigned int modulus, const bool adoptElems, HashBase* hashBase);
       ~RefHashTableOf();
   
  @@ -174,6 +178,7 @@
       void cleanup();
       void reinitialize(HashBase* hashBase);
       void transferElement(const void* const key1, void* key2);
  +    TVal* orphanKey(const void* const key);
   
       // -----------------------------------------------------------------------
       //  Getters
  @@ -181,11 +186,16 @@
       TVal* get(const void* const key);
       const TVal* get(const void* const key) const;
   
  +    // -----------------------------------------------------------------------
  +    //  Setters
  +    // -----------------------------------------------------------------------
  +    void setAdoptElements(const bool aValue);
  +
   
       // -----------------------------------------------------------------------
       //  Putters
       // -----------------------------------------------------------------------
  -    void put(void* key, TVal* const valueToAdopt);
  +	void put(void* key, TVal* const valueToAdopt);
   
   
   private :
  @@ -202,7 +212,6 @@
       RefHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal);
       const RefHashTableBucketElem<TVal>* findBucketElem(const void* const key, unsigned int& hashVal) const;
       void removeBucketElem(const void* const key, unsigned int& hashVal);
  -    void removeBucketElemSafe(const void* const key, unsigned int& hashVal);
       void initialize(const unsigned int modulus);
   
   
  @@ -221,14 +230,14 @@
       //  fHashModulus
       //      The modulus used for this hash table, to hash the keys. This is
       //      also the number of elements in the bucket list.
  -    //
  -    //  fHash
  -    //      The hasher for the key data type.
  +	//
  +	//  fHash
  +	//      The hasher for the key data type.
       // -----------------------------------------------------------------------
       bool                                fAdoptedElems;
       RefHashTableBucketElem<TVal>**      fBucketList;
       unsigned int                        fHashModulus;
  -    HashBase*                           fHash;
  +	HashBase*							fHash;
   };
   
   
  
  
  
  1.5       +67 -59    xml-xerces/c/src/xercesc/util/RefHashTableOf.c
  
  Index: RefHashTableOf.c
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/xercesc/util/RefHashTableOf.c,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RefHashTableOf.c	5 Jul 2002 11:31:04 -0000	1.4
  +++ RefHashTableOf.c	11 Jul 2002 18:49:53 -0000	1.5
  @@ -56,6 +56,10 @@
   
   /**
    * $Log$
  + * Revision 1.5  2002/07/11 18:49:53  knoaman
  + * Add setAdoptElements method.
  + * Rename removeBucketElemSafe to orphanKey.
  + *
    * Revision 1.4  2002/07/05 11:31:04  tng
    * Fix typo.
    *
  @@ -226,6 +230,60 @@
       }
   }
   
  +// 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> TVal* RefHashTableOf<TVal>::
  +orphanKey(const void* const key)
  +{
  +    // Hash the key
  +    TVal* retVal = 0;
  +    unsigned int hashVal = fHash->getHashVal(key, fHashModulus);
  +    if (hashVal > fHashModulus)
  +        ThrowXML(RuntimeException, XMLExcepts::HshTbl_BadHashFromKey);
  +
  +    //
  +    //  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 (fHash->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;
  +            break;
  +        }
  +
  +        // Move both pointers upwards
  +        lastElem = curElem;
  +        curElem = curElem->fNext;
  +    }
  +
  +    // We never found that key
  +    if (!retVal)
  +        ThrowXML(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists);
  +
  +    return retVal;
  +}
  +
   //
   // cleanup():
   //   similar to destructor
  @@ -270,12 +328,7 @@
   // whatever key2 has originally will be purged (if adopted)
   template <class TElem> void RefHashTableOf<TElem>::transferElement(const void* const key1, void* key2)
   {
  -    TElem* data = get(key1);
  -
  -    unsigned int hashVal;
  -    removeBucketElemSafe(key1, hashVal);
  -
  -    put(key2, data);
  +    put(key2, orphanKey(key1));
   }
   
   
  @@ -303,6 +356,15 @@
   
   
   // ---------------------------------------------------------------------------
  +//  RefHashTableOf: Getters
  +// ---------------------------------------------------------------------------
  +template <class TVal>
  +void RefHashTableOf<TVal>::setAdoptElements(const bool aValue)
  +{
  +    fAdoptedElems = aValue;
  +}
  +
  +// ---------------------------------------------------------------------------
   //  RefHashTableOf: Putters
   // ---------------------------------------------------------------------------
   template <class TVal> void RefHashTableOf<TVal>::put(void* key, TVal* const valueToAdopt)
  @@ -424,59 +486,6 @@
       ThrowXML(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists);
   }
   
  -
  -// This is same as removeBucketElem except that it does not delete the data
  -//   even it is adopted
  -// This function is called by transferElement so that the undeleted data can be transferred
  -//   the key2 will own this data.
  -template <class TVal> void RefHashTableOf<TVal>::
  -removeBucketElemSafe(const void* const key, unsigned int& hashVal)
  -{
  -    // Hash the key
  -    hashVal = fHash->getHashVal(key, fHashModulus);
  -    if (hashVal > fHashModulus)
  -        ThrowXML(RuntimeException, XMLExcepts::HshTbl_BadHashFromKey);
  -
  -    //
  -    //  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 (fHash->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;
  -            }
  -
  -            // even if we adopted the elements, do not delete the data
  -            // if (fAdoptedElems)
  -            //    delete curElem->fData;
  -
  -            // Delete the current element
  -            delete curElem;
  -
  -            return;
  -        }
  -
  -        // Move both pointers upwards
  -        lastElem = curElem;
  -        curElem = curElem->fNext;
  -    }
  -
  -    // We never found that key
  -    ThrowXML(NoSuchElementException, XMLExcepts::HshTbl_NoSuchKeyExists);
  -}
   
   
   // ---------------------------------------------------------------------------
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org