Xalan-C++ API Reference  1.12.0
XalanDOMStringHashTable.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #if !defined(XALANDOMSTRINGHASHTABLE_HEADER_GUARD_1357924680)
19 #define XALANDOMSTRINGHASHTABLE_HEADER_GUARD_1357924680
20 
21 
22 
23 // Base include file. Must be first.
25 
26 
27 
29 
30 
31 
33 
34 
35 
36 namespace XALAN_CPP_NAMESPACE {
37 
38 
39 
41 {
42 public:
43 
49 
50  enum { eDefaultBucketCount = 101, eDefaultBucketSize = 15 };
51 
52 
53  /**
54  * Create a hash table.
55  *
56  * @param theBucketCount The number of buckets to use for the hash table. This should be a prime number for best results.
57  * @param theBucketSize The initial size of each bucket in the hash table.
58  */
59  explicit
61  MemoryManager& theManager,
62  size_t theBucketCount = eDefaultBucketCount,
63  bucket_size_type theBucketSize = eDefaultBucketSize);
64 
66 
67  /**
68  * Clear the hash table.
69  */
70  void
71  clear();
72 
73  /**
74  * Get the number of strings in the table
75  *
76  * @return The number of strings in the table
77  */
78  size_t
79  size() const
80  {
81  return m_count;
82  }
83 
84  /**
85  * Get the number of buckets in the table
86  *
87  * @return The number of buckets in the table
88  */
89  size_t
90  bucketCount() const
91  {
92  return m_bucketCount;
93  }
94 
95  /**
96  * Get the size of each of the buckets in the table
97  *
98  * @param A vector to return the bucket counts.
99  */
100  void
101  getBucketCounts(BucketCountsType& theVector) const;
102 
103  /**
104  * Get the collision count. Release builds will always
105  * return 0.
106  *
107  * @return The number of collisions. Valid only for non-release builds.
108  */
109  size_t
110  collisions() const
111  {
112  return m_collisions;
113  }
114 
115  /**
116  * Find a string. If the string is not found, return null.
117  *
118  * @param theString The string to find.
119  * @param theBucketIndex The index of the bucket for the string.
120  * @return a pointer to the string, or null if not found.
121  */
122  const XalanDOMString*
123  find(
124  const XalanDOMString& theString,
125  size_t* theBucketIndex = 0) const;
126 
127  /**
128  * Find a string. If the string is not found, return null.
129  * If theBucketIndex is not null, the variable pointed to
130  * will be updated with the bucket index that was calculated
131  * for the string. This index can be used in a later call to
132  * insert() to avoid recalculating the index.
133  *
134  * @param theString The string to find.
135  * @param theLength The number of characters in the string.
136  * @param theBucketIndex A pointer to a parameter to get the bucket index
137  * @return a pointer to the string, or null if not found.
138  */
139  const XalanDOMString*
140  find(
141  const XalanDOMChar* theString,
142  XalanDOMString::size_type theLength = XalanDOMString::npos,
143  size_t* theBucketIndex = 0) const;
144 
145  /**
146  * Insert a pointer to a string into the table. If the string
147  * is already present, the string will still be added, but it
148  * will never be found, since it will be placed after the identical
149  * string.
150  *
151  * Note that this class only stores a _pointer_ to a XalanDOMString.
152  * It's expected that the string will be allocated and managed outside
153  * of the hash table.
154  *
155  * @param theString The string to insert.
156  */
157  void
158  insert(const XalanDOMString& theString);
159 
160  /**
161  * Insert a pointer to a string into the table. If the string
162  * is already present, the string will still be added, but it
163  * will never be found, since it will be placed after the identical
164  * string. theBucketIndex _must_ be the index returned from a
165  * previous call to find. If not, then the behavior is undefined.
166  * This is meant as an optimization to avoid re-hashing the string.
167  *
168  * Note that this class only stores a _pointer_ to a XalanDOMString.
169  * It's expected that the string will be allocated and managed outside
170  * of the hash table.
171  *
172  * @param theString The string to insert.
173  * @param theBucketIndex The index of the bucket for the string.
174  */
175  void
176  insert(
177  const XalanDOMString& theString,
178  size_t theBucketIndex);
179 
180  MemoryManager&
182  {
183  return m_buckets.getMemoryManager();
184  }
185 
186  const MemoryManager&
188  {
189  return m_buckets.getMemoryManager();
190  }
191 
192 private:
193 
194  // Not implemented, for now...
196 
198  operator=(const XalanDOMStringHashTable&);
199 
200  bool
201  operator==(const XalanDOMStringHashTable&) const;
202 
203 
204  // Data members...
205  const size_t m_bucketCount;
206 
207  const bucket_size_type m_bucketSize;
208 
209  BucketVectorType m_buckets;
210 
211  size_t m_count;
212 
213  size_t m_collisions;
214 };
215 
216 
217 
218 }
219 
220 
221 
222 #endif // !defined(XALANDOMSTRINGPOOL_HEADER_GUARD_1357924680)
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::clear
void clear(XalanDOMString &theString)
Remove all elements from target string.
Definition: DOMStringHelper.hpp:2475
xalanc::XalanDOMStringHashTable::getMemoryManager
MemoryManager & getMemoryManager()
Definition: XalanDOMStringHashTable.hpp:181
XalanDOMString.hpp
xalanc::XalanVector< const XalanDOMString * >
xalanc::XalanDOMStringHashTable::collisions
size_t collisions() const
Get the collision count.
Definition: XalanDOMStringHashTable.hpp:110
xalanc::ExplicitMemoryManagedConstructionTraits
Definition: XalanMemoryManagement.hpp:583
xalanc::XalanDOMStringHashTable::ConstructionTraits
ExplicitMemoryManagedConstructionTraits< BucketType > ConstructionTraits
Definition: XalanDOMStringHashTable.hpp:47
xalanc::size_type
size_t size_type
Definition: XalanMap.hpp:46
XalanVector.hpp
xalanc::XalanDOMStringHashTable::getMemoryManager
const MemoryManager & getMemoryManager() const
Definition: XalanDOMStringHashTable.hpp:187
xalanc::operator==
bool operator==(const XalanVector< Type > &theLHS, const XalanVector< Type > &theRHS)
Definition: XalanVector.hpp:1118
XALAN_PLATFORMSUPPORT_EXPORT
#define XALAN_PLATFORMSUPPORT_EXPORT
Definition: PlatformSupportDefinitions.hpp:35
PlatformSupportDefinitions.hpp
xalanc::XalanDOMStringHashTable::BucketType
XalanVector< const XalanDOMString * > BucketType
Definition: XalanDOMStringHashTable.hpp:44
xalanc::XalanDOMStringHashTable
Definition: XalanDOMStringHashTable.hpp:40
xalanc::XalanDOMStringHashTable::bucket_size_type
BucketType::size_type bucket_size_type
Definition: XalanDOMStringHashTable.hpp:45
xalanc::XalanDOMStringHashTable::size
size_t size() const
Get the number of strings in the table.
Definition: XalanDOMStringHashTable.hpp:79
xalanc::XalanDOMStringHashTable::bucketCount
size_t bucketCount() const
Get the number of buckets in the table.
Definition: XalanDOMStringHashTable.hpp:90
xalanc::XalanDOMStringHashTable::~XalanDOMStringHashTable
~XalanDOMStringHashTable()
Definition: XalanDOMStringHashTable.hpp:65
xalanc::insert
XalanDOMString & insert(XalanDOMString &theString, XalanDOMString::size_type thePosition, const XalanDOMString &theStringToInsert)
Insert a string into another string.
Definition: DOMStringHelper.hpp:2418
xalanc::XalanDOMString::size_type
XalanSize_t size_type
Definition: XalanDOMString.hpp:57
xalanc::XalanDOMStringHashTable::BucketVectorType
XalanVector< BucketType, ConstructionTraits > BucketVectorType
Definition: XalanDOMStringHashTable.hpp:48
xalanc::XalanDOMString
Definition: XalanDOMString.hpp:45
xalanc::XalanDOMStringHashTable::BucketCountsType
XalanVector< bucket_size_type > BucketCountsType
Definition: XalanDOMStringHashTable.hpp:46