ranges_algo.h 118 KB

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