BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_cstringhash.h
Go to the documentation of this file.
1/// @file bdlb_cstringhash.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_cstringhash.h -*-C++-*-
8#ifndef INCLUDED_BDLB_CSTRINGHASH
9#define INCLUDED_BDLB_CSTRINGHASH
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_cstringhash bdlb_cstringhash
15/// @brief Provide a functor enabling C-strings as unordered-container keys.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_cstringhash
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_cstringhash-purpose"> Purpose</a>
25/// * <a href="#bdlb_cstringhash-classes"> Classes </a>
26/// * <a href="#bdlb_cstringhash-description"> Description </a>
27/// * <a href="#bdlb_cstringhash-usage"> Usage </a>
28/// * <a href="#bdlb_cstringhash-example-1-basic-use-of-bdlb-cstringhash"> Example 1: Basic Use of bdlb::CStringHash </a>
29///
30/// # Purpose {#bdlb_cstringhash-purpose}
31/// Provide a functor enabling C-strings as unordered-container keys.
32///
33/// # Classes {#bdlb_cstringhash-classes}
34///
35/// - bdlb::CStringHash: functor enabling C-strings as unordered-container keys
36///
37/// @see
38///
39/// # Description {#bdlb_cstringhash-description}
40/// This component provides a `struct`, `bdlb::CStringHash`, that
41/// defines a functor to generate a hash code for a null-terminated string,
42/// rather than simply generating a hash code for the address of the string, as
43/// the `std::hash` functor would do. This hash functor is suitable for
44/// supporting C-strings as keys in unordered associative containers. Note that
45/// the container behavior would be undefined if the strings referenced by such
46/// pointers were to change value.
47///
48/// ## Usage {#bdlb_cstringhash-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Basic Use of bdlb::CStringHash {#bdlb_cstringhash-example-1-basic-use-of-bdlb-cstringhash}
54///
55///
56/// Suppose we need an associative container to store some objects, uniquely
57/// identified by C-strings. The following code illustrates how to use
58/// `bdlb::CStringHash` as a hash function for the standard container
59/// `unordered_map` taking C-string as a key.
60///
61/// First, let us define our mapped type class:
62/// @code
63/// /// This class implements a value semantic type that represents
64/// /// ownership of a security.
65/// class Security
66/// {
67///
68/// // DATA
69/// char *d_name_p; // Security name
70/// unsigned int d_sharesOwned; // The number of owned shares
71///
72/// public:
73/// // CREATORS
74///
75/// /// Create a `Security` object having the specified `name` and
76/// /// `sharesOwned`.
77/// Security(const char *name, unsigned int sharesOwned);
78///
79/// /// Create a `Security` object having the value of the specified
80/// /// `original` security.
81/// Security(const Security& original);
82///
83/// /// Destroy this security object.
84/// ~Security();
85///
86///
87/// // ACCESSORS
88///
89/// /// Return the value of the `sharesOwned` attribute of this security
90/// /// object.
91/// unsigned int sharesOwned() const;
92///
93/// // MANIPULATORS
94///
95/// /// Assign to this security object the value of the specified
96/// /// `other` object, and return a reference providing modifiable
97/// /// access to this object.
98/// Security& operator=(Security other);
99///
100/// /// Efficiently exchange the value of this security with the value
101/// /// of the specified `other` security.
102/// void swap(Security& other);
103/// };
104///
105/// // CREATORS
106/// inline
107/// Security::Security(const char *name, unsigned int sharesOwned)
108/// : d_sharesOwned(sharesOwned)
109/// {
110/// d_name_p = new char [strlen(name) + 1];
111/// strncpy(d_name_p, name, strlen(name) + 1);
112/// }
113///
114/// inline
115/// Security::Security(const Security& original)
116/// : d_sharesOwned(original.d_sharesOwned)
117/// {
118/// if (this != &original)
119/// {
120/// d_name_p = new char [strlen(original.d_name_p) + 1];
121/// strncpy(d_name_p,
122/// original.d_name_p,
123/// strlen(original.d_name_p) + 1);
124/// }
125/// }
126///
127/// inline
128/// Security::~Security()
129/// {
130/// delete [] d_name_p;
131/// }
132///
133/// // ACCESSORS
134///
135/// inline unsigned int Security::sharesOwned() const
136/// {
137/// return d_sharesOwned;
138/// }
139///
140/// // MANIPULATORS
141/// inline
142/// Security& Security::operator=(Security other)
143/// {
144/// this->swap(other);
145/// return *this;
146/// }
147///
148/// inline
149/// void Security::swap(Security& other)
150/// {
151/// char * tempPtr = d_name_p;
152/// d_name_p = other.d_name_p;
153/// other.d_name_p = tempPtr;
154/// unsigned int tempInt = d_sharesOwned;
155/// d_sharesOwned = other.d_sharesOwned;
156/// other.d_sharesOwned = tempInt;
157/// }
158/// @endcode
159/// Next, we define container type using `bdlb::CStringHash` as a hash function
160/// and `bdlb::CstringEqualTo` as a comparator:
161/// @code
162/// typedef unordered_map<const char *,
163/// Security,
164/// bdlb::CStringHash,
165/// bdlb::CStringEqualTo> SecuritiesUM;
166/// @endcode
167/// This container stores objects of `Security` class and allow access to them
168/// by their names.
169///
170/// Then, we create several C-strings with security names:
171/// @code
172/// const char *ibm = "IBM";
173/// const char *msft = "Microsoft";
174/// const char *goog = "Google";
175/// @endcode
176/// Now, we create a container for securities and fill it:
177/// @code
178/// SecuritiesUM securities;
179///
180/// securities.insert(
181/// std::make_pair<const char *, Security>(ibm, Security(ibm, 616)));
182/// securities.insert(
183/// std::make_pair<const char *, Security>(msft, Security(msft, 6150000)));
184/// @endcode
185/// Finally, we make sure, that we able to access securities by their names:
186/// @code
187/// SecuritiesUM::iterator it = securities.find(ibm);
188/// assert(616 == it->second.sharesOwned());
189/// it = securities.find(msft);
190/// assert(6150000 == it->second.sharesOwned());
191/// it = securities.find(goog);
192/// assert(securities.end() == it);
193/// @endcode
194/// @}
195/** @} */
196/** @} */
197
198/** @addtogroup bdl
199 * @{
200 */
201/** @addtogroup bdlb
202 * @{
203 */
204/** @addtogroup bdlb_cstringhash
205 * @{
206 */
207
208#include <bdlscm_version.h>
209
211
216
217#include <bsls_assert.h>
218#include <bsls_review.h>
219
220#include <bsl_cstring.h>
221#include <bsl_cstddef.h>
222
223
224namespace bdlb {
225
226 // ==================
227 // struct CStringHash
228 // ==================
229
230/// This `struct` defines a hash operation for null-terminated character
231/// strings enabling them to be used as keys in the standard unordered
232/// associative containers such as `bsl::unordered_map` and
233/// `bsl::unordered_set`. Note that this class is an empty POD type.
235
236 // STANDARD TYPEDEFS
237 typedef const char *argument_type;
238 typedef bsl::size_t result_type;
239
240 // TRAITS
244
245 /// Create a `CStringHash` object.
246 CStringHash() = default;
247
248 /// Create a `CStringHash` object. Note that as `CStringHash` is an
249 /// empty (stateless) type, this operation will have no observable
250 /// effect.
251 CStringHash(const CStringHash& original) = default;
252
253 /// Destroy this object.
254 ~CStringHash() = default;
255
256 // MANIPULATORS
257
258 /// Assign to this object the value of the specified `rhs` object, and
259 /// a return a reference providing modifiable access to this object.
260 /// Note that as `CStringHash` is an empty (stateless) type, this
261 /// operation will have no observable effect.
262 CStringHash& operator=(const CStringHash& rhs) = default;
263
264 // ACCESSORS
265
266 /// Return a hash code generated from the contents of the specified
267 /// null-terminated `argument` string. The behavior is undefined
268 /// unless `argument` points to a null-terminated string.
269 bsl::size_t operator()(const char *argument) const;
270};
271
272// ============================================================================
273// INLINE DEFINITIONS
274// ============================================================================
275
276 // ------------------
277 // struct CStringHash
278 // ------------------
279
280// ACCESSORS
281inline
282bsl::size_t bdlb::CStringHash::operator()(const char *argument) const
283{
284 BSLS_ASSERT(argument);
285
287 hash(argument, bsl::strlen(argument));
288 return static_cast<bsl::size_t>(hash.computeHash());
289}
290
291} // close package namespace
292
293
294#endif
295
296// ----------------------------------------------------------------------------
297// Copyright 2015 Bloomberg Finance L.P.
298//
299// Licensed under the Apache License, Version 2.0 (the "License");
300// you may not use this file except in compliance with the License.
301// You may obtain a copy of the License at
302//
303// http://www.apache.org/licenses/LICENSE-2.0
304//
305// Unless required by applicable law or agreed to in writing, software
306// distributed under the License is distributed on an "AS IS" BASIS,
307// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
308// See the License for the specific language governing permissions and
309// limitations under the License.
310// ----------------------------- END-OF-FILE ----------------------------------
311
312/** @} */
313/** @} */
314/** @} */
Definition bslh_spookyhashalgorithm.h:362
result_type computeHash()
Definition bslh_spookyhashalgorithm.h:476
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_cstringhash.h:234
const char * argument_type
Definition bdlb_cstringhash.h:237
CStringHash & operator=(const CStringHash &rhs)=default
~CStringHash()=default
Destroy this object.
BSLMF_NESTED_TRAIT_DECLARATION(CStringHash, bsl::is_trivially_copyable)
CStringHash()=default
Create a CStringHash object.
BSLMF_NESTED_TRAIT_DECLARATION(CStringHash, bsl::is_trivially_default_constructible)
CStringHash(const CStringHash &original)=default
bsl::size_t result_type
Definition bdlb_cstringhash.h:238
bsl::size_t operator()(const char *argument) const
Definition bdlb_cstringhash.h:282
Definition bslmf_istriviallycopyable.h:329
Definition bslmf_istriviallydefaultconstructible.h:293