BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_algorithmworkaroundutil.h
Go to the documentation of this file.
1/// @file bdlb_algorithmworkaroundutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_algorithmworkaroundutil.h -*-C++-*-
8#ifndef INCLUDED_BDLB_ALGORITHMWORKAROUNDUTIL
9#define INCLUDED_BDLB_ALGORITHMWORKAROUNDUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_algorithmworkaroundutil bdlb_algorithmworkaroundutil
15/// @brief Provide a namespace for workarounds for faulty standard algorithms
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_algorithmworkaroundutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_algorithmworkaroundutil-purpose"> Purpose</a>
25/// * <a href="#bdlb_algorithmworkaroundutil-classes"> Classes </a>
26/// * <a href="#bdlb_algorithmworkaroundutil-description"> Description </a>
27/// * <a href="#bdlb_algorithmworkaroundutil-usage"> Usage </a>
28///
29/// # Purpose {#bdlb_algorithmworkaroundutil-purpose}
30/// Provide a namespace for workarounds for faulty standard algorithms
31///
32/// # Classes {#bdlb_algorithmworkaroundutil-classes}
33///
34/// - AlgorithmWorkaroundUtil: namespace for fixed faulty standard algorithms
35///
36/// @see std::lower_bound, std::upper_bound
37///
38/// # Description {#bdlb_algorithmworkaroundutil-description}
39/// This component provides a `struct` namespace for replacement
40/// workarounds for some platform supplied faulty algorithms (particularly for
41/// the Sun Studio compiler).
42///
43/// ## Usage {#bdlb_algorithmworkaroundutil-usage}
44///
45///
46/// Suppose that we
47/// @}
48/** @} */
49/** @} */
50
51/** @addtogroup bdl
52 * @{
53 */
54/** @addtogroup bdlb
55 * @{
56 */
57/** @addtogroup bdlb_algorithmworkaroundutil
58 * @{
59 */
60
61#include <bdlscm_version.h>
62
64#include <bsls_platform.h>
65
66#include <bsl_algorithm.h>
67#include <bsl_iterator.h>
68
69#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
70#include <bsls_nativestd.h>
71#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
72
73
74namespace bdlb {
75
76 // ==============================
77 // struct AlgorithmWorkaroundUtil
78 // ==============================
79
80/// A namespace for replacement workarounds for some platform supplied
81/// faulty algorithms (particularly for the Sun Studio compiler).
82///
83/// See @ref bdlb_algorithmworkaroundutil
85
86 // CLASS METHODS
87
88 /// Returns an iterator pointing to the first element in the range
89 /// starting at the specified `first` iterator and ending immediately
90 /// before the specified `last` iterator, that is greater than or equal
91 /// to the specified `value`, or `last` if no such element is found.
92 /// This method works around a deficiency in the STLPort standard
93 /// library that prevents using @ref lower_bound to search for a value of a
94 /// different type than that held in the range being searched (even if
95 /// an appropriate `operator<` is defined). See DRQS 139734639.
96 template<class FORWARD_IT, class TYPE>
97 static
98 FORWARD_IT lowerBound(FORWARD_IT first,
99 FORWARD_IT last,
100 const TYPE& value);
101
102 /// Returns an iterator pointing to the first element in the range
103 /// starting at the specified `first` iterator and ending immediately
104 /// before the specified `last` iterator, that is greater than or equal
105 /// to the specified `value` as determined by the specified `comp`, or
106 /// `last` if no such element is found. This method works around a
107 /// deficiency in the STLPort standard library that prevents using
108 /// @ref lower_bound to search for a value of a different type than that
109 /// held in the range being searched (even if an appropriate `operator<`
110 /// is defined). See DRQS 139734639.
111 template <class FORWARD_IT, class TYPE, class COMPARE>
112 static
113 FORWARD_IT lowerBound(FORWARD_IT first,
114 FORWARD_IT last,
115 const TYPE& value,
116 COMPARE comp);
117
118 /// Returns an iterator pointing to the first element in the range
119 /// starting at the specified `first` iterator and ending immediately
120 /// before the specified `last` iterator, that is greater than the
121 /// specified `value`, or `last` if no such element is found. This
122 /// method works around a deficiency in the STLPort standard library
123 /// that prevents using @ref upper_bound to search for a value of a
124 /// different type than that held in the range being searched (even if
125 /// an appropriate `operator<` is defined). See DRQS 139734639.
126 template<class FORWARD_IT, class TYPE>
127 static
128 FORWARD_IT upperBound(FORWARD_IT first,
129 FORWARD_IT last,
130 const TYPE& value);
131
132 /// Returns an iterator pointing to the first element in the range
133 /// starting at the specified `first` iterator and ending immediately
134 /// before the specified `last` iterator, that is greater than the
135 /// specified `value` as determined by the specified `comp`, or `last`
136 /// if no such element is found. This method works around a deficiency
137 /// in the STLPort standard library that prevents using @ref upper_bound to
138 /// search for a value of a different type than that held in the range
139 /// being searched (even if an appropriate `operator<` is defined). See
140 /// DRQS 139734639.
141 template <class FORWARD_IT, class TYPE, class COMPARE>
142 static
143 FORWARD_IT upperBound(FORWARD_IT first,
144 FORWARD_IT last,
145 const TYPE& value,
146 COMPARE comp);
147};
148
149// ============================================================================
150// INLINE FUNCTION DEFINITIONS
151// ============================================================================
152
153 // ------------------------------
154 // struct AlgorithmWorkaroundUtil
155 // ------------------------------
156
157template<class FORWARD_IT, class TYPE>
158inline
159FORWARD_IT AlgorithmWorkaroundUtil::lowerBound(FORWARD_IT first,
160 FORWARD_IT last,
161 const TYPE& value)
162{
163#if defined(BSLS_LIBRARYFEATURES_STDCPP_STLPORT) \
164 && defined(_STLPORT_VERSION) \
165 && (_STLPORT_VERSION <= 0x452)
166
167 typedef typename bsl::iterator_traits<FORWARD_IT>::difference_type
168 difference_type;
169
170 difference_type length = bsl::distance(first, last);
171
172 while (length > 0) {
173 difference_type half = length >> 1;
174 FORWARD_IT it = first;
175
176 bsl::advance(it, half);
177 if (*it < value) {
178 first = ++it;
179 length -= half + 1;
180 }
181 else {
182 length = half;
183 }
184 }
185 return first;
186#else
187 return std::lower_bound(first, last, value);
188#endif
189}
190
191template <class FORWARD_IT, class TYPE, class COMPARE>
192inline
193FORWARD_IT AlgorithmWorkaroundUtil::lowerBound(FORWARD_IT first,
194 FORWARD_IT last,
195 const TYPE& value,
196 COMPARE comp)
197{
198#if defined(BSLS_LIBRARYFEATURES_STDCPP_STLPORT) \
199 && defined(_STLPORT_VERSION) \
200 && (_STLPORT_VERSION <= 0x452)
201
202 typedef typename bsl::iterator_traits<FORWARD_IT>::difference_type
203 difference_type;
204
205 difference_type length = bsl::distance(first, last);
206
207 while (length > 0) {
208 difference_type half = length >> 1;
209 FORWARD_IT it = first;
210
211 bsl::advance(it, half);
212 if (comp(*it, value)) {
213 first = ++it;
214 length -= half + 1;
215 }
216 else {
217 length = half;
218 }
219 }
220 return first;
221#else
222 return std::lower_bound(first, last, value, comp);
223#endif
224}
225
226template<class FORWARD_IT, class TYPE>
227inline
228FORWARD_IT AlgorithmWorkaroundUtil::upperBound(FORWARD_IT first,
229 FORWARD_IT last,
230 const TYPE& value)
231{
232#if defined(BSLS_LIBRARYFEATURES_STDCPP_STLPORT) \
233 && defined(_STLPORT_VERSION) \
234 && (_STLPORT_VERSION <= 0x452)
235
236 typedef typename bsl::iterator_traits<FORWARD_IT>::difference_type
237 difference_type;
238
239 difference_type length = bsl::distance(first, last);
240
241 while (length > 0) {
242 difference_type half = length >> 1;
243 FORWARD_IT it = first;
244
245 bsl::advance(it, half);
246 if (!(value < *it)) {
247 first = ++it;
248 length -= half + 1;
249 }
250 else {
251 length = half;
252 }
253 }
254 return first;
255#else
256 return std::upper_bound(first, last, value);
257#endif
258}
259
260template <class FORWARD_IT, class TYPE, class COMPARE>
261inline
262FORWARD_IT AlgorithmWorkaroundUtil::upperBound(FORWARD_IT first,
263 FORWARD_IT last,
264 const TYPE& value,
265 COMPARE comp)
266{
267#if defined(BSLS_LIBRARYFEATURES_STDCPP_STLPORT) \
268 && defined(_STLPORT_VERSION) \
269 && (_STLPORT_VERSION <= 0x452)
270
271 typedef typename bsl::iterator_traits<FORWARD_IT>::difference_type
272 difference_type;
273
274 difference_type length = bsl::distance(first, last);
275
276 while (length > 0) {
277 difference_type half = length >> 1;
278 FORWARD_IT it = first;
279
280 bsl::advance(it, half);
281 if (!comp(value, *it)) {
282 first = ++it;
283 length -= half + 1;
284 }
285 else {
286 length = half;
287 }
288 }
289 return first;
290#else
291 return std::upper_bound(first, last, value, comp);
292#endif
293}
294
295} // close package namespace
296
297
298#endif
299
300// ----------------------------------------------------------------------------
301// Copyright 2019 Bloomberg Finance L.P.
302//
303// Licensed under the Apache License, Version 2.0 (the "License");
304// you may not use this file except in compliance with the License.
305// You may obtain a copy of the License at
306//
307// http://www.apache.org/licenses/LICENSE-2.0
308//
309// Unless required by applicable law or agreed to in writing, software
310// distributed under the License is distributed on an "AS IS" BASIS,
311// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
312// See the License for the specific language governing permissions and
313// limitations under the License.
314// ----------------------------- END-OF-FILE ----------------------------------
315
316/** @} */
317/** @} */
318/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_algorithmworkaroundutil.h:84
static FORWARD_IT lowerBound(FORWARD_IT first, FORWARD_IT last, const TYPE &value)
Definition bdlb_algorithmworkaroundutil.h:159
static FORWARD_IT upperBound(FORWARD_IT first, FORWARD_IT last, const TYPE &value)
Definition bdlb_algorithmworkaroundutil.h:228