BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_algorithm.h
Go to the documentation of this file.
1/// @file bslstl_algorithm.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_algorithm.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_ALGORITHM
9#define INCLUDED_BSLSTL_ALGORITHM
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_algorithm bslstl_algorithm
15/// @brief Provide implementations for algorithms not in the system library.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_algorithm
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_algorithm-purpose"> Purpose</a>
25/// * <a href="#bslstl_algorithm-classes"> Classes </a>
26/// * <a href="#bslstl_algorithm-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstl_algorithm-description"> Description </a>
28/// * <a href="#bslstl_algorithm-usage"> Usage </a>
29///
30/// # Purpose {#bslstl_algorithm-purpose}
31/// Provide implementations for algorithms not in the system library.
32///
33/// # Classes {#bslstl_algorithm-classes}
34///
35///
36/// # Canonical Header {#bslstl_algorithm-canonical-header}
37/// bsl_algorithm.h
38///
39/// @see bsl+bslhdrs
40///
41/// # Description {#bslstl_algorithm-description}
42/// This component is for internal use only. Please include
43/// `<bsl_algorithm.h>` instead. This component provides a namespace for
44/// implementations for standard algorithms that are not provided by the
45/// underlying standard library implementation. For example, `any_of` is a
46/// C++11 algorithm, and it is provided here for code using C++03.
47///
48/// ## Usage {#bslstl_algorithm-usage}
49///
50///
51/// This component is for use by the `bsl+bslhdrs` package. Use
52/// `bsl_algorithm.h` directly.
53/// @}
54/** @} */
55/** @} */
56
57/** @addtogroup bsl
58 * @{
59 */
60/** @addtogroup bslstl
61 * @{
62 */
63/** @addtogroup bslstl_algorithm
64 * @{
65 */
66
67#include <bslscm_version.h>
68#include <bsls_assert.h>
70#include <bsls_keyword.h>
72
73#include <bslstl_iterator.h> // iterator tags
74#include <bslstl_pair.h>
75
76#include <algorithm>
77#include <functional> // less
78
79#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
80#include <bsls_nativestd.h>
81#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
82
83
84namespace bslstl {
85
86/// Provide a namespace for implementing helper routines for algorithm
87/// implementations.
88///
89/// See @ref bslstl_algorithm
91
92 private:
93 // PRIVATE CLASS METHODS
94
95 /// Copy the specified `count` elements from the specified `first`
96 /// to the specified `result`. Return an iterator pointing past
97 /// the last copied element in the output range.
98 template <class t_INPUT_ITERATOR, class t_SIZE, class t_OUTPUT_ITERATOR>
99 static
100 t_OUTPUT_ITERATOR copyNImp(t_INPUT_ITERATOR first,
101 t_SIZE count,
102 t_OUTPUT_ITERATOR result,
103 const bsl::input_iterator_tag&);
104 template <class t_RANDOM_ACCESS_ITERATOR,
105 class t_SIZE,
106 class t_OUTPUT_ITERATOR>
107 static
108 t_OUTPUT_ITERATOR copyNImp(t_RANDOM_ACCESS_ITERATOR first,
109 t_SIZE count,
110 t_OUTPUT_ITERATOR result,
111 const bsl::random_access_iterator_tag&);
112
113 public:
114 // CLASS FUNCTIONS
115
116 /// Erase all the elements in the specified container `container` that
117 /// satisfy the specified predicate `predicate`. Return the number of
118 /// elements erased.
119 template <class CONTAINER, class PREDICATE>
120 static
121 typename CONTAINER::size_type
122 containerEraseIf(CONTAINER& container, PREDICATE predicate);
123
124 /// Copy the specified `count` elements from the specified `first`
125 /// to the specified `result`. Return an iterator pointing past
126 /// the last copied element in the output range.
127 template <class t_INPUT_ITERATOR, class t_SIZE, class t_OUTPUT_ITERATOR>
128 static
129 t_OUTPUT_ITERATOR copyN(t_INPUT_ITERATOR first,
130 t_SIZE count,
131 t_OUTPUT_ITERATOR result);
132};
133
134} // close package namespace
135
136
137namespace bsl {
138
139 // Import selected symbols into bsl namespace
140 using std::adjacent_find;
141 using std::binary_search;
142 using std::copy;
143 using std::copy_backward;
144
145 using std::equal;
146 using std::equal_range;
147 using std::fill;
148 using std::fill_n;
149 using std::find;
150 using std::find_end;
151 using std::find_first_of;
152 using std::find_if;
153 using std::for_each;
154 using std::generate;
155 using std::generate_n;
156 using std::includes;
157 using std::inplace_merge;
158 using std::iter_swap;
159 using std::lexicographical_compare;
160 using std::lower_bound;
161
162 using std::make_heap;
163 using std::max;
164 using std::max_element;
165 using std::merge;
166 using std::min;
167 using std::min_element;
168 using std::mismatch;
169 using std::next_permutation;
170 using std::nth_element;
171 using std::partial_sort;
172 using std::partial_sort_copy;
173 using std::partition;
174 using std::pop_heap;
175 using std::prev_permutation;
176 using std::push_heap;
177 using std::remove;
178 using std::remove_copy;
179 using std::remove_copy_if;
180 using std::remove_if;
181 using std::replace;
182 using std::replace_copy;
183 using std::replace_copy_if;
184 using std::replace_if;
185 using std::reverse;
186 using std::reverse_copy;
187 using std::rotate;
188 using std::rotate_copy;
189 using std::search;
190 using std::search_n;
191 using std::set_difference;
192 using std::set_intersection;
193 using std::set_symmetric_difference;
194 using std::set_union;
195 using std::sort;
196 using std::sort_heap;
197 using std::stable_partition;
198 using std::stable_sort;
199 using std::swap;
200 using std::swap_ranges;
201 using std::transform;
202 using std::unique;
203 using std::unique_copy;
204 using std::upper_bound;
205
206#if ! defined BSLS_LIBRARYFEATURES_HAS_CPP17_DEPRECATED_REMOVED
207 // These names are removed by C++17
208 using std::random_shuffle;
209#endif
210
211#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
212 using std::all_of;
213 using std::any_of;
214 using std::copy_if;
215
216 using std::copy_n;
217 #define BSLSTL_ALGORITHM_COPY_N_IS_ALIASED
218
219 using std::find_if_not;
220 using std::is_heap;
221 using std::is_heap_until;
222 using std::is_partitioned;
223 using std::is_permutation;
224 using std::is_sorted;
225 using std::is_sorted_until;
226 using std::minmax;
227 using std::minmax_element;
228 using std::move;
229 using std::move_backward;
230 using std::none_of;
231 using std::partition_copy;
232 using std::partition_point;
233 using std::shuffle;
234#endif // BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
235
236// C++14 Algorithms
237// We get these via the "using std::equal" above
238// equal
239// mismatch
240
241#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
242 using std::clamp;
243 using std::for_each_n;
244 using std::sample;
245#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
246
247#ifdef BSLS_COMPILERFEATURES_SUPPORT_THREE_WAY_COMPARISON
248 using std::lexicographical_compare_three_way;
249#endif // BSLS_COMPILERFEATURES_SUPPORT_THREE_WAY_COMPARISON
250
251#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_RANGES
252namespace ranges {
253
254 // Non-modifying sequence operations
255
256 using std::ranges::all_of;
257 using std::ranges::any_of;
258 using std::ranges::none_of;
259 using std::ranges::for_each;
260 using std::ranges::for_each_result;
261 using std::ranges::for_each_n;
262 using std::ranges::for_each_n_result;
263 using std::ranges::count;
264 using std::ranges::count_if;
265 using std::ranges::mismatch;
266 using std::ranges::mismatch_result;
267 using std::ranges::equal;
268 using std::ranges::lexicographical_compare;
269 using std::ranges::find;
270 using std::ranges::find_if;
271 using std::ranges::find_if_not;
272 using std::ranges::find_end;
273 using std::ranges::find_first_of;
274 using std::ranges::adjacent_find;
275 using std::ranges::search;
276 using std::ranges::search_n;
277
278 // Modifying sequence operations
279
280 using std::ranges::copy;
281 using std::ranges::copy_result;
282 using std::ranges::copy_if;
283 using std::ranges::copy_if_result;
284 using std::ranges::copy_n;
285 using std::ranges::copy_n_result;
286 using std::ranges::copy_backward;
287 using std::ranges::copy_backward_result;
288 using std::ranges::move;
289 using std::ranges::move_result;
290 using std::ranges::move_backward;
291 using std::ranges::move_backward_result;
292 using std::ranges::fill;
293 using std::ranges::fill_n;
294 using std::ranges::transform;
295 using std::ranges::unary_transform_result;
296 using std::ranges::binary_transform_result;
297 using std::ranges::generate;
298 using std::ranges::generate_n;
299 using std::ranges::remove;
300 using std::ranges::remove_if;
301 using std::ranges::remove_copy;
302 using std::ranges::remove_copy_result;
303 using std::ranges::remove_copy_if;
304 using std::ranges::remove_copy_if_result;
305 using std::ranges::replace;
306 using std::ranges::replace_if;
307 using std::ranges::replace_copy;
308 using std::ranges::replace_copy_result;
309 using std::ranges::replace_copy_if;
310 using std::ranges::replace_copy_if_result;
311 using std::ranges::swap_ranges;
312 using std::ranges::swap_ranges_result;
313 using std::ranges::reverse;
314 using std::ranges::reverse_copy;
315 using std::ranges::reverse_copy_result;
316 using std::ranges::rotate;
317 using std::ranges::rotate_copy;
318 using std::ranges::rotate_copy_result;
319 using std::ranges::shuffle;
320 using std::ranges::sample;
321 using std::ranges::unique;
322 using std::ranges::unique_copy;
323 using std::ranges::unique_copy_result;
324
325 // Partitioning operations
326
327 using std::ranges::is_partitioned;
328 using std::ranges::partition;
329 using std::ranges::partition_copy;
330 using std::ranges::partition_copy_result;
331 using std::ranges::stable_partition;
332 using std::ranges::partition_point;
333
334 // Sorting operations
335
336 using std::ranges::is_sorted;
337 using std::ranges::is_sorted_until;
338 using std::ranges::sort;
339 using std::ranges::partial_sort;
340 using std::ranges::partial_sort_copy;
341 using std::ranges::stable_sort;
342 using std::ranges::nth_element;
343
344 // Binary search operations (on sorted ranges)
345
346 using std::ranges::lower_bound;
347 using std::ranges::upper_bound;
348 using std::ranges::binary_search;
349 using std::ranges::equal_range;
350
351 // Other operations (on sorted ranges)
352
353 using std::ranges::merge;
354 using std::ranges::merge_result;
355 using std::ranges::inplace_merge;
356
357 // Set operations (on sorted ranges)
358
359 using std::ranges::includes;
360 using std::ranges::set_difference;
361 using std::ranges::set_difference_result;
362 using std::ranges::set_intersection;
363 using std::ranges::set_intersection_result;
364 using std::ranges::set_symmetric_difference;
365 using std::ranges::set_symmetric_difference_result;
366 using std::ranges::set_union;
367 using std::ranges::set_union_result;
368
369 // Heap operations
370
371 using std::ranges::is_heap;
372 using std::ranges::is_heap_until;
373 using std::ranges::make_heap;
374 using std::ranges::push_heap;
375 using std::ranges::pop_heap;
376 using std::ranges::sort_heap;
377
378 // Minimum/maximum operations
379
380 using std::ranges::max;
381 using std::ranges::max_element;
382 using std::ranges::min;
383 using std::ranges::min_element;
384 using std::ranges::minmax;
385 using std::ranges::minmax_result;
386 using std::ranges::minmax_element;
387 using std::ranges::minmax_element_result;
388 using std::ranges::clamp;
389 using std::ranges::is_permutation;
390 using std::ranges::next_permutation;
391 using std::ranges::next_permutation_result;
392 using std::ranges::prev_permutation;
393 using std::ranges::prev_permutation_result;
394
395 // Return types
396
397 using std::ranges::in_fun_result;
398 using std::ranges::in_in_result;
399 using std::ranges::in_out_result;
400 using std::ranges::in_in_out_result;
401 using std::ranges::in_out_out_result;
402 using std::ranges::min_max_result;
403 using std::ranges::in_found_result;
404}
405#endif // BSLS_LIBRARYFEATURES_HAS_CPP20_RANGES
406
407#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGES_SHIFT
408namespace ranges {
409 using std::ranges::shift_left;
410 using std::ranges::shift_right;
411}
412#endif
413
414#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGES_STARTS_ENDS_WITH
415namespace ranges {
416 using std::ranges::starts_with;
417 using std::ranges::ends_with;
418}
419#endif
420
421#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGES_FIND_LAST
422namespace ranges {
423 using std::ranges::find_last;
424 using std::ranges::find_last_if;
425 using std::ranges::find_last_if_not;
426}
427#endif
428
429#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGES_CONTAINS
430namespace ranges {
431 using std::ranges::contains;
432 using std::ranges::contains_subrange;
433}
434#endif
435
436#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGES_FOLD
437namespace ranges {
438 using std::ranges::fold_left;
439 using std::ranges::fold_left_with_iter;
440 using std::ranges::fold_left_with_iter_result;
441 using std::ranges::fold_left_first;
442 using std::ranges::fold_left_first_with_iter;
443 using std::ranges::fold_left_first_with_iter_result;
444 using std::ranges::fold_right;
445 using std::ranges::fold_right_last;
446 using std::ranges::in_value_result;
447}
448#endif
449
450#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGES_IOTA
451namespace ranges {
452 using std::ranges::out_value_result;
453}
454#endif
455
456#ifndef BDE_OMIT_INTERNAL_DEPRECATED
457 // Import additional names expected by existing code, but not mandated by
458 // the standard header.
459 using std::advance;
460 using std::bad_alloc;
461 using std::bidirectional_iterator_tag;
462 using std::forward_iterator_tag;
463 using std::input_iterator_tag;
464# ifndef BSLS_PLATFORM_CMP_MSVC
465 using std::iterator;
466# endif
467# ifndef BSLS_LIBRARYFEATURES_STDCPP_LLVM
468 using std::new_handler;
469# endif
470 using std::nothrow;
471 using std::nothrow_t;
472 using std::output_iterator_tag;
473 using std::random_access_iterator_tag;
474# ifndef BSLS_LIBRARYFEATURES_STDCPP_LLVM
475 using std::set_new_handler;
476# endif
477#endif // BDE_OMIT_INTERNAL_DEPRECATED
478
479#ifndef BSLSTL_ITERATOR_PROVIDE_SUN_CPP98_FIXES
480 // Use the compiler vendor supplied version of `count` and `count_if`.
481 using std::count;
482 using std::count_if;
483#else
484 // Sun-specific fixes
485
486 /// Provide an override for `count` since Sun only provides a 4 argument
487 /// version while the C++ standard requires a 3 argument version.
488 template <class InputIter, class TYPE>
489 typename iterator_traits<InputIter>::difference_type
490 count(InputIter first, InputIter last, const TYPE& value);
491
492 /// Provide an override for `count_if` since Sun only provides a 4
493 /// argument version while the C++ standard requires a 3 argument
494 /// version.
495 template <class InputIter, class PREDICATE>
496 typename iterator_traits<InputIter>::difference_type
497 count_if(InputIter first, InputIter last, PREDICATE pred);
498#endif // BSLSTL_ITERATOR_PROVIDE_SUN_CPP98_FIXES
499
500
501#ifndef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
502 /// Return `true` if, for the specified `[first, last)` range and the
503 /// specified predicate `pred`, the range is either empty or `pred(*i)`
504 /// is `true` for every iterator `i` in the range, and `false` otherwise.
505 ///
506 /// \note Note that at most `last - first` applications of the
507 /// predicate are performed.
508 template <class INPUT_ITERATOR, class PREDICATE>
509 bool all_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred);
510
511 /// Return `false` if, for the specified `[first, last)` range and the
512 /// specified predicate `pred`, the range is either empty or `pred(*i)`
513 /// is `false` for every iterator `i` in the range, and `true` otherwise.
514 ///
515 /// \note Note that at most `last - first` applications of the
516 /// predicate are performed.
517 template <class INPUT_ITERATOR, class PREDICATE>
518 bool any_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred);
519
520 /// Return `true` if, for the specified `[first, last)` range and the
521 /// specified predicate `pred`, the range is either empty or `pred(*i)`
522 /// is `false` for every iterator `i` in the range, and `false` otherwise.
523 ///
524 /// \note Note that at most `last - first` applications of the
525 /// predicate are performed.
526 template <class INPUT_ITERATOR, class PREDICATE>
527 bool none_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred);
528
529# if defined(BSLS_LIBRARYFEATURES_STDCPP_MSVC)
530 // Visual Studio (the versions we support) provides 'copy_if'.
531 using std::copy_if;
532# else
533/// C++03 standard libraries do not provide `std::copy_if` (as it was not
534/// part of the C++03 standard), but it is actually implementable in C++03,
535/// so we inject it here.
536# define BSLSTL_ALGORITHMWORKAROUND_IMPLEMENTS_COPY_IF 1
537
538 /// Copy all elements in the half-open range of the specified `first`,
539 /// and `last` (`[first, last)`) input iterators for which the specified
540 /// `pred` unary predicate is `true` to the specified `result` output
541 /// iterator, incrementing result after each copied element, keeping the element order stable.
542 ///
543 /// \pre The behavior is undefined if the ranges
544 /// `[first, last)` and
545 /// `[result, advance(result, distance(first, last)))` overlap. The
546 /// behavior is also undefined if `pred` attempts to invoke any
547 /// non-constant functions of its argument. See also [alg.copy] in the
548 /// C++11 standard.
549 template <class INPUT_ITERATOR, class OUTPUT_ITERATOR, class PREDICATE>
550 OUTPUT_ITERATOR
551 copy_if(INPUT_ITERATOR first,
552 INPUT_ITERATOR last,
553 OUTPUT_ITERATOR result,
554 PREDICATE pred);
555# endif // !BSLS_LIBRARYFEATURES_STDCPP_MSVC
556#endif // !BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
557
558#ifndef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
559 /// Return the specified `value` adjusted so that it is in the range
560 /// [`low`, `high`), using the specified comparison predicate `comp`.
561 template<class TYPE, class COMPARE>
563 const TYPE&
564 clamp(const TYPE& value, const TYPE& low, const TYPE& high, COMPARE comp);
565
566 /// Return the specified `value` adjusted so that it is in the range
567 /// [`low`, `high`).
568 template<class TYPE>
570 const TYPE&
571 clamp(const TYPE& value, const TYPE& low, const TYPE& high);
572#endif
573
574#ifndef BSLS_LIBRARYFEATURES_HAS_CPP17_SEARCH_OVERLOAD
575 /// Return the position in the specified range `[first, last)` of the
576 /// first occurrence of the pattern sought by the specified `searcher`
577 /// if found, and `last` otherwise. See [alg.search].
578 template<class FORWARD_ITERATOR, class SEARCHER>
580 FORWARD_ITERATOR search(FORWARD_ITERATOR first,
581 FORWARD_ITERATOR last,
582 const SEARCHER& searcher);
583#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_SEARCH_OVERLOAD
584
585} // close namespace bsl
586
587
588
589#ifndef BSLSTL_ALGORITHM_COPY_N_IS_ALIASED
590namespace bsl {
591/// Copy the specified `count` elements from the specified `first` to the
592/// specified `result`. Return an iterator pointing past the last copied
593/// element in the output range.
594template <class t_INPUT_ITERATOR, class t_SIZE, class t_OUTPUT_ITERATOR>
595inline
596t_OUTPUT_ITERATOR copy_n(t_INPUT_ITERATOR first,
597 t_SIZE count,
598 t_OUTPUT_ITERATOR result);
599} // close namespace bsl
600#endif // BSLSTL_ALGORITHM_COPY_N_IS_ALIASED
601
602// ============================================================================
603// INLINE DEFINITIONS
604// ============================================================================
605
606 // --------------------
607 // struct AlgorithmUtil
608 // --------------------
609
610// PRIVATE CLASS METHODS
611template <class t_INPUT_ITERATOR, class t_SIZE, class t_OUTPUT_ITERATOR>
612inline
613t_OUTPUT_ITERATOR BloombergLP::bslstl::AlgorithmUtil::copyNImp(
614 t_INPUT_ITERATOR first,
615 t_SIZE count,
616 t_OUTPUT_ITERATOR result,
617 const bsl::input_iterator_tag&)
618{
619 for ( ; count > 0; --count) {
620 *result = *first;
621 ++first;
622 ++result;
623 }
624 return result;
625}
626
627template <class t_RANDOM_ACCESS_ITERATOR,
628 class t_SIZE,
629 class t_OUTPUT_ITERATOR>
630inline
631t_OUTPUT_ITERATOR BloombergLP::bslstl::AlgorithmUtil::copyNImp(
632 t_RANDOM_ACCESS_ITERATOR first,
633 t_SIZE count,
634 t_OUTPUT_ITERATOR result,
635 const bsl::random_access_iterator_tag&)
636{
637 return std::copy(first, first + count, result);
638}
639
640// CLASS METHODS
641template <class CONTAINER, class PREDICATE>
642inline
643typename CONTAINER::size_type
644BloombergLP::bslstl::AlgorithmUtil::containerEraseIf(CONTAINER& container,
645 PREDICATE predicate)
646{
647 typename CONTAINER::size_type oldSize = container.size();
648 for (typename CONTAINER::iterator it = container.begin();
649 it != container.end();) {
650 if (predicate(*it)) {
651 it = container.erase(it);
652 }
653 else {
654 ++it;
655 }
656 }
657 return oldSize - container.size();
658}
659
660template <class t_INPUT_ITERATOR, class t_SIZE, class t_OUTPUT_ITERATOR>
661inline
662t_OUTPUT_ITERATOR BloombergLP::bslstl::AlgorithmUtil::copyN(
663 t_INPUT_ITERATOR first,
664 t_SIZE count,
665 t_OUTPUT_ITERATOR result)
666{
667 typedef typename bsl::iterator_traits<t_INPUT_ITERATOR>::iterator_category
668 Tag;
669 return BloombergLP::bslstl::AlgorithmUtil::copyNImp(first,
670 count,
671 result,
672 Tag());
673}
674
675#ifdef BSLSTL_ITERATOR_PROVIDE_SUN_CPP98_FIXES
676template <class INPUT_ITERATOR, class TYPE>
677inline
678typename bsl::iterator_traits<INPUT_ITERATOR>::difference_type
679bsl::count(INPUT_ITERATOR first, INPUT_ITERATOR last, const TYPE& value)
680{
681 typename bsl::iterator_traits<INPUT_ITERATOR>::difference_type ret = 0;
682 std::count(first, last, value, ret);
683 return ret;
684}
685
686template <class INPUT_ITERATOR, class PREDICATE>
687inline
688typename bsl::iterator_traits<INPUT_ITERATOR>::difference_type
689bsl::count_if(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred)
690{
691 typename bsl::iterator_traits<INPUT_ITERATOR>::difference_type ret = 0;
692 std::count_if(first, last, pred, ret);
693 return ret;
694}
695#endif // BSLSTL_ITERATOR_PROVIDE_SUN_CPP98_FIXES
696
697
698#ifndef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
699
700template <class INPUT_ITERATOR, class PREDICATE>
701bool bsl::all_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred)
702{
703 for (; first != last; ++first) {
704 if (!pred(*first)) {
705 return false; // RETURN
706 }
707 }
708 return true;
709}
710
711template <class INPUT_ITERATOR, class PREDICATE>
712bool bsl::any_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred)
713{
714 for (; first != last; ++first) {
715 if (pred(*first)) {
716 return true; // RETURN
717 }
718 }
719 return false;
720}
721
722template <class INPUT_ITERATOR, class PREDICATE>
723bool bsl::none_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred)
724{
725 for (; first != last; ++first) {
726 if (pred(*first)) {
727 return false; // RETURN
728 }
729 }
730 return true;
731}
732
733# ifdef BSLSTL_ALGORITHMWORKAROUND_IMPLEMENTS_COPY_IF
734template <class INPUT_ITERATOR, class OUTPUT_ITERATOR, class PREDICATE>
735inline OUTPUT_ITERATOR
736bsl::copy_if(INPUT_ITERATOR first,
737 INPUT_ITERATOR last,
738 OUTPUT_ITERATOR result,
739 PREDICATE pred)
740{
741 while(first != last) {
742 if (pred(*first)) {
743 *result++ = *first;
744 }
745 ++first;
746 }
747 return result;
748}
749# endif // BSLSTL_ALGORITHMWORKAROUND_IMPLEMENTS_COPY_IF
750
751#endif // BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
752
753
754#ifndef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
755template<class TYPE, class COMPARE>
757inline const TYPE&
758bsl::clamp(const TYPE& value, const TYPE& low, const TYPE& high, COMPARE comp)
759{
760 BSLS_ASSERT(!comp(high, low));
761 return comp(value, low) ? low : comp(high, value) ? high : value;
762}
763
764template<class TYPE>
766inline const TYPE&
767bsl::clamp(const TYPE& value, const TYPE& low, const TYPE& high)
768{
769 return bsl::clamp(value, low, high, std::less<TYPE>());
770}
771#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
772
773
774#ifndef BSLS_LIBRARYFEATURES_HAS_CPP17_SEARCH_OVERLOAD
775template<class FORWARD_ITERATOR, class SEARCHER>
776inline
777BSLS_KEYWORD_CONSTEXPR_CPP14 FORWARD_ITERATOR
778bsl::search(FORWARD_ITERATOR first,
779 FORWARD_ITERATOR last,
780 const SEARCHER& searcher)
781{
782 bsl::pair<FORWARD_ITERATOR, FORWARD_ITERATOR> res = searcher(first, last);
783 return res.first;
784}
785#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_SEARCH_OVERLOAD
786
787#ifndef BSLSTL_ALGORITHM_COPY_N_IS_ALIASED
788template <class t_INPUT_ITERATOR, class t_SIZE, class t_OUTPUT_ITERATOR>
789inline
790t_OUTPUT_ITERATOR bsl::copy_n(t_INPUT_ITERATOR first,
791 t_SIZE count,
792 t_OUTPUT_ITERATOR result)
793{
794 return BloombergLP::bslstl::AlgorithmUtil::copyN(first,
795 count,
796 result);
797}
798#endif // BSLSTL_ALGORITHM_COPY_N_IS_ALIASED
799
800#endif // INCLUDED_BSLSTL_ALGORITHM
801
802// ----------------------------------------------------------------------------
803// Copyright 2020 Bloomberg Finance L.P.
804//
805// Licensed under the Apache License, Version 2.0 (the "License");
806// you may not use this file except in compliance with the License.
807// You may obtain a copy of the License at
808//
809// http://www.apache.org/licenses/LICENSE-2.0
810//
811// Unless required by applicable law or agreed to in writing, software
812// distributed under the License is distributed on an "AS IS" BASIS,
813// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
814// See the License for the specific language governing permissions and
815// limitations under the License.
816// ----------------------------- END-OF-FILE ----------------------------------
817
818/** @} */
819/** @} */
820/** @} */
Definition bslstl_pair.h:1280
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_CONSTEXPR_CPP14
Definition bsls_keyword.h:631
Definition bdlat_valuetypefunctions.h:939
BSLS_KEYWORD_CONSTEXPR_CPP14 const TYPE & clamp(const TYPE &value, const TYPE &low, const TYPE &high, COMPARE comp)
BSLS_KEYWORD_CONSTEXPR_CPP14 FORWARD_ITERATOR search(FORWARD_ITERATOR first, FORWARD_ITERATOR last, const SEARCHER &searcher)
bool all_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred)
bool any_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred)
OUTPUT_ITERATOR copy_if(INPUT_ITERATOR first, INPUT_ITERATOR last, OUTPUT_ITERATOR result, PREDICATE pred)
t_OUTPUT_ITERATOR copy_n(t_INPUT_ITERATOR first, t_SIZE count, t_OUTPUT_ITERATOR result)
bool none_of(INPUT_ITERATOR first, INPUT_ITERATOR last, PREDICATE pred)
Definition bslstl_algorithm.h:84
TYPE first
Definition bslstl_pair.h:587
Definition bslstl_algorithm.h:90
static CONTAINER::size_type containerEraseIf(CONTAINER &container, PREDICATE predicate)
static t_OUTPUT_ITERATOR copyN(t_INPUT_ITERATOR first, t_SIZE count, t_OUTPUT_ITERATOR result)