1 /* Copyright 2016-2017 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * See http://www.boost.org/libs/poly_collection for library home page.
9 #ifndef BOOST_POLY_COLLECTION_ALGORITHM_HPP
10 #define BOOST_POLY_COLLECTION_ALGORITHM_HPP
17 #include <boost/poly_collection/detail/auto_iterator.hpp>
18 #include <boost/poly_collection/detail/functional.hpp>
19 #include <boost/poly_collection/detail/iterator_traits.hpp>
20 #include <boost/poly_collection/detail/segment_split.hpp>
21 #include <boost/poly_collection/detail/type_restitution.hpp>
22 #include <boost/poly_collection/detail/workaround_dr1467.hpp>
24 #include <type_traits>
27 /* Improved performance versions of std algorithms over poly_collection.
28 * poly_collection::alg is expected to be faster than the homonym std::alg
29 * because the latter does a traversal over a segmented structured, where
30 * incrementing requires checking for segment change, whereas the former
31 * for-loops over flat segments.
32 * Additionally, poly_collection::alg<Ti...>(...,f) *restitutes* Ti when
33 * passing elements to f, i.e. if the concrete type of the element is Ti
34 * then f is invoked with a [const] Ti&, which can dramatically improve
35 * performance when f has specific overloads for Ti (like, for instance,
36 * generic lambdas) as static optimization can kick in (devirtualization
37 * being a particular example).
42 namespace poly_collection{
48 template<typename Iterator>
49 using enable_if_poly_collection_iterator=typename std::enable_if<
50 !std::is_void<typename poly_collection_of<Iterator>::type>::value
53 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_all_of,std::all_of)
56 typename... Ts,typename Iterator,typename Predicate,
57 enable_if_poly_collection_iterator<Iterator> =nullptr
59 bool all_of(const Iterator& first,const Iterator& last,Predicate pred)
61 auto alg=restitute_range<Ts...>(std_all_of{},pred);
62 for(auto i:detail::segment_split(first,last))if(!alg(i))return false;
66 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_any_of,std::any_of)
69 typename... Ts,typename Iterator,typename Predicate,
70 enable_if_poly_collection_iterator<Iterator> =nullptr
72 bool any_of(const Iterator& first,const Iterator& last,Predicate pred)
74 auto alg=restitute_range<Ts...>(std_any_of{},pred);
75 for(auto i:detail::segment_split(first,last))if(alg(i))return true;
79 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_none_of,std::none_of)
82 typename... Ts,typename Iterator,typename Predicate,
83 enable_if_poly_collection_iterator<Iterator> =nullptr
85 bool none_of(const Iterator& first,const Iterator& last,Predicate pred)
87 auto alg=restitute_range<Ts...>(std_none_of{},pred);
88 for(auto i:detail::segment_split(first,last))if(!alg(i))return false;
94 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(for_each_alg)
96 template<typename InputIterator,typename Function>
98 InputIterator first,InputIterator last,Function& f)const /* note the & */
100 for(;first!=last;++first)f(*first);
105 typename... Ts,typename Iterator,typename Function,
106 enable_if_poly_collection_iterator<Iterator> =nullptr
108 Function for_each(const Iterator& first,const Iterator& last,Function f)
110 for_each_segment(first,last,restitute_range<Ts...>(for_each_alg{},f));
115 typename Algorithm,typename... Ts,
116 typename Iterator,typename... Args,
117 enable_if_poly_collection_iterator<Iterator> =nullptr
119 Iterator generic_find(
120 const Iterator& first,const Iterator& last,Args&&... args)
122 using traits=iterator_traits<Iterator>;
123 using local_base_iterator=typename traits::local_base_iterator;
125 auto alg=restitute_range<Ts...>(
126 cast_return<local_base_iterator>(Algorithm{}),
127 std::forward<Args>(args)...);
128 for(auto i:detail::segment_split(first,last)){
131 return traits::iterator_from(
132 it,traits::end_base_segment_info_iterator_from(last));
137 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_find,std::find)
140 typename... Ts,typename Iterator,typename T,
141 enable_if_poly_collection_iterator<Iterator> =nullptr
143 Iterator find(const Iterator& first,const Iterator& last,const T& x)
145 return generic_find<std_find,Ts...>(first,last,x);
148 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_find_if,std::find_if)
151 typename... Ts,typename Iterator,typename Predicate,
152 enable_if_poly_collection_iterator<Iterator> =nullptr
154 Iterator find_if(const Iterator& first,const Iterator& last,Predicate pred)
156 return generic_find<std_find_if,Ts...>(first,last,pred);
159 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_find_if_not,std::find_if_not)
162 typename... Ts,typename Iterator,typename Predicate,
163 enable_if_poly_collection_iterator<Iterator> =nullptr
165 Iterator find_if_not(const Iterator& first,const Iterator& last,Predicate pred)
167 return generic_find<std_find_if_not,Ts...>(first,last,pred);
170 /* find_end defined after search below */
172 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_find_first_of,std::find_first_of)
175 typename... Ts,typename Iterator,typename ForwardIterator,
176 enable_if_poly_collection_iterator<Iterator> =nullptr
178 Iterator find_first_of(
179 const Iterator& first1,const Iterator& last1,
180 ForwardIterator first2,ForwardIterator last2)
182 return generic_find<std_find_first_of,Ts...>(first1,last1,first2,last2);
186 typename... Ts,typename Iterator,
187 typename ForwardIterator,typename BinaryPredicate,
188 enable_if_poly_collection_iterator<Iterator> =nullptr
190 Iterator find_first_of(
191 const Iterator& first1,const Iterator& last1,
192 ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
194 return generic_find<std_find_first_of,Ts...>(first1,last1,first2,last2,pred);
197 template<typename... Ts>
198 struct adjacent_find_alg
200 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(adjacent_find_alg)
203 typename LocalIterator,typename BinaryPredicate,typename LocalBaseIterator
205 LocalBaseIterator operator()(
206 LocalIterator first,LocalIterator last,BinaryPredicate pred,
207 bool& carry,const std::type_info* prev_info, /* note the &s */
208 LocalBaseIterator& prev)const
210 if(first==last)return LocalBaseIterator{last};
212 auto p=restitute_iterator<Ts...>(deref_to(pred));
213 if(p(*prev_info,prev,first))return prev;
215 auto res=std::adjacent_find(first,last,pred);
219 typename std::iterator_traits<LocalIterator>::value_type);
220 prev=LocalBaseIterator{last-1};
223 return LocalBaseIterator{res};
228 typename... Ts,typename Iterator,typename BinaryPredicate,
229 enable_if_poly_collection_iterator<Iterator> =nullptr
231 Iterator adjacent_find(
232 const Iterator& first,const Iterator& last,BinaryPredicate pred)
234 using traits=iterator_traits<Iterator>;
235 using local_base_iterator=typename traits::local_base_iterator;
238 const std::type_info* prev_info{&typeid(void)};
239 local_base_iterator prev;
240 return generic_find<adjacent_find_alg<Ts...>,Ts...>(
241 first,last,pred,carry,prev_info,prev);
245 typename... Ts,typename Iterator,
246 enable_if_poly_collection_iterator<Iterator> =nullptr
248 Iterator adjacent_find(const Iterator& first,const Iterator& last)
250 return algorithm::adjacent_find<Ts...>(first,last,transparent_equal_to{});
254 typename Algorithm,typename... Ts,
255 typename Iterator,typename... Args,
256 enable_if_poly_collection_iterator<Iterator> =nullptr
258 std::ptrdiff_t generic_count(
259 const Iterator& first,const Iterator& last,Args&&... args)
261 auto alg=restitute_range<Ts...>(Algorithm{},std::forward<Args>(args)...);
262 std::ptrdiff_t res=0;
263 for(auto i:detail::segment_split(first,last))res+=alg(i);
267 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_count,std::count)
270 typename... Ts,typename Iterator,typename T,
271 enable_if_poly_collection_iterator<Iterator> =nullptr
273 std::ptrdiff_t count(const Iterator& first,const Iterator& last,const T& x)
275 return generic_count<std_count,Ts...>(first,last,x);
278 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_count_if,std::count_if)
281 typename... Ts,typename Iterator,typename Predicate,
282 enable_if_poly_collection_iterator<Iterator> =nullptr
284 std::ptrdiff_t count_if(
285 const Iterator& first,const Iterator& last,Predicate pred)
287 return generic_count<std_count_if,Ts...>(first,last,pred);
292 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(mismatch_alg)
295 typename InputIterator1,
296 typename InputIterator2,typename BinaryPredicate
298 InputIterator1 operator()(
299 InputIterator1 first1,InputIterator1 last1,
300 InputIterator2& first2,BinaryPredicate pred)const /* note the & */
302 while(first1!=last1&&pred(*first1,*first2)){
310 typename InputIterator1,
311 typename InputIterator2,typename BinaryPredicate
313 InputIterator1 operator()(
314 InputIterator1 first1,InputIterator1 last1,
315 InputIterator2& first2,InputIterator2 last2, /* note the & */
316 BinaryPredicate pred)const
318 while(first1!=last1&&first2!=last2&&pred(*first1,*first2)){
327 typename... Ts,typename Iterator,
328 typename InputIterator,typename BinaryPredicate,
329 enable_if_poly_collection_iterator<Iterator> =nullptr
331 std::pair<Iterator,InputIterator> mismatch(
332 const Iterator& first1,const Iterator& last1,
333 InputIterator first2,BinaryPredicate pred)
335 auto it=generic_find<mismatch_alg,Ts...>(first1,last1,first2,pred);
340 typename... Ts,typename Iterator,typename InputIterator,
341 enable_if_poly_collection_iterator<Iterator> =nullptr
343 std::pair<Iterator,InputIterator> mismatch(
344 const Iterator& first1,const Iterator& last1,InputIterator first2)
346 return algorithm::mismatch<Ts...>(
347 first1,last1,first2,transparent_equal_to{});
351 typename... Ts,typename Iterator,
352 typename InputIterator,typename BinaryPredicate,
353 enable_if_poly_collection_iterator<Iterator> =nullptr
355 std::pair<Iterator,InputIterator> mismatch(
356 const Iterator& first1,const Iterator& last1,
357 InputIterator first2,InputIterator last2,BinaryPredicate pred)
359 auto it=generic_find<mismatch_alg,Ts...>(first1,last1,first2,last2,pred);
364 typename... Ts,typename Iterator,typename InputIterator,
365 enable_if_poly_collection_iterator<Iterator> =nullptr
367 std::pair<Iterator,InputIterator> mismatch(
368 const Iterator& first1,const Iterator& last1,
369 InputIterator first2,InputIterator last2)
371 return algorithm::mismatch<Ts...>(
372 first1,last1,first2,last2,transparent_equal_to{});
377 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(equal_alg)
380 typename InputIterator1,
381 typename InputIterator2,typename BinaryPredicate
384 InputIterator1 first1,InputIterator1 last1,
385 InputIterator2& first2,BinaryPredicate pred)const /* note the & */
387 for(;first1!=last1;++first1,++first2){
388 if(!pred(*first1,*first2))return false;
394 typename InputIterator1,
395 typename InputIterator2,typename BinaryPredicate
398 InputIterator1 first1,InputIterator1 last1,
399 InputIterator2& first2,InputIterator2 last2, /* note the & */
400 BinaryPredicate pred)const
402 for(;first1!=last1&&first2!=last2;++first1,++first2){
403 if(!pred(*first1,*first2))return false;
405 return first1==last1; /* don't check first2==last2 as op is partial */
410 typename... Ts,typename Iterator,
411 typename InputIterator,typename BinaryPredicate,
412 enable_if_poly_collection_iterator<Iterator> =nullptr
415 const Iterator& first1,const Iterator& last1,
416 InputIterator first2,BinaryPredicate pred)
418 auto alg=restitute_range<Ts...>(equal_alg{},first2,pred);
419 for(auto i:detail::segment_split(first1,last1))if(!alg(i))return false;
424 typename... Ts,typename Iterator,typename InputIterator,
425 enable_if_poly_collection_iterator<Iterator> =nullptr
428 const Iterator& first1,const Iterator& last1,InputIterator first2)
430 return algorithm::equal<Ts...>(first1,last1,first2,transparent_equal_to{});
434 typename... Ts,typename Iterator,
435 typename InputIterator,typename BinaryPredicate,
436 enable_if_poly_collection_iterator<Iterator> =nullptr
439 const Iterator& first1,const Iterator& last1,
440 InputIterator first2,InputIterator last2,BinaryPredicate pred)
442 auto alg=restitute_range<Ts...>(equal_alg{},first2,last2,pred);
443 for(auto i:detail::segment_split(first1,last1))if(!alg(i))return false;
444 return first2==last2;
448 typename... Ts,typename Iterator,typename InputIterator,
449 enable_if_poly_collection_iterator<Iterator> =nullptr
452 const Iterator& first1,const Iterator& last1,
453 InputIterator first2,InputIterator last2)
455 return algorithm::equal<Ts...>(
456 first1,last1,first2,last2,transparent_equal_to{});
460 typename... Ts,typename Iterator,
461 typename ForwardIterator,typename BinaryPredicate,
462 enable_if_poly_collection_iterator<Iterator> =nullptr
464 bool is_permutation_suffix(
465 const Iterator& first1,const Iterator& last1,
466 ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
468 using traits=iterator_traits<Iterator>;
470 auto send=traits::end_base_segment_info_iterator_from(last1);
471 for(auto i:detail::segment_split(first1,last1)){
472 for(auto lbscan=i.begin();lbscan!=i.end();++lbscan){
473 auto& info=i.type_info();
475 restitute_iterator<Ts...>(deref_1st_to(pred)),info,lbscan);
476 auto scan=traits::iterator_from(lbscan,send);
477 if(algorithm::find_if<Ts...>(first1,scan,p)!=scan)continue;
478 std::ptrdiff_t matches=std::count_if(first2,last2,p);
480 matches!=algorithm::count_if<Ts...>(scan,last1,p))return false;
487 typename... Ts,typename Iterator,
488 typename ForwardIterator,typename BinaryPredicate,
489 enable_if_poly_collection_iterator<Iterator> =nullptr
492 Iterator first1,Iterator last1,ForwardIterator first2,BinaryPredicate pred)
494 std::tie(first1,first2)=algorithm::mismatch<Ts...>(first1,last1,first2,pred);
495 auto last2=std::next(first2,std::distance(first1,last1));
496 return is_permutation_suffix<Ts...>(first1,last1,first2,last2,pred);
500 typename... Ts,typename Iterator,typename ForwardIterator,
501 enable_if_poly_collection_iterator<Iterator> =nullptr
504 const Iterator& first1,const Iterator& last1,ForwardIterator first2)
506 return algorithm::is_permutation<Ts...>(
507 first1,last1,first2,transparent_equal_to{});
511 typename... Ts,typename Iterator,
512 typename ForwardIterator,typename BinaryPredicate,
513 enable_if_poly_collection_iterator<Iterator> =nullptr
516 Iterator first1,Iterator last1,
517 ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
519 std::tie(first1,first2)=algorithm::mismatch<Ts...>(
520 first1,last1,first2,last2,pred);
521 if(std::distance(first1,last1)!=std::distance(first2,last2))return false;
522 else return is_permutation_suffix<Ts...>(first1,last1,first2,last2,pred);
526 typename... Ts,typename Iterator,typename ForwardIterator,
527 enable_if_poly_collection_iterator<Iterator> =nullptr
530 const Iterator& first1,const Iterator& last1,
531 ForwardIterator first2,ForwardIterator last2)
533 return algorithm::is_permutation<Ts...>(
534 first1,last1,first2,last2,transparent_equal_to{});
538 typename... Ts,typename Iterator,
539 typename ForwardIterator,typename BinaryPredicate,
540 enable_if_poly_collection_iterator<Iterator> =nullptr
543 const Iterator& first1,const Iterator& last1,
544 ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
546 using traits=iterator_traits<Iterator>;
548 auto send=traits::end_base_segment_info_iterator_from(last1);
549 for(auto i:detail::segment_split(first1,last1)){
550 for(auto lbit=i.begin(),lbend=i.end();lbit!=lbend;++lbit){
551 Iterator it=traits::iterator_from(lbit,send);
552 if(algorithm::mismatch<Ts...>(it,last1,first2,last2,pred).second==last2)
560 typename... Ts,typename Iterator,typename ForwardIterator,
561 enable_if_poly_collection_iterator<Iterator> =nullptr
564 const Iterator& first1,const Iterator& last1,
565 ForwardIterator first2,ForwardIterator last2)
567 return algorithm::search<Ts...>(
568 first1,last1,first2,last2,transparent_equal_to{});
572 typename... Ts,typename Iterator,
573 typename ForwardIterator,typename BinaryPredicate,
574 enable_if_poly_collection_iterator<Iterator> =nullptr
577 Iterator first1,Iterator last1,
578 ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
580 if(first2==last2)return last1;
582 for(Iterator res=last1;;){
583 Iterator res1=algorithm::search<Ts...>(first1,last1,first2,last2,pred);
584 if(res1==last1)return res;
593 typename... Ts,typename Iterator,typename ForwardIterator,
594 enable_if_poly_collection_iterator<Iterator> =nullptr
597 const Iterator& first1,const Iterator& last1,
598 ForwardIterator first2,ForwardIterator last2)
600 return algorithm::find_end<Ts...>(
601 first1,last1,first2,last2,transparent_equal_to{});
606 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(search_n_alg)
609 typename ForwardIterator,typename Size,
610 typename T,typename BinaryPredicate
612 ForwardIterator operator()(
613 ForwardIterator first,ForwardIterator last,
614 Size count,bool& carry,Size& remain,const T& x, /* note the &s */
615 BinaryPredicate pred)const
617 for(;first!=last;++first){
618 if(!pred(*first,x)){carry=false;remain=count;continue;}
621 if(--remain==0||++first==last)return res;
622 if(!pred(*first,x)){carry=false;remain=count;break;}
630 typename... Ts,typename Iterator,
631 typename Size,typename T,typename BinaryPredicate,
632 enable_if_poly_collection_iterator<Iterator> =nullptr
635 const Iterator& first,const Iterator& last,
636 Size count,const T& x,BinaryPredicate pred)
638 using traits=iterator_traits<Iterator>;
639 using local_base_iterator=typename traits::local_base_iterator;
641 if(count<=0)return first;
645 auto alg=restitute_range<Ts...>(
646 cast_return<local_base_iterator>(search_n_alg{}),
647 count,carry,remain,x,pred);
648 local_base_iterator prev;
649 for(auto i:detail::segment_split(first,last)){
653 return traits::iterator_from(
655 traits::end_base_segment_info_iterator_from(last));
656 else if(!carry){prev=it;carry=true;}
663 typename... Ts,typename Iterator,
664 typename Size,typename T,
665 enable_if_poly_collection_iterator<Iterator> =nullptr
668 const Iterator& first,const Iterator& last,Size count,const T& x)
670 return algorithm::search_n<Ts...>(
671 first,last,count,x,transparent_equal_to{});
675 typename Algorithm,typename... Ts,
676 typename Iterator,typename OutputIterator,typename... Args,
677 enable_if_poly_collection_iterator<Iterator> =nullptr
679 OutputIterator generic_copy(
680 const Iterator& first,const Iterator& last,OutputIterator res,Args&&... args)
682 for(auto i:detail::segment_split(first,last)){
683 auto alg=restitute_range<Ts...>(
684 Algorithm{},res,std::forward<Args>(args)...);
690 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_copy,std::copy)
693 typename... Ts,typename Iterator,typename OutputIterator,
694 enable_if_poly_collection_iterator<Iterator> =nullptr
697 const Iterator& first,const Iterator& last,OutputIterator res)
699 return generic_copy<std_copy,Ts...>(first,last,res);
702 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_copy_n,std::copy_n)
705 typename... Ts,typename Iterator,typename Size,typename OutputIterator,
706 enable_if_poly_collection_iterator<Iterator> =nullptr
708 OutputIterator copy_n(const Iterator& first,Size count,OutputIterator res)
710 using traits=iterator_traits<Iterator>;
712 if(count<=0)return res;
714 auto lbit=traits::local_base_iterator_from(first);
715 auto sit=traits::base_segment_info_iterator_from(first);
717 auto n=(std::min)(count,sit->end()-lbit);
718 auto alg=restitute_iterator<Ts...>(std_copy_n{},n,res);
719 res=alg(sit->type_info(),lbit);
720 if((count-=n)==0)break;
727 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_copy_if,std::copy_if)
730 typename... Ts,typename Iterator,typename OutputIterator,typename Predicate,
731 enable_if_poly_collection_iterator<Iterator> =nullptr
733 OutputIterator copy_if(
734 const Iterator& first,const Iterator& last,OutputIterator res,Predicate pred)
736 return generic_copy<std_copy_if,Ts...>(first,last,res,pred);
739 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_move,std::move)
742 typename... Ts,typename Iterator,typename OutputIterator,
743 enable_if_poly_collection_iterator<Iterator> =nullptr
746 const Iterator& first,const Iterator& last,OutputIterator res)
748 return generic_copy<std_move,Ts...>(first,last,res);
751 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_transform,std::transform)
754 typename... Ts,typename Iterator,
755 typename OutputIterator,typename UnaryOperation,
756 enable_if_poly_collection_iterator<Iterator> =nullptr
758 OutputIterator transform(
759 const Iterator& first,const Iterator& last,
760 OutputIterator res,UnaryOperation op)
762 return generic_copy<std_transform,Ts...>(first,last,res,op);
765 struct transform2_alg
767 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(transform2_alg)
770 typename InputIterator1,typename InputIterator2,
771 typename OutputIterator,typename BinaryOperation
773 OutputIterator operator()(
774 InputIterator1 first1,InputIterator1 last1,
775 OutputIterator res, /* third place for compatibility with generic_copy */
776 InputIterator2& first2, BinaryOperation op)const /* note the & */
778 while(first1!=last1)*res++=op(*first1++,*first2++);
784 typename... Ts,typename Iterator,typename InputIterator,
785 typename OutputIterator,typename BinaryOperation,
786 enable_if_poly_collection_iterator<Iterator> =nullptr
788 OutputIterator transform(
789 const Iterator& first1,const Iterator& last1,InputIterator first2,
790 OutputIterator res,BinaryOperation op)
792 return generic_copy<transform2_alg,Ts...>(first1,last1,res,first2,op);
795 struct replace_copy_alg
797 /* std::replace_copy broken in VS2015, internal ticket VSO#279818
798 * "<algorithm>: replace_copy() and replace_copy_if() shouldn't use the
799 * conditional operator".
802 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(replace_copy_alg)
804 template<typename InputIterator,typename OutputIterator,typename T>
805 OutputIterator operator()(
806 InputIterator first,InputIterator last,OutputIterator res,
807 const T& old_x,const T& new_x)
809 for(;first!=last;++first,++res){
810 if(*first==old_x)*res=new_x;
818 typename... Ts,typename Iterator,typename OutputIterator,typename T,
819 enable_if_poly_collection_iterator<Iterator> =nullptr
821 OutputIterator replace_copy(
822 const Iterator& first,const Iterator& last,OutputIterator res,
823 const T& old_x,const T& new_x)
825 return generic_copy<replace_copy_alg,Ts...>(first,last,res,old_x,new_x);
828 struct replace_copy_if_alg
830 /* std::replace_copy_if broken in VS2015, internal ticket VSO#279818
831 * "<algorithm>: replace_copy() and replace_copy_if() shouldn't use the
832 * conditional operator".
835 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(replace_copy_if_alg)
838 typename InputIterator,typename OutputIterator,
839 typename Predicate,typename T
841 OutputIterator operator()(
842 InputIterator first,InputIterator last,OutputIterator res,
843 Predicate pred,const T& new_x)
845 for(;first!=last;++first,++res){
846 if(pred(*first))*res=new_x;
854 typename... Ts,typename Iterator,typename OutputIterator,
855 typename Predicate,typename T,
856 enable_if_poly_collection_iterator<Iterator> =nullptr
858 OutputIterator replace_copy_if(
859 const Iterator& first,const Iterator& last,OutputIterator res,
860 Predicate pred,const T& new_x)
862 return generic_copy<replace_copy_if_alg,Ts...>(first,last,res,pred,new_x);
865 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_remove_copy,std::remove_copy)
868 typename... Ts,typename Iterator,typename OutputIterator,typename T,
869 enable_if_poly_collection_iterator<Iterator> =nullptr
871 OutputIterator remove_copy(
872 const Iterator& first,const Iterator& last,OutputIterator res,const T& x)
874 return generic_copy<std_remove_copy,Ts...>(first,last,res,x);
877 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(
878 std_remove_copy_if,std::remove_copy_if)
881 typename... Ts,typename Iterator,typename OutputIterator,typename Predicate,
882 enable_if_poly_collection_iterator<Iterator> =nullptr
884 OutputIterator remove_copy_if(
885 const Iterator& first,const Iterator& last,OutputIterator res,Predicate pred)
887 return generic_copy<std_remove_copy_if,Ts...>(first,last,res,pred);
890 template<typename... Ts>
891 struct unique_copy_alg
893 BOOST_POLY_COLLECTION_WORKAROUND_DR1467(unique_copy_alg)
896 typename LocalIterator,typename OutputIterator,
897 typename BinaryPredicate,typename LocalBaseIterator
899 OutputIterator operator()(
900 LocalIterator first,LocalIterator last,
901 OutputIterator res, BinaryPredicate pred,
902 bool& carry,const std::type_info* prev_info, /* note the &s */
903 LocalBaseIterator& prev)const
906 auto p=restitute_iterator<Ts...>(deref_to(pred));
907 for(;first!=last;++first)if(!p(*prev_info,prev,first))break;
909 if(first==last)return res;
910 res=std::unique_copy(first,last,res,pred);
913 typename std::iterator_traits<LocalIterator>::value_type);
914 prev=LocalBaseIterator{last-1};
920 typename... Ts,typename Iterator,
921 typename OutputIterator,typename BinaryPredicate,
922 enable_if_poly_collection_iterator<Iterator> =nullptr
924 OutputIterator unique_copy(
925 const Iterator& first,const Iterator& last,
926 OutputIterator res,BinaryPredicate pred)
928 using traits=iterator_traits<Iterator>;
929 using local_base_iterator=typename traits::local_base_iterator;
932 const std::type_info* prev_info{&typeid(void)};
933 local_base_iterator prev;
934 return generic_copy<unique_copy_alg<Ts...>,Ts...>(
935 first,last,res,pred,carry,prev_info,prev);
939 typename... Ts,typename Iterator,typename OutputIterator,
940 enable_if_poly_collection_iterator<Iterator> =nullptr
942 OutputIterator unique_copy(
943 const Iterator& first,const Iterator& last,OutputIterator res)
945 return algorithm::unique_copy<Ts...>(first,last,res,transparent_equal_to{});
949 typename... Ts,typename Iterator,typename OutputIterator,
950 enable_if_poly_collection_iterator<Iterator> =nullptr
952 OutputIterator rotate_copy(
953 const Iterator& first,const Iterator& middle,const Iterator& last,
956 res=algorithm::copy<Ts...>(middle,last,res);
957 return algorithm::copy<Ts...>(first,middle,res);
961 typename... Ts,typename Iterator,typename Predicate,
962 enable_if_poly_collection_iterator<Iterator> =nullptr
964 bool is_partitioned(const Iterator& first,const Iterator& last,Predicate pred)
966 auto it=algorithm::find_if_not<Ts...>(first,last,pred);
967 if(it==last)return true;
968 return algorithm::none_of<Ts...>(++it,last,pred);
971 BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(
972 std_partition_copy,std::partition_copy)
975 typename... Ts,typename Iterator,
976 typename OutputIterator1,typename OutputIterator2,typename Predicate,
977 enable_if_poly_collection_iterator<Iterator> =nullptr
979 std::pair<OutputIterator1,OutputIterator2> partition_copy(
980 const Iterator& first,const Iterator& last,
981 OutputIterator1 rest,OutputIterator2 resf,Predicate pred)
983 for(auto i:detail::segment_split(first,last)){
984 auto alg=restitute_range<Ts...>(std_partition_copy{},rest,resf,pred);
985 std::tie(rest,resf)=alg(i);
990 template<typename Predicate,typename... Ts>
991 struct partition_point_pred
993 partition_point_pred(const Predicate& pred):pred(pred){}
995 template<typename Iterator>
996 bool operator()(const Iterator& it)const
998 using traits=iterator_traits<Iterator>;
999 auto p=restitute_iterator<Ts...>(deref_to(pred));
1001 traits::base_segment_info_iterator_from(it)->type_info(),
1002 traits::local_base_iterator_from(it));
1009 typename... Ts,typename Iterator,typename Predicate,
1010 enable_if_poly_collection_iterator<Iterator> =nullptr
1012 Iterator partition_point(
1013 const Iterator& first,const Iterator& last,Predicate pred)
1015 auto_iterator<Iterator> afirst{first},alast{last};
1016 partition_point_pred<Predicate,Ts...> p{pred};
1017 return *std::partition_point(afirst,alast,p);
1020 } /* namespace poly_collection::detail::algorithm */
1022 } /* namespace poly_collection::detail */
1024 /* non-modifying sequence operations */
1026 using detail::algorithm::all_of;
1027 using detail::algorithm::any_of;
1028 using detail::algorithm::none_of;
1029 using detail::algorithm::for_each;
1030 using detail::algorithm::find;
1031 using detail::algorithm::find_if;
1032 using detail::algorithm::find_if_not;
1033 using detail::algorithm::find_end;
1034 using detail::algorithm::find_first_of;
1035 using detail::algorithm::adjacent_find;
1036 using detail::algorithm::count;
1037 using detail::algorithm::count_if;
1038 using detail::algorithm::mismatch;
1039 using detail::algorithm::equal;
1040 using detail::algorithm::is_permutation;
1041 using detail::algorithm::search;
1042 using detail::algorithm::search_n;
1044 /* modifying sequence operations */
1046 using detail::algorithm::copy;
1047 using detail::algorithm::copy_n;
1048 using detail::algorithm::copy_if;
1049 /* copy_backwards requires BidirectionalIterator */
1050 using detail::algorithm::move;
1051 /* move_backwards requires BidirectionalIterator */
1052 /* swap_ranges requires Swappable */
1053 /* iter_swap requires Swappable */
1054 using detail::algorithm::transform;
1055 /* replace requires Assignable */
1056 /* replace_if requires Assignable */
1057 using detail::algorithm::replace_copy;
1058 using detail::algorithm::replace_copy_if;
1059 /* fill requires Assignable */
1060 /* fill_n requires Assignable */
1061 /* generate requires Assignable */
1062 /* generate_n requires Assignable */
1063 /* remove requires MoveAssignable */
1064 /* remove_if requires MoveAssignable */
1065 using detail::algorithm::remove_copy;
1066 using detail::algorithm::remove_copy_if;
1067 /* unique requires MoveAssignable */
1068 using detail::algorithm::unique_copy;
1069 /* reverse requires BidirectionalIterator */
1070 /* reverse_copy requires BidirectionalIterator */
1071 /* rotate requires MoveAssignable */
1072 using detail::algorithm::rotate_copy;
1073 /* shuffle requires RandomAccessIterator */
1074 using detail::algorithm::is_partitioned;
1075 /* partition requires Swappable */
1076 /* stable_partition requires Swappable */
1077 using detail::algorithm::partition_copy;
1078 using detail::algorithm::partition_point;
1080 /* sorting and related operations not provided */
1082 } /* namespace poly_collection */
1084 } /* namespace boost */