BDE 4.14.0 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.
144template <class HASH_KEY>
146
147 // TYPES
148 typedef ::bsl::hash<HASH_KEY> Type;
149};
150
151/// Partial specialization to treat `const` qualified types in exactly the
152/// same way as the non-`const` qualified type. Users should rarely, if
153/// ever, need this specialization but would be surprised by their results
154/// if used accidentally, and it were not supplied..
155template <class HASH_KEY>
156struct HashSelector<const HASH_KEY> {
157
158 // TYPES
160};
161
162template <>
163struct HashSelector<const char *> {
165};
166
167template <>
168struct HashSelector<char> {
170};
171
172template <>
173struct HashSelector<signed char> {
175};
176
177template <>
178struct HashSelector<unsigned char> {
180};
181
182template <>
183struct HashSelector<short> {
185};
186
187template <>
188struct HashSelector<unsigned short> {
190};
191
192template <>
193struct HashSelector<int> {
195};
196
197template <>
198struct HashSelector<unsigned int> {
200};
201
202template <>
203struct HashSelector<long> {
205};
206
207template <>
208struct HashSelector<unsigned long> {
210};
211
212template <>
213struct HashSelector<long long> {
215};
216
217template <>
218struct HashSelector<unsigned long long> {
220};
221
222 // ==================
223 // struct HashCString
224 // ==================
225
226/// Hash functor to generate a hash for a pointer to a null-terminated
227/// string.
229
230 // TRAITS
232
233 // ACCESSORS
234
235 /// Return a hash value computed using the specified `s`.
236 std::size_t operator()(const char *s) const
237 {
238 unsigned long result = 0;
239
240 for (; *s; ++s) {
241 result = 5 * result + *s;
242 }
243
244 return std::size_t(result);
245 }
246};
247
248 // =============================================
249 // explicit class bslstp::Hash<> specializations
250 // =============================================
251
252/// Specialization of `Hash` for `char` values.
253template <>
254struct Hash<char> {
255
256 // TRAITS
258
259 // ACCESSORS
260
261 /// Return a hash value computed using the specified `x`.
262 std::size_t operator()(char x) const
263 {
264 return x;
265 }
266};
267
268/// Specialization of `Hash` for `unsigned` `char` values.
269template <>
270struct Hash<unsigned char> {
271
272 // TRAITS
274
275 // ACCESSORS
276
277 /// Return a hash value computed using the specified `x`.
278 std::size_t operator()(unsigned char x) const
279 {
280 return x;
281 }
282};
283
284/// Specialization of `Hash` for `signed` `char` values.
285template <>
286struct Hash<signed char> {
287
288 // TRAITS
290
291 // ACCESSORS
292
293 /// Return a hash value computed using the specified `x`.
294 std::size_t operator()(signed char x) const
295 {
296 return x;
297 }
298};
299
300/// Specialization of `Hash` for `short` values.
301template <>
302struct Hash<short> {
303
304 // TRAITS
306
307 // ACCESSORS
308
309 /// Return a hash value computed using the specified `x`.
310 std::size_t operator()(short x) const
311 {
312 return x;
313 }
314};
315
316/// Specialization of `Hash` for `unsigned` `short` values.
317template <>
318struct Hash<unsigned short> {
319
320 // TRAITS
322
323 // ACCESSORS
324
325 /// Return a hash value computed using the specified `x`.
326 std::size_t operator()(unsigned short x) const
327 {
328 return x;
329 }
330};
331
332/// Specialization of `Hash` for `int` values.
333template <>
334struct Hash<int> {
335
336 // TRAITS
338
339 // ACCESSORS
340
341 /// Return a hash value computed using the specified `x`.
342 std::size_t operator()(int x) const
343 {
344 return x;
345 }
346};
347
348/// Specialization of `Hash` for `unsigned` `int` values.
349template <>
350struct Hash<unsigned int> {
351
352 // TRAITS
354
355 // ACCESSORS
356
357 /// Return a hash value computed using the specified `x`.
358 std::size_t operator()(unsigned int x) const
359 {
360 return x;
361 }
362};
363
364/// Specialization of `Hash` for `long` values.
365template <>
366struct Hash<long> {
367
368 // TRAITS
370
371 // ACCESSORS
372
373 /// Return a hash value computed using the specified `x`.
374 std::size_t operator()(long x) const
375 {
376 return x;
377 }
378};
379
380/// Specialization of `Hash` for `unsigned` `long` values.
381template <>
382struct Hash<unsigned long> {
383
384 // TRAITS
386
387 // ACCESSORS
388
389 /// Return a hash value computed using the specified `x`.
390 std::size_t operator()(unsigned long x) const
391 {
392 return x;
393 }
394};
395
396#ifdef BSLS_PLATFORM_CPU_64_BIT
397/// Specialization of `Hash` for `long long` values.
398template <>
399struct Hash<long long> {
400
401 // TRAITS
403
404 // ACCESSORS
405
406 /// Return a hash value computed using the specified `x`.
407 std::size_t operator()(long long x) const
408 {
409 return x;
410 }
411};
412
413/// Specialization of `Hash` for `unsigned` `long long` values.
414template <>
415struct Hash<unsigned long long> {
416
417 // TRAITS
419
420 // ACCESSORS
421
422 /// Return a hash value computed using the specified `x`.
423 std::size_t operator()(unsigned long long x) const
424 {
425 return x;
426 }
427};
428
429#else // BSLS_PLATFORM_CPU_32_BIT
430
431template <>
432struct Hash<long long> {
433 // Specialization of 'Hash' for 'long long' values.
434
435 // TRAITS
437
438 // ACCESSORS
439 std::size_t operator()(unsigned long long x) const
440 // Return a hash value computed using the specified 'x'.
441 {
442 return (std::size_t)(x ^ (x >> 32));
443 }
444};
445
446template <>
447struct Hash<unsigned long long> {
448 // Specialization of 'Hash' for 'unsigned' 'long long' values.
449
450 // TRAITS
452
453 // ACCESSORS
454 std::size_t operator()(unsigned long long x) const
455 // Return a hash value computed using the specified 'x'.
456 {
457 return (std::size_t)(x ^ (x >> 32));
458 }
459};
460#endif // BSLS_PLATFORM_CPU_64_BIT
461
462} // close package namespace
463
464
465#endif
466
467// ----------------------------------------------------------------------------
468// Copyright 2013 Bloomberg Finance L.P.
469//
470// Licensed under the Apache License, Version 2.0 (the "License");
471// you may not use this file except in compliance with the License.
472// You may obtain a copy of the License at
473//
474// http://www.apache.org/licenses/LICENSE-2.0
475//
476// Unless required by applicable law or agreed to in writing, software
477// distributed under the License is distributed on an "AS IS" BASIS,
478// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
479// See the License for the specific language governing permissions and
480// limitations under the License.
481// ----------------------------- END-OF-FILE ----------------------------------
482
483/** @} */
484/** @} */
485/** @} */
#define BSLMF_NESTED_TRAIT_DECLARATION(t_TYPE, t_TRAIT)
Definition bslmf_nestedtraitdeclaration.h:231
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslstp_exfunctional.h:323
Definition bslstl_hash.h:498
Definition bslmf_istriviallycopyable.h:329
Definition bslstp_hash.h:228
std::size_t operator()(const char *s) const
Return a hash value computed using the specified s.
Definition bslstp_hash.h:236
BSLMF_NESTED_TRAIT_DECLARATION(HashCString, bsl::is_trivially_copyable)
Hash< char > Type
Definition bslstp_hash.h:169
HashSelector< HASH_KEY >::Type Type
Definition bslstp_hash.h:159
HashCString Type
Definition bslstp_hash.h:164
Hash< int > Type
Definition bslstp_hash.h:194
Hash< long > Type
Definition bslstp_hash.h:204
Hash< long long > Type
Definition bslstp_hash.h:214
Hash< short > Type
Definition bslstp_hash.h:184
Hash< signed char > Type
Definition bslstp_hash.h:174
Hash< unsigned char > Type
Definition bslstp_hash.h:179
Hash< unsigned int > Type
Definition bslstp_hash.h:199
Hash< unsigned long > Type
Definition bslstp_hash.h:209
Hash< unsigned long long > Type
Definition bslstp_hash.h:219
Hash< unsigned short > Type
Definition bslstp_hash.h:189
Definition bslstp_hash.h:145
::bsl::hash< HASH_KEY > Type
Definition bslstp_hash.h:148
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:262
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:342
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:374
std::size_t operator()(unsigned long long x) const
Definition bslstp_hash.h:439
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:310
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:294
std::size_t operator()(unsigned char x) const
Return a hash value computed using the specified x.
Definition bslstp_hash.h:278
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:358
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:390
BSLMF_NESTED_TRAIT_DECLARATION(Hash, bsl::is_trivially_copyable)
std::size_t operator()(unsigned long long x) const
Definition bslstp_hash.h:454
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:326
Definition bslstp_hash.h:121