source: NonGTP/Xerces/xerces/include/xercesc/util/StringPool.hpp @ 358

Revision 358, 6.5 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Log: StringPool.hpp,v $
19 * Revision 1.8  2004/09/08 13:56:23  peiyongz
20 * Apache License Version 2.0
21 *
22 * Revision 1.7  2003/10/29 16:18:41  peiyongz
23 * Implement serialization/deserialization
24 *
25 * Revision 1.6  2003/10/09 13:49:30  neilg
26 * make StringPool functions virtual so that we can implement a synchronized version of StringPool for thread-safe updates.
27 *
28 * Revision 1.5  2003/05/16 06:01:52  knoaman
29 * Partial implementation of the configurable memory manager.
30 *
31 * Revision 1.4  2003/05/15 19:07:45  knoaman
32 * Partial implementation of the configurable memory manager.
33 *
34 * Revision 1.3  2003/03/07 18:11:54  tng
35 * Return a reference instead of void for operator=
36 *
37 * Revision 1.2  2002/11/04 15:22:04  tng
38 * C++ Namespace Support.
39 *
40 * Revision 1.1.1.1  2002/02/01 22:22:12  peiyongz
41 * sane_include
42 *
43 * Revision 1.5  2001/10/22 15:43:35  tng
44 * [Bug 3361] "String pool id was not legal" error in Attributes::getURI().
45 *
46 * Revision 1.4  2000/07/07 22:16:52  jpolast
47 * remove old put(value) function.  use put(key,value) instead.
48 *
49 * Revision 1.3  2000/02/24 20:05:25  abagchi
50 * Swat for removing Log from API docs
51 *
52 * Revision 1.2  2000/02/06 07:48:04  rahulj
53 * Year 2K copyright swat.
54 *
55 * Revision 1.1.1.1  1999/11/09 01:05:11  twl
56 * Initial checkin
57 *
58 * Revision 1.2  1999/11/08 20:45:15  rahul
59 * Swat for adding in Product name and CVS comment log variable.
60 *
61 */
62
63#if !defined(STRINGPOOL_HPP)
64#define STRINGPOOL_HPP
65
66#include <xercesc/util/RefHashTableOf.hpp>
67
68#include <xercesc/internal/XSerializable.hpp>
69
70XERCES_CPP_NAMESPACE_BEGIN
71
72//
73//  This class implements a string pool, in which strings can be added and
74//  given a unique id by which they can be refered. It has to provide fast
75//  access both mapping from a string to its id and mapping from an id to
76//  its string. This requires that it provide two separate data structures.
77//  The map one is a hash table for quick storage and look up by name. The
78//  other is an array ordered by unique id which maps to the element in the
79//  hash table.
80//
81//  This works because strings cannot be removed from the pool once added,
82//  other than flushing it completely, and because ids are assigned
83//  sequentially from 1.
84//
85class XMLUTIL_EXPORT XMLStringPool : public XSerializable, public XMemory
86{
87public :
88    // -----------------------------------------------------------------------
89    //  Constructors and Destructor
90    // -----------------------------------------------------------------------
91    XMLStringPool
92    (
93          const unsigned int   modulus = 109
94        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
95    );
96    virtual ~XMLStringPool();
97
98
99    // -----------------------------------------------------------------------
100    //  Pool management methods
101    // -----------------------------------------------------------------------
102    virtual unsigned int addOrFind(const XMLCh* const newString);
103    virtual bool exists(const XMLCh* const newString) const;
104    virtual bool exists(const unsigned int id) const;
105    virtual void flushAll();
106    virtual unsigned int getId(const XMLCh* const toFind) const;
107    virtual const XMLCh* getValueForId(const unsigned int id) const;
108    virtual unsigned int getStringCount() const;
109
110    /***
111     * Support for Serialization/De-serialization
112     ***/
113    DECL_XSERIALIZABLE(XMLStringPool)
114
115    XMLStringPool(MemoryManager* const manager);
116
117private :
118    // -----------------------------------------------------------------------
119    //  Private data types
120    // -----------------------------------------------------------------------
121    class PoolElem : public XMemory
122    {
123        public :
124            PoolElem(const XMLCh* const string,
125                     const unsigned int id,
126                     MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
127            ~PoolElem();
128
129            inline const XMLCh* getKey() const { return fString; }
130            void reset(const XMLCh* const string, const unsigned int id);
131
132            unsigned int    fId;
133            XMLCh*          fString;
134            MemoryManager*  fMemoryManager;
135    };
136
137
138    // -----------------------------------------------------------------------
139    //  Unimplemented constructors and operators
140    // -----------------------------------------------------------------------
141    XMLStringPool(const XMLStringPool&);
142    XMLStringPool& operator=(const XMLStringPool&);
143
144
145    // -----------------------------------------------------------------------
146    //  Private helper methods
147    // -----------------------------------------------------------------------
148    unsigned int addNewEntry(const XMLCh* const newString);
149
150
151    // -----------------------------------------------------------------------
152    //  Private data members
153    //
154    //  fIdMap
155    //      This is an array of pointers to the pool elements. It is ordered
156    //      by unique id, so using an id to index it gives instant access to
157    //      the string of that id. This is grown as required.
158    //
159    //  fHashTable
160    //      This is the hash table used to store and quickly access the
161    //      strings.
162    //
163    //  fMapCapacity
164    //      The current capacity of the id map. When the current id hits this
165    //      value the map must must be expanded.
166    //
167    // -----------------------------------------------------------------------
168    MemoryManager*              fMemoryManager;
169    PoolElem**                  fIdMap;
170    RefHashTableOf<PoolElem>*   fHashTable;
171    unsigned int                fMapCapacity;
172
173protected:
174    // protected data members
175    //  fCurId
176    //      This is the counter used to assign unique ids. It is just bumped
177    //      up one for each new string added.
178    unsigned int                fCurId;
179};
180
181XERCES_CPP_NAMESPACE_END
182
183#endif
Note: See TracBrowser for help on using the repository browser.