ranges_algo.h 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651
  1. // Core algorithmic facilities -*- C++ -*-
  2. // Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/ranges_algo.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{algorithm}
  23. */
  24. #ifndef _RANGES_ALGO_H
  25. #define _RANGES_ALGO_H 1
  26. #if __cplusplus > 201703L
  27. #include <bits/ranges_algobase.h>
  28. #include <bits/ranges_util.h>
  29. #include <bits/uniform_int_dist.h> // concept uniform_random_bit_generator
  30. #if __cpp_lib_concepts
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. namespace ranges
  35. {
  36. namespace __detail
  37. {
  38. template<typename _Comp, typename _Proj>
  39. constexpr auto
  40. __make_comp_proj(_Comp& __comp, _Proj& __proj)
  41. {
  42. return [&] (auto&& __lhs, auto&& __rhs) -> bool {
  43. using _TL = decltype(__lhs);
  44. using _TR = decltype(__rhs);
  45. return std::__invoke(__comp,
  46. std::__invoke(__proj, std::forward<_TL>(__lhs)),
  47. std::__invoke(__proj, std::forward<_TR>(__rhs)));
  48. };
  49. }
  50. template<typename _Pred, typename _Proj>
  51. constexpr auto
  52. __make_pred_proj(_Pred& __pred, _Proj& __proj)
  53. {
  54. return [&] <typename _Tp> (_Tp&& __arg) -> bool {
  55. return std::__invoke(__pred,
  56. std::__invoke(__proj, std::forward<_Tp>(__arg)));
  57. };
  58. }
  59. } // namespace __detail
  60. struct __all_of_fn
  61. {
  62. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  63. typename _Proj = identity,
  64. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  65. constexpr bool
  66. operator()(_Iter __first, _Sent __last,
  67. _Pred __pred, _Proj __proj = {}) const
  68. {
  69. for (; __first != __last; ++__first)
  70. if (!(bool)std::__invoke(__pred, std::__invoke(__proj, *__first)))
  71. return false;
  72. return true;
  73. }
  74. template<input_range _Range, typename _Proj = identity,
  75. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  76. _Pred>
  77. constexpr bool
  78. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  79. {
  80. return (*this)(ranges::begin(__r), ranges::end(__r),
  81. std::move(__pred), std::move(__proj));
  82. }
  83. };
  84. inline constexpr __all_of_fn all_of{};
  85. struct __any_of_fn
  86. {
  87. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  88. typename _Proj = identity,
  89. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  90. constexpr bool
  91. operator()(_Iter __first, _Sent __last,
  92. _Pred __pred, _Proj __proj = {}) const
  93. {
  94. for (; __first != __last; ++__first)
  95. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  96. return true;
  97. return false;
  98. }
  99. template<input_range _Range, typename _Proj = identity,
  100. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  101. _Pred>
  102. constexpr bool
  103. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  104. {
  105. return (*this)(ranges::begin(__r), ranges::end(__r),
  106. std::move(__pred), std::move(__proj));
  107. }
  108. };
  109. inline constexpr __any_of_fn any_of{};
  110. struct __none_of_fn
  111. {
  112. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  113. typename _Proj = identity,
  114. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  115. constexpr bool
  116. operator()(_Iter __first, _Sent __last,
  117. _Pred __pred, _Proj __proj = {}) const
  118. {
  119. for (; __first != __last; ++__first)
  120. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  121. return false;
  122. return true;
  123. }
  124. template<input_range _Range, typename _Proj = identity,
  125. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  126. _Pred>
  127. constexpr bool
  128. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  129. {
  130. return (*this)(ranges::begin(__r), ranges::end(__r),
  131. std::move(__pred), std::move(__proj));
  132. }
  133. };
  134. inline constexpr __none_of_fn none_of{};
  135. template<typename _Iter, typename _Fp>
  136. struct in_fun_result
  137. {
  138. [[no_unique_address]] _Iter in;
  139. [[no_unique_address]] _Fp fun;
  140. template<typename _Iter2, typename _F2p>
  141. requires convertible_to<const _Iter&, _Iter2>
  142. && convertible_to<const _Fp&, _F2p>
  143. constexpr
  144. operator in_fun_result<_Iter2, _F2p>() const &
  145. { return {in, fun}; }
  146. template<typename _Iter2, typename _F2p>
  147. requires convertible_to<_Iter, _Iter2> && convertible_to<_Fp, _F2p>
  148. constexpr
  149. operator in_fun_result<_Iter2, _F2p>() &&
  150. { return {std::move(in), std::move(fun)}; }
  151. };
  152. template<typename _Iter, typename _Fp>
  153. using for_each_result = in_fun_result<_Iter, _Fp>;
  154. struct __for_each_fn
  155. {
  156. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  157. typename _Proj = identity,
  158. indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
  159. constexpr for_each_result<_Iter, _Fun>
  160. operator()(_Iter __first, _Sent __last, _Fun __f, _Proj __proj = {}) const
  161. {
  162. for (; __first != __last; ++__first)
  163. std::__invoke(__f, std::__invoke(__proj, *__first));
  164. return { std::move(__first), std::move(__f) };
  165. }
  166. template<input_range _Range, typename _Proj = identity,
  167. indirectly_unary_invocable<projected<iterator_t<_Range>, _Proj>>
  168. _Fun>
  169. constexpr for_each_result<borrowed_iterator_t<_Range>, _Fun>
  170. operator()(_Range&& __r, _Fun __f, _Proj __proj = {}) const
  171. {
  172. return (*this)(ranges::begin(__r), ranges::end(__r),
  173. std::move(__f), std::move(__proj));
  174. }
  175. };
  176. inline constexpr __for_each_fn for_each{};
  177. template<typename _Iter, typename _Fp>
  178. using for_each_n_result = in_fun_result<_Iter, _Fp>;
  179. struct __for_each_n_fn
  180. {
  181. template<input_iterator _Iter, typename _Proj = identity,
  182. indirectly_unary_invocable<projected<_Iter, _Proj>> _Fun>
  183. constexpr for_each_n_result<_Iter, _Fun>
  184. operator()(_Iter __first, iter_difference_t<_Iter> __n,
  185. _Fun __f, _Proj __proj = {}) const
  186. {
  187. if constexpr (random_access_iterator<_Iter>)
  188. {
  189. if (__n <= 0)
  190. return {std::move(__first), std::move(__f)};
  191. auto __last = __first + __n;
  192. return ranges::for_each(std::move(__first), std::move(__last),
  193. std::move(__f), std::move(__proj));
  194. }
  195. else
  196. {
  197. while (__n-- > 0)
  198. {
  199. std::__invoke(__f, std::__invoke(__proj, *__first));
  200. ++__first;
  201. }
  202. return {std::move(__first), std::move(__f)};
  203. }
  204. }
  205. };
  206. inline constexpr __for_each_n_fn for_each_n{};
  207. // find, find_if and find_if_not are defined in <bits/ranges_util.h>.
  208. struct __find_first_of_fn
  209. {
  210. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  211. forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  212. typename _Pred = ranges::equal_to,
  213. typename _Proj1 = identity, typename _Proj2 = identity>
  214. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  215. constexpr _Iter1
  216. operator()(_Iter1 __first1, _Sent1 __last1,
  217. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  218. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  219. {
  220. for (; __first1 != __last1; ++__first1)
  221. for (auto __iter = __first2; __iter != __last2; ++__iter)
  222. if (std::__invoke(__pred,
  223. std::__invoke(__proj1, *__first1),
  224. std::__invoke(__proj2, *__iter)))
  225. return __first1;
  226. return __first1;
  227. }
  228. template<input_range _Range1, forward_range _Range2,
  229. typename _Pred = ranges::equal_to,
  230. typename _Proj1 = identity, typename _Proj2 = identity>
  231. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
  232. _Pred, _Proj1, _Proj2>
  233. constexpr borrowed_iterator_t<_Range1>
  234. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  235. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  236. {
  237. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  238. ranges::begin(__r2), ranges::end(__r2),
  239. std::move(__pred),
  240. std::move(__proj1), std::move(__proj2));
  241. }
  242. };
  243. inline constexpr __find_first_of_fn find_first_of{};
  244. struct __count_fn
  245. {
  246. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  247. typename _Tp, typename _Proj = identity>
  248. requires indirect_binary_predicate<ranges::equal_to,
  249. projected<_Iter, _Proj>,
  250. const _Tp*>
  251. constexpr iter_difference_t<_Iter>
  252. operator()(_Iter __first, _Sent __last,
  253. const _Tp& __value, _Proj __proj = {}) const
  254. {
  255. iter_difference_t<_Iter> __n = 0;
  256. for (; __first != __last; ++__first)
  257. if (std::__invoke(__proj, *__first) == __value)
  258. ++__n;
  259. return __n;
  260. }
  261. template<input_range _Range, typename _Tp, typename _Proj = identity>
  262. requires indirect_binary_predicate<ranges::equal_to,
  263. projected<iterator_t<_Range>, _Proj>,
  264. const _Tp*>
  265. constexpr range_difference_t<_Range>
  266. operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
  267. {
  268. return (*this)(ranges::begin(__r), ranges::end(__r),
  269. __value, std::move(__proj));
  270. }
  271. };
  272. inline constexpr __count_fn count{};
  273. struct __count_if_fn
  274. {
  275. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  276. typename _Proj = identity,
  277. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  278. constexpr iter_difference_t<_Iter>
  279. operator()(_Iter __first, _Sent __last,
  280. _Pred __pred, _Proj __proj = {}) const
  281. {
  282. iter_difference_t<_Iter> __n = 0;
  283. for (; __first != __last; ++__first)
  284. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  285. ++__n;
  286. return __n;
  287. }
  288. template<input_range _Range,
  289. typename _Proj = identity,
  290. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  291. _Pred>
  292. constexpr range_difference_t<_Range>
  293. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  294. {
  295. return (*this)(ranges::begin(__r), ranges::end(__r),
  296. std::move(__pred), std::move(__proj));
  297. }
  298. };
  299. inline constexpr __count_if_fn count_if{};
  300. // in_in_result, mismatch and search are defined in <bits/ranges_util.h>.
  301. struct __search_n_fn
  302. {
  303. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent, typename _Tp,
  304. typename _Pred = ranges::equal_to, typename _Proj = identity>
  305. requires indirectly_comparable<_Iter, const _Tp*, _Pred, _Proj>
  306. constexpr subrange<_Iter>
  307. operator()(_Iter __first, _Sent __last, iter_difference_t<_Iter> __count,
  308. const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
  309. {
  310. if (__count <= 0)
  311. return {__first, __first};
  312. auto __value_comp = [&] <typename _Rp> (_Rp&& __arg) -> bool {
  313. return std::__invoke(__pred, std::forward<_Rp>(__arg), __value);
  314. };
  315. if (__count == 1)
  316. {
  317. __first = ranges::find_if(std::move(__first), __last,
  318. std::move(__value_comp),
  319. std::move(__proj));
  320. if (__first == __last)
  321. return {__first, __first};
  322. else
  323. {
  324. auto __end = __first;
  325. return {__first, ++__end};
  326. }
  327. }
  328. if constexpr (sized_sentinel_for<_Sent, _Iter>
  329. && random_access_iterator<_Iter>)
  330. {
  331. auto __tail_size = __last - __first;
  332. auto __remainder = __count;
  333. while (__remainder <= __tail_size)
  334. {
  335. __first += __remainder;
  336. __tail_size -= __remainder;
  337. auto __backtrack = __first;
  338. while (__value_comp(std::__invoke(__proj, *--__backtrack)))
  339. {
  340. if (--__remainder == 0)
  341. return {__first - __count, __first};
  342. }
  343. __remainder = __count + 1 - (__first - __backtrack);
  344. }
  345. auto __i = __first + __tail_size;
  346. return {__i, __i};
  347. }
  348. else
  349. {
  350. __first = ranges::find_if(__first, __last, __value_comp, __proj);
  351. while (__first != __last)
  352. {
  353. auto __n = __count;
  354. auto __i = __first;
  355. ++__i;
  356. while (__i != __last && __n != 1
  357. && __value_comp(std::__invoke(__proj, *__i)))
  358. {
  359. ++__i;
  360. --__n;
  361. }
  362. if (__n == 1)
  363. return {__first, __i};
  364. if (__i == __last)
  365. return {__i, __i};
  366. __first = ranges::find_if(++__i, __last, __value_comp, __proj);
  367. }
  368. return {__first, __first};
  369. }
  370. }
  371. template<forward_range _Range, typename _Tp,
  372. typename _Pred = ranges::equal_to, typename _Proj = identity>
  373. requires indirectly_comparable<iterator_t<_Range>, const _Tp*,
  374. _Pred, _Proj>
  375. constexpr borrowed_subrange_t<_Range>
  376. operator()(_Range&& __r, range_difference_t<_Range> __count,
  377. const _Tp& __value, _Pred __pred = {}, _Proj __proj = {}) const
  378. {
  379. return (*this)(ranges::begin(__r), ranges::end(__r),
  380. std::move(__count), __value,
  381. std::move(__pred), std::move(__proj));
  382. }
  383. };
  384. inline constexpr __search_n_fn search_n{};
  385. struct __find_end_fn
  386. {
  387. template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  388. forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  389. typename _Pred = ranges::equal_to,
  390. typename _Proj1 = identity, typename _Proj2 = identity>
  391. requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
  392. constexpr subrange<_Iter1>
  393. operator()(_Iter1 __first1, _Sent1 __last1,
  394. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  395. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  396. {
  397. if constexpr (bidirectional_iterator<_Iter1>
  398. && bidirectional_iterator<_Iter2>)
  399. {
  400. auto __i1 = ranges::next(__first1, __last1);
  401. auto __i2 = ranges::next(__first2, __last2);
  402. auto __rresult
  403. = ranges::search(reverse_iterator<_Iter1>{__i1},
  404. reverse_iterator<_Iter1>{__first1},
  405. reverse_iterator<_Iter2>{__i2},
  406. reverse_iterator<_Iter2>{__first2},
  407. std::move(__pred),
  408. std::move(__proj1), std::move(__proj2));
  409. auto __result_first = ranges::end(__rresult).base();
  410. auto __result_last = ranges::begin(__rresult).base();
  411. if (__result_last == __first1)
  412. return {__i1, __i1};
  413. else
  414. return {__result_first, __result_last};
  415. }
  416. else
  417. {
  418. auto __i = ranges::next(__first1, __last1);
  419. if (__first2 == __last2)
  420. return {__i, __i};
  421. auto __result_begin = __i;
  422. auto __result_end = __i;
  423. for (;;)
  424. {
  425. auto __new_range = ranges::search(__first1, __last1,
  426. __first2, __last2,
  427. __pred, __proj1, __proj2);
  428. auto __new_result_begin = ranges::begin(__new_range);
  429. auto __new_result_end = ranges::end(__new_range);
  430. if (__new_result_begin == __last1)
  431. return {__result_begin, __result_end};
  432. else
  433. {
  434. __result_begin = __new_result_begin;
  435. __result_end = __new_result_end;
  436. __first1 = __result_begin;
  437. ++__first1;
  438. }
  439. }
  440. }
  441. }
  442. template<forward_range _Range1, forward_range _Range2,
  443. typename _Pred = ranges::equal_to,
  444. typename _Proj1 = identity, typename _Proj2 = identity>
  445. requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>,
  446. _Pred, _Proj1, _Proj2>
  447. constexpr borrowed_subrange_t<_Range1>
  448. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  449. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  450. {
  451. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  452. ranges::begin(__r2), ranges::end(__r2),
  453. std::move(__pred),
  454. std::move(__proj1), std::move(__proj2));
  455. }
  456. };
  457. inline constexpr __find_end_fn find_end{};
  458. struct __adjacent_find_fn
  459. {
  460. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  461. typename _Proj = identity,
  462. indirect_binary_predicate<projected<_Iter, _Proj>,
  463. projected<_Iter, _Proj>> _Pred
  464. = ranges::equal_to>
  465. constexpr _Iter
  466. operator()(_Iter __first, _Sent __last,
  467. _Pred __pred = {}, _Proj __proj = {}) const
  468. {
  469. if (__first == __last)
  470. return __first;
  471. auto __next = __first;
  472. for (; ++__next != __last; __first = __next)
  473. {
  474. if (std::__invoke(__pred,
  475. std::__invoke(__proj, *__first),
  476. std::__invoke(__proj, *__next)))
  477. return __first;
  478. }
  479. return __next;
  480. }
  481. template<forward_range _Range, typename _Proj = identity,
  482. indirect_binary_predicate<
  483. projected<iterator_t<_Range>, _Proj>,
  484. projected<iterator_t<_Range>, _Proj>> _Pred = ranges::equal_to>
  485. constexpr borrowed_iterator_t<_Range>
  486. operator()(_Range&& __r, _Pred __pred = {}, _Proj __proj = {}) const
  487. {
  488. return (*this)(ranges::begin(__r), ranges::end(__r),
  489. std::move(__pred), std::move(__proj));
  490. }
  491. };
  492. inline constexpr __adjacent_find_fn adjacent_find{};
  493. struct __is_permutation_fn
  494. {
  495. template<forward_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  496. forward_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  497. typename _Proj1 = identity, typename _Proj2 = identity,
  498. indirect_equivalence_relation<projected<_Iter1, _Proj1>,
  499. projected<_Iter2, _Proj2>> _Pred
  500. = ranges::equal_to>
  501. constexpr bool
  502. operator()(_Iter1 __first1, _Sent1 __last1,
  503. _Iter2 __first2, _Sent2 __last2, _Pred __pred = {},
  504. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  505. {
  506. constexpr bool __sized_iters
  507. = (sized_sentinel_for<_Sent1, _Iter1>
  508. && sized_sentinel_for<_Sent2, _Iter2>);
  509. if constexpr (__sized_iters)
  510. {
  511. auto __d1 = ranges::distance(__first1, __last1);
  512. auto __d2 = ranges::distance(__first2, __last2);
  513. if (__d1 != __d2)
  514. return false;
  515. }
  516. // Efficiently compare identical prefixes: O(N) if sequences
  517. // have the same elements in the same order.
  518. for (; __first1 != __last1 && __first2 != __last2;
  519. ++__first1, (void)++__first2)
  520. if (!(bool)std::__invoke(__pred,
  521. std::__invoke(__proj1, *__first1),
  522. std::__invoke(__proj2, *__first2)))
  523. break;
  524. if constexpr (__sized_iters)
  525. {
  526. if (__first1 == __last1)
  527. return true;
  528. }
  529. else
  530. {
  531. auto __d1 = ranges::distance(__first1, __last1);
  532. auto __d2 = ranges::distance(__first2, __last2);
  533. if (__d1 == 0 && __d2 == 0)
  534. return true;
  535. if (__d1 != __d2)
  536. return false;
  537. }
  538. for (auto __scan = __first1; __scan != __last1; ++__scan)
  539. {
  540. auto&& __proj_scan = std::__invoke(__proj1, *__scan);
  541. auto __comp_scan = [&] <typename _Tp> (_Tp&& __arg) -> bool {
  542. return std::__invoke(__pred, __proj_scan,
  543. std::forward<_Tp>(__arg));
  544. };
  545. if (__scan != ranges::find_if(__first1, __scan,
  546. __comp_scan, __proj1))
  547. continue; // We've seen this one before.
  548. auto __matches = ranges::count_if(__first2, __last2,
  549. __comp_scan, __proj2);
  550. if (__matches == 0
  551. || ranges::count_if(__scan, __last1,
  552. __comp_scan, __proj1) != __matches)
  553. return false;
  554. }
  555. return true;
  556. }
  557. template<forward_range _Range1, forward_range _Range2,
  558. typename _Proj1 = identity, typename _Proj2 = identity,
  559. indirect_equivalence_relation<
  560. projected<iterator_t<_Range1>, _Proj1>,
  561. projected<iterator_t<_Range2>, _Proj2>> _Pred = ranges::equal_to>
  562. constexpr bool
  563. operator()(_Range1&& __r1, _Range2&& __r2, _Pred __pred = {},
  564. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  565. {
  566. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  567. ranges::begin(__r2), ranges::end(__r2),
  568. std::move(__pred),
  569. std::move(__proj1), std::move(__proj2));
  570. }
  571. };
  572. inline constexpr __is_permutation_fn is_permutation{};
  573. template<typename _Iter, typename _Out>
  574. using copy_if_result = in_out_result<_Iter, _Out>;
  575. struct __copy_if_fn
  576. {
  577. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  578. weakly_incrementable _Out, typename _Proj = identity,
  579. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  580. requires indirectly_copyable<_Iter, _Out>
  581. constexpr copy_if_result<_Iter, _Out>
  582. operator()(_Iter __first, _Sent __last, _Out __result,
  583. _Pred __pred, _Proj __proj = {}) const
  584. {
  585. for (; __first != __last; ++__first)
  586. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  587. {
  588. *__result = *__first;
  589. ++__result;
  590. }
  591. return {std::move(__first), std::move(__result)};
  592. }
  593. template<input_range _Range, weakly_incrementable _Out,
  594. typename _Proj = identity,
  595. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  596. _Pred>
  597. requires indirectly_copyable<iterator_t<_Range>, _Out>
  598. constexpr copy_if_result<borrowed_iterator_t<_Range>, _Out>
  599. operator()(_Range&& __r, _Out __result,
  600. _Pred __pred, _Proj __proj = {}) const
  601. {
  602. return (*this)(ranges::begin(__r), ranges::end(__r),
  603. std::move(__result),
  604. std::move(__pred), std::move(__proj));
  605. }
  606. };
  607. inline constexpr __copy_if_fn copy_if{};
  608. template<typename _Iter1, typename _Iter2>
  609. using swap_ranges_result = in_in_result<_Iter1, _Iter2>;
  610. struct __swap_ranges_fn
  611. {
  612. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  613. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2>
  614. requires indirectly_swappable<_Iter1, _Iter2>
  615. constexpr swap_ranges_result<_Iter1, _Iter2>
  616. operator()(_Iter1 __first1, _Sent1 __last1,
  617. _Iter2 __first2, _Sent2 __last2) const
  618. {
  619. for (; __first1 != __last1 && __first2 != __last2;
  620. ++__first1, (void)++__first2)
  621. ranges::iter_swap(__first1, __first2);
  622. return {std::move(__first1), std::move(__first2)};
  623. }
  624. template<input_range _Range1, input_range _Range2>
  625. requires indirectly_swappable<iterator_t<_Range1>, iterator_t<_Range2>>
  626. constexpr swap_ranges_result<borrowed_iterator_t<_Range1>,
  627. borrowed_iterator_t<_Range2>>
  628. operator()(_Range1&& __r1, _Range2&& __r2) const
  629. {
  630. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  631. ranges::begin(__r2), ranges::end(__r2));
  632. }
  633. };
  634. inline constexpr __swap_ranges_fn swap_ranges{};
  635. template<typename _Iter, typename _Out>
  636. using unary_transform_result = in_out_result<_Iter, _Out>;
  637. template<typename _Iter1, typename _Iter2, typename _Out>
  638. struct in_in_out_result
  639. {
  640. [[no_unique_address]] _Iter1 in1;
  641. [[no_unique_address]] _Iter2 in2;
  642. [[no_unique_address]] _Out out;
  643. template<typename _IIter1, typename _IIter2, typename _OOut>
  644. requires convertible_to<const _Iter1&, _IIter1>
  645. && convertible_to<const _Iter2&, _IIter2>
  646. && convertible_to<const _Out&, _OOut>
  647. constexpr
  648. operator in_in_out_result<_IIter1, _IIter2, _OOut>() const &
  649. { return {in1, in2, out}; }
  650. template<typename _IIter1, typename _IIter2, typename _OOut>
  651. requires convertible_to<_Iter1, _IIter1>
  652. && convertible_to<_Iter2, _IIter2>
  653. && convertible_to<_Out, _OOut>
  654. constexpr
  655. operator in_in_out_result<_IIter1, _IIter2, _OOut>() &&
  656. { return {std::move(in1), std::move(in2), std::move(out)}; }
  657. };
  658. template<typename _Iter1, typename _Iter2, typename _Out>
  659. using binary_transform_result = in_in_out_result<_Iter1, _Iter2, _Out>;
  660. struct __transform_fn
  661. {
  662. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  663. weakly_incrementable _Out,
  664. copy_constructible _Fp, typename _Proj = identity>
  665. requires indirectly_writable<_Out,
  666. indirect_result_t<_Fp&,
  667. projected<_Iter, _Proj>>>
  668. constexpr unary_transform_result<_Iter, _Out>
  669. operator()(_Iter __first1, _Sent __last1, _Out __result,
  670. _Fp __op, _Proj __proj = {}) const
  671. {
  672. for (; __first1 != __last1; ++__first1, (void)++__result)
  673. *__result = std::__invoke(__op, std::__invoke(__proj, *__first1));
  674. return {std::move(__first1), std::move(__result)};
  675. }
  676. template<input_range _Range, weakly_incrementable _Out,
  677. copy_constructible _Fp, typename _Proj = identity>
  678. requires indirectly_writable<_Out,
  679. indirect_result_t<_Fp&,
  680. projected<iterator_t<_Range>, _Proj>>>
  681. constexpr unary_transform_result<borrowed_iterator_t<_Range>, _Out>
  682. operator()(_Range&& __r, _Out __result, _Fp __op, _Proj __proj = {}) const
  683. {
  684. return (*this)(ranges::begin(__r), ranges::end(__r),
  685. std::move(__result),
  686. std::move(__op), std::move(__proj));
  687. }
  688. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  689. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  690. weakly_incrementable _Out, copy_constructible _Fp,
  691. typename _Proj1 = identity, typename _Proj2 = identity>
  692. requires indirectly_writable<_Out,
  693. indirect_result_t<_Fp&,
  694. projected<_Iter1, _Proj1>,
  695. projected<_Iter2, _Proj2>>>
  696. constexpr binary_transform_result<_Iter1, _Iter2, _Out>
  697. operator()(_Iter1 __first1, _Sent1 __last1,
  698. _Iter2 __first2, _Sent2 __last2,
  699. _Out __result, _Fp __binary_op,
  700. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  701. {
  702. for (; __first1 != __last1 && __first2 != __last2;
  703. ++__first1, (void)++__first2, ++__result)
  704. *__result = std::__invoke(__binary_op,
  705. std::__invoke(__proj1, *__first1),
  706. std::__invoke(__proj2, *__first2));
  707. return {std::move(__first1), std::move(__first2), std::move(__result)};
  708. }
  709. template<input_range _Range1, input_range _Range2,
  710. weakly_incrementable _Out, copy_constructible _Fp,
  711. typename _Proj1 = identity, typename _Proj2 = identity>
  712. requires indirectly_writable<_Out,
  713. indirect_result_t<_Fp&,
  714. projected<iterator_t<_Range1>, _Proj1>,
  715. projected<iterator_t<_Range2>, _Proj2>>>
  716. constexpr binary_transform_result<borrowed_iterator_t<_Range1>,
  717. borrowed_iterator_t<_Range2>, _Out>
  718. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result, _Fp __binary_op,
  719. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  720. {
  721. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  722. ranges::begin(__r2), ranges::end(__r2),
  723. std::move(__result), std::move(__binary_op),
  724. std::move(__proj1), std::move(__proj2));
  725. }
  726. };
  727. inline constexpr __transform_fn transform{};
  728. struct __replace_fn
  729. {
  730. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  731. typename _Tp1, typename _Tp2, typename _Proj = identity>
  732. requires indirectly_writable<_Iter, const _Tp2&>
  733. && indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>,
  734. const _Tp1*>
  735. constexpr _Iter
  736. operator()(_Iter __first, _Sent __last,
  737. const _Tp1& __old_value, const _Tp2& __new_value,
  738. _Proj __proj = {}) const
  739. {
  740. for (; __first != __last; ++__first)
  741. if (std::__invoke(__proj, *__first) == __old_value)
  742. *__first = __new_value;
  743. return __first;
  744. }
  745. template<input_range _Range,
  746. typename _Tp1, typename _Tp2, typename _Proj = identity>
  747. requires indirectly_writable<iterator_t<_Range>, const _Tp2&>
  748. && indirect_binary_predicate<ranges::equal_to,
  749. projected<iterator_t<_Range>, _Proj>,
  750. const _Tp1*>
  751. constexpr borrowed_iterator_t<_Range>
  752. operator()(_Range&& __r,
  753. const _Tp1& __old_value, const _Tp2& __new_value,
  754. _Proj __proj = {}) const
  755. {
  756. return (*this)(ranges::begin(__r), ranges::end(__r),
  757. __old_value, __new_value, std::move(__proj));
  758. }
  759. };
  760. inline constexpr __replace_fn replace{};
  761. struct __replace_if_fn
  762. {
  763. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  764. typename _Tp, typename _Proj = identity,
  765. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  766. requires indirectly_writable<_Iter, const _Tp&>
  767. constexpr _Iter
  768. operator()(_Iter __first, _Sent __last,
  769. _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
  770. {
  771. for (; __first != __last; ++__first)
  772. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  773. *__first = __new_value;
  774. return std::move(__first);
  775. }
  776. template<input_range _Range, typename _Tp, typename _Proj = identity,
  777. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  778. _Pred>
  779. requires indirectly_writable<iterator_t<_Range>, const _Tp&>
  780. constexpr borrowed_iterator_t<_Range>
  781. operator()(_Range&& __r,
  782. _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
  783. {
  784. return (*this)(ranges::begin(__r), ranges::end(__r),
  785. std::move(__pred), __new_value, std::move(__proj));
  786. }
  787. };
  788. inline constexpr __replace_if_fn replace_if{};
  789. template<typename _Iter, typename _Out>
  790. using replace_copy_result = in_out_result<_Iter, _Out>;
  791. struct __replace_copy_fn
  792. {
  793. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  794. typename _Tp1, typename _Tp2, output_iterator<const _Tp2&> _Out,
  795. typename _Proj = identity>
  796. requires indirectly_copyable<_Iter, _Out>
  797. && indirect_binary_predicate<ranges::equal_to,
  798. projected<_Iter, _Proj>, const _Tp1*>
  799. constexpr replace_copy_result<_Iter, _Out>
  800. operator()(_Iter __first, _Sent __last, _Out __result,
  801. const _Tp1& __old_value, const _Tp2& __new_value,
  802. _Proj __proj = {}) const
  803. {
  804. for (; __first != __last; ++__first, (void)++__result)
  805. if (std::__invoke(__proj, *__first) == __old_value)
  806. *__result = __new_value;
  807. else
  808. *__result = *__first;
  809. return {std::move(__first), std::move(__result)};
  810. }
  811. template<input_range _Range, typename _Tp1, typename _Tp2,
  812. output_iterator<const _Tp2&> _Out, typename _Proj = identity>
  813. requires indirectly_copyable<iterator_t<_Range>, _Out>
  814. && indirect_binary_predicate<ranges::equal_to,
  815. projected<iterator_t<_Range>, _Proj>,
  816. const _Tp1*>
  817. constexpr replace_copy_result<borrowed_iterator_t<_Range>, _Out>
  818. operator()(_Range&& __r, _Out __result,
  819. const _Tp1& __old_value, const _Tp2& __new_value,
  820. _Proj __proj = {}) const
  821. {
  822. return (*this)(ranges::begin(__r), ranges::end(__r),
  823. std::move(__result), __old_value,
  824. __new_value, std::move(__proj));
  825. }
  826. };
  827. inline constexpr __replace_copy_fn replace_copy{};
  828. template<typename _Iter, typename _Out>
  829. using replace_copy_if_result = in_out_result<_Iter, _Out>;
  830. struct __replace_copy_if_fn
  831. {
  832. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  833. typename _Tp, output_iterator<const _Tp&> _Out,
  834. typename _Proj = identity,
  835. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  836. requires indirectly_copyable<_Iter, _Out>
  837. constexpr replace_copy_if_result<_Iter, _Out>
  838. operator()(_Iter __first, _Sent __last, _Out __result,
  839. _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
  840. {
  841. for (; __first != __last; ++__first, (void)++__result)
  842. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  843. *__result = __new_value;
  844. else
  845. *__result = *__first;
  846. return {std::move(__first), std::move(__result)};
  847. }
  848. template<input_range _Range,
  849. typename _Tp, output_iterator<const _Tp&> _Out,
  850. typename _Proj = identity,
  851. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  852. _Pred>
  853. requires indirectly_copyable<iterator_t<_Range>, _Out>
  854. constexpr replace_copy_if_result<borrowed_iterator_t<_Range>, _Out>
  855. operator()(_Range&& __r, _Out __result,
  856. _Pred __pred, const _Tp& __new_value, _Proj __proj = {}) const
  857. {
  858. return (*this)(ranges::begin(__r), ranges::end(__r),
  859. std::move(__result), std::move(__pred),
  860. __new_value, std::move(__proj));
  861. }
  862. };
  863. inline constexpr __replace_copy_if_fn replace_copy_if{};
  864. struct __generate_n_fn
  865. {
  866. template<input_or_output_iterator _Out, copy_constructible _Fp>
  867. requires invocable<_Fp&>
  868. && indirectly_writable<_Out, invoke_result_t<_Fp&>>
  869. constexpr _Out
  870. operator()(_Out __first, iter_difference_t<_Out> __n, _Fp __gen) const
  871. {
  872. for (; __n > 0; --__n, (void)++__first)
  873. *__first = std::__invoke(__gen);
  874. return __first;
  875. }
  876. };
  877. inline constexpr __generate_n_fn generate_n{};
  878. struct __generate_fn
  879. {
  880. template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent,
  881. copy_constructible _Fp>
  882. requires invocable<_Fp&>
  883. && indirectly_writable<_Out, invoke_result_t<_Fp&>>
  884. constexpr _Out
  885. operator()(_Out __first, _Sent __last, _Fp __gen) const
  886. {
  887. for (; __first != __last; ++__first)
  888. *__first = std::__invoke(__gen);
  889. return __first;
  890. }
  891. template<typename _Range, copy_constructible _Fp>
  892. requires invocable<_Fp&> && output_range<_Range, invoke_result_t<_Fp&>>
  893. constexpr borrowed_iterator_t<_Range>
  894. operator()(_Range&& __r, _Fp __gen) const
  895. {
  896. return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__gen));
  897. }
  898. };
  899. inline constexpr __generate_fn generate{};
  900. struct __remove_if_fn
  901. {
  902. template<permutable _Iter, sentinel_for<_Iter> _Sent,
  903. typename _Proj = identity,
  904. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  905. constexpr subrange<_Iter>
  906. operator()(_Iter __first, _Sent __last,
  907. _Pred __pred, _Proj __proj = {}) const
  908. {
  909. __first = ranges::find_if(__first, __last, __pred, __proj);
  910. if (__first == __last)
  911. return {__first, __first};
  912. auto __result = __first;
  913. ++__first;
  914. for (; __first != __last; ++__first)
  915. if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
  916. {
  917. *__result = std::move(*__first);
  918. ++__result;
  919. }
  920. return {__result, __first};
  921. }
  922. template<forward_range _Range, typename _Proj = identity,
  923. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  924. _Pred>
  925. requires permutable<iterator_t<_Range>>
  926. constexpr borrowed_subrange_t<_Range>
  927. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  928. {
  929. return (*this)(ranges::begin(__r), ranges::end(__r),
  930. std::move(__pred), std::move(__proj));
  931. }
  932. };
  933. inline constexpr __remove_if_fn remove_if{};
  934. struct __remove_fn
  935. {
  936. template<permutable _Iter, sentinel_for<_Iter> _Sent,
  937. typename _Tp, typename _Proj = identity>
  938. requires indirect_binary_predicate<ranges::equal_to,
  939. projected<_Iter, _Proj>,
  940. const _Tp*>
  941. constexpr subrange<_Iter>
  942. operator()(_Iter __first, _Sent __last,
  943. const _Tp& __value, _Proj __proj = {}) const
  944. {
  945. auto __pred = [&] (auto&& __arg) -> bool {
  946. return std::forward<decltype(__arg)>(__arg) == __value;
  947. };
  948. return ranges::remove_if(__first, __last,
  949. std::move(__pred), std::move(__proj));
  950. }
  951. template<forward_range _Range, typename _Tp, typename _Proj = identity>
  952. requires permutable<iterator_t<_Range>>
  953. && indirect_binary_predicate<ranges::equal_to,
  954. projected<iterator_t<_Range>, _Proj>,
  955. const _Tp*>
  956. constexpr borrowed_subrange_t<_Range>
  957. operator()(_Range&& __r, const _Tp& __value, _Proj __proj = {}) const
  958. {
  959. return (*this)(ranges::begin(__r), ranges::end(__r),
  960. __value, std::move(__proj));
  961. }
  962. };
  963. inline constexpr __remove_fn remove{};
  964. template<typename _Iter, typename _Out>
  965. using remove_copy_if_result = in_out_result<_Iter, _Out>;
  966. struct __remove_copy_if_fn
  967. {
  968. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  969. weakly_incrementable _Out, typename _Proj = identity,
  970. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  971. requires indirectly_copyable<_Iter, _Out>
  972. constexpr remove_copy_if_result<_Iter, _Out>
  973. operator()(_Iter __first, _Sent __last, _Out __result,
  974. _Pred __pred, _Proj __proj = {}) const
  975. {
  976. for (; __first != __last; ++__first)
  977. if (!std::__invoke(__pred, std::__invoke(__proj, *__first)))
  978. {
  979. *__result = *__first;
  980. ++__result;
  981. }
  982. return {std::move(__first), std::move(__result)};
  983. }
  984. template<input_range _Range, weakly_incrementable _Out,
  985. typename _Proj = identity,
  986. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  987. _Pred>
  988. requires indirectly_copyable<iterator_t<_Range>, _Out>
  989. constexpr remove_copy_if_result<borrowed_iterator_t<_Range>, _Out>
  990. operator()(_Range&& __r, _Out __result,
  991. _Pred __pred, _Proj __proj = {}) const
  992. {
  993. return (*this)(ranges::begin(__r), ranges::end(__r),
  994. std::move(__result),
  995. std::move(__pred), std::move(__proj));
  996. }
  997. };
  998. inline constexpr __remove_copy_if_fn remove_copy_if{};
  999. template<typename _Iter, typename _Out>
  1000. using remove_copy_result = in_out_result<_Iter, _Out>;
  1001. struct __remove_copy_fn
  1002. {
  1003. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1004. weakly_incrementable _Out, typename _Tp, typename _Proj = identity>
  1005. requires indirectly_copyable<_Iter, _Out>
  1006. && indirect_binary_predicate<ranges::equal_to,
  1007. projected<_Iter, _Proj>,
  1008. const _Tp*>
  1009. constexpr remove_copy_result<_Iter, _Out>
  1010. operator()(_Iter __first, _Sent __last, _Out __result,
  1011. const _Tp& __value, _Proj __proj = {}) const
  1012. {
  1013. for (; __first != __last; ++__first)
  1014. if (!(std::__invoke(__proj, *__first) == __value))
  1015. {
  1016. *__result = *__first;
  1017. ++__result;
  1018. }
  1019. return {std::move(__first), std::move(__result)};
  1020. }
  1021. template<input_range _Range, weakly_incrementable _Out,
  1022. typename _Tp, typename _Proj = identity>
  1023. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1024. && indirect_binary_predicate<ranges::equal_to,
  1025. projected<iterator_t<_Range>, _Proj>,
  1026. const _Tp*>
  1027. constexpr remove_copy_result<borrowed_iterator_t<_Range>, _Out>
  1028. operator()(_Range&& __r, _Out __result,
  1029. const _Tp& __value, _Proj __proj = {}) const
  1030. {
  1031. return (*this)(ranges::begin(__r), ranges::end(__r),
  1032. std::move(__result), __value, std::move(__proj));
  1033. }
  1034. };
  1035. inline constexpr __remove_copy_fn remove_copy{};
  1036. struct __unique_fn
  1037. {
  1038. template<permutable _Iter, sentinel_for<_Iter> _Sent,
  1039. typename _Proj = identity,
  1040. indirect_equivalence_relation<
  1041. projected<_Iter, _Proj>> _Comp = ranges::equal_to>
  1042. constexpr subrange<_Iter>
  1043. operator()(_Iter __first, _Sent __last,
  1044. _Comp __comp = {}, _Proj __proj = {}) const
  1045. {
  1046. __first = ranges::adjacent_find(__first, __last, __comp, __proj);
  1047. if (__first == __last)
  1048. return {__first, __first};
  1049. auto __dest = __first;
  1050. ++__first;
  1051. while (++__first != __last)
  1052. if (!std::__invoke(__comp,
  1053. std::__invoke(__proj, *__dest),
  1054. std::__invoke(__proj, *__first)))
  1055. *++__dest = std::move(*__first);
  1056. return {++__dest, __first};
  1057. }
  1058. template<forward_range _Range, typename _Proj = identity,
  1059. indirect_equivalence_relation<
  1060. projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
  1061. requires permutable<iterator_t<_Range>>
  1062. constexpr borrowed_subrange_t<_Range>
  1063. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1064. {
  1065. return (*this)(ranges::begin(__r), ranges::end(__r),
  1066. std::move(__comp), std::move(__proj));
  1067. }
  1068. };
  1069. inline constexpr __unique_fn unique{};
  1070. namespace __detail
  1071. {
  1072. template<typename _Out, typename _Tp>
  1073. concept __can_reread_output = input_iterator<_Out>
  1074. && same_as<_Tp, iter_value_t<_Out>>;
  1075. }
  1076. template<typename _Iter, typename _Out>
  1077. using unique_copy_result = in_out_result<_Iter, _Out>;
  1078. struct __unique_copy_fn
  1079. {
  1080. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1081. weakly_incrementable _Out, typename _Proj = identity,
  1082. indirect_equivalence_relation<
  1083. projected<_Iter, _Proj>> _Comp = ranges::equal_to>
  1084. requires indirectly_copyable<_Iter, _Out>
  1085. && (forward_iterator<_Iter>
  1086. || __detail::__can_reread_output<_Out, iter_value_t<_Iter>>
  1087. || indirectly_copyable_storable<_Iter, _Out>)
  1088. constexpr unique_copy_result<_Iter, _Out>
  1089. operator()(_Iter __first, _Sent __last, _Out __result,
  1090. _Comp __comp = {}, _Proj __proj = {}) const
  1091. {
  1092. if (__first == __last)
  1093. return {std::move(__first), std::move(__result)};
  1094. // TODO: perform a closer comparison with reference implementations
  1095. if constexpr (forward_iterator<_Iter>)
  1096. {
  1097. auto __next = __first;
  1098. *__result = *__next;
  1099. while (++__next != __last)
  1100. if (!std::__invoke(__comp,
  1101. std::__invoke(__proj, *__first),
  1102. std::__invoke(__proj, *__next)))
  1103. {
  1104. __first = __next;
  1105. *++__result = *__first;
  1106. }
  1107. return {__next, std::move(++__result)};
  1108. }
  1109. else if constexpr (__detail::__can_reread_output<_Out, iter_value_t<_Iter>>)
  1110. {
  1111. *__result = *__first;
  1112. while (++__first != __last)
  1113. if (!std::__invoke(__comp,
  1114. std::__invoke(__proj, *__result),
  1115. std::__invoke(__proj, *__first)))
  1116. *++__result = *__first;
  1117. return {std::move(__first), std::move(++__result)};
  1118. }
  1119. else // indirectly_copyable_storable<_Iter, _Out>
  1120. {
  1121. auto __value = *__first;
  1122. *__result = __value;
  1123. while (++__first != __last)
  1124. {
  1125. if (!(bool)std::__invoke(__comp,
  1126. std::__invoke(__proj, *__first),
  1127. std::__invoke(__proj, __value)))
  1128. {
  1129. __value = *__first;
  1130. *++__result = __value;
  1131. }
  1132. }
  1133. return {std::move(__first), std::move(++__result)};
  1134. }
  1135. }
  1136. template<input_range _Range,
  1137. weakly_incrementable _Out, typename _Proj = identity,
  1138. indirect_equivalence_relation<
  1139. projected<iterator_t<_Range>, _Proj>> _Comp = ranges::equal_to>
  1140. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1141. && (forward_iterator<iterator_t<_Range>>
  1142. || __detail::__can_reread_output<_Out, range_value_t<_Range>>
  1143. || indirectly_copyable_storable<iterator_t<_Range>, _Out>)
  1144. constexpr unique_copy_result<borrowed_iterator_t<_Range>, _Out>
  1145. operator()(_Range&& __r, _Out __result,
  1146. _Comp __comp = {}, _Proj __proj = {}) const
  1147. {
  1148. return (*this)(ranges::begin(__r), ranges::end(__r),
  1149. std::move(__result),
  1150. std::move(__comp), std::move(__proj));
  1151. }
  1152. };
  1153. inline constexpr __unique_copy_fn unique_copy{};
  1154. struct __reverse_fn
  1155. {
  1156. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent>
  1157. requires permutable<_Iter>
  1158. constexpr _Iter
  1159. operator()(_Iter __first, _Sent __last) const
  1160. {
  1161. auto __i = ranges::next(__first, __last);
  1162. auto __tail = __i;
  1163. if constexpr (random_access_iterator<_Iter>)
  1164. {
  1165. if (__first != __last)
  1166. {
  1167. --__tail;
  1168. while (__first < __tail)
  1169. {
  1170. ranges::iter_swap(__first, __tail);
  1171. ++__first;
  1172. --__tail;
  1173. }
  1174. }
  1175. return __i;
  1176. }
  1177. else
  1178. {
  1179. for (;;)
  1180. if (__first == __tail || __first == --__tail)
  1181. break;
  1182. else
  1183. {
  1184. ranges::iter_swap(__first, __tail);
  1185. ++__first;
  1186. }
  1187. return __i;
  1188. }
  1189. }
  1190. template<bidirectional_range _Range>
  1191. requires permutable<iterator_t<_Range>>
  1192. constexpr borrowed_iterator_t<_Range>
  1193. operator()(_Range&& __r) const
  1194. {
  1195. return (*this)(ranges::begin(__r), ranges::end(__r));
  1196. }
  1197. };
  1198. inline constexpr __reverse_fn reverse{};
  1199. template<typename _Iter, typename _Out>
  1200. using reverse_copy_result = in_out_result<_Iter, _Out>;
  1201. struct __reverse_copy_fn
  1202. {
  1203. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  1204. weakly_incrementable _Out>
  1205. requires indirectly_copyable<_Iter, _Out>
  1206. constexpr reverse_copy_result<_Iter, _Out>
  1207. operator()(_Iter __first, _Sent __last, _Out __result) const
  1208. {
  1209. auto __i = ranges::next(__first, __last);
  1210. auto __tail = __i;
  1211. while (__first != __tail)
  1212. {
  1213. --__tail;
  1214. *__result = *__tail;
  1215. ++__result;
  1216. }
  1217. return {__i, std::move(__result)};
  1218. }
  1219. template<bidirectional_range _Range, weakly_incrementable _Out>
  1220. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1221. constexpr reverse_copy_result<borrowed_iterator_t<_Range>, _Out>
  1222. operator()(_Range&& __r, _Out __result) const
  1223. {
  1224. return (*this)(ranges::begin(__r), ranges::end(__r),
  1225. std::move(__result));
  1226. }
  1227. };
  1228. inline constexpr __reverse_copy_fn reverse_copy{};
  1229. struct __rotate_fn
  1230. {
  1231. template<permutable _Iter, sentinel_for<_Iter> _Sent>
  1232. constexpr subrange<_Iter>
  1233. operator()(_Iter __first, _Iter __middle, _Sent __last) const
  1234. {
  1235. auto __lasti = ranges::next(__first, __last);
  1236. if (__first == __middle)
  1237. return {__lasti, __lasti};
  1238. if (__last == __middle)
  1239. return {std::move(__first), std::move(__lasti)};
  1240. if constexpr (random_access_iterator<_Iter>)
  1241. {
  1242. auto __n = __lasti - __first;
  1243. auto __k = __middle - __first;
  1244. if (__k == __n - __k)
  1245. {
  1246. ranges::swap_ranges(__first, __middle, __middle, __middle + __k);
  1247. return {std::move(__middle), std::move(__lasti)};
  1248. }
  1249. auto __p = __first;
  1250. auto __ret = __first + (__lasti - __middle);
  1251. for (;;)
  1252. {
  1253. if (__k < __n - __k)
  1254. {
  1255. // TODO: is_pod is deprecated, but this condition is
  1256. // consistent with the STL implementation.
  1257. if constexpr (__is_pod(iter_value_t<_Iter>))
  1258. if (__k == 1)
  1259. {
  1260. auto __t = std::move(*__p);
  1261. ranges::move(__p + 1, __p + __n, __p);
  1262. *(__p + __n - 1) = std::move(__t);
  1263. return {std::move(__ret), std::move(__lasti)};
  1264. }
  1265. auto __q = __p + __k;
  1266. for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
  1267. {
  1268. ranges::iter_swap(__p, __q);
  1269. ++__p;
  1270. ++__q;
  1271. }
  1272. __n %= __k;
  1273. if (__n == 0)
  1274. return {std::move(__ret), std::move(__lasti)};
  1275. ranges::swap(__n, __k);
  1276. __k = __n - __k;
  1277. }
  1278. else
  1279. {
  1280. __k = __n - __k;
  1281. // TODO: is_pod is deprecated, but this condition is
  1282. // consistent with the STL implementation.
  1283. if constexpr (__is_pod(iter_value_t<_Iter>))
  1284. if (__k == 1)
  1285. {
  1286. auto __t = std::move(*(__p + __n - 1));
  1287. ranges::move_backward(__p, __p + __n - 1, __p + __n);
  1288. *__p = std::move(__t);
  1289. return {std::move(__ret), std::move(__lasti)};
  1290. }
  1291. auto __q = __p + __n;
  1292. __p = __q - __k;
  1293. for (decltype(__n) __i = 0; __i < __n - __k; ++ __i)
  1294. {
  1295. --__p;
  1296. --__q;
  1297. ranges::iter_swap(__p, __q);
  1298. }
  1299. __n %= __k;
  1300. if (__n == 0)
  1301. return {std::move(__ret), std::move(__lasti)};
  1302. std::swap(__n, __k);
  1303. }
  1304. }
  1305. }
  1306. else if constexpr (bidirectional_iterator<_Iter>)
  1307. {
  1308. auto __tail = __lasti;
  1309. ranges::reverse(__first, __middle);
  1310. ranges::reverse(__middle, __tail);
  1311. while (__first != __middle && __middle != __tail)
  1312. {
  1313. ranges::iter_swap(__first, --__tail);
  1314. ++__first;
  1315. }
  1316. if (__first == __middle)
  1317. {
  1318. ranges::reverse(__middle, __tail);
  1319. return {std::move(__tail), std::move(__lasti)};
  1320. }
  1321. else
  1322. {
  1323. ranges::reverse(__first, __middle);
  1324. return {std::move(__first), std::move(__lasti)};
  1325. }
  1326. }
  1327. else
  1328. {
  1329. auto __first2 = __middle;
  1330. do
  1331. {
  1332. ranges::iter_swap(__first, __first2);
  1333. ++__first;
  1334. ++__first2;
  1335. if (__first == __middle)
  1336. __middle = __first2;
  1337. } while (__first2 != __last);
  1338. auto __ret = __first;
  1339. __first2 = __middle;
  1340. while (__first2 != __last)
  1341. {
  1342. ranges::iter_swap(__first, __first2);
  1343. ++__first;
  1344. ++__first2;
  1345. if (__first == __middle)
  1346. __middle = __first2;
  1347. else if (__first2 == __last)
  1348. __first2 = __middle;
  1349. }
  1350. return {std::move(__ret), std::move(__lasti)};
  1351. }
  1352. }
  1353. template<forward_range _Range>
  1354. requires permutable<iterator_t<_Range>>
  1355. constexpr borrowed_subrange_t<_Range>
  1356. operator()(_Range&& __r, iterator_t<_Range> __middle) const
  1357. {
  1358. return (*this)(ranges::begin(__r), std::move(__middle),
  1359. ranges::end(__r));
  1360. }
  1361. };
  1362. inline constexpr __rotate_fn rotate{};
  1363. template<typename _Iter, typename _Out>
  1364. using rotate_copy_result = in_out_result<_Iter, _Out>;
  1365. struct __rotate_copy_fn
  1366. {
  1367. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  1368. weakly_incrementable _Out>
  1369. requires indirectly_copyable<_Iter, _Out>
  1370. constexpr rotate_copy_result<_Iter, _Out>
  1371. operator()(_Iter __first, _Iter __middle, _Sent __last,
  1372. _Out __result) const
  1373. {
  1374. auto __copy1 = ranges::copy(__middle,
  1375. std::move(__last),
  1376. std::move(__result));
  1377. auto __copy2 = ranges::copy(std::move(__first),
  1378. std::move(__middle),
  1379. std::move(__copy1.out));
  1380. return { std::move(__copy1.in), std::move(__copy2.out) };
  1381. }
  1382. template<forward_range _Range, weakly_incrementable _Out>
  1383. requires indirectly_copyable<iterator_t<_Range>, _Out>
  1384. constexpr rotate_copy_result<borrowed_iterator_t<_Range>, _Out>
  1385. operator()(_Range&& __r, iterator_t<_Range> __middle, _Out __result) const
  1386. {
  1387. return (*this)(ranges::begin(__r), std::move(__middle),
  1388. ranges::end(__r), std::move(__result));
  1389. }
  1390. };
  1391. inline constexpr __rotate_copy_fn rotate_copy{};
  1392. struct __sample_fn
  1393. {
  1394. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  1395. weakly_incrementable _Out, typename _Gen>
  1396. requires (forward_iterator<_Iter> || random_access_iterator<_Out>)
  1397. && indirectly_copyable<_Iter, _Out>
  1398. && uniform_random_bit_generator<remove_reference_t<_Gen>>
  1399. _Out
  1400. operator()(_Iter __first, _Sent __last, _Out __out,
  1401. iter_difference_t<_Iter> __n, _Gen&& __g) const
  1402. {
  1403. if constexpr (forward_iterator<_Iter>)
  1404. {
  1405. // FIXME: Forwarding to std::sample here requires computing __lasti
  1406. // which may take linear time.
  1407. auto __lasti = ranges::next(__first, __last);
  1408. return _GLIBCXX_STD_A::
  1409. sample(std::move(__first), std::move(__lasti), std::move(__out),
  1410. __n, std::forward<_Gen>(__g));
  1411. }
  1412. else
  1413. {
  1414. using __distrib_type
  1415. = uniform_int_distribution<iter_difference_t<_Iter>>;
  1416. using __param_type = typename __distrib_type::param_type;
  1417. __distrib_type __d{};
  1418. iter_difference_t<_Iter> __sample_sz = 0;
  1419. while (__first != __last && __sample_sz != __n)
  1420. {
  1421. __out[__sample_sz++] = *__first;
  1422. ++__first;
  1423. }
  1424. for (auto __pop_sz = __sample_sz; __first != __last;
  1425. ++__first, (void) ++__pop_sz)
  1426. {
  1427. const auto __k = __d(__g, __param_type{0, __pop_sz});
  1428. if (__k < __n)
  1429. __out[__k] = *__first;
  1430. }
  1431. return __out + __sample_sz;
  1432. }
  1433. }
  1434. template<input_range _Range, weakly_incrementable _Out, typename _Gen>
  1435. requires (forward_range<_Range> || random_access_iterator<_Out>)
  1436. && indirectly_copyable<iterator_t<_Range>, _Out>
  1437. && uniform_random_bit_generator<remove_reference_t<_Gen>>
  1438. _Out
  1439. operator()(_Range&& __r, _Out __out,
  1440. range_difference_t<_Range> __n, _Gen&& __g) const
  1441. {
  1442. return (*this)(ranges::begin(__r), ranges::end(__r),
  1443. std::move(__out), __n,
  1444. std::forward<_Gen>(__g));
  1445. }
  1446. };
  1447. inline constexpr __sample_fn sample{};
  1448. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  1449. struct __shuffle_fn
  1450. {
  1451. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1452. typename _Gen>
  1453. requires permutable<_Iter>
  1454. && uniform_random_bit_generator<remove_reference_t<_Gen>>
  1455. _Iter
  1456. operator()(_Iter __first, _Sent __last, _Gen&& __g) const
  1457. {
  1458. auto __lasti = ranges::next(__first, __last);
  1459. std::shuffle(std::move(__first), __lasti, std::forward<_Gen>(__g));
  1460. return __lasti;
  1461. }
  1462. template<random_access_range _Range, typename _Gen>
  1463. requires permutable<iterator_t<_Range>>
  1464. && uniform_random_bit_generator<remove_reference_t<_Gen>>
  1465. borrowed_iterator_t<_Range>
  1466. operator()(_Range&& __r, _Gen&& __g) const
  1467. {
  1468. return (*this)(ranges::begin(__r), ranges::end(__r),
  1469. std::forward<_Gen>(__g));
  1470. }
  1471. };
  1472. inline constexpr __shuffle_fn shuffle{};
  1473. #endif
  1474. struct __push_heap_fn
  1475. {
  1476. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1477. typename _Comp = ranges::less, typename _Proj = identity>
  1478. requires sortable<_Iter, _Comp, _Proj>
  1479. constexpr _Iter
  1480. operator()(_Iter __first, _Sent __last,
  1481. _Comp __comp = {}, _Proj __proj = {}) const
  1482. {
  1483. auto __lasti = ranges::next(__first, __last);
  1484. std::push_heap(__first, __lasti,
  1485. __detail::__make_comp_proj(__comp, __proj));
  1486. return __lasti;
  1487. }
  1488. template<random_access_range _Range,
  1489. typename _Comp = ranges::less, typename _Proj = identity>
  1490. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1491. constexpr borrowed_iterator_t<_Range>
  1492. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1493. {
  1494. return (*this)(ranges::begin(__r), ranges::end(__r),
  1495. std::move(__comp), std::move(__proj));
  1496. }
  1497. };
  1498. inline constexpr __push_heap_fn push_heap{};
  1499. struct __pop_heap_fn
  1500. {
  1501. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1502. typename _Comp = ranges::less, typename _Proj = identity>
  1503. requires sortable<_Iter, _Comp, _Proj>
  1504. constexpr _Iter
  1505. operator()(_Iter __first, _Sent __last,
  1506. _Comp __comp = {}, _Proj __proj = {}) const
  1507. {
  1508. auto __lasti = ranges::next(__first, __last);
  1509. std::pop_heap(__first, __lasti,
  1510. __detail::__make_comp_proj(__comp, __proj));
  1511. return __lasti;
  1512. }
  1513. template<random_access_range _Range,
  1514. typename _Comp = ranges::less, typename _Proj = identity>
  1515. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1516. constexpr borrowed_iterator_t<_Range>
  1517. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1518. {
  1519. return (*this)(ranges::begin(__r), ranges::end(__r),
  1520. std::move(__comp), std::move(__proj));
  1521. }
  1522. };
  1523. inline constexpr __pop_heap_fn pop_heap{};
  1524. struct __make_heap_fn
  1525. {
  1526. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1527. typename _Comp = ranges::less, typename _Proj = identity>
  1528. requires sortable<_Iter, _Comp, _Proj>
  1529. constexpr _Iter
  1530. operator()(_Iter __first, _Sent __last,
  1531. _Comp __comp = {}, _Proj __proj = {}) const
  1532. {
  1533. auto __lasti = ranges::next(__first, __last);
  1534. std::make_heap(__first, __lasti,
  1535. __detail::__make_comp_proj(__comp, __proj));
  1536. return __lasti;
  1537. }
  1538. template<random_access_range _Range,
  1539. typename _Comp = ranges::less, typename _Proj = identity>
  1540. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1541. constexpr borrowed_iterator_t<_Range>
  1542. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1543. {
  1544. return (*this)(ranges::begin(__r), ranges::end(__r),
  1545. std::move(__comp), std::move(__proj));
  1546. }
  1547. };
  1548. inline constexpr __make_heap_fn make_heap{};
  1549. struct __sort_heap_fn
  1550. {
  1551. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1552. typename _Comp = ranges::less, typename _Proj = identity>
  1553. requires sortable<_Iter, _Comp, _Proj>
  1554. constexpr _Iter
  1555. operator()(_Iter __first, _Sent __last,
  1556. _Comp __comp = {}, _Proj __proj = {}) const
  1557. {
  1558. auto __lasti = ranges::next(__first, __last);
  1559. std::sort_heap(__first, __lasti,
  1560. __detail::__make_comp_proj(__comp, __proj));
  1561. return __lasti;
  1562. }
  1563. template<random_access_range _Range,
  1564. typename _Comp = ranges::less, typename _Proj = identity>
  1565. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1566. constexpr borrowed_iterator_t<_Range>
  1567. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1568. {
  1569. return (*this)(ranges::begin(__r), ranges::end(__r),
  1570. std::move(__comp), std::move(__proj));
  1571. }
  1572. };
  1573. inline constexpr __sort_heap_fn sort_heap{};
  1574. struct __is_heap_until_fn
  1575. {
  1576. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1577. typename _Proj = identity,
  1578. indirect_strict_weak_order<projected<_Iter, _Proj>>
  1579. _Comp = ranges::less>
  1580. constexpr _Iter
  1581. operator()(_Iter __first, _Sent __last,
  1582. _Comp __comp = {}, _Proj __proj = {}) const
  1583. {
  1584. iter_difference_t<_Iter> __n = ranges::distance(__first, __last);
  1585. iter_difference_t<_Iter> __parent = 0, __child = 1;
  1586. for (; __child < __n; ++__child)
  1587. if (std::__invoke(__comp,
  1588. std::__invoke(__proj, *(__first + __parent)),
  1589. std::__invoke(__proj, *(__first + __child))))
  1590. return __first + __child;
  1591. else if ((__child & 1) == 0)
  1592. ++__parent;
  1593. return __first + __n;
  1594. }
  1595. template<random_access_range _Range,
  1596. typename _Proj = identity,
  1597. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  1598. _Comp = ranges::less>
  1599. constexpr borrowed_iterator_t<_Range>
  1600. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1601. {
  1602. return (*this)(ranges::begin(__r), ranges::end(__r),
  1603. std::move(__comp), std::move(__proj));
  1604. }
  1605. };
  1606. inline constexpr __is_heap_until_fn is_heap_until{};
  1607. struct __is_heap_fn
  1608. {
  1609. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1610. typename _Proj = identity,
  1611. indirect_strict_weak_order<projected<_Iter, _Proj>>
  1612. _Comp = ranges::less>
  1613. constexpr bool
  1614. operator()(_Iter __first, _Sent __last,
  1615. _Comp __comp = {}, _Proj __proj = {}) const
  1616. {
  1617. return (__last
  1618. == ranges::is_heap_until(__first, __last,
  1619. std::move(__comp),
  1620. std::move(__proj)));
  1621. }
  1622. template<random_access_range _Range,
  1623. typename _Proj = identity,
  1624. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  1625. _Comp = ranges::less>
  1626. constexpr bool
  1627. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1628. {
  1629. return (*this)(ranges::begin(__r), ranges::end(__r),
  1630. std::move(__comp), std::move(__proj));
  1631. }
  1632. };
  1633. inline constexpr __is_heap_fn is_heap{};
  1634. struct __sort_fn
  1635. {
  1636. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1637. typename _Comp = ranges::less, typename _Proj = identity>
  1638. requires sortable<_Iter, _Comp, _Proj>
  1639. constexpr _Iter
  1640. operator()(_Iter __first, _Sent __last,
  1641. _Comp __comp = {}, _Proj __proj = {}) const
  1642. {
  1643. auto __lasti = ranges::next(__first, __last);
  1644. _GLIBCXX_STD_A::sort(std::move(__first), __lasti,
  1645. __detail::__make_comp_proj(__comp, __proj));
  1646. return __lasti;
  1647. }
  1648. template<random_access_range _Range,
  1649. typename _Comp = ranges::less, typename _Proj = identity>
  1650. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1651. constexpr borrowed_iterator_t<_Range>
  1652. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1653. {
  1654. return (*this)(ranges::begin(__r), ranges::end(__r),
  1655. std::move(__comp), std::move(__proj));
  1656. }
  1657. };
  1658. inline constexpr __sort_fn sort{};
  1659. struct __stable_sort_fn
  1660. {
  1661. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1662. typename _Comp = ranges::less, typename _Proj = identity>
  1663. requires sortable<_Iter, _Comp, _Proj>
  1664. _Iter
  1665. operator()(_Iter __first, _Sent __last,
  1666. _Comp __comp = {}, _Proj __proj = {}) const
  1667. {
  1668. auto __lasti = ranges::next(__first, __last);
  1669. std::stable_sort(std::move(__first), __lasti,
  1670. __detail::__make_comp_proj(__comp, __proj));
  1671. return __lasti;
  1672. }
  1673. template<random_access_range _Range,
  1674. typename _Comp = ranges::less, typename _Proj = identity>
  1675. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1676. borrowed_iterator_t<_Range>
  1677. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1678. {
  1679. return (*this)(ranges::begin(__r), ranges::end(__r),
  1680. std::move(__comp), std::move(__proj));
  1681. }
  1682. };
  1683. inline constexpr __stable_sort_fn stable_sort{};
  1684. struct __partial_sort_fn
  1685. {
  1686. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1687. typename _Comp = ranges::less, typename _Proj = identity>
  1688. requires sortable<_Iter, _Comp, _Proj>
  1689. constexpr _Iter
  1690. operator()(_Iter __first, _Iter __middle, _Sent __last,
  1691. _Comp __comp = {}, _Proj __proj = {}) const
  1692. {
  1693. if (__first == __middle)
  1694. return ranges::next(__first, __last);
  1695. ranges::make_heap(__first, __middle, __comp, __proj);
  1696. auto __i = __middle;
  1697. for (; __i != __last; ++__i)
  1698. if (std::__invoke(__comp,
  1699. std::__invoke(__proj, *__i),
  1700. std::__invoke(__proj, *__first)))
  1701. {
  1702. ranges::pop_heap(__first, __middle, __comp, __proj);
  1703. ranges::iter_swap(__middle-1, __i);
  1704. ranges::push_heap(__first, __middle, __comp, __proj);
  1705. }
  1706. ranges::sort_heap(__first, __middle, __comp, __proj);
  1707. return __i;
  1708. }
  1709. template<random_access_range _Range,
  1710. typename _Comp = ranges::less, typename _Proj = identity>
  1711. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1712. constexpr borrowed_iterator_t<_Range>
  1713. operator()(_Range&& __r, iterator_t<_Range> __middle,
  1714. _Comp __comp = {}, _Proj __proj = {}) const
  1715. {
  1716. return (*this)(ranges::begin(__r), std::move(__middle),
  1717. ranges::end(__r),
  1718. std::move(__comp), std::move(__proj));
  1719. }
  1720. };
  1721. inline constexpr __partial_sort_fn partial_sort{};
  1722. template<typename _Iter, typename _Out>
  1723. using partial_sort_copy_result = in_out_result<_Iter, _Out>;
  1724. struct __partial_sort_copy_fn
  1725. {
  1726. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  1727. random_access_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  1728. typename _Comp = ranges::less,
  1729. typename _Proj1 = identity, typename _Proj2 = identity>
  1730. requires indirectly_copyable<_Iter1, _Iter2>
  1731. && sortable<_Iter2, _Comp, _Proj2>
  1732. && indirect_strict_weak_order<_Comp,
  1733. projected<_Iter1, _Proj1>,
  1734. projected<_Iter2, _Proj2>>
  1735. constexpr partial_sort_copy_result<_Iter1, _Iter2>
  1736. operator()(_Iter1 __first, _Sent1 __last,
  1737. _Iter2 __result_first, _Sent2 __result_last,
  1738. _Comp __comp = {},
  1739. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  1740. {
  1741. if (__result_first == __result_last)
  1742. {
  1743. // TODO: Eliminating the variable __lasti triggers an ICE.
  1744. auto __lasti = ranges::next(std::move(__first),
  1745. std::move(__last));
  1746. return {std::move(__lasti), std::move(__result_first)};
  1747. }
  1748. auto __result_real_last = __result_first;
  1749. while (__first != __last && __result_real_last != __result_last)
  1750. {
  1751. *__result_real_last = *__first;
  1752. ++__result_real_last;
  1753. ++__first;
  1754. }
  1755. ranges::make_heap(__result_first, __result_real_last, __comp, __proj2);
  1756. for (; __first != __last; ++__first)
  1757. if (std::__invoke(__comp,
  1758. std::__invoke(__proj1, *__first),
  1759. std::__invoke(__proj2, *__result_first)))
  1760. {
  1761. ranges::pop_heap(__result_first, __result_real_last,
  1762. __comp, __proj2);
  1763. *(__result_real_last-1) = *__first;
  1764. ranges::push_heap(__result_first, __result_real_last,
  1765. __comp, __proj2);
  1766. }
  1767. ranges::sort_heap(__result_first, __result_real_last, __comp, __proj2);
  1768. return {std::move(__first), std::move(__result_real_last)};
  1769. }
  1770. template<input_range _Range1, random_access_range _Range2,
  1771. typename _Comp = ranges::less,
  1772. typename _Proj1 = identity, typename _Proj2 = identity>
  1773. requires indirectly_copyable<iterator_t<_Range1>, iterator_t<_Range2>>
  1774. && sortable<iterator_t<_Range2>, _Comp, _Proj2>
  1775. && indirect_strict_weak_order<_Comp,
  1776. projected<iterator_t<_Range1>, _Proj1>,
  1777. projected<iterator_t<_Range2>, _Proj2>>
  1778. constexpr partial_sort_copy_result<borrowed_iterator_t<_Range1>,
  1779. borrowed_iterator_t<_Range2>>
  1780. operator()(_Range1&& __r, _Range2&& __out, _Comp __comp = {},
  1781. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  1782. {
  1783. return (*this)(ranges::begin(__r), ranges::end(__r),
  1784. ranges::begin(__out), ranges::end(__out),
  1785. std::move(__comp),
  1786. std::move(__proj1), std::move(__proj2));
  1787. }
  1788. };
  1789. inline constexpr __partial_sort_copy_fn partial_sort_copy{};
  1790. struct __is_sorted_until_fn
  1791. {
  1792. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  1793. typename _Proj = identity,
  1794. indirect_strict_weak_order<projected<_Iter, _Proj>>
  1795. _Comp = ranges::less>
  1796. constexpr _Iter
  1797. operator()(_Iter __first, _Sent __last,
  1798. _Comp __comp = {}, _Proj __proj = {}) const
  1799. {
  1800. if (__first == __last)
  1801. return __first;
  1802. auto __next = __first;
  1803. for (++__next; __next != __last; __first = __next, (void)++__next)
  1804. if (std::__invoke(__comp,
  1805. std::__invoke(__proj, *__next),
  1806. std::__invoke(__proj, *__first)))
  1807. return __next;
  1808. return __next;
  1809. }
  1810. template<forward_range _Range, typename _Proj = identity,
  1811. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  1812. _Comp = ranges::less>
  1813. constexpr borrowed_iterator_t<_Range>
  1814. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1815. {
  1816. return (*this)(ranges::begin(__r), ranges::end(__r),
  1817. std::move(__comp), std::move(__proj));
  1818. }
  1819. };
  1820. inline constexpr __is_sorted_until_fn is_sorted_until{};
  1821. struct __is_sorted_fn
  1822. {
  1823. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  1824. typename _Proj = identity,
  1825. indirect_strict_weak_order<projected<_Iter, _Proj>>
  1826. _Comp = ranges::less>
  1827. constexpr bool
  1828. operator()(_Iter __first, _Sent __last,
  1829. _Comp __comp = {}, _Proj __proj = {}) const
  1830. {
  1831. if (__first == __last)
  1832. return true;
  1833. auto __next = __first;
  1834. for (++__next; __next != __last; __first = __next, (void)++__next)
  1835. if (std::__invoke(__comp,
  1836. std::__invoke(__proj, *__next),
  1837. std::__invoke(__proj, *__first)))
  1838. return false;
  1839. return true;
  1840. }
  1841. template<forward_range _Range, typename _Proj = identity,
  1842. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  1843. _Comp = ranges::less>
  1844. constexpr bool
  1845. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  1846. {
  1847. return (*this)(ranges::begin(__r), ranges::end(__r),
  1848. std::move(__comp), std::move(__proj));
  1849. }
  1850. };
  1851. inline constexpr __is_sorted_fn is_sorted{};
  1852. struct __nth_element_fn
  1853. {
  1854. template<random_access_iterator _Iter, sentinel_for<_Iter> _Sent,
  1855. typename _Comp = ranges::less, typename _Proj = identity>
  1856. requires sortable<_Iter, _Comp, _Proj>
  1857. constexpr _Iter
  1858. operator()(_Iter __first, _Iter __nth, _Sent __last,
  1859. _Comp __comp = {}, _Proj __proj = {}) const
  1860. {
  1861. auto __lasti = ranges::next(__first, __last);
  1862. _GLIBCXX_STD_A::nth_element(std::move(__first), std::move(__nth),
  1863. __lasti,
  1864. __detail::__make_comp_proj(__comp, __proj));
  1865. return __lasti;
  1866. }
  1867. template<random_access_range _Range,
  1868. typename _Comp = ranges::less, typename _Proj = identity>
  1869. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  1870. constexpr borrowed_iterator_t<_Range>
  1871. operator()(_Range&& __r, iterator_t<_Range> __nth,
  1872. _Comp __comp = {}, _Proj __proj = {}) const
  1873. {
  1874. return (*this)(ranges::begin(__r), std::move(__nth),
  1875. ranges::end(__r), std::move(__comp), std::move(__proj));
  1876. }
  1877. };
  1878. inline constexpr __nth_element_fn nth_element{};
  1879. struct __lower_bound_fn
  1880. {
  1881. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  1882. typename _Tp, typename _Proj = identity,
  1883. indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
  1884. _Comp = ranges::less>
  1885. constexpr _Iter
  1886. operator()(_Iter __first, _Sent __last,
  1887. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  1888. {
  1889. auto __len = ranges::distance(__first, __last);
  1890. while (__len > 0)
  1891. {
  1892. auto __half = __len / 2;
  1893. auto __middle = __first;
  1894. ranges::advance(__middle, __half);
  1895. if (std::__invoke(__comp, std::__invoke(__proj, *__middle), __value))
  1896. {
  1897. __first = __middle;
  1898. ++__first;
  1899. __len = __len - __half - 1;
  1900. }
  1901. else
  1902. __len = __half;
  1903. }
  1904. return __first;
  1905. }
  1906. template<forward_range _Range, typename _Tp, typename _Proj = identity,
  1907. indirect_strict_weak_order<const _Tp*,
  1908. projected<iterator_t<_Range>, _Proj>>
  1909. _Comp = ranges::less>
  1910. constexpr borrowed_iterator_t<_Range>
  1911. operator()(_Range&& __r,
  1912. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  1913. {
  1914. return (*this)(ranges::begin(__r), ranges::end(__r),
  1915. __value, std::move(__comp), std::move(__proj));
  1916. }
  1917. };
  1918. inline constexpr __lower_bound_fn lower_bound{};
  1919. struct __upper_bound_fn
  1920. {
  1921. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  1922. typename _Tp, typename _Proj = identity,
  1923. indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
  1924. _Comp = ranges::less>
  1925. constexpr _Iter
  1926. operator()(_Iter __first, _Sent __last,
  1927. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  1928. {
  1929. auto __len = ranges::distance(__first, __last);
  1930. while (__len > 0)
  1931. {
  1932. auto __half = __len / 2;
  1933. auto __middle = __first;
  1934. ranges::advance(__middle, __half);
  1935. if (std::__invoke(__comp, __value, std::__invoke(__proj, *__middle)))
  1936. __len = __half;
  1937. else
  1938. {
  1939. __first = __middle;
  1940. ++__first;
  1941. __len = __len - __half - 1;
  1942. }
  1943. }
  1944. return __first;
  1945. }
  1946. template<forward_range _Range, typename _Tp, typename _Proj = identity,
  1947. indirect_strict_weak_order<const _Tp*,
  1948. projected<iterator_t<_Range>, _Proj>>
  1949. _Comp = ranges::less>
  1950. constexpr borrowed_iterator_t<_Range>
  1951. operator()(_Range&& __r,
  1952. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  1953. {
  1954. return (*this)(ranges::begin(__r), ranges::end(__r),
  1955. __value, std::move(__comp), std::move(__proj));
  1956. }
  1957. };
  1958. inline constexpr __upper_bound_fn upper_bound{};
  1959. struct __equal_range_fn
  1960. {
  1961. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  1962. typename _Tp, typename _Proj = identity,
  1963. indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
  1964. _Comp = ranges::less>
  1965. constexpr subrange<_Iter>
  1966. operator()(_Iter __first, _Sent __last,
  1967. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  1968. {
  1969. auto __len = ranges::distance(__first, __last);
  1970. while (__len > 0)
  1971. {
  1972. auto __half = __len / 2;
  1973. auto __middle = __first;
  1974. ranges::advance(__middle, __half);
  1975. if (std::__invoke(__comp,
  1976. std::__invoke(__proj, *__middle),
  1977. __value))
  1978. {
  1979. __first = __middle;
  1980. ++__first;
  1981. __len = __len - __half - 1;
  1982. }
  1983. else if (std::__invoke(__comp,
  1984. __value,
  1985. std::__invoke(__proj, *__middle)))
  1986. __len = __half;
  1987. else
  1988. {
  1989. auto __left
  1990. = ranges::lower_bound(__first, __middle,
  1991. __value, __comp, __proj);
  1992. ranges::advance(__first, __len);
  1993. auto __right
  1994. = ranges::upper_bound(++__middle, __first,
  1995. __value, __comp, __proj);
  1996. return {__left, __right};
  1997. }
  1998. }
  1999. return {__first, __first};
  2000. }
  2001. template<forward_range _Range,
  2002. typename _Tp, typename _Proj = identity,
  2003. indirect_strict_weak_order<const _Tp*,
  2004. projected<iterator_t<_Range>, _Proj>>
  2005. _Comp = ranges::less>
  2006. constexpr borrowed_subrange_t<_Range>
  2007. operator()(_Range&& __r, const _Tp& __value,
  2008. _Comp __comp = {}, _Proj __proj = {}) const
  2009. {
  2010. return (*this)(ranges::begin(__r), ranges::end(__r),
  2011. __value, std::move(__comp), std::move(__proj));
  2012. }
  2013. };
  2014. inline constexpr __equal_range_fn equal_range{};
  2015. struct __binary_search_fn
  2016. {
  2017. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2018. typename _Tp, typename _Proj = identity,
  2019. indirect_strict_weak_order<const _Tp*, projected<_Iter, _Proj>>
  2020. _Comp = ranges::less>
  2021. constexpr bool
  2022. operator()(_Iter __first, _Sent __last,
  2023. const _Tp& __value, _Comp __comp = {}, _Proj __proj = {}) const
  2024. {
  2025. auto __i = ranges::lower_bound(__first, __last, __value, __comp, __proj);
  2026. if (__i == __last)
  2027. return false;
  2028. return !(bool)std::__invoke(__comp, __value,
  2029. std::__invoke(__proj, *__i));
  2030. }
  2031. template<forward_range _Range,
  2032. typename _Tp, typename _Proj = identity,
  2033. indirect_strict_weak_order<const _Tp*,
  2034. projected<iterator_t<_Range>, _Proj>>
  2035. _Comp = ranges::less>
  2036. constexpr bool
  2037. operator()(_Range&& __r, const _Tp& __value, _Comp __comp = {},
  2038. _Proj __proj = {}) const
  2039. {
  2040. return (*this)(ranges::begin(__r), ranges::end(__r),
  2041. __value, std::move(__comp), std::move(__proj));
  2042. }
  2043. };
  2044. inline constexpr __binary_search_fn binary_search{};
  2045. struct __is_partitioned_fn
  2046. {
  2047. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  2048. typename _Proj = identity,
  2049. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2050. constexpr bool
  2051. operator()(_Iter __first, _Sent __last,
  2052. _Pred __pred, _Proj __proj = {}) const
  2053. {
  2054. __first = ranges::find_if_not(std::move(__first), __last,
  2055. __pred, __proj);
  2056. if (__first == __last)
  2057. return true;
  2058. ++__first;
  2059. return ranges::none_of(std::move(__first), std::move(__last),
  2060. std::move(__pred), std::move(__proj));
  2061. }
  2062. template<input_range _Range, typename _Proj = identity,
  2063. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2064. _Pred>
  2065. constexpr bool
  2066. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  2067. {
  2068. return (*this)(ranges::begin(__r), ranges::end(__r),
  2069. std::move(__pred), std::move(__proj));
  2070. }
  2071. };
  2072. inline constexpr __is_partitioned_fn is_partitioned{};
  2073. struct __partition_fn
  2074. {
  2075. template<permutable _Iter, sentinel_for<_Iter> _Sent,
  2076. typename _Proj = identity,
  2077. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2078. constexpr subrange<_Iter>
  2079. operator()(_Iter __first, _Sent __last,
  2080. _Pred __pred, _Proj __proj = {}) const
  2081. {
  2082. if constexpr (bidirectional_iterator<_Iter>)
  2083. {
  2084. auto __lasti = ranges::next(__first, __last);
  2085. auto __tail = __lasti;
  2086. for (;;)
  2087. {
  2088. for (;;)
  2089. if (__first == __tail)
  2090. return {std::move(__first), std::move(__lasti)};
  2091. else if (std::__invoke(__pred,
  2092. std::__invoke(__proj, *__first)))
  2093. ++__first;
  2094. else
  2095. break;
  2096. --__tail;
  2097. for (;;)
  2098. if (__first == __tail)
  2099. return {std::move(__first), std::move(__lasti)};
  2100. else if (!(bool)std::__invoke(__pred,
  2101. std::__invoke(__proj, *__tail)))
  2102. --__tail;
  2103. else
  2104. break;
  2105. ranges::iter_swap(__first, __tail);
  2106. ++__first;
  2107. }
  2108. }
  2109. else
  2110. {
  2111. if (__first == __last)
  2112. return {__first, __first};
  2113. while (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  2114. if (++__first == __last)
  2115. return {__first, __first};
  2116. auto __next = __first;
  2117. while (++__next != __last)
  2118. if (std::__invoke(__pred, std::__invoke(__proj, *__next)))
  2119. {
  2120. ranges::iter_swap(__first, __next);
  2121. ++__first;
  2122. }
  2123. return {std::move(__first), std::move(__next)};
  2124. }
  2125. }
  2126. template<forward_range _Range, typename _Proj = identity,
  2127. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2128. _Pred>
  2129. requires permutable<iterator_t<_Range>>
  2130. constexpr borrowed_subrange_t<_Range>
  2131. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  2132. {
  2133. return (*this)(ranges::begin(__r), ranges::end(__r),
  2134. std::move(__pred), std::move(__proj));
  2135. }
  2136. };
  2137. inline constexpr __partition_fn partition{};
  2138. struct __stable_partition_fn
  2139. {
  2140. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  2141. typename _Proj = identity,
  2142. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2143. requires permutable<_Iter>
  2144. subrange<_Iter>
  2145. operator()(_Iter __first, _Sent __last,
  2146. _Pred __pred, _Proj __proj = {}) const
  2147. {
  2148. auto __lasti = ranges::next(__first, __last);
  2149. auto __middle
  2150. = std::stable_partition(std::move(__first), __lasti,
  2151. __detail::__make_pred_proj(__pred, __proj));
  2152. return {std::move(__middle), std::move(__lasti)};
  2153. }
  2154. template<bidirectional_range _Range, typename _Proj = identity,
  2155. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2156. _Pred>
  2157. requires permutable<iterator_t<_Range>>
  2158. borrowed_subrange_t<_Range>
  2159. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  2160. {
  2161. return (*this)(ranges::begin(__r), ranges::end(__r),
  2162. std::move(__pred), std::move(__proj));
  2163. }
  2164. };
  2165. inline constexpr __stable_partition_fn stable_partition{};
  2166. template<typename _Iter, typename _Out1, typename _Out2>
  2167. struct in_out_out_result
  2168. {
  2169. [[no_unique_address]] _Iter in;
  2170. [[no_unique_address]] _Out1 out1;
  2171. [[no_unique_address]] _Out2 out2;
  2172. template<typename _IIter, typename _OOut1, typename _OOut2>
  2173. requires convertible_to<const _Iter&, _IIter>
  2174. && convertible_to<const _Out1&, _OOut1>
  2175. && convertible_to<const _Out2&, _OOut2>
  2176. constexpr
  2177. operator in_out_out_result<_IIter, _OOut1, _OOut2>() const &
  2178. { return {in, out1, out2}; }
  2179. template<typename _IIter, typename _OOut1, typename _OOut2>
  2180. requires convertible_to<_Iter, _IIter>
  2181. && convertible_to<_Out1, _OOut1>
  2182. && convertible_to<_Out2, _OOut2>
  2183. constexpr
  2184. operator in_out_out_result<_IIter, _OOut1, _OOut2>() &&
  2185. { return {std::move(in), std::move(out1), std::move(out2)}; }
  2186. };
  2187. template<typename _Iter, typename _Out1, typename _Out2>
  2188. using partition_copy_result = in_out_out_result<_Iter, _Out1, _Out2>;
  2189. struct __partition_copy_fn
  2190. {
  2191. template<input_iterator _Iter, sentinel_for<_Iter> _Sent,
  2192. weakly_incrementable _Out1, weakly_incrementable _Out2,
  2193. typename _Proj = identity,
  2194. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2195. requires indirectly_copyable<_Iter, _Out1>
  2196. && indirectly_copyable<_Iter, _Out2>
  2197. constexpr partition_copy_result<_Iter, _Out1, _Out2>
  2198. operator()(_Iter __first, _Sent __last,
  2199. _Out1 __out_true, _Out2 __out_false,
  2200. _Pred __pred, _Proj __proj = {}) const
  2201. {
  2202. for (; __first != __last; ++__first)
  2203. if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
  2204. {
  2205. *__out_true = *__first;
  2206. ++__out_true;
  2207. }
  2208. else
  2209. {
  2210. *__out_false = *__first;
  2211. ++__out_false;
  2212. }
  2213. return {std::move(__first),
  2214. std::move(__out_true), std::move(__out_false)};
  2215. }
  2216. template<input_range _Range, weakly_incrementable _Out1,
  2217. weakly_incrementable _Out2,
  2218. typename _Proj = identity,
  2219. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2220. _Pred>
  2221. requires indirectly_copyable<iterator_t<_Range>, _Out1>
  2222. && indirectly_copyable<iterator_t<_Range>, _Out2>
  2223. constexpr partition_copy_result<borrowed_iterator_t<_Range>, _Out1, _Out2>
  2224. operator()(_Range&& __r, _Out1 __out_true, _Out2 __out_false,
  2225. _Pred __pred, _Proj __proj = {}) const
  2226. {
  2227. return (*this)(ranges::begin(__r), ranges::end(__r),
  2228. std::move(__out_true), std::move(__out_false),
  2229. std::move(__pred), std::move(__proj));
  2230. }
  2231. };
  2232. inline constexpr __partition_copy_fn partition_copy{};
  2233. struct __partition_point_fn
  2234. {
  2235. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2236. typename _Proj = identity,
  2237. indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
  2238. constexpr _Iter
  2239. operator()(_Iter __first, _Sent __last,
  2240. _Pred __pred, _Proj __proj = {}) const
  2241. {
  2242. auto __len = ranges::distance(__first, __last);
  2243. while (__len > 0)
  2244. {
  2245. auto __half = __len / 2;
  2246. auto __middle = __first;
  2247. ranges::advance(__middle, __half);
  2248. if (std::__invoke(__pred, std::__invoke(__proj, *__middle)))
  2249. {
  2250. __first = __middle;
  2251. ++__first;
  2252. __len = __len - __half - 1;
  2253. }
  2254. else
  2255. __len = __half;
  2256. }
  2257. return __first;
  2258. }
  2259. template<forward_range _Range, typename _Proj = identity,
  2260. indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>>
  2261. _Pred>
  2262. constexpr borrowed_iterator_t<_Range>
  2263. operator()(_Range&& __r, _Pred __pred, _Proj __proj = {}) const
  2264. {
  2265. return (*this)(ranges::begin(__r), ranges::end(__r),
  2266. std::move(__pred), std::move(__proj));
  2267. }
  2268. };
  2269. inline constexpr __partition_point_fn partition_point{};
  2270. template<typename _Iter1, typename _Iter2, typename _Out>
  2271. using merge_result = in_in_out_result<_Iter1, _Iter2, _Out>;
  2272. struct __merge_fn
  2273. {
  2274. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2275. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2276. weakly_incrementable _Out, typename _Comp = ranges::less,
  2277. typename _Proj1 = identity, typename _Proj2 = identity>
  2278. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2279. constexpr merge_result<_Iter1, _Iter2, _Out>
  2280. operator()(_Iter1 __first1, _Sent1 __last1,
  2281. _Iter2 __first2, _Sent2 __last2, _Out __result,
  2282. _Comp __comp = {},
  2283. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2284. {
  2285. while (__first1 != __last1 && __first2 != __last2)
  2286. {
  2287. if (std::__invoke(__comp,
  2288. std::__invoke(__proj2, *__first2),
  2289. std::__invoke(__proj1, *__first1)))
  2290. {
  2291. *__result = *__first2;
  2292. ++__first2;
  2293. }
  2294. else
  2295. {
  2296. *__result = *__first1;
  2297. ++__first1;
  2298. }
  2299. ++__result;
  2300. }
  2301. auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
  2302. std::move(__result));
  2303. auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
  2304. std::move(__copy1.out));
  2305. return { std::move(__copy1.in), std::move(__copy2.in),
  2306. std::move(__copy2.out) };
  2307. }
  2308. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2309. typename _Comp = ranges::less,
  2310. typename _Proj1 = identity, typename _Proj2 = identity>
  2311. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2312. _Comp, _Proj1, _Proj2>
  2313. constexpr merge_result<borrowed_iterator_t<_Range1>,
  2314. borrowed_iterator_t<_Range2>,
  2315. _Out>
  2316. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
  2317. _Comp __comp = {},
  2318. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2319. {
  2320. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2321. ranges::begin(__r2), ranges::end(__r2),
  2322. std::move(__result), std::move(__comp),
  2323. std::move(__proj1), std::move(__proj2));
  2324. }
  2325. };
  2326. inline constexpr __merge_fn merge{};
  2327. struct __inplace_merge_fn
  2328. {
  2329. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  2330. typename _Comp = ranges::less,
  2331. typename _Proj = identity>
  2332. requires sortable<_Iter, _Comp, _Proj>
  2333. _Iter
  2334. operator()(_Iter __first, _Iter __middle, _Sent __last,
  2335. _Comp __comp = {}, _Proj __proj = {}) const
  2336. {
  2337. auto __lasti = ranges::next(__first, __last);
  2338. std::inplace_merge(std::move(__first), std::move(__middle), __lasti,
  2339. __detail::__make_comp_proj(__comp, __proj));
  2340. return __lasti;
  2341. }
  2342. template<bidirectional_range _Range,
  2343. typename _Comp = ranges::less, typename _Proj = identity>
  2344. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  2345. borrowed_iterator_t<_Range>
  2346. operator()(_Range&& __r, iterator_t<_Range> __middle,
  2347. _Comp __comp = {}, _Proj __proj = {}) const
  2348. {
  2349. return (*this)(ranges::begin(__r), std::move(__middle),
  2350. ranges::end(__r),
  2351. std::move(__comp), std::move(__proj));
  2352. }
  2353. };
  2354. inline constexpr __inplace_merge_fn inplace_merge{};
  2355. struct __includes_fn
  2356. {
  2357. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2358. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2359. typename _Proj1 = identity, typename _Proj2 = identity,
  2360. indirect_strict_weak_order<projected<_Iter1, _Proj1>,
  2361. projected<_Iter2, _Proj2>>
  2362. _Comp = ranges::less>
  2363. constexpr bool
  2364. operator()(_Iter1 __first1, _Sent1 __last1,
  2365. _Iter2 __first2, _Sent2 __last2,
  2366. _Comp __comp = {},
  2367. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2368. {
  2369. while (__first1 != __last1 && __first2 != __last2)
  2370. if (std::__invoke(__comp,
  2371. std::__invoke(__proj2, *__first2),
  2372. std::__invoke(__proj1, *__first1)))
  2373. return false;
  2374. else if (std::__invoke(__comp,
  2375. std::__invoke(__proj1, *__first1),
  2376. std::__invoke(__proj2, *__first2)))
  2377. ++__first1;
  2378. else
  2379. {
  2380. ++__first1;
  2381. ++__first2;
  2382. }
  2383. return __first2 == __last2;
  2384. }
  2385. template<input_range _Range1, input_range _Range2,
  2386. typename _Proj1 = identity, typename _Proj2 = identity,
  2387. indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
  2388. projected<iterator_t<_Range2>, _Proj2>>
  2389. _Comp = ranges::less>
  2390. constexpr bool
  2391. operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
  2392. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2393. {
  2394. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2395. ranges::begin(__r2), ranges::end(__r2),
  2396. std::move(__comp),
  2397. std::move(__proj1), std::move(__proj2));
  2398. }
  2399. };
  2400. inline constexpr __includes_fn includes{};
  2401. template<typename _Iter1, typename _Iter2, typename _Out>
  2402. using set_union_result = in_in_out_result<_Iter1, _Iter2, _Out>;
  2403. struct __set_union_fn
  2404. {
  2405. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2406. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2407. weakly_incrementable _Out, typename _Comp = ranges::less,
  2408. typename _Proj1 = identity, typename _Proj2 = identity>
  2409. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2410. constexpr set_union_result<_Iter1, _Iter2, _Out>
  2411. operator()(_Iter1 __first1, _Sent1 __last1,
  2412. _Iter2 __first2, _Sent2 __last2,
  2413. _Out __result, _Comp __comp = {},
  2414. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2415. {
  2416. while (__first1 != __last1 && __first2 != __last2)
  2417. {
  2418. if (std::__invoke(__comp,
  2419. std::__invoke(__proj1, *__first1),
  2420. std::__invoke(__proj2, *__first2)))
  2421. {
  2422. *__result = *__first1;
  2423. ++__first1;
  2424. }
  2425. else if (std::__invoke(__comp,
  2426. std::__invoke(__proj2, *__first2),
  2427. std::__invoke(__proj1, *__first1)))
  2428. {
  2429. *__result = *__first2;
  2430. ++__first2;
  2431. }
  2432. else
  2433. {
  2434. *__result = *__first1;
  2435. ++__first1;
  2436. ++__first2;
  2437. }
  2438. ++__result;
  2439. }
  2440. auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
  2441. std::move(__result));
  2442. auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
  2443. std::move(__copy1.out));
  2444. return {std::move(__copy1.in), std::move(__copy2.in),
  2445. std::move(__copy2.out)};
  2446. }
  2447. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2448. typename _Comp = ranges::less,
  2449. typename _Proj1 = identity, typename _Proj2 = identity>
  2450. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2451. _Comp, _Proj1, _Proj2>
  2452. constexpr set_union_result<borrowed_iterator_t<_Range1>,
  2453. borrowed_iterator_t<_Range2>, _Out>
  2454. operator()(_Range1&& __r1, _Range2&& __r2,
  2455. _Out __result, _Comp __comp = {},
  2456. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2457. {
  2458. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2459. ranges::begin(__r2), ranges::end(__r2),
  2460. std::move(__result), std::move(__comp),
  2461. std::move(__proj1), std::move(__proj2));
  2462. }
  2463. };
  2464. inline constexpr __set_union_fn set_union{};
  2465. template<typename _Iter1, typename _Iter2, typename _Out>
  2466. using set_intersection_result = in_in_out_result<_Iter1, _Iter2, _Out>;
  2467. struct __set_intersection_fn
  2468. {
  2469. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2470. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2471. weakly_incrementable _Out, typename _Comp = ranges::less,
  2472. typename _Proj1 = identity, typename _Proj2 = identity>
  2473. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2474. constexpr set_intersection_result<_Iter1, _Iter2, _Out>
  2475. operator()(_Iter1 __first1, _Sent1 __last1,
  2476. _Iter2 __first2, _Sent2 __last2, _Out __result,
  2477. _Comp __comp = {},
  2478. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2479. {
  2480. while (__first1 != __last1 && __first2 != __last2)
  2481. if (std::__invoke(__comp,
  2482. std::__invoke(__proj1, *__first1),
  2483. std::__invoke(__proj2, *__first2)))
  2484. ++__first1;
  2485. else if (std::__invoke(__comp,
  2486. std::__invoke(__proj2, *__first2),
  2487. std::__invoke(__proj1, *__first1)))
  2488. ++__first2;
  2489. else
  2490. {
  2491. *__result = *__first1;
  2492. ++__first1;
  2493. ++__first2;
  2494. ++__result;
  2495. }
  2496. // TODO: Eliminating these variables triggers an ICE.
  2497. auto __last1i = ranges::next(std::move(__first1), std::move(__last1));
  2498. auto __last2i = ranges::next(std::move(__first2), std::move(__last2));
  2499. return {std::move(__last1i), std::move(__last2i), std::move(__result)};
  2500. }
  2501. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2502. typename _Comp = ranges::less,
  2503. typename _Proj1 = identity, typename _Proj2 = identity>
  2504. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2505. _Comp, _Proj1, _Proj2>
  2506. constexpr set_intersection_result<borrowed_iterator_t<_Range1>,
  2507. borrowed_iterator_t<_Range2>, _Out>
  2508. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
  2509. _Comp __comp = {},
  2510. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2511. {
  2512. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2513. ranges::begin(__r2), ranges::end(__r2),
  2514. std::move(__result), std::move(__comp),
  2515. std::move(__proj1), std::move(__proj2));
  2516. }
  2517. };
  2518. inline constexpr __set_intersection_fn set_intersection{};
  2519. template<typename _Iter, typename _Out>
  2520. using set_difference_result = in_out_result<_Iter, _Out>;
  2521. struct __set_difference_fn
  2522. {
  2523. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2524. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2525. weakly_incrementable _Out, typename _Comp = ranges::less,
  2526. typename _Proj1 = identity, typename _Proj2 = identity>
  2527. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2528. constexpr set_difference_result<_Iter1, _Out>
  2529. operator()(_Iter1 __first1, _Sent1 __last1,
  2530. _Iter2 __first2, _Sent2 __last2, _Out __result,
  2531. _Comp __comp = {},
  2532. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2533. {
  2534. while (__first1 != __last1 && __first2 != __last2)
  2535. if (std::__invoke(__comp,
  2536. std::__invoke(__proj1, *__first1),
  2537. std::__invoke(__proj2, *__first2)))
  2538. {
  2539. *__result = *__first1;
  2540. ++__first1;
  2541. ++__result;
  2542. }
  2543. else if (std::__invoke(__comp,
  2544. std::__invoke(__proj2, *__first2),
  2545. std::__invoke(__proj1, *__first1)))
  2546. ++__first2;
  2547. else
  2548. {
  2549. ++__first1;
  2550. ++__first2;
  2551. }
  2552. return ranges::copy(std::move(__first1), std::move(__last1),
  2553. std::move(__result));
  2554. }
  2555. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2556. typename _Comp = ranges::less,
  2557. typename _Proj1 = identity, typename _Proj2 = identity>
  2558. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2559. _Comp, _Proj1, _Proj2>
  2560. constexpr set_difference_result<borrowed_iterator_t<_Range1>, _Out>
  2561. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
  2562. _Comp __comp = {},
  2563. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2564. {
  2565. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2566. ranges::begin(__r2), ranges::end(__r2),
  2567. std::move(__result), std::move(__comp),
  2568. std::move(__proj1), std::move(__proj2));
  2569. }
  2570. };
  2571. inline constexpr __set_difference_fn set_difference{};
  2572. template<typename _Iter1, typename _Iter2, typename _Out>
  2573. using set_symmetric_difference_result
  2574. = in_in_out_result<_Iter1, _Iter2, _Out>;
  2575. struct __set_symmetric_difference_fn
  2576. {
  2577. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  2578. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  2579. weakly_incrementable _Out, typename _Comp = ranges::less,
  2580. typename _Proj1 = identity, typename _Proj2 = identity>
  2581. requires mergeable<_Iter1, _Iter2, _Out, _Comp, _Proj1, _Proj2>
  2582. constexpr set_symmetric_difference_result<_Iter1, _Iter2, _Out>
  2583. operator()(_Iter1 __first1, _Sent1 __last1,
  2584. _Iter2 __first2, _Sent2 __last2,
  2585. _Out __result, _Comp __comp = {},
  2586. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2587. {
  2588. while (__first1 != __last1 && __first2 != __last2)
  2589. if (std::__invoke(__comp,
  2590. std::__invoke(__proj1, *__first1),
  2591. std::__invoke(__proj2, *__first2)))
  2592. {
  2593. *__result = *__first1;
  2594. ++__first1;
  2595. ++__result;
  2596. }
  2597. else if (std::__invoke(__comp,
  2598. std::__invoke(__proj2, *__first2),
  2599. std::__invoke(__proj1, *__first1)))
  2600. {
  2601. *__result = *__first2;
  2602. ++__first2;
  2603. ++__result;
  2604. }
  2605. else
  2606. {
  2607. ++__first1;
  2608. ++__first2;
  2609. }
  2610. auto __copy1 = ranges::copy(std::move(__first1), std::move(__last1),
  2611. std::move(__result));
  2612. auto __copy2 = ranges::copy(std::move(__first2), std::move(__last2),
  2613. std::move(__copy1.out));
  2614. return {std::move(__copy1.in), std::move(__copy2.in),
  2615. std::move(__copy2.out)};
  2616. }
  2617. template<input_range _Range1, input_range _Range2, weakly_incrementable _Out,
  2618. typename _Comp = ranges::less,
  2619. typename _Proj1 = identity, typename _Proj2 = identity>
  2620. requires mergeable<iterator_t<_Range1>, iterator_t<_Range2>, _Out,
  2621. _Comp, _Proj1, _Proj2>
  2622. constexpr set_symmetric_difference_result<borrowed_iterator_t<_Range1>,
  2623. borrowed_iterator_t<_Range2>,
  2624. _Out>
  2625. operator()(_Range1&& __r1, _Range2&& __r2, _Out __result,
  2626. _Comp __comp = {},
  2627. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  2628. {
  2629. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  2630. ranges::begin(__r2), ranges::end(__r2),
  2631. std::move(__result), std::move(__comp),
  2632. std::move(__proj1), std::move(__proj2));
  2633. }
  2634. };
  2635. inline constexpr __set_symmetric_difference_fn set_symmetric_difference{};
  2636. struct __min_fn
  2637. {
  2638. template<typename _Tp, typename _Proj = identity,
  2639. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2640. _Comp = ranges::less>
  2641. constexpr const _Tp&
  2642. operator()(const _Tp& __a, const _Tp& __b,
  2643. _Comp __comp = {}, _Proj __proj = {}) const
  2644. {
  2645. if (std::__invoke(__comp,
  2646. std::__invoke(__proj, __b),
  2647. std::__invoke(__proj, __a)))
  2648. return __b;
  2649. else
  2650. return __a;
  2651. }
  2652. template<input_range _Range, typename _Proj = identity,
  2653. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2654. _Comp = ranges::less>
  2655. requires indirectly_copyable_storable<iterator_t<_Range>,
  2656. range_value_t<_Range>*>
  2657. constexpr range_value_t<_Range>
  2658. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2659. {
  2660. auto __first = ranges::begin(__r);
  2661. auto __last = ranges::end(__r);
  2662. __glibcxx_assert(__first != __last);
  2663. auto __result = *__first;
  2664. while (++__first != __last)
  2665. {
  2666. auto __tmp = *__first;
  2667. if (std::__invoke(__comp,
  2668. std::__invoke(__proj, __tmp),
  2669. std::__invoke(__proj, __result)))
  2670. __result = std::move(__tmp);
  2671. }
  2672. return __result;
  2673. }
  2674. template<copyable _Tp, typename _Proj = identity,
  2675. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2676. _Comp = ranges::less>
  2677. constexpr _Tp
  2678. operator()(initializer_list<_Tp> __r,
  2679. _Comp __comp = {}, _Proj __proj = {}) const
  2680. {
  2681. return (*this)(ranges::subrange(__r),
  2682. std::move(__comp), std::move(__proj));
  2683. }
  2684. };
  2685. inline constexpr __min_fn min{};
  2686. struct __max_fn
  2687. {
  2688. template<typename _Tp, typename _Proj = identity,
  2689. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2690. _Comp = ranges::less>
  2691. constexpr const _Tp&
  2692. operator()(const _Tp& __a, const _Tp& __b,
  2693. _Comp __comp = {}, _Proj __proj = {}) const
  2694. {
  2695. if (std::__invoke(__comp,
  2696. std::__invoke(__proj, __a),
  2697. std::__invoke(__proj, __b)))
  2698. return __b;
  2699. else
  2700. return __a;
  2701. }
  2702. template<input_range _Range, typename _Proj = identity,
  2703. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2704. _Comp = ranges::less>
  2705. requires indirectly_copyable_storable<iterator_t<_Range>,
  2706. range_value_t<_Range>*>
  2707. constexpr range_value_t<_Range>
  2708. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2709. {
  2710. auto __first = ranges::begin(__r);
  2711. auto __last = ranges::end(__r);
  2712. __glibcxx_assert(__first != __last);
  2713. auto __result = *__first;
  2714. while (++__first != __last)
  2715. {
  2716. auto __tmp = *__first;
  2717. if (std::__invoke(__comp,
  2718. std::__invoke(__proj, __result),
  2719. std::__invoke(__proj, __tmp)))
  2720. __result = std::move(__tmp);
  2721. }
  2722. return __result;
  2723. }
  2724. template<copyable _Tp, typename _Proj = identity,
  2725. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2726. _Comp = ranges::less>
  2727. constexpr _Tp
  2728. operator()(initializer_list<_Tp> __r,
  2729. _Comp __comp = {}, _Proj __proj = {}) const
  2730. {
  2731. return (*this)(ranges::subrange(__r),
  2732. std::move(__comp), std::move(__proj));
  2733. }
  2734. };
  2735. inline constexpr __max_fn max{};
  2736. struct __clamp_fn
  2737. {
  2738. template<typename _Tp, typename _Proj = identity,
  2739. indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp
  2740. = ranges::less>
  2741. constexpr const _Tp&
  2742. operator()(const _Tp& __val, const _Tp& __lo, const _Tp& __hi,
  2743. _Comp __comp = {}, _Proj __proj = {}) const
  2744. {
  2745. __glibcxx_assert(!(std::__invoke(__comp,
  2746. std::__invoke(__proj, __hi),
  2747. std::__invoke(__proj, __lo))));
  2748. auto&& __proj_val = std::__invoke(__proj, __val);
  2749. if (std::__invoke(__comp, __proj_val, std::__invoke(__proj, __lo)))
  2750. return __lo;
  2751. else if (std::__invoke(__comp, std::__invoke(__proj, __hi), __proj_val))
  2752. return __hi;
  2753. else
  2754. return __val;
  2755. }
  2756. };
  2757. inline constexpr __clamp_fn clamp{};
  2758. template<typename _Tp>
  2759. struct min_max_result
  2760. {
  2761. [[no_unique_address]] _Tp min;
  2762. [[no_unique_address]] _Tp max;
  2763. template<typename _Tp2>
  2764. requires convertible_to<const _Tp&, _Tp2>
  2765. constexpr
  2766. operator min_max_result<_Tp2>() const &
  2767. { return {min, max}; }
  2768. template<typename _Tp2>
  2769. requires convertible_to<_Tp, _Tp2>
  2770. constexpr
  2771. operator min_max_result<_Tp2>() &&
  2772. { return {std::move(min), std::move(max)}; }
  2773. };
  2774. template<typename _Tp>
  2775. using minmax_result = min_max_result<_Tp>;
  2776. struct __minmax_fn
  2777. {
  2778. template<typename _Tp, typename _Proj = identity,
  2779. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2780. _Comp = ranges::less>
  2781. constexpr minmax_result<const _Tp&>
  2782. operator()(const _Tp& __a, const _Tp& __b,
  2783. _Comp __comp = {}, _Proj __proj = {}) const
  2784. {
  2785. if (std::__invoke(__comp,
  2786. std::__invoke(__proj, __b),
  2787. std::__invoke(__proj, __a)))
  2788. return {__b, __a};
  2789. else
  2790. return {__a, __b};
  2791. }
  2792. template<input_range _Range, typename _Proj = identity,
  2793. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2794. _Comp = ranges::less>
  2795. requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
  2796. constexpr minmax_result<range_value_t<_Range>>
  2797. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2798. {
  2799. auto __first = ranges::begin(__r);
  2800. auto __last = ranges::end(__r);
  2801. __glibcxx_assert(__first != __last);
  2802. auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
  2803. minmax_result<range_value_t<_Range>> __result = {*__first, __result.min};
  2804. if (++__first == __last)
  2805. return __result;
  2806. else
  2807. {
  2808. // At this point __result.min == __result.max, so a single
  2809. // comparison with the next element suffices.
  2810. auto&& __val = *__first;
  2811. if (__comp_proj(__val, __result.min))
  2812. __result.min = std::forward<decltype(__val)>(__val);
  2813. else
  2814. __result.max = std::forward<decltype(__val)>(__val);
  2815. }
  2816. while (++__first != __last)
  2817. {
  2818. // Now process two elements at a time so that we perform at most
  2819. // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
  2820. // iterations of this loop performs three comparisons).
  2821. range_value_t<_Range> __val1 = *__first;
  2822. if (++__first == __last)
  2823. {
  2824. // N is odd; in this final iteration, we perform at most two
  2825. // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
  2826. // which is not more than 3*N/2, as required.
  2827. if (__comp_proj(__val1, __result.min))
  2828. __result.min = std::move(__val1);
  2829. else if (!__comp_proj(__val1, __result.max))
  2830. __result.max = std::move(__val1);
  2831. break;
  2832. }
  2833. auto&& __val2 = *__first;
  2834. if (!__comp_proj(__val2, __val1))
  2835. {
  2836. if (__comp_proj(__val1, __result.min))
  2837. __result.min = std::move(__val1);
  2838. if (!__comp_proj(__val2, __result.max))
  2839. __result.max = std::forward<decltype(__val2)>(__val2);
  2840. }
  2841. else
  2842. {
  2843. if (__comp_proj(__val2, __result.min))
  2844. __result.min = std::forward<decltype(__val2)>(__val2);
  2845. if (!__comp_proj(__val1, __result.max))
  2846. __result.max = std::move(__val1);
  2847. }
  2848. }
  2849. return __result;
  2850. }
  2851. template<copyable _Tp, typename _Proj = identity,
  2852. indirect_strict_weak_order<projected<const _Tp*, _Proj>>
  2853. _Comp = ranges::less>
  2854. constexpr minmax_result<_Tp>
  2855. operator()(initializer_list<_Tp> __r,
  2856. _Comp __comp = {}, _Proj __proj = {}) const
  2857. {
  2858. return (*this)(ranges::subrange(__r),
  2859. std::move(__comp), std::move(__proj));
  2860. }
  2861. };
  2862. inline constexpr __minmax_fn minmax{};
  2863. struct __min_element_fn
  2864. {
  2865. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2866. typename _Proj = identity,
  2867. indirect_strict_weak_order<projected<_Iter, _Proj>>
  2868. _Comp = ranges::less>
  2869. constexpr _Iter
  2870. operator()(_Iter __first, _Sent __last,
  2871. _Comp __comp = {}, _Proj __proj = {}) const
  2872. {
  2873. if (__first == __last)
  2874. return __first;
  2875. auto __i = __first;
  2876. while (++__i != __last)
  2877. {
  2878. if (std::__invoke(__comp,
  2879. std::__invoke(__proj, *__i),
  2880. std::__invoke(__proj, *__first)))
  2881. __first = __i;
  2882. }
  2883. return __first;
  2884. }
  2885. template<forward_range _Range, typename _Proj = identity,
  2886. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2887. _Comp = ranges::less>
  2888. constexpr borrowed_iterator_t<_Range>
  2889. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2890. {
  2891. return (*this)(ranges::begin(__r), ranges::end(__r),
  2892. std::move(__comp), std::move(__proj));
  2893. }
  2894. };
  2895. inline constexpr __min_element_fn min_element{};
  2896. struct __max_element_fn
  2897. {
  2898. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2899. typename _Proj = identity,
  2900. indirect_strict_weak_order<projected<_Iter, _Proj>>
  2901. _Comp = ranges::less>
  2902. constexpr _Iter
  2903. operator()(_Iter __first, _Sent __last,
  2904. _Comp __comp = {}, _Proj __proj = {}) const
  2905. {
  2906. if (__first == __last)
  2907. return __first;
  2908. auto __i = __first;
  2909. while (++__i != __last)
  2910. {
  2911. if (std::__invoke(__comp,
  2912. std::__invoke(__proj, *__first),
  2913. std::__invoke(__proj, *__i)))
  2914. __first = __i;
  2915. }
  2916. return __first;
  2917. }
  2918. template<forward_range _Range, typename _Proj = identity,
  2919. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2920. _Comp = ranges::less>
  2921. constexpr borrowed_iterator_t<_Range>
  2922. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2923. {
  2924. return (*this)(ranges::begin(__r), ranges::end(__r),
  2925. std::move(__comp), std::move(__proj));
  2926. }
  2927. };
  2928. inline constexpr __max_element_fn max_element{};
  2929. template<typename _Iter>
  2930. using minmax_element_result = min_max_result<_Iter>;
  2931. struct __minmax_element_fn
  2932. {
  2933. template<forward_iterator _Iter, sentinel_for<_Iter> _Sent,
  2934. typename _Proj = identity,
  2935. indirect_strict_weak_order<projected<_Iter, _Proj>>
  2936. _Comp = ranges::less>
  2937. constexpr minmax_element_result<_Iter>
  2938. operator()(_Iter __first, _Sent __last,
  2939. _Comp __comp = {}, _Proj __proj = {}) const
  2940. {
  2941. auto __comp_proj = __detail::__make_comp_proj(__comp, __proj);
  2942. minmax_element_result<_Iter> __result = {__first, __first};
  2943. if (__first == __last || ++__first == __last)
  2944. return __result;
  2945. else
  2946. {
  2947. // At this point __result.min == __result.max, so a single
  2948. // comparison with the next element suffices.
  2949. if (__comp_proj(*__first, *__result.min))
  2950. __result.min = __first;
  2951. else
  2952. __result.max = __first;
  2953. }
  2954. while (++__first != __last)
  2955. {
  2956. // Now process two elements at a time so that we perform at most
  2957. // 1 + 3*(N-2)/2 comparisons in total (each of the (N-2)/2
  2958. // iterations of this loop performs three comparisons).
  2959. auto __prev = __first;
  2960. if (++__first == __last)
  2961. {
  2962. // N is odd; in this final iteration, we perform at most two
  2963. // comparisons, for a total of 1 + 3*(N-3)/2 + 2 comparisons,
  2964. // which is not more than 3*N/2, as required.
  2965. if (__comp_proj(*__prev, *__result.min))
  2966. __result.min = __prev;
  2967. else if (!__comp_proj(*__prev, *__result.max))
  2968. __result.max = __prev;
  2969. break;
  2970. }
  2971. if (!__comp_proj(*__first, *__prev))
  2972. {
  2973. if (__comp_proj(*__prev, *__result.min))
  2974. __result.min = __prev;
  2975. if (!__comp_proj(*__first, *__result.max))
  2976. __result.max = __first;
  2977. }
  2978. else
  2979. {
  2980. if (__comp_proj(*__first, *__result.min))
  2981. __result.min = __first;
  2982. if (!__comp_proj(*__prev, *__result.max))
  2983. __result.max = __prev;
  2984. }
  2985. }
  2986. return __result;
  2987. }
  2988. template<forward_range _Range, typename _Proj = identity,
  2989. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>>
  2990. _Comp = ranges::less>
  2991. constexpr minmax_element_result<borrowed_iterator_t<_Range>>
  2992. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  2993. {
  2994. return (*this)(ranges::begin(__r), ranges::end(__r),
  2995. std::move(__comp), std::move(__proj));
  2996. }
  2997. };
  2998. inline constexpr __minmax_element_fn minmax_element{};
  2999. struct __lexicographical_compare_fn
  3000. {
  3001. template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
  3002. input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
  3003. typename _Proj1 = identity, typename _Proj2 = identity,
  3004. indirect_strict_weak_order<projected<_Iter1, _Proj1>,
  3005. projected<_Iter2, _Proj2>>
  3006. _Comp = ranges::less>
  3007. constexpr bool
  3008. operator()(_Iter1 __first1, _Sent1 __last1,
  3009. _Iter2 __first2, _Sent2 __last2,
  3010. _Comp __comp = {},
  3011. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  3012. {
  3013. if constexpr (__detail::__is_normal_iterator<_Iter1>
  3014. && same_as<_Iter1, _Sent1>)
  3015. return (*this)(__first1.base(), __last1.base(),
  3016. std::move(__first2), std::move(__last2),
  3017. std::move(__comp),
  3018. std::move(__proj1), std::move(__proj2));
  3019. else if constexpr (__detail::__is_normal_iterator<_Iter2>
  3020. && same_as<_Iter2, _Sent2>)
  3021. return (*this)(std::move(__first1), std::move(__last1),
  3022. __first2.base(), __last2.base(),
  3023. std::move(__comp),
  3024. std::move(__proj1), std::move(__proj2));
  3025. else
  3026. {
  3027. constexpr bool __sized_iters
  3028. = (sized_sentinel_for<_Sent1, _Iter1>
  3029. && sized_sentinel_for<_Sent2, _Iter2>);
  3030. if constexpr (__sized_iters)
  3031. {
  3032. using _ValueType1 = iter_value_t<_Iter1>;
  3033. using _ValueType2 = iter_value_t<_Iter2>;
  3034. // This condition is consistent with the one in
  3035. // __lexicographical_compare_aux in <bits/stl_algobase.h>.
  3036. constexpr bool __use_memcmp
  3037. = (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
  3038. && __ptr_to_nonvolatile<_Iter1>
  3039. && __ptr_to_nonvolatile<_Iter2>
  3040. && (is_same_v<_Comp, ranges::less>
  3041. || is_same_v<_Comp, ranges::greater>)
  3042. && is_same_v<_Proj1, identity>
  3043. && is_same_v<_Proj2, identity>);
  3044. if constexpr (__use_memcmp)
  3045. {
  3046. const auto __d1 = __last1 - __first1;
  3047. const auto __d2 = __last2 - __first2;
  3048. if (const auto __len = std::min(__d1, __d2))
  3049. {
  3050. const auto __c
  3051. = std::__memcmp(__first1, __first2, __len);
  3052. if constexpr (is_same_v<_Comp, ranges::less>)
  3053. {
  3054. if (__c < 0)
  3055. return true;
  3056. if (__c > 0)
  3057. return false;
  3058. }
  3059. else if constexpr (is_same_v<_Comp, ranges::greater>)
  3060. {
  3061. if (__c > 0)
  3062. return true;
  3063. if (__c < 0)
  3064. return false;
  3065. }
  3066. }
  3067. return __d1 < __d2;
  3068. }
  3069. }
  3070. for (; __first1 != __last1 && __first2 != __last2;
  3071. ++__first1, (void) ++__first2)
  3072. {
  3073. if (std::__invoke(__comp,
  3074. std::__invoke(__proj1, *__first1),
  3075. std::__invoke(__proj2, *__first2)))
  3076. return true;
  3077. if (std::__invoke(__comp,
  3078. std::__invoke(__proj2, *__first2),
  3079. std::__invoke(__proj1, *__first1)))
  3080. return false;
  3081. }
  3082. return __first1 == __last1 && __first2 != __last2;
  3083. }
  3084. }
  3085. template<input_range _Range1, input_range _Range2,
  3086. typename _Proj1 = identity, typename _Proj2 = identity,
  3087. indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
  3088. projected<iterator_t<_Range2>, _Proj2>>
  3089. _Comp = ranges::less>
  3090. constexpr bool
  3091. operator()(_Range1&& __r1, _Range2&& __r2, _Comp __comp = {},
  3092. _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const
  3093. {
  3094. return (*this)(ranges::begin(__r1), ranges::end(__r1),
  3095. ranges::begin(__r2), ranges::end(__r2),
  3096. std::move(__comp),
  3097. std::move(__proj1), std::move(__proj2));
  3098. }
  3099. private:
  3100. template<typename _Iter, typename _Ref = iter_reference_t<_Iter>>
  3101. static constexpr bool __ptr_to_nonvolatile
  3102. = is_pointer_v<_Iter> && !is_volatile_v<remove_reference_t<_Ref>>;
  3103. };
  3104. inline constexpr __lexicographical_compare_fn lexicographical_compare;
  3105. template<typename _Iter>
  3106. struct in_found_result
  3107. {
  3108. [[no_unique_address]] _Iter in;
  3109. bool found;
  3110. template<typename _Iter2>
  3111. requires convertible_to<const _Iter&, _Iter2>
  3112. constexpr
  3113. operator in_found_result<_Iter2>() const &
  3114. { return {in, found}; }
  3115. template<typename _Iter2>
  3116. requires convertible_to<_Iter, _Iter2>
  3117. constexpr
  3118. operator in_found_result<_Iter2>() &&
  3119. { return {std::move(in), found}; }
  3120. };
  3121. template<typename _Iter>
  3122. using next_permutation_result = in_found_result<_Iter>;
  3123. struct __next_permutation_fn
  3124. {
  3125. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  3126. typename _Comp = ranges::less, typename _Proj = identity>
  3127. requires sortable<_Iter, _Comp, _Proj>
  3128. constexpr next_permutation_result<_Iter>
  3129. operator()(_Iter __first, _Sent __last,
  3130. _Comp __comp = {}, _Proj __proj = {}) const
  3131. {
  3132. if (__first == __last)
  3133. return {std::move(__first), false};
  3134. auto __i = __first;
  3135. ++__i;
  3136. if (__i == __last)
  3137. return {std::move(__i), false};
  3138. auto __lasti = ranges::next(__first, __last);
  3139. __i = __lasti;
  3140. --__i;
  3141. for (;;)
  3142. {
  3143. auto __ii = __i;
  3144. --__i;
  3145. if (std::__invoke(__comp,
  3146. std::__invoke(__proj, *__i),
  3147. std::__invoke(__proj, *__ii)))
  3148. {
  3149. auto __j = __lasti;
  3150. while (!(bool)std::__invoke(__comp,
  3151. std::__invoke(__proj, *__i),
  3152. std::__invoke(__proj, *--__j)))
  3153. ;
  3154. ranges::iter_swap(__i, __j);
  3155. ranges::reverse(__ii, __last);
  3156. return {std::move(__lasti), true};
  3157. }
  3158. if (__i == __first)
  3159. {
  3160. ranges::reverse(__first, __last);
  3161. return {std::move(__lasti), false};
  3162. }
  3163. }
  3164. }
  3165. template<bidirectional_range _Range, typename _Comp = ranges::less,
  3166. typename _Proj = identity>
  3167. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  3168. constexpr next_permutation_result<borrowed_iterator_t<_Range>>
  3169. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  3170. {
  3171. return (*this)(ranges::begin(__r), ranges::end(__r),
  3172. std::move(__comp), std::move(__proj));
  3173. }
  3174. };
  3175. inline constexpr __next_permutation_fn next_permutation{};
  3176. template<typename _Iter>
  3177. using prev_permutation_result = in_found_result<_Iter>;
  3178. struct __prev_permutation_fn
  3179. {
  3180. template<bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent,
  3181. typename _Comp = ranges::less, typename _Proj = identity>
  3182. requires sortable<_Iter, _Comp, _Proj>
  3183. constexpr prev_permutation_result<_Iter>
  3184. operator()(_Iter __first, _Sent __last,
  3185. _Comp __comp = {}, _Proj __proj = {}) const
  3186. {
  3187. if (__first == __last)
  3188. return {std::move(__first), false};
  3189. auto __i = __first;
  3190. ++__i;
  3191. if (__i == __last)
  3192. return {std::move(__i), false};
  3193. auto __lasti = ranges::next(__first, __last);
  3194. __i = __lasti;
  3195. --__i;
  3196. for (;;)
  3197. {
  3198. auto __ii = __i;
  3199. --__i;
  3200. if (std::__invoke(__comp,
  3201. std::__invoke(__proj, *__ii),
  3202. std::__invoke(__proj, *__i)))
  3203. {
  3204. auto __j = __lasti;
  3205. while (!(bool)std::__invoke(__comp,
  3206. std::__invoke(__proj, *--__j),
  3207. std::__invoke(__proj, *__i)))
  3208. ;
  3209. ranges::iter_swap(__i, __j);
  3210. ranges::reverse(__ii, __last);
  3211. return {std::move(__lasti), true};
  3212. }
  3213. if (__i == __first)
  3214. {
  3215. ranges::reverse(__first, __last);
  3216. return {std::move(__lasti), false};
  3217. }
  3218. }
  3219. }
  3220. template<bidirectional_range _Range, typename _Comp = ranges::less,
  3221. typename _Proj = identity>
  3222. requires sortable<iterator_t<_Range>, _Comp, _Proj>
  3223. constexpr prev_permutation_result<borrowed_iterator_t<_Range>>
  3224. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
  3225. {
  3226. return (*this)(ranges::begin(__r), ranges::end(__r),
  3227. std::move(__comp), std::move(__proj));
  3228. }
  3229. };
  3230. inline constexpr __prev_permutation_fn prev_permutation{};
  3231. } // namespace ranges
  3232. #define __cpp_lib_shift 201806L
  3233. template<typename _ForwardIterator>
  3234. constexpr _ForwardIterator
  3235. shift_left(_ForwardIterator __first, _ForwardIterator __last,
  3236. typename iterator_traits<_ForwardIterator>::difference_type __n)
  3237. {
  3238. __glibcxx_assert(__n >= 0);
  3239. if (__n == 0)
  3240. return __last;
  3241. auto __mid = ranges::next(__first, __n, __last);
  3242. if (__mid == __last)
  3243. return __first;
  3244. return std::move(std::move(__mid), std::move(__last), std::move(__first));
  3245. }
  3246. template<typename _ForwardIterator>
  3247. constexpr _ForwardIterator
  3248. shift_right(_ForwardIterator __first, _ForwardIterator __last,
  3249. typename iterator_traits<_ForwardIterator>::difference_type __n)
  3250. {
  3251. __glibcxx_assert(__n >= 0);
  3252. if (__n == 0)
  3253. return __first;
  3254. using _Cat
  3255. = typename iterator_traits<_ForwardIterator>::iterator_category;
  3256. if constexpr (derived_from<_Cat, bidirectional_iterator_tag>)
  3257. {
  3258. auto __mid = ranges::next(__last, -__n, __first);
  3259. if (__mid == __first)
  3260. return __last;
  3261. return std::move_backward(std::move(__first), std::move(__mid),
  3262. std::move(__last));
  3263. }
  3264. else
  3265. {
  3266. auto __result = ranges::next(__first, __n, __last);
  3267. if (__result == __last)
  3268. return __last;
  3269. auto __dest_head = __first, __dest_tail = __result;
  3270. while (__dest_head != __result)
  3271. {
  3272. if (__dest_tail == __last)
  3273. {
  3274. // If we get here, then we must have
  3275. // 2*n >= distance(__first, __last)
  3276. // i.e. we are shifting out at least half of the range. In
  3277. // this case we can safely perform the shift with a single
  3278. // move.
  3279. std::move(std::move(__first), std::move(__dest_head), __result);
  3280. return __result;
  3281. }
  3282. ++__dest_head;
  3283. ++__dest_tail;
  3284. }
  3285. for (;;)
  3286. {
  3287. // At the start of each iteration of this outer loop, the range
  3288. // [__first, __result) contains those elements that after shifting
  3289. // the whole range right by __n, should end up in
  3290. // [__dest_head, __dest_tail) in order.
  3291. // The below inner loop swaps the elements of [__first, __result)
  3292. // and [__dest_head, __dest_tail), while simultaneously shifting
  3293. // the latter range by __n.
  3294. auto __cursor = __first;
  3295. while (__cursor != __result)
  3296. {
  3297. if (__dest_tail == __last)
  3298. {
  3299. // At this point the ranges [__first, result) and
  3300. // [__dest_head, dest_tail) are disjoint, so we can safely
  3301. // move the remaining elements.
  3302. __dest_head = std::move(__cursor, __result,
  3303. std::move(__dest_head));
  3304. std::move(std::move(__first), std::move(__cursor),
  3305. std::move(__dest_head));
  3306. return __result;
  3307. }
  3308. std::iter_swap(__cursor, __dest_head);
  3309. ++__dest_head;
  3310. ++__dest_tail;
  3311. ++__cursor;
  3312. }
  3313. }
  3314. }
  3315. }
  3316. _GLIBCXX_END_NAMESPACE_VERSION
  3317. } // namespace std
  3318. #endif // concepts
  3319. #endif // C++20
  3320. #endif // _RANGES_ALGO_H