BDE 4.14.0 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 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///
45/// ## Usage {#bdlb_transparenthash-usage}
46///
47///
48/// This section illustrates intended use of this component.
49///
50/// ### Example 1: Basic Use of bdlb::TransparentHash {#bdlb_transparenthash-example-1-basic-use-of-bdlb-transparenthash}
51///
52///
53/// Suppose we need a container to store set of `bsl::string` unique objects.
54/// `bsl::unordered_set` is designed exactly for this purpose. But imagine that
55/// we want to use `bsl::string_view` objects for search operations within our
56/// container. `bsl::unordered_set` uses `bsl::hash` as default hash functor.
57/// The problem is that even though the hash function for `bsl::string_view`
58/// exists, compiler tries to convert `bsl::string_view` objects to the
59/// `bsl::string` since `bsl::hash` is parameterized by `bsl::string`. And
60/// compilation fails, because there is no such implicit conversion. In
61/// addition, implicit conversions where they are available, may lead to
62/// additional memory allocation for temporary objects. The following code
63/// illustrates how to use `bdlb::TransparentHash` as a hash functor for the
64/// standard container `unordered_set`, in this case to allow a
65/// `bsl::unordered_set<bsl::string>` to be searched with a `bsl::string_view`.
66///
67/// First, we define a transparent equality predicate, that is required by the
68/// `bsl::unordered_set` along with the transparent hash:
69/// @code
70/// // =============================
71/// // struct TestTransparentEqualTo
72/// // =============================
73///
74/// /// This `struct` defines an equality of objects of different types,
75/// /// enabling them for use for heterogeneous comparison in the standard
76/// /// associative containers such as `bsl::unordered_map`. Note that this
77/// /// class is an empty POD type.
78/// struct TestTransparentEqualTo {
79///
80/// // TYPES
81///
82/// // Type alias indicating this is a transparent comparator.
83/// typedef void is_transparent;
84///
85/// // ACCESSORS
86///
87/// /// Return `true` if the specified `lhs` is equal to the specified
88/// /// `rhs` and `false` otherwise.
89/// template <class LHS, class RHS>
90/// bool operator()(const LHS& lhs, const RHS& rhs) const
91/// {
92/// return lhs == rhs;
93/// }
94/// };
95/// @endcode
96/// Note that this struct is defined only to avoid cycle dependencies between
97/// BDE components. In real code for these purposes it is recommended to use
98/// `bdlb::TransparentEqualTo`.
99///
100/// Then, we create a container that uses `bdlb::TransparentHash`. We use the
101/// transparent comparator defined above to avoid implicit conversions:
102/// @code
103/// typedef bsl::unordered_set<bsl::string,
104/// bdlb::TransparentHash,
105/// TestTransparentEqualTo> TransparentHashSet;
106///
107/// TransparentHashSet transparentSet;
108/// @endcode
109/// Now, we fill the container with the strings:
110/// @code
111/// transparentSet.insert("NY");
112/// transparentSet.insert("LA");
113/// @endcode
114/// Finally, we observe that the container allows to use `bsl::string_view`
115/// objects as a key and does not make any implicit conversions:
116/// @code
117/// bsl::string_view newYork ("NY");
118/// bsl::string_view losAngeles ("LA");
119/// bsl::string_view sanFrancisco("SF");
120///
121/// assert(transparentSet.end() != transparentSet.find(newYork ));
122/// assert(transparentSet.end() != transparentSet.find(losAngeles ));
123/// assert(transparentSet.end() == transparentSet.find(sanFrancisco));
124/// @endcode
125/// @}
126/** @} */
127/** @} */
128
129/** @addtogroup bdl
130 * @{
131 */
132/** @addtogroup bdlb
133 * @{
134 */
135/** @addtogroup bdlb_transparenthash
136 * @{
137 */
138
139#include <bdlscm_version.h>
140
144
145#include <bsl_functional.h>
146
147
148namespace bdlb {
149
150 // ======================
151 // struct TransparentHash
152 // ======================
153
154/// This `struct` defines a hash operation for different types, enabling
155/// them for use for heterogeneous lookup in the standard associative
156/// containers such as `bsl::unordered_map`. Note that this class is an
157/// empty POD type.
159
160 // TYPES
161
162 /// Type alias indicating this is a transparent hash functor.
163 typedef void is_transparent;
164
165 // CREATORS
166
167 /// Create a `TransparentHash` object.
168 TransparentHash() = default;
169
170 /// Create a `TransparentHash` object. Note that as `TransparentHash`
171 /// is an empty (stateless) type, this operation has no observable
172 /// effect.
173 TransparentHash(const TransparentHash& original) = default;
174
175 /// Destroy this object.
176 ~TransparentHash() = default;
177
178 // MANIPULATORS
179
180 /// Assign to this object the value of the specified `rhs` object, and
181 /// return a reference providing modifiable access to this object.
182 /// Note that as `TransparentHash` is an empty (stateless) type, this
183 /// operation has no observable effect.
185
186 // ACCESSORS
187
188 /// Return a hash code generated from the contents of the specified
189 /// `value`.
190 template <class TYPE>
191 std::size_t operator()(const TYPE &value) const;
192};
193
194// ============================================================================
195// INLINE DEFINITIONS
196// ============================================================================
197
198 // ---------------------
199 // struct TransparentHash
200 // ---------------------
201
202// ACCESSORS
203template <class TYPE>
204inline
205std::size_t TransparentHash::operator()(const TYPE& value) const
206{
207 return bsl::hash<TYPE>().operator()(value);
208}
209
210} // close package namespace
211
212
213#endif
214
215// ----------------------------------------------------------------------------
216// Copyright 2021 Bloomberg Finance L.P.
217//
218// Licensed under the Apache License, Version 2.0 (the "License");
219// you may not use this file except in compliance with the License.
220// You may obtain a copy of the License at
221//
222// http://www.apache.org/licenses/LICENSE-2.0
223//
224// Unless required by applicable law or agreed to in writing, software
225// distributed under the License is distributed on an "AS IS" BASIS,
226// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
227// See the License for the specific language governing permissions and
228// limitations under the License.
229// ----------------------------- END-OF-FILE ----------------------------------
230
231/** @} */
232/** @} */
233/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_transparenthash.h:158
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:163
std::size_t operator()(const TYPE &value) const
Definition bdlb_transparenthash.h:205
TransparentHash(const TransparentHash &original)=default
~TransparentHash()=default
Destroy this object.
Definition bslstl_hash.h:498