BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_transparentstringhash.h
Go to the documentation of this file.
1/// @file bdlb_transparentstringhash.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_transparentstringhash.h -*-C++-*-
8#ifndef INCLUDED_BDLB_TRANSPARENTSTRINGHASH
9#define INCLUDED_BDLB_TRANSPARENTSTRINGHASH
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_transparentstringhash bdlb_transparentstringhash
15/// @brief Provide a transparent hash functor.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_transparentstringhash
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_transparentstringhash-purpose"> Purpose</a>
25/// * <a href="#bdlb_transparentstringhash-classes"> Classes </a>
26/// * <a href="#bdlb_transparentstringhash-description"> Description </a>
27/// * <a href="#bdlb_transparentstringhash-usage"> Usage </a>
28/// * <a href="#bdlb_transparentstringhash-example-1-basic-use-of-bdlb-transparentstringhash"> Example 1: Basic Use of bdlb::TransparentStringHash </a>
29///
30/// # Purpose {#bdlb_transparentstringhash-purpose}
31/// Provide a transparent hash functor.
32///
33/// # Classes {#bdlb_transparentstringhash-classes}
34///
35/// - bdlb::TransparentStringHash: a transparent hash functor for strings
36///
37/// @see bsl_unordered_map, bsl_unordered_set
38///
39/// # Description {#bdlb_transparentstringhash-description}
40/// This component provides a `struct`,
41/// `bdlb::TransparentStringHash`, that defines a functor to generate a hash
42/// code for different 'string-like' types and can be used as transparent hash
43/// functor for heterogeneous lookup.
44///
45/// ## Usage {#bdlb_transparentstringhash-usage}
46///
47///
48/// This section illustrates intended use of this component.
49///
50/// ### Example 1: Basic Use of bdlb::TransparentStringHash {#bdlb_transparentstringhash-example-1-basic-use-of-bdlb-transparentstringhash}
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::TransparentStringHash` as a hash functor for
64/// the 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::TransparentStringHash`. We use
101/// the transparent comparator defined above to avoid implicit conversions:
102/// @code
103/// typedef bsl::unordered_set<bsl::string,
104/// bdlb::TransparentStringHash,
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/// const char *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_transparentstringhash
136 * @{
137 */
138
139#include <bdlscm_version.h>
140
141#include <bsl_string.h> // 'bsl::basic_string'
142#include <bsl_string_view.h> // 'bsl::basic_string_view'
143
144#include <bsl_functional.h> // 'bsl::hash'
145
146#include <string> // 'std::string'
147#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
148#include <string_view> // 'std::string_view'
149#endif
150
151
152namespace bdlb {
153
154 // ============================
155 // struct TransparentStringHash
156 // ============================
157
158/// This `struct` defines a hash operation for different string types, enabling
159/// them for use for heterogeneous lookup in the standard associative containers such as `bsl::unordered_map`.
160///
161/// \note Note that this class is an
162/// empty POD type.
163///
164/// See @ref bdlb_transparentstringhash
166
167 // TYPES
168
169 /// Type alias indicating this is a transparent hash functor.
170 typedef void is_transparent;
171
172 // CREATORS
173
174 /// Create a `TransparentStringHash` object.
176
177 /// Create a `TransparentStringHash` object.
178 /// \note Note that as
179 /// `TransparentStringHash` is an empty (stateless) type, this operation
180 /// has no observable effect.
182
183 /// Destroy this object.
185
186 // MANIPULATORS
187
188 /// Assign to this object the value of the specified `rhs` object, and
189 /// return a reference providing modifiable access to this object.
190 ///
191 /// \note Note that as `TransparentStringHash` is an empty (stateless) type, this
192 /// operation has no observable effect.
194
195 // ACCESSORS
196
197 /// Return a hash code generated from the contents of the specified
198 /// `str`.
199 template <class CHAR>
200 std::size_t operator()(const CHAR *str) const;
201
202 template <class CHAR, class TRAITS, class ALLOCATOR>
203 std::size_t operator()(
205
206 template <class CHAR, class TRAITS, class ALLOCATOR>
207 std::size_t operator()(
208 const std::basic_string<CHAR, TRAITS, ALLOCATOR>& str) const;
209
210#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
211 template <class CHAR, class TRAITS>
212 std::size_t operator()(
213 const std::basic_string_view<CHAR, TRAITS>& str) const;
214#endif
215
216#ifndef BSLSTL_STRING_VIEW_IS_ALIASED
217 template <class CHAR, class TRAITS>
218 std::size_t operator()(
219 const bsl::basic_string_view<CHAR, TRAITS>& str) const;
220#endif
221};
222
223// ============================================================================
224// INLINE DEFINITIONS
225// ============================================================================
226
227 // ----------------------------
228 // struct TransparentStringHash
229 // ----------------------------
230
231template <class CHAR>
232inline std::size_t
234{
236 return bsl::hash<bsl::basic_string_view<CHAR> >().operator()(sv);
237}
238
239template <class CHAR, class TRAITS, class ALLOCATOR>
240inline std::size_t
247
248template <class CHAR, class TRAITS, class ALLOCATOR>
249inline std::size_t
251 const std::basic_string<CHAR, TRAITS, ALLOCATOR>& str) const
252{
253 bsl::basic_string_view<CHAR> sv(str.data(), str.size());
254 return bsl::hash<bsl::basic_string_view<CHAR> >().operator()(sv);
255}
256
257#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
258template <class CHAR, class TRAITS>
259inline std::size_t
261 const std::basic_string_view<CHAR, TRAITS>& str) const
262{
263 bsl::basic_string_view<CHAR> sv(str.data(), str.size());
264 return bsl::hash<bsl::basic_string_view<CHAR> >().operator()(sv);
265}
266#endif
267
268#ifndef BSLSTL_STRING_VIEW_IS_ALIASED
269template <class CHAR, class TRAITS>
270inline std::size_t
276#endif
277
278} // close package namespace
279
280
281#endif
282
283// ----------------------------------------------------------------------------
284// Copyright 2024 Bloomberg Finance L.P.
285//
286// Licensed under the Apache License, Version 2.0 (the "License");
287// you may not use this file except in compliance with the License.
288// You may obtain a copy of the License at
289//
290// http://www.apache.org/licenses/LICENSE-2.0
291//
292// Unless required by applicable law or agreed to in writing, software
293// distributed under the License is distributed on an "AS IS" BASIS,
294// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
295// See the License for the specific language governing permissions and
296// limitations under the License.
297// ----------------------------- END-OF-FILE ----------------------------------
298
299/** @} */
300/** @} */
301/** @} */
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
size_type size() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:7292
CHAR_TYPE * data() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:7177
#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_transparentstringhash.h:165
void is_transparent
Type alias indicating this is a transparent hash functor.
Definition bdlb_transparentstringhash.h:170
TransparentStringHash(const TransparentStringHash &original)=default
TransparentStringHash()=default
Create a TransparentStringHash object.
std::size_t operator()(const CHAR *str) const
Definition bdlb_transparentstringhash.h:233
~TransparentStringHash()=default
Destroy this object.
TransparentStringHash & operator=(const TransparentStringHash &rhs)=default
Definition bslstl_hash.h:495