Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:12

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_NAMEIDPOOL_HPP)
0023 #define XERCESC_INCLUDE_GUARD_NAMEIDPOOL_HPP
0024 
0025 #include <xercesc/util/XMemory.hpp>
0026 #include <xercesc/util/XMLString.hpp>
0027 #include <xercesc/util/PlatformUtils.hpp>
0028 #include <xercesc/util/RefHashTableOf.hpp>
0029 
0030 XERCES_CPP_NAMESPACE_BEGIN
0031 
0032 //
0033 //  Forward declare the enumerator so he can be our friend. Can you say
0034 //  friend? Sure...
0035 //
0036 template <class TElem> class NameIdPoolEnumerator;
0037 
0038 
0039 //
0040 //  This class is provided to serve as the basis of many of the pools that
0041 //  are used by the scanner and validators. They often need to be able to
0042 //  store objects in such a way that they can be quickly accessed by the
0043 //  name field of the object, and such that each element added is assigned
0044 //  a unique id via which it can be accessed almost instantly.
0045 //
0046 //  Object names are enforced as being unique, since that's what all these
0047 //  pools require. So its effectively a hash table in conjunction with an
0048 //  array of references into the hash table by id. Ids are assigned such that
0049 //  id N can be used to get the Nth element from the array of references.
0050 //  This provides very fast access by id.
0051 //
0052 //  The way these pools are used, elements are never removed except when the
0053 //  whole thing is flushed. This makes it very easy to maintain the two
0054 //  access methods in sync.
0055 //
0056 //  For efficiency reasons, the id reference array is never flushed until
0057 //  the dtor. This way, it does not have to be regrown every time its reused.
0058 //
0059 //  All elements are assumed to be owned by the pool!
0060 //
0061 
0062 template <class TElem> class NameIdPool : public XMemory
0063 {
0064 public :
0065     // -----------------------------------------------------------------------
0066     //  Constructors and Destructor
0067     // -----------------------------------------------------------------------
0068     NameIdPool
0069     (
0070         const   XMLSize_t       hashModulus
0071         , const XMLSize_t       initSize = 128
0072         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
0073     );
0074 
0075     ~NameIdPool();
0076 
0077 
0078     // -----------------------------------------------------------------------
0079     //  Element management
0080     // -----------------------------------------------------------------------
0081     bool containsKey(const XMLCh* const key) const;
0082     void removeAll();
0083 
0084 
0085     // -----------------------------------------------------------------------
0086     //  Getters
0087     // -----------------------------------------------------------------------
0088     TElem* getByKey(const XMLCh* const key);
0089     const TElem* getByKey(const XMLCh* const key) const;
0090     TElem* getById(const XMLSize_t elemId);
0091     const TElem* getById(const XMLSize_t elemId) const;
0092 
0093     MemoryManager* getMemoryManager() const;
0094     // -----------------------------------------------------------------------
0095     //  Putters
0096     //
0097     //  Dups are not allowed and cause an IllegalArgumentException. The id
0098     //  of the new element is returned.
0099     // -----------------------------------------------------------------------
0100     XMLSize_t put(TElem* const valueToAdopt);
0101 
0102 
0103 protected :
0104     // -----------------------------------------------------------------------
0105     //  Declare the enumerator our friend so he can see our members
0106     // -----------------------------------------------------------------------
0107     friend class NameIdPoolEnumerator<TElem>;
0108 
0109 
0110 private :
0111     // -----------------------------------------------------------------------
0112     //  Unused constructors and operators
0113     // -----------------------------------------------------------------------
0114     NameIdPool(const NameIdPool<TElem>&);
0115     NameIdPool<TElem>& operator=(const NameIdPool<TElem>&);
0116 
0117     // -----------------------------------------------------------------------
0118     //  Data members
0119     //
0120     //  fBucketList
0121     //      This is the hash table that contains the values.
0122     //
0123     //  fIdPtrs
0124     //  fIdPtrsCount
0125     //      This is the array of pointers to the bucket elements in order of
0126     //      their assigned ids. So taking id N and referencing this array
0127     //      gives you the element with that id. The count field indicates
0128     //      the current size of this list. When fIdCounter+1 reaches this
0129     //      value the list must be expanded.
0130     //
0131     //  fIdCounter
0132     //      This is used to give out unique ids to added elements. It starts
0133     //      at zero (which means empty), and is bumped up for each newly added
0134     //      element. So the first element is 1, the next is 2, etc... This
0135     //      means that this value is set to the top index of the fIdPtrs array.
0136     //
0137     // -----------------------------------------------------------------------
0138     MemoryManager*                  fMemoryManager;
0139     TElem**                         fIdPtrs;
0140     XMLSize_t                       fIdPtrsCount;
0141     XMLSize_t                       fIdCounter;
0142     RefHashTableOf<TElem>           fBucketList;
0143 };
0144 
0145 
0146 //
0147 //  An enumerator for a name id pool. It derives from the basic enumerator
0148 //  class, so that pools can be generically enumerated.
0149 //
0150 template <class TElem> class NameIdPoolEnumerator : public XMLEnumerator<TElem>, public XMemory
0151 {
0152 public :
0153     // -----------------------------------------------------------------------
0154     //  Constructors and Destructor
0155     // -----------------------------------------------------------------------
0156     NameIdPoolEnumerator
0157     (
0158                 NameIdPool<TElem>* const    toEnum
0159                 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
0160     );
0161 
0162     NameIdPoolEnumerator
0163     (
0164         const   NameIdPoolEnumerator<TElem>& toCopy
0165     );
0166 
0167     virtual ~NameIdPoolEnumerator();
0168 
0169     // -----------------------------------------------------------------------
0170     //  Public operators
0171     // -----------------------------------------------------------------------
0172     NameIdPoolEnumerator<TElem>& operator=
0173     (
0174         const   NameIdPoolEnumerator<TElem>& toAssign
0175     );
0176 
0177     // -----------------------------------------------------------------------
0178     //  Enum interface
0179     // -----------------------------------------------------------------------
0180     bool hasMoreElements() const;
0181     TElem& nextElement();
0182     void Reset();
0183     XMLSize_t size()  const;
0184 
0185 private :
0186     // -----------------------------------------------------------------------
0187     //  Data Members
0188     //
0189     //  fCurIndex
0190     //      This is the current index into the pool's id mapping array. This
0191     //      is now we enumerate it.
0192     //
0193     //  fToEnum
0194     //      The name id pool that is being enumerated.
0195     // -----------------------------------------------------------------------
0196     XMLSize_t               fCurIndex;
0197     NameIdPool<TElem>*      fToEnum;
0198     MemoryManager*          fMemoryManager;
0199 };
0200 
0201 XERCES_CPP_NAMESPACE_END
0202 
0203 #if !defined(XERCES_TMPLSINC)
0204 #include <xercesc/util/NameIdPool.c>
0205 #endif
0206 
0207 #endif