ranges_algo.h 118 KB

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