Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/xercesc/util/StringPool.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  * 
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  * 
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_STRINGPOOL_HPP)
0023 #define XERCESC_INCLUDE_GUARD_STRINGPOOL_HPP
0024 
0025 #include <xercesc/util/RefHashTableOf.hpp>
0026 #include <xercesc/internal/XSerializable.hpp>
0027 
0028 XERCES_CPP_NAMESPACE_BEGIN
0029 
0030 //
0031 //  This class implements a string pool, in which strings can be added and
0032 //  given a unique id by which they can be referred. It has to provide fast
0033 //  access both mapping from a string to its id and mapping from an id to
0034 //  its string. This requires that it provide two separate data structures.
0035 //  The map one is a hash table for quick storage and look up by name. The
0036 //  other is an array ordered by unique id which maps to the element in the
0037 //  hash table.
0038 //
0039 //  This works because strings cannot be removed from the pool once added,
0040 //  other than flushing it completely, and because ids are assigned
0041 //  sequentially from 1.
0042 //
0043 class XMLUTIL_EXPORT XMLStringPool : public XSerializable, public XMemory
0044 {
0045 public :
0046     // -----------------------------------------------------------------------
0047     //  Constructors and Destructor
0048     // -----------------------------------------------------------------------
0049     XMLStringPool
0050     (
0051           const unsigned int   modulus = 109
0052         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
0053     );
0054     virtual ~XMLStringPool();
0055 
0056 
0057     // -----------------------------------------------------------------------
0058     //  Pool management methods
0059     // -----------------------------------------------------------------------
0060     virtual unsigned int addOrFind(const XMLCh* const newString);
0061     virtual bool exists(const XMLCh* const newString) const;
0062     virtual bool exists(const unsigned int id) const;
0063     virtual void flushAll();
0064     virtual unsigned int getId(const XMLCh* const toFind) const;
0065     virtual const XMLCh* getValueForId(const unsigned int id) const;
0066     virtual unsigned int getStringCount() const;
0067 
0068     /***
0069      * Support for Serialization/De-serialization
0070      ***/
0071     DECL_XSERIALIZABLE(XMLStringPool)
0072 
0073     XMLStringPool(MemoryManager* const manager);
0074 
0075 private :
0076     // -----------------------------------------------------------------------
0077     //  Private data types
0078     // -----------------------------------------------------------------------
0079     struct PoolElem
0080     {
0081         unsigned int  fId;
0082         XMLCh*        fString;
0083     };
0084 
0085     // -----------------------------------------------------------------------
0086     //  Unimplemented constructors and operators
0087     // -----------------------------------------------------------------------
0088     XMLStringPool(const XMLStringPool&);
0089     XMLStringPool& operator=(const XMLStringPool&);
0090 
0091 
0092     // -----------------------------------------------------------------------
0093     //  Private helper methods
0094     // -----------------------------------------------------------------------
0095     unsigned int addNewEntry(const XMLCh* const newString);
0096 
0097 
0098     // -----------------------------------------------------------------------
0099     //  Private data members
0100     //
0101     //  fIdMap
0102     //      This is an array of pointers to the pool elements. It is ordered
0103     //      by unique id, so using an id to index it gives instant access to
0104     //      the string of that id. This is grown as required.
0105     //
0106     //  fHashTable
0107     //      This is the hash table used to store and quickly access the
0108     //      strings.
0109     //
0110     //  fMapCapacity
0111     //      The current capacity of the id map. When the current id hits this
0112     //      value the map must must be expanded.
0113     //
0114     // -----------------------------------------------------------------------
0115     MemoryManager*              fMemoryManager;
0116     PoolElem**                  fIdMap;
0117     RefHashTableOf<PoolElem>*   fHashTable;
0118     unsigned int                fMapCapacity;
0119 
0120 protected:
0121     // protected data members
0122     //  fCurId
0123     //      This is the counter used to assign unique ids. It is just bumped
0124     //      up one for each new string added.
0125     unsigned int                fCurId;
0126 };
0127 
0128 
0129 // Provide inline versions of some of the simple functions to improve performance.
0130 inline unsigned int XMLStringPool::addOrFind(const XMLCh* const newString)
0131 {
0132     PoolElem* elemToFind = fHashTable->get(newString);
0133     if (elemToFind)
0134         return elemToFind->fId;
0135 
0136     return addNewEntry(newString);
0137 }
0138 
0139 inline unsigned int XMLStringPool::getId(const XMLCh* const toFind) const
0140 {
0141     PoolElem* elemToFind = fHashTable->get(toFind);
0142     if (elemToFind)
0143         return elemToFind->fId;
0144 
0145     // Not found, so return zero, which is never a legal id
0146     return 0;
0147 }
0148 
0149 inline bool XMLStringPool::exists(const XMLCh* const newString) const
0150 {
0151     return fHashTable->containsKey(newString);
0152 }
0153 
0154 inline bool XMLStringPool::exists(const unsigned int id) const
0155 {
0156     return (id > 0 && (id < fCurId));
0157 }
0158 
0159 inline const XMLCh* XMLStringPool::getValueForId(const unsigned int id) const
0160 {
0161     if (!id || (id >= fCurId))
0162         ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::StrPool_IllegalId, fMemoryManager);
0163 
0164     // Just index the id map and return that element's string
0165     return fIdMap[id]->fString;
0166 }
0167 
0168 inline unsigned int XMLStringPool::getStringCount() const
0169 {
0170     return fCurId-1;
0171 }
0172 
0173 XERCES_CPP_NAMESPACE_END
0174 
0175 #endif