BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_mapcomparator.h
Go to the documentation of this file.
1/// @file bslstl_mapcomparator.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_mapcomparator.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_MAPCOMPARATOR
9#define INCLUDED_BSLSTL_MAPCOMPARATOR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_mapcomparator bslstl_mapcomparator
15/// @brief Provide a comparator for `TreeNode` objects and a lookup key.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_mapcomparator
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_mapcomparator-purpose"> Purpose</a>
25/// * <a href="#bslstl_mapcomparator-classes"> Classes </a>
26/// * <a href="#bslstl_mapcomparator-description"> Description </a>
27/// * <a href="#bslstl_mapcomparator-usage"> Usage </a>
28/// * <a href="#bslstl_mapcomparator-example-1-create-a-simple-tree-of-treenode-objects"> Example 1: Create a Simple Tree of TreeNode Objects </a>
29///
30/// # Purpose {#bslstl_mapcomparator-purpose}
31/// Provide a comparator for `TreeNode` objects and a lookup key.
32///
33/// # Classes {#bslstl_mapcomparator-classes}
34///
35/// - bslstl::MapComparator: comparator for `TreeNode` objects and key objects
36///
37/// @see bslstl_map, bslstl_treenode, bslalg_rbtreeutil
38///
39/// # Description {#bslstl_mapcomparator-description}
40/// This component provides a functor adapter, `MapComparator`,
41/// that adapts a parameterized `COMPARATOR` comparing objects of a
42/// parameterized `KEY` type into a functor comparing a object of `KEY` type
43/// with objects of `TreeNode` type holding a `bsl::pair<KEY, VALUE>` object.
44/// Note that this functor was designed to be supplied to functions in
45/// `bslalg::RbTreeUtil`, primarily for the purpose of implementing a `map`
46/// container using the utilities defined in `bslalg::RbTreeUtil`.
47///
48/// ## Usage {#bslstl_mapcomparator-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Create a Simple Tree of TreeNode Objects {#bslstl_mapcomparator-example-1-create-a-simple-tree-of-treenode-objects}
54///
55///
56/// Suppose that we want to create a tree of `TreeNode` objects arranged
57/// according to a functor that we supply.
58///
59/// First, we create an array of `bslstl::TreeNode` objects, each holding a pair
60/// of integers:
61/// @code
62/// typedef bsl::allocator<TreeNode<bsl::pair<int, int> > > Alloc;
63///
64/// bslma::TestAllocator oa;
65/// Alloc allocator(&oa);
66///
67/// enum { NUM_NODES = 5 };
68///
69/// bslstl::TreeNode<bsl::pair<int, int> >* nodes[NUM_NODES];
70///
71/// typedef bsl::allocator_traits<Alloc> AllocTraits;
72///
73/// for (int i = 0; i < NUM_NODES; ++i) {
74/// nodes[i] = AllocTraits::allocate(allocator, 1);
75/// AllocTraits::construct(allocator, &nodes[i]->value(),
76/// i, 2*i);
77/// }
78/// @endcode
79/// Then, we define a `MapComparator` object, `comparator`, for comparing
80/// `bslstl::TreeNode<pair<int, int> >` objects with integers.
81/// @code
82/// MapComparator<int, int, std::less<int> > comparator;
83/// @endcode
84/// Now, we can use the functions in `bslalg::RbTreeUtil` to arrange our tree:
85/// @code
86/// bslalg::RbTreeAnchor tree;
87///
88/// for (int i = 0; i < NUM_NODES; ++i) {
89/// int comparisonResult;
90/// bslalg::RbTreeNode *insertLocation =
91/// bslalg::RbTreeUtil::findUniqueInsertLocation(
92/// &comparisonResult,
93/// &tree,
94/// comparator,
95/// nodes[i]->value().first);
96///
97/// assert(0 != comparisonResult);
98///
99/// bslalg::RbTreeUtil::insertAt(&tree,
100/// insertLocation,
101/// comparisonResult < 0,
102/// nodes[i]);
103/// }
104///
105/// assert(5 == tree.numNodes());
106/// @endcode
107/// Then, we use `bslalg::RbTreeUtil::next()` to navigate the elements of the
108/// tree, printing their values:
109/// @code
110/// const bslalg::RbTreeNode *nodeIterator = tree.firstNode();
111///
112/// while (nodeIterator != tree.sentinel()) {
113/// const TreeNode<bsl::pair<int, int> > *node =
114/// static_cast<const TreeNode<bsl::pair<int, int> >*>(nodeIterator);
115/// printf("Node value: (%d, %d)\n",
116/// node->value().first, node->value().second);
117/// nodeIterator = bslalg::RbTreeUtil::next(nodeIterator);
118/// }
119/// @endcode
120/// Next, we destroy and deallocate each of the `bslstl::TreeNode` objects:
121/// @code
122/// for (int i = 0; i < NUM_NODES; ++i) {
123/// AllocTraits::destroy(allocator, &nodes[i]->value());
124/// AllocTraits::deallocate(allocator, nodes[i], 1);
125/// }
126/// @endcode
127/// Finally, we observe the console output:
128/// @code
129/// Node value: (0, 0)
130/// Node value: (1, 2)
131/// Node value: (2, 4)
132/// Node value: (3, 6)
133/// Node value: (4, 8)
134/// @endcode
135/// @}
136/** @} */
137/** @} */
138
139/** @addtogroup bsl
140 * @{
141 */
142/** @addtogroup bslstl
143 * @{
144 */
145/** @addtogroup bslstl_mapcomparator
146 * @{
147 */
148
149#include <bslscm_version.h>
150
151#include <bslstl_pair.h>
152#include <bslstl_treenode.h>
153
155#include <bslalg_swaputil.h>
156
157#include <bsls_platform.h>
158#include <bsls_util.h>
159
160
161namespace bslstl {
162
163 // ===================
164 // class MapComparator
165 // ===================
166
167template <class KEY, class VALUE, class COMPARATOR>
168#ifdef BSLS_PLATFORM_CMP_MSVC
169// Visual studio compiler fails to resolve the conversion operator in
170// 'bslalg::FunctorAdapter_FunctionPointer' when using private inheritance.
171// Below is a workaround until a more suitable way the resolve this issue can
172// be found.
173class MapComparator : public bslalg::FunctorAdapter<COMPARATOR>::Type {
174#else
175class MapComparator : private bslalg::FunctorAdapter<COMPARATOR>::Type {
176#endif
177 // This class overloads the function-call operator to compare a referenced
178 // 'bslalg::RbTreeNode' object with a object of the parameterized 'KEY'
179 // type, assuming the reference to 'bslalg::RbTreeNode' is a base of a
180 // 'bslstl::TreeNode' holding a 'pair<KEY, VALUE>', using a functor of the
181 // parameterized 'COMPARATOR' type.
182
183 private:
184 // This class does not support assignment.
185
186 MapComparator& operator=(const MapComparator&); // Declared but not
187 // defined
188
189 public:
190 // TYPES
191
192 /// This alias represents the type of the values held by nodes in an
193 /// `bslalg::RbTree` object.
195
196 /// This alias represents the type of node holding a `ValueType` object.
198
199 // CREATORS
200
201 /// Create a `MapComparator` object that will use a default constructed
202 /// `COMPARATOR`.
204
205 /// Create a `MapComparator` object holding a copy of the specified
206 /// `keyComparator`.
207 explicit MapComparator(const COMPARATOR& keyComparator);
208
209 MapComparator(const MapComparator& original) = default;
210 // Create a 'MapComparator' object with the 'COMPARATOR' object of the
211 // specified 'original' object.
212
213 ~MapComparator() = default;
214 // Destroy this object.
215
216 // MANIPULATORS
217
218 /// Return `true` if the specified `lhs` is less than (ordered before,
219 /// according to the comparator held by this object) `value().first` of
220 /// the specified `rhs` after being cast to `NodeType`, and `false` otherwise.
221 ///
222 /// \pre The behavior is undefined unless `rhs` can be safely
223 /// cast to `NodeType`.
224 template <class LOOKUP_KEY>
225 bool operator()(const LOOKUP_KEY& lhs,
226 const bslalg::RbTreeNode& rhs);
227
228 /// Return `true` if `value().first()` of the specified `lhs` after
229 /// being cast to `NodeType` is less than (ordered before, according to
230 /// the comparator held by this object) the specified `rhs`, and `false` otherwise.
231 ///
232 /// \pre The behavior is undefined unless `rhs` can be safely
233 /// cast to `NodeType`.
234 template <class LOOKUP_KEY>
235 bool operator()(const bslalg::RbTreeNode& lhs,
236 const LOOKUP_KEY& rhs);
237
238 /// Efficiently exchange the value of this object with the value of the
239 /// specified `other` object. This method provides the no-throw
240 /// exception-safety guarantee.
241 void swap(MapComparator& other);
242
243 // ACCESSORS
244
245 /// Return `true` if the specified `lhs` is less than (ordered before,
246 /// according to the comparator held by this object) `value().first` of
247 /// the specified `rhs` after being cast to `NodeType`, and `false` otherwise.
248 ///
249 /// \pre The behavior is undefined unless `rhs` can be safely
250 /// cast to `NodeType`.
251 template <class LOOKUP_KEY>
252 bool operator()(const LOOKUP_KEY& lhs,
253 const bslalg::RbTreeNode& rhs) const;
254
255 /// Return `true` if `value().first()` of the specified `lhs` after
256 /// being cast to `NodeType` is less than (ordered before, according to
257 /// the comparator held by this object) the specified `rhs`, and `false` otherwise.
258 ///
259 /// \pre The behavior is undefined unless `rhs` can be safely
260 /// cast to `NodeType`.
261 template <class LOOKUP_KEY>
262 bool operator()(const bslalg::RbTreeNode& lhs,
263 const LOOKUP_KEY& rhs) const;
264
265 /// Return a reference providing modifiable access to the function
266 /// pointer or functor to which this comparator delegates comparison
267 /// operations.
268 COMPARATOR& keyComparator();
269
270 /// Return a reference providing non-modifiable access to the function
271 /// pointer or functor to which this comparator delegates comparison
272 /// operations.
273 const COMPARATOR& keyComparator() const;
274};
275
276// FREE FUNCTIONS
277
278/// Efficiently exchange the values of the specified `a` and `b` objects.
279/// This function provides the no-throw exception-safety guarantee.
280template <class KEY, class VALUE, class COMPARATOR>
283
284// ============================================================================
285// TEMPLATE AND INLINE FUNCTION DEFINITIONS
286// ============================================================================
287
288 // -------------------
289 // class MapComparator
290 // -------------------
291
292// CREATORS
293template <class KEY, class VALUE, class COMPARATOR>
294inline
296: bslalg::FunctorAdapter<COMPARATOR>::Type()
297{
298}
299
300template <class KEY, class VALUE, class COMPARATOR>
301inline
303MapComparator(const COMPARATOR& valueComparator)
304: bslalg::FunctorAdapter<COMPARATOR>::Type(valueComparator)
305{
306}
307
308// MANIPULATORS
309template <class KEY, class VALUE, class COMPARATOR>
310inline
319
320// ACCESSOR
321template <class KEY, class VALUE, class COMPARATOR>
322template <class LOOKUP_KEY>
323inline
325 const LOOKUP_KEY& lhs,
326 const bslalg::RbTreeNode& rhs)
327{
328 return keyComparator()(lhs,
329 static_cast<const NodeType&>(rhs).value().first);
330}
331
332template <class KEY, class VALUE, class COMPARATOR>
333template <class LOOKUP_KEY>
334inline
336 const LOOKUP_KEY& lhs,
337 const bslalg::RbTreeNode& rhs) const
338{
339 return keyComparator()(lhs,
340 static_cast<const NodeType&>(rhs).value().first);
341}
342
343template <class KEY, class VALUE, class COMPARATOR>
344template <class LOOKUP_KEY>
345inline
347 const bslalg::RbTreeNode& lhs,
348 const LOOKUP_KEY& rhs)
349{
350 return keyComparator()(static_cast<const NodeType&>(lhs).value().first,
351 rhs);
352}
353
354template <class KEY, class VALUE, class COMPARATOR>
355template <class LOOKUP_KEY>
356inline
358 const bslalg::RbTreeNode& lhs,
359 const LOOKUP_KEY& rhs) const
360{
361 return keyComparator()(static_cast<const NodeType&>(lhs).value().first,
362 rhs);
363}
364
365template <class KEY, class VALUE, class COMPARATOR>
366inline
367COMPARATOR&
372
373template <class KEY, class VALUE, class COMPARATOR>
374inline
375const COMPARATOR&
380
381
382// FREE FUNCTIONS
383template <class KEY, class VALUE, class COMPARATOR>
389
390} // close package namespace
391
392
393#endif
394
395// ----------------------------------------------------------------------------
396// Copyright 2013 Bloomberg Finance L.P.
397//
398// Licensed under the Apache License, Version 2.0 (the "License");
399// you may not use this file except in compliance with the License.
400// You may obtain a copy of the License at
401//
402// http://www.apache.org/licenses/LICENSE-2.0
403//
404// Unless required by applicable law or agreed to in writing, software
405// distributed under the License is distributed on an "AS IS" BASIS,
406// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
407// See the License for the specific language governing permissions and
408// limitations under the License.
409// ----------------------------- END-OF-FILE ----------------------------------
410
411/** @} */
412/** @} */
413/** @} */
Definition bslstl_pair.h:1280
Definition bslalg_functoradapter.h:230
CALLABLE_OBJECT Type
This typedef is an alias for the functor.
Definition bslalg_functoradapter.h:236
Definition bslalg_rbtreenode.h:377
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:182
Definition bslstl_mapcomparator.h:175
MapComparator(const MapComparator &original)=default
COMPARATOR & keyComparator()
Definition bslstl_mapcomparator.h:368
TreeNode< ValueType > NodeType
This alias represents the type of node holding a ValueType object.
Definition bslstl_mapcomparator.h:197
MapComparator()
Definition bslstl_mapcomparator.h:295
bsl::pair< const KEY, VALUE > ValueType
Definition bslstl_mapcomparator.h:194
void swap(MapComparator &other)
Definition bslstl_mapcomparator.h:311
bool operator()(const LOOKUP_KEY &lhs, const bslalg::RbTreeNode &rhs)
Definition bslstl_mapcomparator.h:324
Definition bslstl_treenode.h:395
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_UTIL_ADDRESSOF(OBJ)
Definition bsls_util.h:296
Definition bdlc_flathashmap.h:2218
Definition bslstl_algorithm.h:84