1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186 |
- /* Copyright 2016-2020 Joaquin M Lopez Munoz.
- * Distributed under the Boost Software License, Version 1.0.
- * (See accompanying file LICENSE_1_0.txt or copy at
- * http://www.boost.org/LICENSE_1_0.txt)
- *
- * See http://www.boost.org/libs/poly_collection for library home page.
- */
- #ifndef BOOST_POLY_COLLECTION_ALGORITHM_HPP
- #define BOOST_POLY_COLLECTION_ALGORITHM_HPP
- #if defined(_MSC_VER)
- #pragma once
- #endif
- #include <algorithm>
- #include <boost/poly_collection/detail/auto_iterator.hpp>
- #include <boost/poly_collection/detail/functional.hpp>
- #include <boost/poly_collection/detail/iterator_traits.hpp>
- #include <boost/poly_collection/detail/segment_split.hpp>
- #include <boost/poly_collection/detail/type_restitution.hpp>
- #include <iterator>
- #include <random>
- #include <type_traits>
- #include <utility>
- /* Improved performance versions of std algorithms over poly_collection.
- * poly_collection::alg is expected to be faster than the homonym std::alg
- * because the latter does a traversal over a segmented structured, where
- * incrementing requires checking for segment change, whereas the former
- * for-loops over flat segments.
- * Additionally, poly_collection::alg<Ti...>(...,f) *restitutes* Ti when
- * passing elements to f, i.e. if the concrete type of the element is Ti
- * then f is invoked with a [const] Ti&, which can dramatically improve
- * performance when f has specific overloads for Ti (like, for instance,
- * generic lambdas) as static optimization can kick in (devirtualization
- * being a particular example).
- */
- namespace boost{
- namespace poly_collection{
- namespace detail{
- namespace algorithm{
- template<typename Iterator>
- using enable_if_poly_collection_iterator=typename std::enable_if<
- !std::is_void<typename poly_collection_of<Iterator>::type>::value
- >::type*;
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_all_of,std::all_of)
- template<
- typename... Ts,typename Iterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool all_of(const Iterator& first,const Iterator& last,Predicate pred)
- {
- auto alg=restitute_range<Ts...>(std_all_of{},pred);
- for(auto i:detail::segment_split(first,last))if(!alg(i))return false;
- return true;
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_any_of,std::any_of)
- template<
- typename... Ts,typename Iterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool any_of(const Iterator& first,const Iterator& last,Predicate pred)
- {
- auto alg=restitute_range<Ts...>(std_any_of{},pred);
- for(auto i:detail::segment_split(first,last))if(alg(i))return true;
- return false;
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_none_of,std::none_of)
- template<
- typename... Ts,typename Iterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool none_of(const Iterator& first,const Iterator& last,Predicate pred)
- {
- auto alg=restitute_range<Ts...>(std_none_of{},pred);
- for(auto i:detail::segment_split(first,last))if(!alg(i))return false;
- return true;
- }
- struct for_each_alg
- {
- template<typename InputIterator,typename Function>
- void operator()(
- InputIterator first,InputIterator last,Function& f)const /* note the & */
- {
- for(;first!=last;++first)f(*first);
- }
- };
- template<
- typename... Ts,typename Iterator,typename Function,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Function for_each(const Iterator& first,const Iterator& last,Function f)
- {
- for_each_segment(first,last,restitute_range<Ts...>(for_each_alg{},f));
- return f;
- }
- struct for_each_n_alg
- {
- template<
- typename InputIterator,typename Size,typename Function
- >
- InputIterator operator()(
- InputIterator first,Size n,Function& f)const /* note the & */
- {
- for(;n>0;++first,(void)--n)f(*first);
- return first;
- }
- };
- template<
- typename... Ts,typename Iterator,typename Size,typename Function,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator for_each_n(const Iterator& first,Size n,Function f)
- {
- using traits=iterator_traits<Iterator>;
- using local_base_iterator=typename traits::local_base_iterator;
- if(n<=0)return first;
- auto alg=restitute_iterator<Ts...>(
- cast_return<local_base_iterator>(for_each_n_alg{}));
- auto lbit=traits::local_base_iterator_from(first);
- auto sit=traits::base_segment_info_iterator_from(first);
- for(;;){
- auto m=sit->end()-lbit;
- if(n<=m){
- auto it=alg(sit->type_info(),lbit,n,f);
- return traits::iterator_from(
- it,traits::end_base_segment_info_iterator_from(first));
- }
- else{
- alg(sit->type_info(),lbit,m,f);
- n-=m;
- }
- ++sit;
- lbit=sit->begin();
- }
- }
- template<
- typename Algorithm,typename... Ts,
- typename Iterator,typename... Args,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator generic_find(
- const Iterator& first,const Iterator& last,Args&&... args)
- {
- using traits=iterator_traits<Iterator>;
- using local_base_iterator=typename traits::local_base_iterator;
- auto alg=restitute_range<Ts...>(
- cast_return<local_base_iterator>(Algorithm{}),
- std::forward<Args>(args)...);
- for(auto i:detail::segment_split(first,last)){
- auto it=alg(i);
- if(it!=i.end())
- return traits::iterator_from(
- it,traits::end_base_segment_info_iterator_from(last));
- }
- return last;
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_find,std::find)
- template<
- typename... Ts,typename Iterator,typename T,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator find(const Iterator& first,const Iterator& last,const T& x)
- {
- return generic_find<std_find,Ts...>(first,last,x);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_find_if,std::find_if)
- template<
- typename... Ts,typename Iterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator find_if(const Iterator& first,const Iterator& last,Predicate pred)
- {
- return generic_find<std_find_if,Ts...>(first,last,pred);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_find_if_not,std::find_if_not)
- template<
- typename... Ts,typename Iterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator find_if_not(const Iterator& first,const Iterator& last,Predicate pred)
- {
- return generic_find<std_find_if_not,Ts...>(first,last,pred);
- }
- /* find_end defined after search below */
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_find_first_of,std::find_first_of)
- template<
- typename... Ts,typename Iterator,typename ForwardIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator find_first_of(
- const Iterator& first1,const Iterator& last1,
- ForwardIterator first2,ForwardIterator last2)
- {
- return generic_find<std_find_first_of,Ts...>(first1,last1,first2,last2);
- }
- template<
- typename... Ts,typename Iterator,
- typename ForwardIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator find_first_of(
- const Iterator& first1,const Iterator& last1,
- ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
- {
- return generic_find<std_find_first_of,Ts...>(first1,last1,first2,last2,pred);
- }
- template<typename... Ts>
- struct adjacent_find_alg
- {
- template<
- typename LocalIterator,typename BinaryPredicate,typename LocalBaseIterator
- >
- LocalBaseIterator operator()(
- LocalIterator first,LocalIterator last,BinaryPredicate pred,
- bool& carry,const std::type_info* prev_info, /* note the &s */
- LocalBaseIterator& prev)const
- {
- if(first==last)return LocalBaseIterator{last};
- if(carry){
- auto p=restitute_iterator<Ts...>(deref_to(pred));
- if(p(*prev_info,prev,first))return prev;
- }
- auto res=std::adjacent_find(first,last,pred);
- if(res==last){
- carry=true;
- prev_info=&typeid(
- typename std::iterator_traits<LocalIterator>::value_type);
- prev=LocalBaseIterator{last-1};
- }
- else carry=false;
- return LocalBaseIterator{res};
- }
- };
- template<
- typename... Ts,typename Iterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator adjacent_find(
- const Iterator& first,const Iterator& last,BinaryPredicate pred)
- {
- using traits=iterator_traits<Iterator>;
- using local_base_iterator=typename traits::local_base_iterator;
- bool carry=false;
- const std::type_info* prev_info{&typeid(void)};
- local_base_iterator prev;
- return generic_find<adjacent_find_alg<Ts...>,Ts...>(
- first,last,pred,carry,prev_info,prev);
- }
- template<
- typename... Ts,typename Iterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator adjacent_find(const Iterator& first,const Iterator& last)
- {
- return algorithm::adjacent_find<Ts...>(first,last,transparent_equal_to{});
- }
- template<
- typename Algorithm,typename... Ts,
- typename Iterator,typename... Args,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::ptrdiff_t generic_count(
- const Iterator& first,const Iterator& last,Args&&... args)
- {
- auto alg=restitute_range<Ts...>(Algorithm{},std::forward<Args>(args)...);
- std::ptrdiff_t res=0;
- for(auto i:detail::segment_split(first,last))res+=alg(i);
- return res;
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_count,std::count)
- template<
- typename... Ts,typename Iterator,typename T,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::ptrdiff_t count(const Iterator& first,const Iterator& last,const T& x)
- {
- return generic_count<std_count,Ts...>(first,last,x);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_count_if,std::count_if)
- template<
- typename... Ts,typename Iterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::ptrdiff_t count_if(
- const Iterator& first,const Iterator& last,Predicate pred)
- {
- return generic_count<std_count_if,Ts...>(first,last,pred);
- }
- struct mismatch_alg
- {
- template<
- typename InputIterator1,
- typename InputIterator2,typename BinaryPredicate
- >
- InputIterator1 operator()(
- InputIterator1 first1,InputIterator1 last1,
- InputIterator2& first2,BinaryPredicate pred)const /* note the & */
- {
- while(first1!=last1&&pred(*first1,*first2)){
- ++first1;
- ++first2;
- }
- return first1;
- }
- template<
- typename InputIterator1,
- typename InputIterator2,typename BinaryPredicate
- >
- InputIterator1 operator()(
- InputIterator1 first1,InputIterator1 last1,
- InputIterator2& first2,InputIterator2 last2, /* note the & */
- BinaryPredicate pred)const
- {
- while(first1!=last1&&first2!=last2&&pred(*first1,*first2)){
- ++first1;
- ++first2;
- }
- return first1;
- }
- };
- template<
- typename... Ts,typename Iterator,
- typename InputIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::pair<Iterator,InputIterator> mismatch(
- const Iterator& first1,const Iterator& last1,
- InputIterator first2,BinaryPredicate pred)
- {
- auto it=generic_find<mismatch_alg,Ts...>(first1,last1,first2,pred);
- return {it,first2};
- }
- template<
- typename... Ts,typename Iterator,typename InputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::pair<Iterator,InputIterator> mismatch(
- const Iterator& first1,const Iterator& last1,InputIterator first2)
- {
- return algorithm::mismatch<Ts...>(
- first1,last1,first2,transparent_equal_to{});
- }
- template<
- typename... Ts,typename Iterator,
- typename InputIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::pair<Iterator,InputIterator> mismatch(
- const Iterator& first1,const Iterator& last1,
- InputIterator first2,InputIterator last2,BinaryPredicate pred)
- {
- auto it=generic_find<mismatch_alg,Ts...>(first1,last1,first2,last2,pred);
- return {it,first2};
- }
- template<
- typename... Ts,typename Iterator,typename InputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::pair<Iterator,InputIterator> mismatch(
- const Iterator& first1,const Iterator& last1,
- InputIterator first2,InputIterator last2)
- {
- return algorithm::mismatch<Ts...>(
- first1,last1,first2,last2,transparent_equal_to{});
- }
- struct equal_alg
- {
- template<
- typename InputIterator1,
- typename InputIterator2,typename BinaryPredicate
- >
- bool operator()(
- InputIterator1 first1,InputIterator1 last1,
- InputIterator2& first2,BinaryPredicate pred)const /* note the & */
- {
- for(;first1!=last1;++first1,++first2){
- if(!pred(*first1,*first2))return false;
- }
- return true;
- }
- template<
- typename InputIterator1,
- typename InputIterator2,typename BinaryPredicate
- >
- bool operator()(
- InputIterator1 first1,InputIterator1 last1,
- InputIterator2& first2,InputIterator2 last2, /* note the & */
- BinaryPredicate pred)const
- {
- for(;first1!=last1&&first2!=last2;++first1,++first2){
- if(!pred(*first1,*first2))return false;
- }
- return first1==last1; /* don't check first2==last2 as op is partial */
- }
- };
- template<
- typename... Ts,typename Iterator,
- typename InputIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool equal(
- const Iterator& first1,const Iterator& last1,
- InputIterator first2,BinaryPredicate pred)
- {
- auto alg=restitute_range<Ts...>(equal_alg{},first2,pred);
- for(auto i:detail::segment_split(first1,last1))if(!alg(i))return false;
- return true;
- }
- template<
- typename... Ts,typename Iterator,typename InputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool equal(
- const Iterator& first1,const Iterator& last1,InputIterator first2)
- {
- return algorithm::equal<Ts...>(first1,last1,first2,transparent_equal_to{});
- }
- template<
- typename... Ts,typename Iterator,
- typename InputIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool equal(
- const Iterator& first1,const Iterator& last1,
- InputIterator first2,InputIterator last2,BinaryPredicate pred)
- {
- auto alg=restitute_range<Ts...>(equal_alg{},first2,last2,pred);
- for(auto i:detail::segment_split(first1,last1))if(!alg(i))return false;
- return first2==last2;
- }
- template<
- typename... Ts,typename Iterator,typename InputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool equal(
- const Iterator& first1,const Iterator& last1,
- InputIterator first2,InputIterator last2)
- {
- return algorithm::equal<Ts...>(
- first1,last1,first2,last2,transparent_equal_to{});
- }
- template<
- typename Iterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::ptrdiff_t fast_distance(const Iterator& first,const Iterator& last)
- {
- using traits=iterator_traits<Iterator>;
- if(first==last)return 0;
- auto sfirst=traits::base_segment_info_iterator_from(first),
- slast=traits::base_segment_info_iterator_from(last);
- if(sfirst==slast){
- return std::distance(
- traits::local_base_iterator_from(first),
- traits::local_base_iterator_from(last));
- }
- else{
- std::ptrdiff_t m=std::distance(
- traits::local_base_iterator_from(first),sfirst->end());
- while(++sfirst!=slast)m+=std::distance(sfirst->begin(),sfirst->end());
- if(slast!=traits::end_base_segment_info_iterator_from(last)){
- m+=std::distance(
- slast->begin(),traits::local_base_iterator_from(last));
- }
- return m;
- }
- }
- template<
- typename... Ts,typename Iterator,
- typename ForwardIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool is_permutation_suffix(
- const Iterator& first1,const Iterator& last1,
- ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
- {
- using traits=iterator_traits<Iterator>;
- auto send=traits::end_base_segment_info_iterator_from(last1);
- for(auto i:detail::segment_split(first1,last1)){
- for(auto lbscan=i.begin();lbscan!=i.end();++lbscan){
- auto& info=i.type_info();
- auto p=head_closure(
- restitute_iterator<Ts...>(deref_1st_to(pred)),info,lbscan);
- auto scan=traits::iterator_from(lbscan,send);
- if(algorithm::find_if<Ts...>(first1,scan,p)!=scan)continue;
- std::ptrdiff_t matches=std::count_if(first2,last2,p);
- if(matches==0||
- matches!=algorithm::count_if<Ts...>(scan,last1,p))return false;
- }
- }
- return true;
- }
- template<
- typename... Ts,typename Iterator,
- typename ForwardIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool is_permutation(
- Iterator first1,Iterator last1,ForwardIterator first2,BinaryPredicate pred)
- {
- std::tie(first1,first2)=algorithm::mismatch<Ts...>(first1,last1,first2,pred);
- auto last2=std::next(first2,algorithm::fast_distance(first1,last1));
- return is_permutation_suffix<Ts...>(first1,last1,first2,last2,pred);
- }
- template<
- typename... Ts,typename Iterator,typename ForwardIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool is_permutation(
- const Iterator& first1,const Iterator& last1,ForwardIterator first2)
- {
- return algorithm::is_permutation<Ts...>(
- first1,last1,first2,transparent_equal_to{});
- }
- template<
- typename... Ts,typename Iterator,
- typename ForwardIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool is_permutation(
- Iterator first1,Iterator last1,
- ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
- {
- std::tie(first1,first2)=algorithm::mismatch<Ts...>(
- first1,last1,first2,last2,pred);
- if(algorithm::fast_distance(first1,last1)!=std::distance(first2,last2))
- return false;
- else return is_permutation_suffix<Ts...>(first1,last1,first2,last2,pred);
- }
- template<
- typename... Ts,typename Iterator,typename ForwardIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool is_permutation(
- const Iterator& first1,const Iterator& last1,
- ForwardIterator first2,ForwardIterator last2)
- {
- return algorithm::is_permutation<Ts...>(
- first1,last1,first2,last2,transparent_equal_to{});
- }
- template<
- typename... Ts,typename Iterator,
- typename ForwardIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator search(
- const Iterator& first1,const Iterator& last1,
- ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
- {
- using traits=iterator_traits<Iterator>;
- auto send=traits::end_base_segment_info_iterator_from(last1);
- for(auto i:detail::segment_split(first1,last1)){
- for(auto lbit=i.begin(),lbend=i.end();lbit!=lbend;++lbit){
- Iterator it=traits::iterator_from(lbit,send);
- if(algorithm::mismatch<Ts...>(it,last1,first2,last2,pred).second==last2)
- return it;
- }
- }
- return last1;
- }
- template<
- typename... Ts,typename Iterator,typename ForwardIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator search(
- const Iterator& first1,const Iterator& last1,
- ForwardIterator first2,ForwardIterator last2)
- {
- return algorithm::search<Ts...>(
- first1,last1,first2,last2,transparent_equal_to{});
- }
- template<
- typename... Ts,typename Iterator,
- typename ForwardIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator find_end(
- Iterator first1,Iterator last1,
- ForwardIterator first2,ForwardIterator last2,BinaryPredicate pred)
- {
- if(first2==last2)return last1;
- for(Iterator res=last1;;){
- Iterator res1=algorithm::search<Ts...>(first1,last1,first2,last2,pred);
- if(res1==last1)return res;
- else{
- first1=res=res1;
- ++first1;
- }
- }
- }
- template<
- typename... Ts,typename Iterator,typename ForwardIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator find_end(
- const Iterator& first1,const Iterator& last1,
- ForwardIterator first2,ForwardIterator last2)
- {
- return algorithm::find_end<Ts...>(
- first1,last1,first2,last2,transparent_equal_to{});
- }
- struct search_n_alg
- {
- template<
- typename ForwardIterator,typename Size,
- typename T,typename BinaryPredicate
- >
- ForwardIterator operator()(
- ForwardIterator first,ForwardIterator last,
- Size count,bool& carry,Size& remain,const T& x, /* note the &s */
- BinaryPredicate pred)const
- {
- for(;first!=last;++first){
- if(!pred(*first,x)){carry=false;remain=count;continue;}
- auto res=first;
- for(;;){
- if(--remain==0||++first==last)return res;
- if(!pred(*first,x)){carry=false;remain=count;break;}
- }
- }
- return last;
- }
- };
- template<
- typename... Ts,typename Iterator,
- typename Size,typename T,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator search_n(
- const Iterator& first,const Iterator& last,
- Size count,const T& x,BinaryPredicate pred)
- {
- using traits=iterator_traits<Iterator>;
- using local_base_iterator=typename traits::local_base_iterator;
- if(count<=0)return first;
- bool carry=false;
- auto remain=count;
- auto alg=restitute_range<Ts...>(
- cast_return<local_base_iterator>(search_n_alg{}),
- count,carry,remain,x,pred);
- local_base_iterator prev;
- for(auto i:detail::segment_split(first,last)){
- auto it=alg(i);
- if(it!=i.end()){
- if(remain==0)
- return traits::iterator_from(
- carry?prev:it,
- traits::end_base_segment_info_iterator_from(last));
- else if(!carry){prev=it;carry=true;}
- }
- }
- return last;
- }
- template<
- typename... Ts,typename Iterator,
- typename Size,typename T,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator search_n(
- const Iterator& first,const Iterator& last,Size count,const T& x)
- {
- return algorithm::search_n<Ts...>(
- first,last,count,x,transparent_equal_to{});
- }
- template<
- typename Algorithm,typename... Ts,
- typename Iterator,typename OutputIterator,typename... Args,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator generic_copy(
- const Iterator& first,const Iterator& last,OutputIterator res,Args&&... args)
- {
- for(auto i:detail::segment_split(first,last)){
- auto alg=restitute_range<Ts...>(
- Algorithm{},res,std::forward<Args>(args)...);
- res=alg(i);
- }
- return res;
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_copy,std::copy)
- template<
- typename... Ts,typename Iterator,typename OutputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator copy(
- const Iterator& first,const Iterator& last,OutputIterator res)
- {
- return generic_copy<std_copy,Ts...>(first,last,res);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_copy_n,std::copy_n)
- template<
- typename... Ts,typename Iterator,typename Size,typename OutputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator copy_n(const Iterator& first,Size count,OutputIterator res)
- {
- using traits=iterator_traits<Iterator>;
- if(count<=0)return res;
- auto lbit=traits::local_base_iterator_from(first);
- auto sit=traits::base_segment_info_iterator_from(first);
- for(;;){
- auto n=(std::min)(count,sit->end()-lbit);
- auto alg=restitute_iterator<Ts...>(std_copy_n{},n,res);
- res=alg(sit->type_info(),lbit);
- if((count-=n)==0)break;
- ++sit;
- lbit=sit->begin();
- }
- return res;
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_copy_if,std::copy_if)
- template<
- typename... Ts,typename Iterator,typename OutputIterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator copy_if(
- const Iterator& first,const Iterator& last,OutputIterator res,Predicate pred)
- {
- return generic_copy<std_copy_if,Ts...>(first,last,res,pred);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_move,std::move)
- template<
- typename... Ts,typename Iterator,typename OutputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator move(
- const Iterator& first,const Iterator& last,OutputIterator res)
- {
- return generic_copy<std_move,Ts...>(first,last,res);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_transform,std::transform)
- template<
- typename... Ts,typename Iterator,
- typename OutputIterator,typename UnaryOperation,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator transform(
- const Iterator& first,const Iterator& last,
- OutputIterator res,UnaryOperation op)
- {
- return generic_copy<std_transform,Ts...>(first,last,res,op);
- }
- struct transform2_alg
- {
- template<
- typename InputIterator1,typename InputIterator2,
- typename OutputIterator,typename BinaryOperation
- >
- OutputIterator operator()(
- InputIterator1 first1,InputIterator1 last1,
- OutputIterator res, /* third place for compatibility with generic_copy */
- InputIterator2& first2, BinaryOperation op)const /* note the & */
- {
- while(first1!=last1)*res++=op(*first1++,*first2++);
- return res;
- }
- };
- template<
- typename... Ts,typename Iterator,typename InputIterator,
- typename OutputIterator,typename BinaryOperation,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator transform(
- const Iterator& first1,const Iterator& last1,InputIterator first2,
- OutputIterator res,BinaryOperation op)
- {
- return generic_copy<transform2_alg,Ts...>(first1,last1,res,first2,op);
- }
- struct replace_copy_alg
- {
- /* std::replace_copy broken in VS2015, internal ticket VSO#279818
- * "<algorithm>: replace_copy() and replace_copy_if() shouldn't use the
- * conditional operator".
- */
- template<typename InputIterator,typename OutputIterator,typename T>
- OutputIterator operator()(
- InputIterator first,InputIterator last,OutputIterator res,
- const T& old_x,const T& new_x)
- {
- for(;first!=last;++first,++res){
- if(*first==old_x)*res=new_x;
- else *res=*first;
- }
- return res;
- }
- };
- template<
- typename... Ts,typename Iterator,typename OutputIterator,typename T,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator replace_copy(
- const Iterator& first,const Iterator& last,OutputIterator res,
- const T& old_x,const T& new_x)
- {
- return generic_copy<replace_copy_alg,Ts...>(first,last,res,old_x,new_x);
- }
- struct replace_copy_if_alg
- {
- /* std::replace_copy_if broken in VS2015, internal ticket VSO#279818
- * "<algorithm>: replace_copy() and replace_copy_if() shouldn't use the
- * conditional operator".
- */
- template<
- typename InputIterator,typename OutputIterator,
- typename Predicate,typename T
- >
- OutputIterator operator()(
- InputIterator first,InputIterator last,OutputIterator res,
- Predicate pred,const T& new_x)
- {
- for(;first!=last;++first,++res){
- if(pred(*first))*res=new_x;
- else *res=*first;
- }
- return res;
- }
- };
- template<
- typename... Ts,typename Iterator,typename OutputIterator,
- typename Predicate,typename T,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator replace_copy_if(
- const Iterator& first,const Iterator& last,OutputIterator res,
- Predicate pred,const T& new_x)
- {
- return generic_copy<replace_copy_if_alg,Ts...>(first,last,res,pred,new_x);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(std_remove_copy,std::remove_copy)
- template<
- typename... Ts,typename Iterator,typename OutputIterator,typename T,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator remove_copy(
- const Iterator& first,const Iterator& last,OutputIterator res,const T& x)
- {
- return generic_copy<std_remove_copy,Ts...>(first,last,res,x);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(
- std_remove_copy_if,std::remove_copy_if)
- template<
- typename... Ts,typename Iterator,typename OutputIterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator remove_copy_if(
- const Iterator& first,const Iterator& last,OutputIterator res,Predicate pred)
- {
- return generic_copy<std_remove_copy_if,Ts...>(first,last,res,pred);
- }
- template<typename... Ts>
- struct unique_copy_alg
- {
- template<
- typename LocalIterator,typename OutputIterator,
- typename BinaryPredicate,typename LocalBaseIterator
- >
- OutputIterator operator()(
- LocalIterator first,LocalIterator last,
- OutputIterator res, BinaryPredicate pred,
- bool& carry,const std::type_info* prev_info, /* note the &s */
- LocalBaseIterator& prev)const
- {
- if(carry){
- auto p=restitute_iterator<Ts...>(deref_to(pred));
- for(;first!=last;++first)if(!p(*prev_info,prev,first))break;
- }
- if(first==last)return res;
- res=std::unique_copy(first,last,res,pred);
- carry=true;
- prev_info=&typeid(
- typename std::iterator_traits<LocalIterator>::value_type);
- prev=LocalBaseIterator{last-1};
- return res;
- }
- };
- template<
- typename... Ts,typename Iterator,
- typename OutputIterator,typename BinaryPredicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator unique_copy(
- const Iterator& first,const Iterator& last,
- OutputIterator res,BinaryPredicate pred)
- {
- using traits=iterator_traits<Iterator>;
- using local_base_iterator=typename traits::local_base_iterator;
- bool carry=false;
- const std::type_info* prev_info{&typeid(void)};
- local_base_iterator prev;
- return generic_copy<unique_copy_alg<Ts...>,Ts...>(
- first,last,res,pred,carry,prev_info,prev);
- }
- template<
- typename... Ts,typename Iterator,typename OutputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator unique_copy(
- const Iterator& first,const Iterator& last,OutputIterator res)
- {
- return algorithm::unique_copy<Ts...>(first,last,res,transparent_equal_to{});
- }
- template<
- typename... Ts,typename Iterator,typename OutputIterator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator rotate_copy(
- const Iterator& first,const Iterator& middle,const Iterator& last,
- OutputIterator res)
- {
- res=algorithm::copy<Ts...>(middle,last,res);
- return algorithm::copy<Ts...>(first,middle,res);
- }
- struct sample_alg
- {
- template<
- typename InputIterator,typename OutputIterator,
- typename Distance,typename UniformRandomBitGenerator
- >
- OutputIterator operator()(
- InputIterator first,InputIterator last,
- OutputIterator res,Distance& n,Distance& m, /* note the &s */
- UniformRandomBitGenerator& g)const
- {
- for(;first!=last&&n!=0;++first){
- auto r=std::uniform_int_distribution<Distance>(0,--m)(g);
- if (r<n){
- *res++=*first;
- --n;
- }
- }
- return res;
- }
- };
- template<
- typename... Ts,typename Iterator,typename OutputIterator,
- typename Distance,typename UniformRandomBitGenerator,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- OutputIterator sample(
- const Iterator& first,const Iterator& last,
- OutputIterator res,Distance n,UniformRandomBitGenerator&& g)
- {
- Distance m=algorithm::fast_distance(first,last);
- n=(std::min)(n,m);
- for(auto i:detail::segment_split(first,last)){
- auto alg=restitute_range<Ts...>(sample_alg{},res,n,m,g);
- res=alg(i);
- if(n==0)return res;
- }
- return res; /* never reached */
- }
- template<
- typename... Ts,typename Iterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- bool is_partitioned(const Iterator& first,const Iterator& last,Predicate pred)
- {
- auto it=algorithm::find_if_not<Ts...>(first,last,pred);
- if(it==last)return true;
- return algorithm::none_of<Ts...>(++it,last,pred);
- }
- BOOST_POLY_COLLECTION_DEFINE_OVERLOAD_SET(
- std_partition_copy,std::partition_copy)
- template<
- typename... Ts,typename Iterator,
- typename OutputIterator1,typename OutputIterator2,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- std::pair<OutputIterator1,OutputIterator2> partition_copy(
- const Iterator& first,const Iterator& last,
- OutputIterator1 rest,OutputIterator2 resf,Predicate pred)
- {
- for(auto i:detail::segment_split(first,last)){
- auto alg=restitute_range<Ts...>(std_partition_copy{},rest,resf,pred);
- std::tie(rest,resf)=alg(i);
- }
- return {rest,resf};
- }
- template<typename Predicate,typename... Ts>
- struct partition_point_pred
- {
- partition_point_pred(const Predicate& pred):pred(pred){}
- template<typename Iterator>
- bool operator()(const Iterator& it)const
- {
- using traits=iterator_traits<Iterator>;
- auto p=restitute_iterator<Ts...>(deref_to(pred));
- return p(
- traits::base_segment_info_iterator_from(it)->type_info(),
- traits::local_base_iterator_from(it));
- }
- Predicate pred;
- };
- template<
- typename... Ts,typename Iterator,typename Predicate,
- enable_if_poly_collection_iterator<Iterator> =nullptr
- >
- Iterator partition_point(
- const Iterator& first,const Iterator& last,Predicate pred)
- {
- auto_iterator<Iterator> afirst{first},alast{last};
- partition_point_pred<Predicate,Ts...> p{pred};
- return *std::partition_point(afirst,alast,p);
- }
- } /* namespace poly_collection::detail::algorithm */
- } /* namespace poly_collection::detail */
- /* non-modifying sequence operations */
- using detail::algorithm::all_of;
- using detail::algorithm::any_of;
- using detail::algorithm::none_of;
- using detail::algorithm::for_each;
- using detail::algorithm::for_each_n;
- using detail::algorithm::find;
- using detail::algorithm::find_if;
- using detail::algorithm::find_if_not;
- using detail::algorithm::find_end;
- using detail::algorithm::find_first_of;
- using detail::algorithm::adjacent_find;
- using detail::algorithm::count;
- using detail::algorithm::count_if;
- using detail::algorithm::mismatch;
- using detail::algorithm::equal;
- using detail::algorithm::is_permutation;
- using detail::algorithm::search;
- using detail::algorithm::search_n;
- /* modifying sequence operations */
- using detail::algorithm::copy;
- using detail::algorithm::copy_n;
- using detail::algorithm::copy_if;
- /* copy_backward requires BidirectionalIterator */
- using detail::algorithm::move;
- /* move_backward requires BidirectionalIterator */
- /* swap_ranges requires Swappable */
- /* iter_swap requires Swappable */
- using detail::algorithm::transform;
- /* replace requires Assignable */
- /* replace_if requires Assignable */
- using detail::algorithm::replace_copy;
- using detail::algorithm::replace_copy_if;
- /* fill requires Assignable */
- /* fill_n requires Assignable */
- /* generate requires Assignable */
- /* generate_n requires Assignable */
- /* remove requires MoveAssignable */
- /* remove_if requires MoveAssignable */
- using detail::algorithm::remove_copy;
- using detail::algorithm::remove_copy_if;
- /* unique requires MoveAssignable */
- using detail::algorithm::unique_copy;
- /* reverse requires BidirectionalIterator */
- /* reverse_copy requires BidirectionalIterator */
- /* rotate requires MoveAssignable */
- using detail::algorithm::rotate_copy;
- using detail::algorithm::sample;
- /* shuffle requires RandomAccessIterator */
- using detail::algorithm::is_partitioned;
- /* partition requires Swappable */
- /* stable_partition requires Swappable */
- using detail::algorithm::partition_copy;
- using detail::algorithm::partition_point;
- /* sorting and related operations not provided */
- } /* namespace poly_collection */
- } /* namespace boost */
- #endif
|