SourceForge.net Logo
StringPool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2008
3  * DecisionSoft Limited. All rights reserved.
4  * Copyright (c) 2004-2008
5  * Oracle. All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * $Id$
20  */
21 
22 #ifndef __STRINGPOOL_HPP
23 #define __STRINGPOOL_HPP
24 
25 #include <xqilla/framework/XQillaExport.hpp>
27 #include <memory>
28 #include <cstring>
29 //Added so xqilla will compile under CC on Solaris
30 #include <string>
31 // Added so xqilla will compile on SunOS 10 using STLPort
32 #include <memory.h>
33 
34 class XQILLA_API StringPool
35 {
36 public:
37  StringPool(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mm);
38  ~StringPool();
39 
41  const XMLCh* getPooledString(const XMLCh *src);
43  const XMLCh* getPooledString(const XMLCh *src, unsigned int length);
45  const XMLCh* getPooledString(const char *src);
46 
47  unsigned int getCount() const { return _count; }
48  unsigned int getHits() const { return _hits; }
49  unsigned int getMisses() const { return _misses; }
50  unsigned int getTooBig() const { return _toobig; }
51  void dumpStatistics() const;
52 
53 private:
54  StringPool(const StringPool&);
55  StringPool &operator=(const StringPool&);
56 
57  static unsigned int hash(const XMLCh *v, unsigned int length);
58  const XMLCh *replicate(const XMLCh *v, unsigned int length) const;
59  void resize();
60 
61  class Bucket
62  {
63  public:
64  Bucket(const XMLCh *v, unsigned int l, unsigned int h, Bucket *n)
65  : value(v), length(l), hashValue(h), next(n) {}
66 
67  const XMLCh *value;
68  unsigned int length;
69  unsigned int hashValue;
70  Bucket *next;
71  };
72 
73  XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *_mm;
74  Bucket **_bucketList;
75  unsigned int _modulus;
76  unsigned int _count;
77  unsigned int _hits;
78  unsigned int _misses;
79  unsigned int _toobig;
80 };
81 
82 inline unsigned int StringPool::hash(const XMLCh *v, unsigned int length)
83 {
84  unsigned int hashVal = 0;
85  while(length) {
86  hashVal += (hashVal * 37) + (hashVal >> 24) + (unsigned int)(*v);
87  ++v;
88  --length;
89  }
90  return hashVal;
91 }
92 
93 inline const XMLCh *StringPool::replicate(const XMLCh *v, unsigned int length) const
94 {
95  unsigned int size = length * sizeof(XMLCh);
96  XMLCh *ret = (XMLCh*)_mm->allocate(size + sizeof(XMLCh));
97  memcpy(ret, v, size);
98  ret[length] = 0;
99  return ret;
100 }
101 
102 #endif