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