source: icXML/icXML-devel/src/xercesc/util/StringPool.hpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 6.0 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: StringPool.hpp 932887 2010-04-11 13:04:59Z borisk $
20 */
21
22#if !defined(XERCESC_INCLUDE_GUARD_STRINGPOOL_HPP)
23#define XERCESC_INCLUDE_GUARD_STRINGPOOL_HPP
24
25#include <icxercesc/util/RefHashTableOf.hpp>
26#include <xercesc/internal/XSerializable.hpp>
27
28XERCES_CPP_NAMESPACE_BEGIN
29
30//
31//  This class implements a string pool, in which strings can be added and
32//  given a unique id by which they can be referred. It has to provide fast
33//  access both mapping from a string to its id and mapping from an id to
34//  its string. This requires that it provide two separate data structures.
35//  The map one is a hash table for quick storage and look up by name. The
36//  other is an array ordered by unique id which maps to the element in the
37//  hash table.
38//
39//  This works because strings cannot be removed from the pool once added,
40//  other than flushing it completely, and because ids are assigned
41//  sequentially from 1.
42//
43class XMLUTIL_EXPORT XMLStringPool : public XSerializable, public XMemory
44{
45public :
46    // -----------------------------------------------------------------------
47    //  Constructors and Destructor
48    // -----------------------------------------------------------------------
49    XMLStringPool
50    (
51          const unsigned int   modulus = 109
52        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
53    );
54    virtual ~XMLStringPool();
55
56
57    // -----------------------------------------------------------------------
58    //  Pool management methods
59    // -----------------------------------------------------------------------
60    virtual unsigned int addOrFind(const XMLCh* const newString);
61    virtual bool exists(const XMLCh* const newString) const;
62    virtual bool exists(const unsigned int id) const;
63    virtual void flushAll();
64    virtual unsigned int getId(const XMLCh* const toFind) const;
65    virtual const XMLCh* getValueForId(const unsigned int id) const;
66    virtual unsigned int getStringCount() const;
67
68    /***
69     * Support for Serialization/De-serialization
70     ***/
71    DECL_XSERIALIZABLE(XMLStringPool)
72
73    XMLStringPool(MemoryManager* const manager);
74
75private :
76    // -----------------------------------------------------------------------
77    //  Private data types
78    // -----------------------------------------------------------------------
79    struct PoolElem
80    {
81        unsigned int  fId;
82        XMLCh*        fString;
83    };
84
85    // -----------------------------------------------------------------------
86    //  Unimplemented constructors and operators
87    // -----------------------------------------------------------------------
88    XMLStringPool(const XMLStringPool&);
89    XMLStringPool& operator=(const XMLStringPool&);
90
91
92    // -----------------------------------------------------------------------
93    //  Private helper methods
94    // -----------------------------------------------------------------------
95    unsigned int addNewEntry(const XMLCh* const newString);
96
97
98    // -----------------------------------------------------------------------
99    //  Private data members
100    //
101    //  fIdMap
102    //      This is an array of pointers to the pool elements. It is ordered
103    //      by unique id, so using an id to index it gives instant access to
104    //      the string of that id. This is grown as required.
105    //
106    //  fHashTable
107    //      This is the hash table used to store and quickly access the
108    //      strings.
109    //
110    //  fMapCapacity
111    //      The current capacity of the id map. When the current id hits this
112    //      value the map must must be expanded.
113    //
114    // -----------------------------------------------------------------------
115    MemoryManager*              fMemoryManager;
116    PoolElem**                  fIdMap;
117    RefHashTableOf<PoolElem>*   fHashTable;
118    unsigned int                fMapCapacity;
119
120protected:
121    // protected data members
122    //  fCurId
123    //      This is the counter used to assign unique ids. It is just bumped
124    //      up one for each new string added.
125    unsigned int                fCurId;
126};
127
128
129// Provide inline versions of some of the simple functions to improve performance.
130inline unsigned int XMLStringPool::addOrFind(const XMLCh* const newString)
131{
132    PoolElem* elemToFind = fHashTable->get(newString);
133    if (elemToFind)
134        return elemToFind->fId;
135
136    return addNewEntry(newString);
137}
138
139inline unsigned int XMLStringPool::getId(const XMLCh* const toFind) const
140{
141    PoolElem* elemToFind = fHashTable->get(toFind);
142    if (elemToFind)
143        return elemToFind->fId;
144
145    // Not found, so return zero, which is never a legal id
146    return 0;
147}
148
149inline bool XMLStringPool::exists(const XMLCh* const newString) const
150{
151    return fHashTable->containsKey(newString);
152}
153
154inline bool XMLStringPool::exists(const unsigned int id) const
155{
156    return (id > 0 && (id < fCurId));
157}
158
159inline const XMLCh* XMLStringPool::getValueForId(const unsigned int id) const
160{
161    if (!id || (id >= fCurId))
162        ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::StrPool_IllegalId, fMemoryManager);
163
164    // Just index the id map and return that element's string
165    return fIdMap[id]->fString;
166}
167
168inline unsigned int XMLStringPool::getStringCount() const
169{
170    return fCurId-1;
171}
172
173XERCES_CPP_NAMESPACE_END
174
175#endif
Note: See TracBrowser for help on using the repository browser.