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