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