BDE 4.14.0 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).
83
84 // CLASS METHODS
85
86 /// Returns an iterator pointing to the first element in the range
87 /// starting at the specified `first` iterator and ending immediately
88 /// before the specified `last` iterator, that is greater than or equal
89 /// to the specified `value`, or `last` if no such element is found.
90 /// This method works around a deficiency in the STLPort standard
91 /// library that prevents using @ref lower_bound to search for a value of a
92 /// different type than that held in the range being searched (even if
93 /// an appropriate `operator<` is defined). See DRQS 139734639.
94 template<class FORWARD_IT, class TYPE>
95 static
96 FORWARD_IT lowerBound(FORWARD_IT first,
97 FORWARD_IT last,
98 const TYPE& value);
99
100 /// Returns an iterator pointing to the first element in the range
101 /// starting at the specified `first` iterator and ending immediately
102 /// before the specified `last` iterator, that is greater than or equal
103 /// to the specified `value` as determined by the specified `comp`, or
104 /// `last` if no such element is found. This method works around a
105 /// deficiency in the STLPort standard library that prevents using
106 /// @ref lower_bound to search for a value of a different type than that
107 /// held in the range being searched (even if an appropriate `operator<`
108 /// is defined). See DRQS 139734639.
109 template <class FORWARD_IT, class TYPE, class COMPARE>
110 static
111 FORWARD_IT lowerBound(FORWARD_IT first,
112 FORWARD_IT last,
113 const TYPE& value,
114 COMPARE comp);
115
116 /// Returns an iterator pointing to the first element in the range
117 /// starting at the specified `first` iterator and ending immediately
118 /// before the specified `last` iterator, that is greater than the
119 /// specified `value`, or `last` if no such element is found. This
120 /// method works around a deficiency in the STLPort standard library
121 /// that prevents using @ref upper_bound to search for a value of a
122 /// different type than that held in the range being searched (even if
123 /// an appropriate `operator<` is defined). See DRQS 139734639.
124 template<class FORWARD_IT, class TYPE>
125 static
126 FORWARD_IT upperBound(FORWARD_IT first,
127 FORWARD_IT last,
128 const TYPE& value);
129
130 /// Returns an iterator pointing to the first element in the range
131 /// starting at the specified `first` iterator and ending immediately
132 /// before the specified `last` iterator, that is greater than the
133 /// specified `value` as determined by the specified `comp`, or `last`
134 /// if no such element is found. This method works around a deficiency
135 /// in the STLPort standard library that prevents using @ref upper_bound to
136 /// search for a value of a different type than that held in the range
137 /// being searched (even if an appropriate `operator<` is defined). See
138 /// DRQS 139734639.
139 template <class FORWARD_IT, class TYPE, class COMPARE>
140 static
141 FORWARD_IT upperBound(FORWARD_IT first,
142 FORWARD_IT last,
143 const TYPE& value,
144 COMPARE comp);
145};
146
147// ============================================================================
148// INLINE FUNCTION DEFINITIONS
149// ============================================================================
150
151 // ------------------------------
152 // struct AlgorithmWorkaroundUtil
153 // ------------------------------
154
155template<class FORWARD_IT, class TYPE>
156inline
157FORWARD_IT AlgorithmWorkaroundUtil::lowerBound(FORWARD_IT first,
158 FORWARD_IT last,
159 const TYPE& value)
160{
161#if defined(BSLS_LIBRARYFEATURES_STDCPP_STLPORT) \
162 && defined(_STLPORT_VERSION) \
163 && (_STLPORT_VERSION <= 0x452)
164
165 typedef typename bsl::iterator_traits<FORWARD_IT>::difference_type
166 difference_type;
167
168 difference_type length = bsl::distance(first, last);
169
170 while (length > 0) {
171 difference_type half = length >> 1;
172 FORWARD_IT it = first;
173
174 bsl::advance(it, half);
175 if (*it < value) {
176 first = ++it;
177 length -= half + 1;
178 }
179 else {
180 length = half;
181 }
182 }
183 return first;
184#else
185 return std::lower_bound(first, last, value);
186#endif
187}
188
189template <class FORWARD_IT, class TYPE, class COMPARE>
190inline
191FORWARD_IT AlgorithmWorkaroundUtil::lowerBound(FORWARD_IT first,
192 FORWARD_IT last,
193 const TYPE& value,
194 COMPARE comp)
195{
196#if defined(BSLS_LIBRARYFEATURES_STDCPP_STLPORT) \
197 && defined(_STLPORT_VERSION) \
198 && (_STLPORT_VERSION <= 0x452)
199
200 typedef typename bsl::iterator_traits<FORWARD_IT>::difference_type
201 difference_type;
202
203 difference_type length = bsl::distance(first, last);
204
205 while (length > 0) {
206 difference_type half = length >> 1;
207 FORWARD_IT it = first;
208
209 bsl::advance(it, half);
210 if (comp(*it, value)) {
211 first = ++it;
212 length -= half + 1;
213 }
214 else {
215 length = half;
216 }
217 }
218 return first;
219#else
220 return std::lower_bound(first, last, value, comp);
221#endif
222}
223
224template<class FORWARD_IT, class TYPE>
225inline
226FORWARD_IT AlgorithmWorkaroundUtil::upperBound(FORWARD_IT first,
227 FORWARD_IT last,
228 const TYPE& value)
229{
230#if defined(BSLS_LIBRARYFEATURES_STDCPP_STLPORT) \
231 && defined(_STLPORT_VERSION) \
232 && (_STLPORT_VERSION <= 0x452)
233
234 typedef typename bsl::iterator_traits<FORWARD_IT>::difference_type
235 difference_type;
236
237 difference_type length = bsl::distance(first, last);
238
239 while (length > 0) {
240 difference_type half = length >> 1;
241 FORWARD_IT it = first;
242
243 bsl::advance(it, half);
244 if (!(value < *it)) {
245 first = ++it;
246 length -= half + 1;
247 }
248 else {
249 length = half;
250 }
251 }
252 return first;
253#else
254 return std::upper_bound(first, last, value);
255#endif
256}
257
258template <class FORWARD_IT, class TYPE, class COMPARE>
259inline
260FORWARD_IT AlgorithmWorkaroundUtil::upperBound(FORWARD_IT first,
261 FORWARD_IT last,
262 const TYPE& value,
263 COMPARE comp)
264{
265#if defined(BSLS_LIBRARYFEATURES_STDCPP_STLPORT) \
266 && defined(_STLPORT_VERSION) \
267 && (_STLPORT_VERSION <= 0x452)
268
269 typedef typename bsl::iterator_traits<FORWARD_IT>::difference_type
270 difference_type;
271
272 difference_type length = bsl::distance(first, last);
273
274 while (length > 0) {
275 difference_type half = length >> 1;
276 FORWARD_IT it = first;
277
278 bsl::advance(it, half);
279 if (!comp(value, *it)) {
280 first = ++it;
281 length -= half + 1;
282 }
283 else {
284 length = half;
285 }
286 }
287 return first;
288#else
289 return std::upper_bound(first, last, value, comp);
290#endif
291}
292
293} // close package namespace
294
295
296#endif
297
298// ----------------------------------------------------------------------------
299// Copyright 2019 Bloomberg Finance L.P.
300//
301// Licensed under the Apache License, Version 2.0 (the "License");
302// you may not use this file except in compliance with the License.
303// You may obtain a copy of the License at
304//
305// http://www.apache.org/licenses/LICENSE-2.0
306//
307// Unless required by applicable law or agreed to in writing, software
308// distributed under the License is distributed on an "AS IS" BASIS,
309// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
310// See the License for the specific language governing permissions and
311// limitations under the License.
312// ----------------------------- END-OF-FILE ----------------------------------
313
314/** @} */
315/** @} */
316/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_algorithmworkaroundutil.h:82
static FORWARD_IT lowerBound(FORWARD_IT first, FORWARD_IT last, const TYPE &value)
Definition bdlb_algorithmworkaroundutil.h:157
static FORWARD_IT upperBound(FORWARD_IT first, FORWARD_IT last, const TYPE &value)
Definition bdlb_algorithmworkaroundutil.h:226