BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslh_hashpair.h
Go to the documentation of this file.
1/// @file bslh_hashpair.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslh_hashpair.h -*-C++-*-
8#ifndef INCLUDED_BSLH_HASHPAIR
9#define INCLUDED_BSLH_HASHPAIR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslh_hashpair bslh_hashpair
15/// @brief Provide `hashAppend` for `std::pair`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslh
19/// @{
20/// @addtogroup bslh_hashpair
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslh_hashpair-purpose"> Purpose</a>
25/// * <a href="#bslh_hashpair-description"> Description </a>
26/// * <a href="#bslh_hashpair-usage"> Usage </a>
27/// * <a href="#bslh_hashpair-example-1-hashing-a-pair-of-integer-values"> Example 1: Hashing a Pair of Integer Values </a>
28///
29/// # Purpose {#bslh_hashpair-purpose}
30/// Provide `hashAppend` for `std::pair`.
31///
32/// # Description {#bslh_hashpair-description}
33/// This component provides a free function template,
34/// `bslh::hashAppend`, overloaded for the `std::pair` class template.
35/// Including this function allows for `std::pair` types (and types that contain
36/// them) to be used as keys in BDE hashed containers.
37///
38/// ## Usage {#bslh_hashpair-usage}
39///
40///
41/// This section illustrates intended usage of this component.
42///
43/// ### Example 1: Hashing a Pair of Integer Values {#bslh_hashpair-example-1-hashing-a-pair-of-integer-values}
44///
45///
46/// Suppose one must compute has that combines the hashes of several integer
47/// values, each of a different type:
48/// @code
49/// char c = 'a';
50/// short s = static_cast<short>(1);
51/// @endcode
52/// First, we can make that calculation by repeated invocations of a
53/// 'bslh::DefaultHashAlogorithm object:
54/// @code
55/// bslh::DefaultHashAlgorithm hasherS;
56/// hasherS(&c, sizeof(char));
57/// hasherS(&s, sizeof(short));
58///
59/// bslh::DefaultHashAlgorithm::result_type hashS = hasherS.computeHash();
60/// @endcode
61/// Now, the same calculation can also be be made if those same values are
62/// contained in a single `std::pair` object.
63/// @code
64/// std::pair<char, short> t = std::make_pair(c, s);
65///
66/// bslh::DefaultHashAlgorithm hasherT;
67/// bslh::hashAppend(hasherT, t);
68///
69/// bslh::DefaultHashAlgorithm::result_type hashT = hasherT.computeHash();
70/// @endcode
71/// Finally, we confirm that we computed the same result.
72/// @code
73/// assert(hashS == hashT);
74/// @endcode
75/// @}
76/** @} */
77/** @} */
78
79/** @addtogroup bsl
80 * @{
81 */
82/** @addtogroup bslh
83 * @{
84 */
85/** @addtogroup bslh_hashpair
86 * @{
87 */
88
89#include <bslscm_version.h>
90
91#include <bslh_hash.h>
92
93#include <utility> // 'std::pair'
94
95
96namespace bslh {
97
98// FREE FUNCTIONS
99
100/// Invoke the (appropriate) `hashAppend` function, with the specified
101/// `algorithm` on the `first` and `second` members, in that order, of the
102/// specified `input` pair.
103template <class HASH_ALGORITHM, class TYPE1, class TYPE2>
104void
105hashAppend(HASH_ALGORITHM& algorithm, const std::pair<TYPE1, TYPE2>& input);
106
107// ============================================================================
108// INLINE DEFINITIONS
109// ============================================================================
110
111// FREE FUNCTIONS
112template <class HASH_ALGORITHM, class TYPE1, class TYPE2>
113inline
114void
115hashAppend(HASH_ALGORITHM& algorithm, const std::pair<TYPE1, TYPE2>& input)
116{
117 hashAppend(algorithm, input.first);
118 hashAppend(algorithm, input.second);
119}
120
121} // close package namespace
122
123
124#endif
125
126// ----------------------------------------------------------------------------
127// Copyright 2021 Bloomberg Finance L.P.
128//
129// Licensed under the Apache License, Version 2.0 (the "License");
130// you may not use this file except in compliance with the License.
131// You may obtain a copy of the License at
132//
133// http://www.apache.org/licenses/LICENSE-2.0
134//
135// Unless required by applicable law or agreed to in writing, software
136// distributed under the License is distributed on an "AS IS" BASIS,
137// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138// See the License for the specific language governing permissions and
139// limitations under the License.
140// ----------------------------- END-OF-FILE ----------------------------------
141
142/** @} */
143/** @} */
144/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslh_defaulthashalgorithm.h:339
bsl::enable_if<(bsl::is_integral< TYPE >::value||bsl::is_pointer< TYPE >::value||bsl::is_enum< TYPE >::value)&&!bsl::is_same< TYPE, bool >::value >::type hashAppend(HASH_ALGORITHM &hashAlg, TYPE input)
Definition bslh_hash.h:638