source: NonGTP/Xerces/xercesc/util/StringPool.hpp @ 188

Revision 188, 8.5 KB checked in by mattausch, 19 years ago (diff)

added xercesc to support

Line 
1/*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the
17 *    distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 *    if any, must include the following acknowledgment:
21 *       "This product includes software developed by the
22 *        Apache Software Foundation (http://www.apache.org/)."
23 *    Alternately, this acknowledgment may appear in the software itself,
24 *    if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Xerces" and "Apache Software Foundation" must
27 *    not be used to endorse or promote products derived from this
28 *    software without prior written permission. For written
29 *    permission, please contact apache\@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 *    nor may "Apache" appear in their name, without prior written
33 *    permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation, and was
51 * originally based on software copyright (c) 1999, International
52 * Business Machines, Inc., http://www.ibm.com .  For more information
53 * on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57/*
58 * $Log: StringPool.hpp,v $
59 * Revision 1.7  2003/10/29 16:18:41  peiyongz
60 * Implement serialization/deserialization
61 *
62 * Revision 1.6  2003/10/09 13:49:30  neilg
63 * make StringPool functions virtual so that we can implement a synchronized version of StringPool for thread-safe updates.
64 *
65 * Revision 1.5  2003/05/16 06:01:52  knoaman
66 * Partial implementation of the configurable memory manager.
67 *
68 * Revision 1.4  2003/05/15 19:07:45  knoaman
69 * Partial implementation of the configurable memory manager.
70 *
71 * Revision 1.3  2003/03/07 18:11:54  tng
72 * Return a reference instead of void for operator=
73 *
74 * Revision 1.2  2002/11/04 15:22:04  tng
75 * C++ Namespace Support.
76 *
77 * Revision 1.1.1.1  2002/02/01 22:22:12  peiyongz
78 * sane_include
79 *
80 * Revision 1.5  2001/10/22 15:43:35  tng
81 * [Bug 3361] "String pool id was not legal" error in Attributes::getURI().
82 *
83 * Revision 1.4  2000/07/07 22:16:52  jpolast
84 * remove old put(value) function.  use put(key,value) instead.
85 *
86 * Revision 1.3  2000/02/24 20:05:25  abagchi
87 * Swat for removing Log from API docs
88 *
89 * Revision 1.2  2000/02/06 07:48:04  rahulj
90 * Year 2K copyright swat.
91 *
92 * Revision 1.1.1.1  1999/11/09 01:05:11  twl
93 * Initial checkin
94 *
95 * Revision 1.2  1999/11/08 20:45:15  rahul
96 * Swat for adding in Product name and CVS comment log variable.
97 *
98 */
99
100#if !defined(STRINGPOOL_HPP)
101#define STRINGPOOL_HPP
102
103#include <xercesc/util/RefHashTableOf.hpp>
104
105#include <xercesc/internal/XSerializable.hpp>
106
107XERCES_CPP_NAMESPACE_BEGIN
108
109//
110//  This class implements a string pool, in which strings can be added and
111//  given a unique id by which they can be refered. It has to provide fast
112//  access both mapping from a string to its id and mapping from an id to
113//  its string. This requires that it provide two separate data structures.
114//  The map one is a hash table for quick storage and look up by name. The
115//  other is an array ordered by unique id which maps to the element in the
116//  hash table.
117//
118//  This works because strings cannot be removed from the pool once added,
119//  other than flushing it completely, and because ids are assigned
120//  sequentially from 1.
121//
122class XMLUTIL_EXPORT XMLStringPool : public XSerializable, public XMemory
123{
124public :
125    // -----------------------------------------------------------------------
126    //  Constructors and Destructor
127    // -----------------------------------------------------------------------
128    XMLStringPool
129    (
130          const unsigned int   modulus = 109
131        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
132    );
133    virtual ~XMLStringPool();
134
135
136    // -----------------------------------------------------------------------
137    //  Pool management methods
138    // -----------------------------------------------------------------------
139    virtual unsigned int addOrFind(const XMLCh* const newString);
140    virtual bool exists(const XMLCh* const newString) const;
141    virtual bool exists(const unsigned int id) const;
142    virtual void flushAll();
143    virtual unsigned int getId(const XMLCh* const toFind) const;
144    virtual const XMLCh* getValueForId(const unsigned int id) const;
145    virtual unsigned int getStringCount() const;
146
147    /***
148     * Support for Serialization/De-serialization
149     ***/
150    DECL_XSERIALIZABLE(XMLStringPool)
151
152    XMLStringPool(MemoryManager* const manager);
153
154private :
155    // -----------------------------------------------------------------------
156    //  Private data types
157    // -----------------------------------------------------------------------
158    class PoolElem : public XMemory
159    {
160        public :
161            PoolElem(const XMLCh* const string,
162                     const unsigned int id,
163                     MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
164            ~PoolElem();
165
166            inline const XMLCh* getKey() const { return fString; }
167            void reset(const XMLCh* const string, const unsigned int id);
168
169            unsigned int    fId;
170            XMLCh*          fString;
171            MemoryManager*  fMemoryManager;
172    };
173
174
175    // -----------------------------------------------------------------------
176    //  Unimplemented constructors and operators
177    // -----------------------------------------------------------------------
178    XMLStringPool(const XMLStringPool&);
179    XMLStringPool& operator=(const XMLStringPool&);
180
181
182    // -----------------------------------------------------------------------
183    //  Private helper methods
184    // -----------------------------------------------------------------------
185    unsigned int addNewEntry(const XMLCh* const newString);
186
187
188    // -----------------------------------------------------------------------
189    //  Private data members
190    //
191    //  fIdMap
192    //      This is an array of pointers to the pool elements. It is ordered
193    //      by unique id, so using an id to index it gives instant access to
194    //      the string of that id. This is grown as required.
195    //
196    //  fHashTable
197    //      This is the hash table used to store and quickly access the
198    //      strings.
199    //
200    //  fMapCapacity
201    //      The current capacity of the id map. When the current id hits this
202    //      value the map must must be expanded.
203    //
204    // -----------------------------------------------------------------------
205    MemoryManager*              fMemoryManager;
206    PoolElem**                  fIdMap;
207    RefHashTableOf<PoolElem>*   fHashTable;
208    unsigned int                fMapCapacity;
209
210protected:
211    // protected data members
212    //  fCurId
213    //      This is the counter used to assign unique ids. It is just bumped
214    //      up one for each new string added.
215    unsigned int                fCurId;
216};
217
218XERCES_CPP_NAMESPACE_END
219
220#endif
Note: See TracBrowser for help on using the repository browser.