BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstp_hash.h
Go to the documentation of this file.
1/// @file bslstp_hash.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstp_hash.h -*-C++-*-
8#ifndef INCLUDED_BSLSTP_HASH
9#define INCLUDED_BSLSTP_HASH
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstp_hash bslstp_hash
15/// @brief Provide a namespace for hash functions.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstp
19/// @{
20/// @addtogroup bslstp_hash
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstp_hash-purpose"> Purpose</a>
25/// * <a href="#bslstp_hash-classes"> Classes </a>
26/// * <a href="#bslstp_hash-description"> Description </a>
27/// * <a href="#bslstp_hash-usage"> Usage </a>
28///
29/// # Purpose {#bslstp_hash-purpose}
30/// Provide a namespace for hash functions.
31///
32/// @deprecated Do not use directly.
33///
34/// # Classes {#bslstp_hash-classes}
35///
36/// - bslstp::Hash: hash function for primitive types
37/// - bslstp::HashCString: hash function pointers to null-terminated strings
38/// - bslstp::HashSelector: metafunction to select a preferred hash functor type
39///
40/// @see bslalg_hashutil
41///
42/// # Description {#bslstp_hash-description}
43/// This component provides a namespace for hash functions used by
44/// `hash_map` and `hash_set`.
45///
46/// Note that the hash functions here are based on STLPort's implementation,
47/// with copyright notice as follows:
48/// @code
49/// -----------------------------------------------------------------------------
50/// Copyright (c) 1996-1998
51/// Silicon Graphics Computer Systems, Inc.
52///
53/// Permission to use, copy, modify, distribute and sell this software
54/// and its documentation for any purpose is hereby granted without fee,
55/// provided that the above copyright notice appear in all copies and
56/// that both that copyright notice and this permission notice appear
57/// in supporting documentation. Silicon Graphics makes no
58/// representations about the suitability of this software for any
59/// purpose. It is provided "as is" without express or implied warranty.
60///
61///
62/// Copyright (c) 1994
63/// Hewlett-Packard Company
64///
65/// Permission to use, copy, modify, distribute and sell this software
66/// and its documentation for any purpose is hereby granted without fee,
67/// provided that the above copyright notice appear in all copies and
68/// that both that copyright notice and this permission notice appear
69/// in supporting documentation. Hewlett-Packard Company makes no
70/// representations about the suitability of this software for any
71/// purpose. It is provided "as is" without express or implied warranty.
72/// -----------------------------------------------------------------------------
73/// @endcode
74///
75/// ## Usage {#bslstp_hash-usage}
76///
77///
78/// This component is for internal use only.
79/// @}
80/** @} */
81/** @} */
82
83/** @addtogroup bsl
84 * @{
85 */
86/** @addtogroup bslstp
87 * @{
88 */
89/** @addtogroup bslstp_hash
90 * @{
91 */
92
93#include <bslscm_version.h>
94
97
98#include <bsls_platform.h>
99
100#include <bslstl_hash.h>
101
102#include <cstddef> // for 'std::size_t'
103
104#ifndef BDE_OMIT_INTERNAL_DEPRECATED
105#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
106#include <bslalg_typetraits.h>
107#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
108#endif // BDE_OMIT_INTERNAL_DEPRECATED
109
110
111namespace bslstp {
112
113 // ==================
114 // class bslst::hash
115 // ==================
116
117/// Empty base class for hashing. No general hash struct defined, each type
118/// requires a specialization. Leaving this struct declared but undefined
119/// will generate error messages that are more clear when someone tries to
120/// use a key that does not have a corresponding hash function.
121template <class HASH_KEY> struct Hash;
122
123template <> struct Hash<char>;
124template <> struct Hash<signed char>;
125template <> struct Hash<unsigned char>;
126template <> struct Hash<short>;
127template <> struct Hash<unsigned short>;
128template <> struct Hash<int>;
129template <> struct Hash<unsigned int>;
130template <> struct Hash<long>;
131template <> struct Hash<unsigned long>;
132template <> struct Hash<long long>;
133template <> struct Hash<unsigned long long>;
134
135struct HashCString;
136
137 // ==========================
138 // class bslstp::HashSelector
139 // ==========================
140
141/// This meta-function selects the appropriate implementation for comparing
142/// the parameterized `TYPE`. This generic template uses the
143/// `std::equal_to` functor.
144///
145/// See @ref bslstp_hash
146template <class HASH_KEY>
148
149 // TYPES
150 typedef ::bsl::hash<HASH_KEY> Type;
151};
152
153/// Partial specialization to treat `const` qualified types in exactly the
154/// same way as the non-`const` qualified type. Users should rarely, if
155/// ever, need this specialization but would be surprised by their results
156/// if used accidentally, and it were not supplied..
157template <class HASH_KEY>
158struct HashSelector<const HASH_KEY> {
159
160 // TYPES
162};
163
164template <>
165struct HashSelector<const char *> {
167};
168
169template <>
170struct HashSelector<char> {
172};
173
174template <>
175struct HashSelector<signed char> {
177};
178
179template <>
180struct HashSelector<unsigned char> {
182};
183
184template <>
185struct HashSelector<short> {
187};
188
189template <>
190struct HashSelector<unsigned short> {
192};
193
194template <>
195struct HashSelector<int> {
197};
198
199template <>
200struct HashSelector<unsigned int> {
202};
203
204template <>
205struct HashSelector<long> {
207};
208
209template <>
210struct HashSelector<unsigned long> {
212};
213
214template <>
215struct HashSelector<long long> {
217};
218
219template <>
220struct HashSelector<unsigned long long> {
222};
223
224 // ==================
225 // struct HashCString
226 // ==================
227
228/// Hash functor to generate a hash for a pointer to a null-terminated
229/// string.
230///
231/// See @ref bslstp_hash
233
234 // TRAITS
236
237 // ACCESSORS
238
239 /// Return a hash value computed using the specified `s`.
240 std::size_t operator()(const char *s) const
241 {
242 unsigned long result = 0;
243
244 for (; *s; ++s) {
245 result = 5 * result + *s;
246 }
247
248 return std::size_t(result);
249 }
250};
251
252 // =============================================
253 // explicit class bslstp::Hash<> specializations
254 // =============================================
255
256/// Specialization of `Hash` for `char` values.
257template <>
258struct Hash<char> {
259
260 // TRAITS
262
263 // ACCESSORS
264
265 /// Return a hash value computed using the specified `x`.
266 std::size_t operator()(char x) const
267 {
268 return x;
269 }
270};
271
272/// Specialization of `Hash` for `unsigned` `char` values.
273template <>
274struct Hash<unsigned char> {
275
276 // TRAITS
278
279 // ACCESSORS
280
281 /// Return a hash value computed using the specified `x`.
282 std::size_t operator()(unsigned char x) const
283 {
284 return x;
285 }
286};
287
288/// Specialization of `Hash` for `signed` `char` values.
289template <>
290struct Hash<signed char> {
291
292 // TRAITS
294
295 // ACCESSORS
296
297 /// Return a hash value computed using the specified `x`.
298 std::size_t operator()(signed char x) const
299 {
300 return x;
301 }
302};
303
304/// Specialization of `Hash` for `short` values.
305template <>
306struct Hash<short> {
307
308 // TRAITS
310
311 // ACCESSORS
312
313 /// Return a hash value computed using the specified `x`.
314 std::size_t operator()(short x) const
315 {
316 return x;
317 }
318};
319
320/// Specialization of `Hash` for `unsigned` `short` values.
321template <>
322struct Hash<unsigned short> {
323
324 // TRAITS
326
327 // ACCESSORS
328
329 /// Return a hash value computed using the specified `x`.
330 std::size_t operator()(unsigned short x) const
331 {
332 return x;
333 }
334};
335
336/// Specialization of `Hash` for `int` values.
337template <>
338struct Hash<int> {
339
340 // TRAITS
342
343 // ACCESSORS
344
345 /// Return a hash value computed using the specified `x`.
346 std::size_t operator()(int x) const
347 {
348 return x;
349 }
350};
351
352/// Specialization of `Hash` for `unsigned` `int` values.
353template <>
354struct Hash<unsigned int> {
355
356 // TRAITS
358
359 // ACCESSORS
360
361 /// Return a hash value computed using the specified `x`.
362 std::size_t operator()(unsigned int x) const
363 {
364 return x;
365 }
366};
367
368/// Specialization of `Hash` for `long` values.
369template <>
370struct Hash<long> {
371
372 // TRAITS
374
375 // ACCESSORS
376
377 /// Return a hash value computed using the specified `x`.
378 std::size_t operator()(long x) const
379 {
380 return x;
381 }
382};
383
384/// Specialization of `Hash` for `unsigned` `long` values.
385template <>
386struct Hash<unsigned long> {
387
388 // TRAITS
390
391 // ACCESSORS
392
393 /// Return a hash value computed using the specified `x`.
394 std::size_t operator()(unsigned long x) const
395 {
396 return x;
397 }
398};
399
400#ifdef BSLS_PLATFORM_CPU_64_BIT
401/// Specialization of `Hash` for `long long` values.
402template <>
403struct Hash<long long> {
404
405 // TRAITS
407
408 // ACCESSORS
409
410 /// Return a hash value computed using the specified `x`.
411 std::size_t operator()(long long x) const
412 {
413 return x;
414 }
415};
416
417/// Specialization of `Hash` for `unsigned` `long long` values.
418template <>
419struct Hash<unsigned long long> {
420
421 // TRAITS
423
424 // ACCESSORS
425
426 /// Return a hash value computed using the specified `x`.
427 std::size_t operator()(unsigned long long x) const
428 {
429 return x;
430 }
431};
432
433#else // BSLS_PLATFORM_CPU_32_BIT
434
435template <>
436struct Hash<long long> {
437 // Specialization of 'Hash' for 'long long' values.
438
439 // TRAITS
441
442 // ACCESSORS
443 std::size_t operator()(unsigned long long x) const
444 // Return a hash value computed using the specified 'x'.
445 {
446 return (std::size_t)(x ^ (x >> 32));
447 }
448};
449
450template <>
451struct Hash<unsigned long long> {
452 // Specialization of 'Hash' for 'unsigned' 'long long' values.
453
454 // TRAITS
456
457 // ACCESSORS
458 std::size_t operator()(unsigned long long x) const
459 // Return a hash value computed using the specified 'x'.
460 {
461 return (std::size_t)(x ^ (x >> 32));
462 }
463};
464#endif // BSLS_PLATFORM_CPU_64_BIT
465
466} // close package namespace
467
468
469#endif
470
471// ----------------------------------------------------------------------------
472// Copyright 2013 Bloomberg Finance L.P.
473//
474// Licensed under the Apache License, Version 2.0 (the "License");
475// you may not use this file except in compliance with the License.
476// You may obtain a copy of the License at
477//
478// http://www.apache.org/licenses/LICENSE-2.0
479//
480// Unless required by applicable law or agreed to in writing, software
481// distributed under the License is distributed on an "AS IS" BASIS,
482// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
483// See the License for the specific language governing permissions and
484// limitations under the License.
485// ----------------------------- END-OF-FILE ----------------------------------
486
487/** @} */
488/** @} */
489/** @} */
#define BSLMF_NESTED_TRAIT_DECLARATION(t_TYPE, t_TRAIT)
Definition bslmf_nestedtraitdeclaration.h:231
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslstp_exfunctional.h:325
Definition bslmf_istriviallycopyable.h:324
Definition bslstp_hash.h:232
std::size_t operator()(const char *s) const
Return a hash value computed using the specified s.
Definition bslstp_hash.h:240
BSLMF_NESTED_TRAIT_DECLARATION(HashCString, bsl::is_trivially_copyable)
Hash< char > Type
Definition bslstp_hash.h:171
HashSelector< HASH_KEY >::Type Type
Definition bslstp_hash.h:161
HashCString Type
Definition bslstp_hash.h:166
Hash< int > Type
Definition bslstp_hash.h:196
Hash< long > Type
Definition bslstp_hash.h:206
Hash< long long > Type
Definition bslstp_hash.h:216
Hash< short > Type
Definition bslstp_hash.h:186
Hash< signed char > Type
Definition bslstp_hash.h:176
Hash< unsigned char > Type
Definition bslstp_hash.h:181
Hash< unsigned int > Type
Definition bslstp_hash.h:201
Hash< unsigned long > Type
Definition bslstp_hash.h:211
Hash< unsigned long long > Type
Definition bslstp_hash.h:221
Hash< unsigned short > Type
Definition bslstp_hash.h:191
Definition bslstp_hash.h:147
::bsl::hash< HASH_KEY > Type
Definition bslstp_hash.h:150
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(char x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:266
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(int x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:346
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(long x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:378
std::size_t operator()(unsigned long long x) const
Definition bslstp_hash.h:443
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(short x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:314
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(signed char x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:298
std::size_t operator()(unsigned char x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:282
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(unsigned int x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:362
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(unsigned long x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:394
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(unsigned long long x) const
Definition bslstp_hash.h:458
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(unsigned short x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:330
Definition bslstp_hash.h:121