BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_caselessstringviewhash.h
Go to the documentation of this file.
1/// @file bdlb_caselessstringviewhash.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_caselessstringviewhash.h -*-C++-*-
8#ifndef INCLUDED_BDLB_CASELESSSTRINGVIEWHASH
9#define INCLUDED_BDLB_CASELESSSTRINGVIEWHASH
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_caselessstringviewhash bdlb_caselessstringviewhash
15/// @brief Provide a case-insensitive hash functor for string views.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_caselessstringviewhash
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_caselessstringviewhash-purpose"> Purpose</a>
25/// * <a href="#bdlb_caselessstringviewhash-classes"> Classes </a>
26/// * <a href="#bdlb_caselessstringviewhash-description"> Description </a>
27/// * <a href="#bdlb_caselessstringviewhash-usage"> Usage </a>
28/// * <a href="#bdlb_caselessstringviewhash-example-1-basic-use-of-bdlb-caselessstringviewhash"> Example 1: Basic Use of bdlb::CaselessStringViewHash: </a>
29///
30/// # Purpose {#bdlb_caselessstringviewhash-purpose}
31/// Provide a case-insensitive hash functor for string views.
32///
33/// # Classes {#bdlb_caselessstringviewhash-classes}
34///
35/// - bdlb::CaselessStringViewHash: case-insensive hash functor
36///
37/// @see bdlb_caselessstringviewequalto, bdlb_caselessstringviewless
38///
39/// # Description {#bdlb_caselessstringviewhash-description}
40/// This component provides a `struct`,
41/// `bdlb::CaselessStringViewHash`, that defines a functor to generate a
42/// case-insensitivee hash code for a string view. This hash functor is
43/// suitable for supporting `bsl::string`s and `bsl::string_view`s as keys in
44/// unordered associative containers.
45///
46/// Note that using this component to hash keys into a container and then using
47/// the @ref bdlb_caselessstringviewequalto component to compare them is less
48/// efficient than converting all the keys to the same case prior to insertion
49/// and then just doing straight hashes and string compares.
50///
51/// ## Usage {#bdlb_caselessstringviewhash-usage}
52///
53///
54/// This section illustrates intended use of this component.
55///
56/// ### Example 1: Basic Use of bdlb::CaselessStringViewHash: {#bdlb_caselessstringviewhash-example-1-basic-use-of-bdlb-caselessstringviewhash}
57///
58///
59/// Suppose we need an associative container to store, for any stock name, the
60/// number of shares of that stock we own:
61/// @code
62/// typedef bsl::unordered_map<bsl::string, int> SecuritiesOwnedCS;
63/// // case sensitive
64///
65///
66/// typedef bsl::unordered_map<bsl::string,
67/// int,
68/// bdlb::CaselessStringViewHash,
69/// bdlb::CaselessStringViewEqualTo>
70/// SecuritiesOwnedCI;
71/// // case insensitive
72/// @endcode
73/// This type of container stores quantities of shares and allows access to them
74/// by their names, in a case-insensitive manner.
75///
76/// First, we create a container for securities holdings, case-sensitive, and
77/// fill it:
78/// @code
79/// SecuritiesOwnedCS securitiesOwnedCS;
80///
81/// securitiesOwnedCS["IBM"] = 616;
82/// securitiesOwnedCS["Microsoft"] = 6160000;
83///
84/// assert(2 == securitiesOwnedCS.size());
85/// @endcode
86/// Then, we create a container for securities holdings, case-insensitive, and
87/// fill it:
88/// @code
89/// SecuritiesOwnedCI securitiesOwnedCI;
90///
91/// securitiesOwnedCI["IBM"] = 616;
92/// securitiesOwnedCI["Microsoft"] = 6160000;
93///
94/// assert(2 == securitiesOwnedCI.size());
95/// @endcode
96/// Now, we try accessing the case-sensitive `securitiesc`:
97/// @code
98/// assert(1 == securitiesOwnedCS.count("IBM"));
99/// assert(616 == securitiesOwnedCS[ "IBM"]);
100///
101/// assert(0 == securitiesOwnedCS.count("ibm"));
102/// assert(0 == securitiesOwnedCS.count("Ibm"));
103/// assert(0 == securitiesOwnedCS.count("iBm"));
104///
105/// assert(1 == securitiesOwnedCS.count("Microsoft"));
106/// assert(6160000 == securitiesOwnedCS[ "Microsoft"]);
107///
108/// assert(0 == securitiesOwnedCS.count("MICROSOFT"));
109/// assert(0 == securitiesOwnedCS.count("microsoft"));
110/// assert(0 == securitiesOwnedCS.count("MICROSOFT"));
111///
112/// assert(0 == securitiesOwnedCS.count("Google"));
113/// @endcode
114/// Finally, we access the case-insensitive `securitiesci`:
115/// @code
116/// assert(1 == securitiesOwnedCI.count("IBM"));
117/// assert(616 == securitiesOwnedCI[ "IBM"]);
118/// assert(1 == securitiesOwnedCI.count("ibm"));
119/// assert(616 == securitiesOwnedCI[ "ibm"]);
120/// assert(1 == securitiesOwnedCI.count("Ibm"));
121/// assert(616 == securitiesOwnedCI[ "Ibm"]);
122/// assert(1 == securitiesOwnedCI.count("iBm"));
123/// assert(616 == securitiesOwnedCI[ "iBm"]);
124///
125/// assert(1 == securitiesOwnedCI.count("Microsoft"));
126/// assert(6160000 == securitiesOwnedCI[ "Microsoft"]);
127/// assert(1 == securitiesOwnedCI.count("MICROSOFT"));
128/// assert(6160000 == securitiesOwnedCI[ "MICROSOFT"]);
129/// assert(1 == securitiesOwnedCI.count("microsoft"));
130/// assert(6160000 == securitiesOwnedCI[ "microsoft"]);
131/// assert(1 == securitiesOwnedCI.count("MICROSOFT"));
132/// assert(6160000 == securitiesOwnedCI[ "MICROSOFT"]);
133///
134/// assert(0 == securitiesOwnedCI.count("Google"));
135/// @endcode
136/// @}
137/** @} */
138/** @} */
139
140/** @addtogroup bdl
141 * @{
142 */
143/** @addtogroup bdlb
144 * @{
145 */
146/** @addtogroup bdlb_caselessstringviewhash
147 * @{
148 */
149
150#include <bdlscm_version.h>
151
155
156#include <bsl_cstddef.h>
157#include <bsl_string_view.h>
158
159
160namespace bdlb {
161
162 // =============================
163 // struct CaselessStringViewHash
164 // =============================
165
166/// This `struct` defines a hash operation for string views enabling
167/// `bsl::string`s or `bsl::string_view`s to be used as keys in the standard
168/// unordered associative containers such as `bsl::unordered_map` and
169/// `bsl::unordered_set`. Note that this class is an empty POD type.
171
172 // PUBLIC TYPES
174 typedef bsl::size_t result_type;
175
176 /// Type alias indicating this is a transparent hash functor.
177 typedef void is_transparent;
178
179 // TRAITS
184
185 /// Create a `CaselessStringViewHash` object.
187
188 /// Create a `CaselessStringViewHash` object. Note that as
189 /// `CaselessStringViewHash` is an empty (stateless) type, this
190 /// operation will have no observable effect.
192 default;
193
194 /// Destroy this object.
196
197 // MANIPULATORS
198
199 /// Assign to this object the value of the specified `rhs` object, and a
200 /// return a reference providing modifiable access to this object. Note
201 /// that as `CaselessStringViewHash` is an empty (stateless) type, this
202 /// operation will have no observable effect.
204 default;
205
206 // ACCESSORS
207
208 /// Return a hash code generated from the contents of the specified
209 /// null-terminated `argument` string, cast to lower case. The behavior
210 /// is undefined unless `argument` points to a null-terminated string.
211 bsl::size_t operator()(bsl::string_view argument) const;
212};
213
214} // close package namespace
215
216
217#endif
218
219// ----------------------------------------------------------------------------
220// Copyright 2021 Bloomberg Finance L.P.
221//
222// Licensed under the Apache License, Version 2.0 (the "License");
223// you may not use this file except in compliance with the License.
224// You may obtain a copy of the License at
225//
226// http://www.apache.org/licenses/LICENSE-2.0
227//
228// Unless required by applicable law or agreed to in writing, software
229// distributed under the License is distributed on an "AS IS" BASIS,
230// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
231// See the License for the specific language governing permissions and
232// limitations under the License.
233// ----------------------------- END-OF-FILE ----------------------------------
234
235/** @} */
236/** @} */
237/** @} */
Definition bslstl_stringview.h:441
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_caselessstringviewhash.h:170
void is_transparent
Type alias indicating this is a transparent hash functor.
Definition bdlb_caselessstringviewhash.h:177
bsl::size_t result_type
Definition bdlb_caselessstringviewhash.h:174
CaselessStringViewHash(const CaselessStringViewHash &original)=default
bsl::size_t operator()(bsl::string_view argument) const
CaselessStringViewHash()=default
Create a CaselessStringViewHash object.
~CaselessStringViewHash()=default
Destroy this object.
CaselessStringViewHash & operator=(const CaselessStringViewHash &rhs)=default
BSLMF_NESTED_TRAIT_DECLARATION(CaselessStringViewHash, bsl::is_trivially_default_constructible)
BSLMF_NESTED_TRAIT_DECLARATION(CaselessStringViewHash, bsl::is_trivially_copyable)
bsl::string_view argument_type
Definition bdlb_caselessstringviewhash.h:173
Definition bslmf_istriviallycopyable.h:329
Definition bslmf_istriviallydefaultconstructible.h:293