source: NonGTP/Xerces/xerces-c_2_8_0/include/xercesc/util/StringPool.hpp @ 2674

Revision 2674, 6.0 KB checked in by mattausch, 16 years ago (diff)
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 568078 2007-08-21 11:43:25Z amassari $
20 */
21
22#if !defined(STRINGPOOL_HPP)
23#define STRINGPOOL_HPP
24
25#include <xercesc/util/RefHashTableOf.hpp>
26
27#include <xercesc/internal/XSerializable.hpp>
28
29XERCES_CPP_NAMESPACE_BEGIN
30
31//
32//  This class implements a string pool, in which strings can be added and
33//  given a unique id by which they can be refered. It has to provide fast
34//  access both mapping from a string to its id and mapping from an id to
35//  its string. This requires that it provide two separate data structures.
36//  The map one is a hash table for quick storage and look up by name. The
37//  other is an array ordered by unique id which maps to the element in the
38//  hash table.
39//
40//  This works because strings cannot be removed from the pool once added,
41//  other than flushing it completely, and because ids are assigned
42//  sequentially from 1.
43//
44class XMLUTIL_EXPORT XMLStringPool : public XSerializable, public XMemory
45{
46public :
47    // -----------------------------------------------------------------------
48    //  Constructors and Destructor
49    // -----------------------------------------------------------------------
50    XMLStringPool
51    (
52          const unsigned int   modulus = 109
53        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
54    );
55    virtual ~XMLStringPool();
56
57
58    // -----------------------------------------------------------------------
59    //  Pool management methods
60    // -----------------------------------------------------------------------
61    virtual unsigned int addOrFind(const XMLCh* const newString);
62    virtual bool exists(const XMLCh* const newString) const;
63    virtual bool exists(const unsigned int id) const;
64    virtual void flushAll();
65    virtual unsigned int getId(const XMLCh* const toFind) const;
66    virtual const XMLCh* getValueForId(const unsigned int id) const;
67    virtual unsigned int getStringCount() const;
68
69    /***
70     * Support for Serialization/De-serialization
71     ***/
72    DECL_XSERIALIZABLE(XMLStringPool)
73
74    XMLStringPool(MemoryManager* const manager);
75
76private :
77    // -----------------------------------------------------------------------
78    //  Private data types
79    // -----------------------------------------------------------------------
80    struct PoolElem
81    {
82        unsigned int  fId;
83        XMLCh*        fString;
84    };
85
86    // -----------------------------------------------------------------------
87    //  Unimplemented constructors and operators
88    // -----------------------------------------------------------------------
89    XMLStringPool(const XMLStringPool&);
90    XMLStringPool& operator=(const XMLStringPool&);
91
92
93    // -----------------------------------------------------------------------
94    //  Private helper methods
95    // -----------------------------------------------------------------------
96    unsigned int addNewEntry(const XMLCh* const newString);
97
98
99    // -----------------------------------------------------------------------
100    //  Private data members
101    //
102    //  fIdMap
103    //      This is an array of pointers to the pool elements. It is ordered
104    //      by unique id, so using an id to index it gives instant access to
105    //      the string of that id. This is grown as required.
106    //
107    //  fHashTable
108    //      This is the hash table used to store and quickly access the
109    //      strings.
110    //
111    //  fMapCapacity
112    //      The current capacity of the id map. When the current id hits this
113    //      value the map must must be expanded.
114    //
115    // -----------------------------------------------------------------------
116    MemoryManager*              fMemoryManager;
117    PoolElem**                  fIdMap;
118    RefHashTableOf<PoolElem>*   fHashTable;
119    unsigned int                fMapCapacity;
120
121protected:
122    // protected data members
123    //  fCurId
124    //      This is the counter used to assign unique ids. It is just bumped
125    //      up one for each new string added.
126    unsigned int                fCurId;
127};
128
129
130// Provid inline versions of some of the simple functions to improve performance.
131inline unsigned int XMLStringPool::addOrFind(const XMLCh* const newString)
132{
133    PoolElem* elemToFind = fHashTable->get(newString);
134    if (elemToFind)
135        return elemToFind->fId;
136
137    return addNewEntry(newString);
138}
139
140inline unsigned int XMLStringPool::getId(const XMLCh* const toFind) const
141{
142    PoolElem* elemToFind = fHashTable->get(toFind);
143    if (elemToFind)
144        return elemToFind->fId;
145
146    // Not found, so return zero, which is never a legal id
147    return 0;
148}
149
150inline bool XMLStringPool::exists(const XMLCh* const newString) const
151{
152    return fHashTable->containsKey(newString);
153}
154
155inline bool XMLStringPool::exists(const unsigned int id) const
156{
157    if (!id || (id >= fCurId))
158        return false;
159
160    return true;
161}
162
163inline const XMLCh* XMLStringPool::getValueForId(const unsigned int id) const
164{
165    if (!id || (id >= fCurId))
166        ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::StrPool_IllegalId, fMemoryManager);
167
168    // Just index the id map and return that element's string
169    return fIdMap[id]->fString;
170}
171
172inline unsigned int XMLStringPool::getStringCount() const
173{
174    return fCurId-1;
175}
176
177XERCES_CPP_NAMESPACE_END
178
179#endif
Note: See TracBrowser for help on using the repository browser.