BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_transparenthash.h
Go to the documentation of this file.
1/// @file bdlb_transparenthash.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_transparenthash.h -*-C++-*-
8#ifndef INCLUDED_BDLB_TRANSPARENTHASH
9#define INCLUDED_BDLB_TRANSPARENTHASH
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_transparenthash bdlb_transparenthash
15/// @brief Provide a transparent hash functor.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_transparenthash
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_transparenthash-purpose"> Purpose</a>
25/// * <a href="#bdlb_transparenthash-classes"> Classes </a>
26/// * <a href="#bdlb_transparenthash-description"> Description </a>
27/// * <a href="#bdlb_transparenthash-usage"> Usage </a>
28/// * <a href="#bdlb_transparenthash-example-1-basic-use-of-bdlb-transparenthash"> Example 1: Basic Use of bdlb::TransparentHash </a>
29///
30/// # Purpose {#bdlb_transparenthash-purpose}
31/// Provide a transparent hash functor.
32///
33/// # Classes {#bdlb_transparenthash-classes}
34///
35/// - bdlb::TransparentHash: a transparent hash functor
36///
37/// @see bdlb::TransparentStringHash, bsl_map, bsl_set
38///
39/// # Description {#bdlb_transparenthash-description}
40/// This component provides a `struct`, `bdlb::TransparentHash`,
41/// that defines a functor to generate a hash code for different types and can
42/// be used as transparent hash functor for heterogeneous lookup.
43///
44/// `TransparentHash` should not be used for hashing strings, however, because
45/// it gives unexpected results for `char *`s. `TransparentStringHash` should
46/// be used instead.
47///
48///
49/// ## Usage {#bdlb_transparenthash-usage}
50///
51///
52/// This section illustrates intended use of this component.
53///
54/// ### Example 1: Basic Use of bdlb::TransparentHash {#bdlb_transparenthash-example-1-basic-use-of-bdlb-transparenthash}
55///
56///
57/// Suppose we need a container to store set of `bsl::string` unique objects.
58/// `bsl::unordered_set` is designed exactly for this purpose. But imagine that
59/// we want to use `bsl::string_view` objects for search operations within our
60/// container. `bsl::unordered_set` uses `bsl::hash` as default hash functor.
61/// The problem is that even though the hash function for `bsl::string_view`
62/// exists, compiler tries to convert `bsl::string_view` objects to the
63/// `bsl::string` since `bsl::hash` is parameterized by `bsl::string`. And
64/// compilation fails, because there is no such implicit conversion. In
65/// addition, implicit conversions where they are available, may lead to
66/// additional memory allocation for temporary objects. The following code
67/// illustrates how to use `bdlb::TransparentHash` as a hash functor for the
68/// standard container `unordered_set`, in this case to allow a
69/// `bsl::unordered_set<bsl::string>` to be searched with a `bsl::string_view`.
70///
71/// First, we define a transparent equality predicate, that is required by the
72/// `bsl::unordered_set` along with the transparent hash:
73/// @code
74/// // =============================
75/// // struct TestTransparentEqualTo
76/// // =============================
77///
78/// /// This `struct` defines an equality of objects of different types,
79/// /// enabling them for use for heterogeneous comparison in the standard
80/// /// associative containers such as `bsl::unordered_map`. Note that this
81/// /// class is an empty POD type.
82/// struct TestTransparentEqualTo {
83///
84/// // TYPES
85///
86/// // Type alias indicating this is a transparent comparator.
87/// typedef void is_transparent;
88///
89/// // ACCESSORS
90///
91/// /// Return `true` if the specified `lhs` is equal to the specified
92/// /// `rhs` and `false` otherwise.
93/// template <class LHS, class RHS>
94/// bool operator()(const LHS& lhs, const RHS& rhs) const
95/// {
96/// return lhs == rhs;
97/// }
98/// };
99/// @endcode
100/// Note that this struct is defined only to avoid cycle dependencies between
101/// BDE components. In real code for these purposes it is recommended to use
102/// `bdlb::TransparentEqualTo`.
103///
104/// Then, we create a container that uses `bdlb::TransparentHash`. We use the
105/// transparent comparator defined above to avoid implicit conversions:
106/// @code
107/// typedef bsl::unordered_set<bsl::optional<int>,
108/// bdlb::TransparentHash,
109/// TestTransparentEqualTo> TransparentHashSet;
110///
111/// TransparentHashSet transparentSet;
112/// @endcode
113/// Now, we fill the container with the strings:
114/// @code
115/// transparentSet.insert(10001); // New York
116/// transparentSet.insert(90001); // Los Angeles
117/// @endcode
118/// Finally, we observe that the container allows to use `bsl::string_view`
119/// objects as a key and does not make any implicit conversions:
120/// @code
121/// int newYork = 10001;
122/// int losAngeles = 90001;
123/// bsl::optional<int> sanFrancisco = 94102;
124/// bsl::optional<int> nowhere;
125///
126/// ASSERT(transparentSet.end() != transparentSet.find(newYork ));
127/// ASSERT(transparentSet.end() != transparentSet.find(losAngeles ));
128/// ASSERT(transparentSet.end() == transparentSet.find(sanFrancisco));
129/// ASSERT(transparentSet.end() == transparentSet.find(nowhere ));
130/// @endcode
131/// @}
132/** @} */
133/** @} */
134
135/** @addtogroup bdl
136 * @{
137 */
138/** @addtogroup bdlb
139 * @{
140 */
141/** @addtogroup bdlb_transparenthash
142 * @{
143 */
144
145#include <bdlscm_version.h>
146
150
151#include <bsl_functional.h>
152
153
154namespace bdlb {
155
156 // ======================
157 // struct TransparentHash
158 // ======================
159
160/// This `struct` defines a hash operation for different types, enabling
161/// them for use for heterogeneous lookup in the standard associative containers such as `bsl::unordered_map`.
162///
163/// \note Note that this class is an
164/// empty POD type.
165///
166/// See @ref bdlb_transparenthash
168
169 // TYPES
170
171 /// Type alias indicating this is a transparent hash functor.
172 typedef void is_transparent;
173
174 // CREATORS
175
176 /// Create a `TransparentHash` object.
177 TransparentHash() = default;
178
179 /// Create a `TransparentHash` object.
180 /// \note Note that as `TransparentHash`
181 /// is an empty (stateless) type, this operation has no observable
182 /// effect.
183 TransparentHash(const TransparentHash& original) = default;
184
185 /// Destroy this object.
186 ~TransparentHash() = default;
187
188 // MANIPULATORS
189
190 /// Assign to this object the value of the specified `rhs` object, and
191 /// return a reference providing modifiable access to this object.
192 ///
193 /// \note Note that as `TransparentHash` is an empty (stateless) type, this
194 /// operation has no observable effect.
196
197 // ACCESSORS
198
199 /// Return a hash code generated from the contents of the specified
200 /// `value`.
201 template <class TYPE>
202 std::size_t operator()(const TYPE &value) const;
203};
204
205// ============================================================================
206// INLINE DEFINITIONS
207// ============================================================================
208
209 // ---------------------
210 // struct TransparentHash
211 // ---------------------
212
213// ACCESSORS
214template <class TYPE>
215inline
216std::size_t TransparentHash::operator()(const TYPE& value) const
217{
218 return bsl::hash<TYPE>().operator()(value);
219}
220
221} // close package namespace
222
223
224#endif
225
226// ----------------------------------------------------------------------------
227// Copyright 2021 Bloomberg Finance L.P.
228//
229// Licensed under the Apache License, Version 2.0 (the "License");
230// you may not use this file except in compliance with the License.
231// You may obtain a copy of the License at
232//
233// http://www.apache.org/licenses/LICENSE-2.0
234//
235// Unless required by applicable law or agreed to in writing, software
236// distributed under the License is distributed on an "AS IS" BASIS,
237// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
238// See the License for the specific language governing permissions and
239// limitations under the License.
240// ----------------------------- END-OF-FILE ----------------------------------
241
242/** @} */
243/** @} */
244/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_transparenthash.h:167
TransparentHash & operator=(const TransparentHash &rhs)=default
TransparentHash()=default
Create a TransparentHash object.
void is_transparent
Type alias indicating this is a transparent hash functor.
Definition bdlb_transparenthash.h:172
std::size_t operator()(const TYPE &value) const
Definition bdlb_transparenthash.h:216
TransparentHash(const TransparentHash &original)=default
~TransparentHash()=default
Destroy this object.
Definition bslstl_hash.h:495