BDE 4.14.0 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`
221 /// otherwise. The behavior is undefined unless `rhs` can be safely
222 /// cast to `NodeType`.
223 template <class LOOKUP_KEY>
224 bool operator()(const LOOKUP_KEY& lhs,
225 const bslalg::RbTreeNode& rhs);
226
227 /// Return `true` if `value().first()` of the specified `lhs` after
228 /// being cast to `NodeType` is less than (ordered before, according to
229 /// the comparator held by this object) the specified `rhs`, and `false`
230 /// otherwise. The behavior is undefined unless `rhs` can be safely
231 /// cast to `NodeType`.
232 template <class LOOKUP_KEY>
233 bool operator()(const bslalg::RbTreeNode& lhs,
234 const LOOKUP_KEY& rhs);
235
236 /// Efficiently exchange the value of this object with the value of the
237 /// specified `other` object. This method provides the no-throw
238 /// exception-safety guarantee.
239 void swap(MapComparator& other);
240
241 // ACCESSORS
242
243 /// Return `true` if the specified `lhs` is less than (ordered before,
244 /// according to the comparator held by this object) `value().first` of
245 /// the specified `rhs` after being cast to `NodeType`, and `false`
246 /// otherwise. The behavior is undefined unless `rhs` can be safely
247 /// cast to `NodeType`.
248 template <class LOOKUP_KEY>
249 bool operator()(const LOOKUP_KEY& lhs,
250 const bslalg::RbTreeNode& rhs) const;
251
252 /// Return `true` if `value().first()` of the specified `lhs` after
253 /// being cast to `NodeType` is less than (ordered before, according to
254 /// the comparator held by this object) the specified `rhs`, and `false`
255 /// otherwise. The behavior is undefined unless `rhs` can be safely
256 /// cast to `NodeType`.
257 template <class LOOKUP_KEY>
258 bool operator()(const bslalg::RbTreeNode& lhs,
259 const LOOKUP_KEY& rhs) const;
260
261 /// Return a reference providing modifiable access to the function
262 /// pointer or functor to which this comparator delegates comparison
263 /// operations.
264 COMPARATOR& keyComparator();
265
266 /// Return a reference providing non-modifiable access to the function
267 /// pointer or functor to which this comparator delegates comparison
268 /// operations.
269 const COMPARATOR& keyComparator() const;
270};
271
272// FREE FUNCTIONS
273
274/// Efficiently exchange the values of the specified `a` and `b` objects.
275/// This function provides the no-throw exception-safety guarantee.
276template <class KEY, class VALUE, class COMPARATOR>
279
280// ============================================================================
281// TEMPLATE AND INLINE FUNCTION DEFINITIONS
282// ============================================================================
283
284 // -------------------
285 // class MapComparator
286 // -------------------
287
288// CREATORS
289template <class KEY, class VALUE, class COMPARATOR>
290inline
292: bslalg::FunctorAdapter<COMPARATOR>::Type()
293{
294}
295
296template <class KEY, class VALUE, class COMPARATOR>
297inline
299MapComparator(const COMPARATOR& valueComparator)
300: bslalg::FunctorAdapter<COMPARATOR>::Type(valueComparator)
301{
302}
303
304// MANIPULATORS
305template <class KEY, class VALUE, class COMPARATOR>
306inline
315
316// ACCESSOR
317template <class KEY, class VALUE, class COMPARATOR>
318template <class LOOKUP_KEY>
319inline
321 const LOOKUP_KEY& lhs,
322 const bslalg::RbTreeNode& rhs)
323{
324 return keyComparator()(lhs,
325 static_cast<const NodeType&>(rhs).value().first);
326}
327
328template <class KEY, class VALUE, class COMPARATOR>
329template <class LOOKUP_KEY>
330inline
332 const LOOKUP_KEY& lhs,
333 const bslalg::RbTreeNode& rhs) const
334{
335 return keyComparator()(lhs,
336 static_cast<const NodeType&>(rhs).value().first);
337}
338
339template <class KEY, class VALUE, class COMPARATOR>
340template <class LOOKUP_KEY>
341inline
343 const bslalg::RbTreeNode& lhs,
344 const LOOKUP_KEY& rhs)
345{
346 return keyComparator()(static_cast<const NodeType&>(lhs).value().first,
347 rhs);
348}
349
350template <class KEY, class VALUE, class COMPARATOR>
351template <class LOOKUP_KEY>
352inline
354 const bslalg::RbTreeNode& lhs,
355 const LOOKUP_KEY& rhs) const
356{
357 return keyComparator()(static_cast<const NodeType&>(lhs).value().first,
358 rhs);
359}
360
361template <class KEY, class VALUE, class COMPARATOR>
362inline
363COMPARATOR&
368
369template <class KEY, class VALUE, class COMPARATOR>
370inline
371const COMPARATOR&
376
377
378// FREE FUNCTIONS
379template <class KEY, class VALUE, class COMPARATOR>
385
386} // close package namespace
387
388
389#endif
390
391// ----------------------------------------------------------------------------
392// Copyright 2013 Bloomberg Finance L.P.
393//
394// Licensed under the Apache License, Version 2.0 (the "License");
395// you may not use this file except in compliance with the License.
396// You may obtain a copy of the License at
397//
398// http://www.apache.org/licenses/LICENSE-2.0
399//
400// Unless required by applicable law or agreed to in writing, software
401// distributed under the License is distributed on an "AS IS" BASIS,
402// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
403// See the License for the specific language governing permissions and
404// limitations under the License.
405// ----------------------------- END-OF-FILE ----------------------------------
406
407/** @} */
408/** @} */
409/** @} */
Definition bslstl_pair.h:1210
Definition bslalg_functoradapter.h:228
CALLABLE_OBJECT Type
This typedef is an alias for the functor.
Definition bslalg_functoradapter.h:234
Definition bslalg_rbtreenode.h:376
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:194
Definition bslstl_mapcomparator.h:175
MapComparator(const MapComparator &original)=default
COMPARATOR & keyComparator()
Definition bslstl_mapcomparator.h:364
TreeNode< ValueType > NodeType
This alias represents the type of node holding a ValueType object.
Definition bslstl_mapcomparator.h:197
MapComparator()
Definition bslstl_mapcomparator.h:291
bsl::pair< const KEY, VALUE > ValueType
Definition bslstl_mapcomparator.h:194
void swap(MapComparator &other)
Definition bslstl_mapcomparator.h:307
bool operator()(const LOOKUP_KEY &lhs, const bslalg::RbTreeNode &rhs)
Definition bslstl_mapcomparator.h:320
Definition bslstl_treenode.h:393
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_UTIL_ADDRESSOF(OBJ)
Definition bsls_util.h:289
Definition bdlc_flathashmap.h:1805
Definition bslstl_algorithm.h:82