BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_unorderedmapkeyconfiguration.h
Go to the documentation of this file.
1/// @file bslstl_unorderedmapkeyconfiguration.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_unorderedmapkeyconfiguration.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_UNORDEREDMAPKEYCONFIGURATION
9#define INCLUDED_BSLSTL_UNORDEREDMAPKEYCONFIGURATION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_unorderedmapkeyconfiguration bslstl_unorderedmapkeyconfiguration
15/// @brief Provide a class template to extract keys as the `first` attribute.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_unorderedmapkeyconfiguration
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_unorderedmapkeyconfiguration-purpose"> Purpose</a>
25/// * <a href="#bslstl_unorderedmapkeyconfiguration-classes"> Classes </a>
26/// * <a href="#bslstl_unorderedmapkeyconfiguration-description"> Description </a>
27/// * <a href="#bslstl_unorderedmapkeyconfiguration-usage"> Usage </a>
28/// * <a href="#bslstl_unorderedmapkeyconfiguration-example-1-using-multiple-extractors-to-sort-an-array-on-different-keys"> Example 1: Using Multiple Extractors to Sort an Array on Different Keys </a>
29///
30/// # Purpose {#bslstl_unorderedmapkeyconfiguration-purpose}
31/// Provide a class template to extract keys as the `first` attribute.
32///
33/// # Classes {#bslstl_unorderedmapkeyconfiguration-classes}
34///
35/// - bslalg::UnorderedMapKeyConfiguration : extracts `key` from `value` type
36///
37/// @see bslalg_hashtableimputil
38///
39/// # Description {#bslstl_unorderedmapkeyconfiguration-description}
40/// This component will, given an object of a value type consisting
41/// of a key type and some other information, return a const reference to only
42/// the key type within that object. The object passed will be of parameterized
43/// type `VALUE_TYPE`, for which a type `VALUE_TYPE::first_type` must be
44/// defined and be of the key type, and for which the operation `.first` must be
45/// defined and must yield the object of the key type.
46///
47/// `bslalg::HashTableImpUtil` has a static `extractKey` function template that,
48/// given a `value type`, will represent objects stored in a data structure,
49/// will abstract out the `key type` portion of that object. In the case of the
50/// `unordered_map` data structure, the `value type` will be `bsl::pair`, and
51/// the key type will `bsl::pair::first_type`.
52///
53/// ## Usage {#bslstl_unorderedmapkeyconfiguration-usage}
54///
55///
56/// This section illustrates intended use of this component.
57///
58/// ### Example 1: Using Multiple Extractors to Sort an Array on Different Keys {#bslstl_unorderedmapkeyconfiguration-example-1-using-multiple-extractors-to-sort-an-array-on-different-keys}
59///
60///
61/// Suppose we want to define a `sort` function which will work on a variety
62/// of different object types. The object has to have a `key` within it,
63/// possibly the whole object, which will compare with the `key` of other
64/// objects with a transitive `<` operator.
65///
66/// First, we define our function `mySort`, which takes two template args:
67/// `VALUE_TYPE`, the type of object being sorted, and `KEY_EXTRACTOR`, the
68/// utility class that will extra which part of the objects to be sorted is the
69/// key which will drive the sort:
70/// @code
71/// // This function provides an order-preserving sort of the items in the
72/// // range `[begin .. end)`, where `KEY_EXTRACTOR::extractKey` yields the
73/// // key being sorted over. We require that `VALUE_TYPE` support copy
74/// // construction and assignment.
75/// template <class VALUE_TYPE, class KEY_EXTRACTOR>
76/// void mySort(VALUE_TYPE *begin, VALUE_TYPE *end, const KEY_EXTRACTOR&)
77/// {
78/// while (begin < --end) {
79/// for (VALUE_TYPE *it = begin; it < end; ++it) {
80/// if (KEY_EXTRACTOR::extractKey(it[1]) <
81/// KEY_EXTRACTOR::extractKey(it[0])) {
82/// // they're in the wrong order -- swap them
83///
84/// VALUE_TYPE tmp(it[0]);
85/// it[0] = it[1];
86/// it[1] = tmp;
87/// }
88/// }
89///
90/// // '*end' is now the highest element in the range '[begin .. end]',
91/// // so we only have to sort the elements before it in the next pass.
92/// }
93/// }
94/// @endcode
95/// Then, we define `StudentRecord`, which keeps some vital statistics on
96/// students:
97/// @code
98/// struct StudentRecord {
99/// const char *d_name;
100/// double d_gpa;
101/// int d_age;
102/// };
103/// @endcode
104/// Next, we define two extractors for `StudentRecord`, which will yield the
105/// `GPA` or `Age` fields:
106/// @code
107/// struct StudentRecordGPAExtractor {
108/// static
109/// const double& extractKey(const StudentRecord& record)
110/// {
111/// return record.d_gpa;
112/// }
113/// };
114///
115/// struct StudentRecordAgeExtractor {
116/// static
117/// const int& extractKey(const StudentRecord& record)
118/// {
119/// return record.d_age;
120/// }
121/// };
122/// @endcode
123/// Then, in `main`, we create an array of `StudentRecord`s describing a set of
124/// students, with their names, GPA's, and ages.
125/// @code
126/// StudentRecord studentArray[] = {
127/// { "Phil", 3.4, 19 },
128/// { "Bob", 2.7, 20 },
129/// { "Bill", 4.2, 21 },
130/// { "Stan", 1.9, 18 },
131/// { "Ann", 2.3, 21 },
132/// { "Julie", 2.3, 20 } };
133/// const int NUM_STUDENTS = sizeof studentArray / sizeof *studentArray;
134/// @endcode
135/// Next, using our GPA extractor and our `mySort` function, we sort the
136/// students by GPA:
137/// @code
138/// StudentRecordGPAExtractor gpaExtractor;
139///
140/// mySort(studentArray + 0,
141/// studentArray + NUM_STUDENTS,
142/// gpaExtractor);
143/// @endcode
144/// Then, we print out the sorted array of students:
145/// @code
146/// if (verbose) {
147/// printf("\nList of students, lowest GPA first:\n");
148/// printf( "===================================\n");
149///
150/// printf("Name GPA AGE\n"
151/// "----- --- ---\n");
152/// for (int i = 0; i < NUM_STUDENTS; ++i) {
153/// const StudentRecord& record = studentArray[i];
154///
155/// printf("%-5s %g %3d\n", record.d_name,
156/// record.d_gpa,
157/// record.d_age);
158/// }
159/// }
160/// @endcode
161/// The output produced is:
162/// @code
163/// List of students, lowest GPA first:
164/// ===================================
165/// Name GPA AGE
166/// ----- --- ---
167/// Stan 1.9 18
168/// Ann 2.3 21
169/// Julie 2.3 20
170/// Bob 2.7 20
171/// Phil 3.4 19
172/// Bill 4.2 21
173/// @endcode
174/// Note that Ann and Julie, who have the same GPA, are still in the same order
175/// as they were before the sort, as `mySort` was an order-preserving sort:
176///
177/// Next, we sort by age with our age extractor, and print out the results:
178/// @code
179/// StudentRecordAgeExtractor ageExtractor;
180///
181/// mySort(studentArray + 0,
182/// studentArray + NUM_STUDENTS,
183/// ageExtractor);
184///
185/// if (verbose) {
186/// printf("\nList of students, youngest first:\n");
187/// printf( "================================\n");
188///
189/// printf("Name GPA AGE\n"
190/// "----- --- ---\n");
191/// for (int i = 0; i < NUM_STUDENTS; ++i) {
192/// const StudentRecord& record = studentArray[i];
193///
194/// printf("%-5s %g %3d\n", record.d_name,
195/// record.d_gpa,
196/// record.d_age);
197/// }
198/// }
199/// @endcode
200/// The output is:
201/// @code
202/// List of students, youngest first:
203/// ================================
204/// Name GPA AGE
205/// ----- --- ---
206/// Stan 1.9 18
207/// Phil 3.4 19
208/// Julie 2.3 20
209/// Bob 2.7 20
210/// Ann 2.3 21
211/// Bill 4.2 21
212/// @endcode
213/// Note again, the ordering of students with identical ages is preserved.
214///
215/// Then, suppose we are storing information about employees in `MyPair`
216/// objects, where `first` is a double storing the employees hourly wage, and
217/// `second` in the employee's name. Suppose we want to sort the employees by
218/// their hourly wages, which is the `.first` field of the pair.
219///
220/// We declare our employee pair type:
221/// @code
222/// typedef MyPair<double, const char *> EmployeePair;
223/// @endcode
224/// Next, we define an array of employee pairs for employees' wages and names:
225/// @code
226/// EmployeePair employees[] = {
227/// { 12.25, "Kyle" },
228/// { 15.00, "Eric" },
229/// { 12.25, "Stan" },
230/// { 7.75, "Kenny" } };
231/// const int NUM_EMPLOYEES = sizeof employees / sizeof *employees;
232/// @endcode
233/// Then, we create an `UnorderedMapKeyConfiguration` type parameterized on
234/// `EmployeePair`, which will extract the `.first` field, which is the wage,
235/// from an employee pair:
236/// @code
237/// bslstl::UnorderedMapKeyConfiguration<EmployeePair> wageExtractor;
238/// @endcode
239/// Next, we sort:
240/// @code
241/// mySort(employees + 0, employees + NUM_EMPLOYEES, wageExtractor);
242/// @endcode
243/// Now, we print out our results:
244/// @code
245/// if (verbose) {
246/// printf("\nList of employees, cheapest first:\n"
247/// "==================================\n");
248///
249/// printf("Name Wage\n"
250/// "----- -----\n");
251///
252/// for (int i = 0; i < NUM_EMPLOYEES; ++i) {
253/// const EmployeePair& employee = employees[i];
254///
255/// printf("%-5s %5.2f\n", employee.second, employee.first);
256/// }
257/// }
258/// @endcode
259/// Finally, we see our output. Note that the ordering of Kyle and Stan, who
260/// are paid the same wage, is preserved.
261/// @code
262/// List of employees, cheapest first:
263/// ==================================
264/// Name Wage
265/// ----- -----
266/// Kenny 7.75
267/// Kyle 12.25
268/// Stan 12.25
269/// Eric 15.00
270/// @endcode
271/// @}
272/** @} */
273/** @} */
274
275/** @addtogroup bsl
276 * @{
277 */
278/** @addtogroup bslstl
279 * @{
280 */
281/** @addtogroup bslstl_unorderedmapkeyconfiguration
282 * @{
283 */
284
285#include <bslscm_version.h>
286
287
288namespace bslstl {
289
290 // ===================================
291 // struct UnorderedMapKeyConfiguration
292 // ===================================
293
294template <class KEY, class VALUE_TYPE>
296 public:
297 typedef VALUE_TYPE ValueType;
298 typedef KEY KeyType;
299
300 // Choosing to implement for each configuration, to reduce the template
301 // mess. With only two policies, not much is saved using a shared
302 // dependent base class to provide a common implementation. This is the
303 // key abstraction, turning 'bslalg::BidirectionalLink*' into 'VALUE_TYPE&'
304
305 // CLASS METHODS
306
307 /// Return the member `first` of the specified object `obj`.
308 /// `obj.first` must be of type `VALUE_TYPE::first_type`, which is the
309 /// `key` portion of `obj`.
310 static const KeyType& extractKey(const VALUE_TYPE& obj);
311};
312
313// ============================================================================
314// TEMPLATE AND INLINE FUNCTION DEFINITIONS
315// ============================================================================
316
317 //-----------------------------------
318 // class UnorderedMapKeyConfiguration
319 //-----------------------------------
320
321// CLASS METHODS
322template <class KEY, class VALUE_TYPE>
323inline
324const KEY&
326{
327 return obj.first;
328}
329
330} // close package namespace
331
332
333
334#endif
335
336// ----------------------------------------------------------------------------
337// Copyright 2013 Bloomberg Finance L.P.
338//
339// Licensed under the Apache License, Version 2.0 (the "License");
340// you may not use this file except in compliance with the License.
341// You may obtain a copy of the License at
342//
343// http://www.apache.org/licenses/LICENSE-2.0
344//
345// Unless required by applicable law or agreed to in writing, software
346// distributed under the License is distributed on an "AS IS" BASIS,
347// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
348// See the License for the specific language governing permissions and
349// limitations under the License.
350// ----------------------------- END-OF-FILE ----------------------------------
351
352/** @} */
353/** @} */
354/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslstl_algorithm.h:82
Definition bslstl_unorderedmapkeyconfiguration.h:295
static const KeyType & extractKey(const VALUE_TYPE &obj)
Definition bslstl_unorderedmapkeyconfiguration.h:325
VALUE_TYPE ValueType
Definition bslstl_unorderedmapkeyconfiguration.h:297
KEY KeyType
Definition bslstl_unorderedmapkeyconfiguration.h:298