BDE 4.39.x 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` otherwise.
213 ///
214 /// \pre The behavior is undefined unless `rhs` can be safely
215 /// cast to `NodeType`.
216 template <class LOOKUP_KEY>
217 bool operator()(const LOOKUP_KEY& lhs,
218 const bslalg::RbTreeNode& rhs);
219
220 /// Return `true` if `value()` of the specified `lhs` after
221 /// being cast to `NodeType` is less than (ordered before, according to
222 /// the comparator held by this object) the specified `rhs`, and `false` otherwise.
223 ///
224 /// \pre The behavior is undefined unless `rhs` can be safely
225 /// cast to `NodeType`.
226 template <class LOOKUP_KEY>
227 bool operator()(const bslalg::RbTreeNode& lhs,
228 const LOOKUP_KEY& rhs);
229
230 /// Efficiently exchange the value of this object with the value of the
231 /// specified `other` object. This method provides the no-throw
232 /// exception-safety guarantee.
233 void swap(SetComparator& other);
234
235 // ACCESSORS
236
237 /// Return `true` if the specified `lhs` is less than (ordered before,
238 /// according to the comparator held by this object) `value()` of the
239 /// specified `rhs` after being cast to `NodeType`, and `false` otherwise.
240 ///
241 /// \pre The behavior is undefined unless `rhs` can be safely
242 /// cast to `NodeType`.
243 template <class LOOKUP_KEY>
244 bool operator()(const LOOKUP_KEY& lhs,
245 const bslalg::RbTreeNode& rhs) const;
246
247 /// Return `true` if `value()` of the specified `lhs` after
248 /// being cast to `NodeType` is less than (ordered before, according to
249 /// the comparator held by this object) the specified `rhs`, and `false` otherwise.
250 ///
251 /// \pre The behavior is undefined unless `rhs` can be safely
252 /// cast to `NodeType`.
253 template <class LOOKUP_KEY>
254 bool operator()(const bslalg::RbTreeNode& lhs,
255 const LOOKUP_KEY& rhs) const;
256
257 /// Return a reference providing modifiable access to the function
258 /// pointer or functor to which this comparator delegates comparison
259 /// operations.
260 COMPARATOR& keyComparator();
261
262 /// Return a reference providing non-modifiable access to the function
263 /// pointer or functor to which this comparator delegates comparison
264 /// operations.
265 const COMPARATOR& keyComparator() const;
266};
267
268
269// FREE FUNCTIONS
270
271/// Efficiently exchange the values of the specified `a` and `b` objects.
272/// This function provides the no-throw exception-safety guarantee.
273template <class KEY, class COMPARATOR>
276
277// ============================================================================
278// TEMPLATE AND INLINE FUNCTION DEFINITIONS
279// ============================================================================
280
281 // -------------------
282 // class SetComparator
283 // -------------------
284
285// CREATORS
286template <class KEY, class COMPARATOR>
287inline
289: bslalg::FunctorAdapter<COMPARATOR>::Type()
290{
291}
292
293template <class KEY, class COMPARATOR>
294inline
296SetComparator(const COMPARATOR& valueComparator)
297: bslalg::FunctorAdapter<COMPARATOR>::Type(valueComparator)
298{
299}
300
301// MANIPULATORS
302template <class KEY, class COMPARATOR>
303template <class LOOKUP_KEY>
304inline
306 const LOOKUP_KEY& lhs,
307 const bslalg::RbTreeNode& rhs)
308{
309 return keyComparator()(lhs, static_cast<const NodeType&>(rhs).value());
310}
311
312template <class KEY, class COMPARATOR>
313template <class LOOKUP_KEY>
314inline
316 const bslalg::RbTreeNode& lhs,
317 const LOOKUP_KEY& rhs)
318
319{
320 return keyComparator()(static_cast<const NodeType&>(lhs).value(), rhs);
321}
322
323template <class KEY, class COMPARATOR>
324inline
333
334// ACCESSORS
335template <class KEY, class COMPARATOR>
336template <class LOOKUP_KEY>
337inline
339 const LOOKUP_KEY& lhs,
340 const bslalg::RbTreeNode& rhs) const
341{
342 return keyComparator()(lhs, static_cast<const NodeType&>(rhs).value());
343}
344
345template <class KEY, class COMPARATOR>
346template <class LOOKUP_KEY>
347inline
349 const bslalg::RbTreeNode& lhs,
350 const LOOKUP_KEY& rhs) const
351
352{
353 return keyComparator()(static_cast<const NodeType&>(lhs).value(), rhs);
354}
355
356template <class KEY, class COMPARATOR>
357inline
359{
360 return *this;
361}
362
363template <class KEY, class COMPARATOR>
364inline
366{
367 return *this;
368}
369
370// FREE FUNCTIONS
371template <class KEY, class COMPARATOR>
372inline
375{
376 a.swap(b);
377}
378
379} // close package namespace
380
381
382#endif
383
384// ----------------------------------------------------------------------------
385// Copyright 2013 Bloomberg Finance L.P.
386//
387// Licensed under the Apache License, Version 2.0 (the "License");
388// you may not use this file except in compliance with the License.
389// You may obtain a copy of the License at
390//
391// http://www.apache.org/licenses/LICENSE-2.0
392//
393// Unless required by applicable law or agreed to in writing, software
394// distributed under the License is distributed on an "AS IS" BASIS,
395// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
396// See the License for the specific language governing permissions and
397// limitations under the License.
398// ----------------------------- END-OF-FILE ----------------------------------
399
400/** @} */
401/** @} */
402/** @} */
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_setcomparator.h:176
COMPARATOR & keyComparator()
Definition bslstl_setcomparator.h:358
bool operator()(const LOOKUP_KEY &lhs, const bslalg::RbTreeNode &rhs)
Definition bslstl_setcomparator.h:305
SetComparator(const SetComparator &)=default
~SapComparator()=default
Destroy this object.
void swap(SetComparator &other)
Definition bslstl_setcomparator.h:325
TreeNode< KEY > NodeType
This alias represents the type of node holding a KEY object.
Definition bslstl_setcomparator.h:189
SetComparator()
Definition bslstl_setcomparator.h:288
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