stl_algobase.h 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  1. // Core algorithmic facilities -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996-1998
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_algobase.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{algorithm}
  48. */
  49. #ifndef _STL_ALGOBASE_H
  50. #define _STL_ALGOBASE_H 1
  51. #include <bits/c++config.h>
  52. #include <bits/functexcept.h>
  53. #include <bits/cpp_type_traits.h>
  54. #include <ext/type_traits.h>
  55. #include <ext/numeric_traits.h>
  56. #include <bits/stl_pair.h>
  57. #include <bits/stl_iterator_base_types.h>
  58. #include <bits/stl_iterator_base_funcs.h>
  59. #include <bits/stl_iterator.h>
  60. #include <bits/concept_check.h>
  61. #include <debug/debug.h>
  62. #include <bits/move.h> // For std::swap
  63. #include <bits/predefined_ops.h>
  64. #if __cplusplus >= 201103L
  65. # include <type_traits>
  66. #endif
  67. #if __cplusplus > 201703L
  68. # include <compare>
  69. #endif
  70. namespace std _GLIBCXX_VISIBILITY(default)
  71. {
  72. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  73. /*
  74. * A constexpr wrapper for __builtin_memcmp.
  75. * @param __num The number of elements of type _Tp (not bytes).
  76. */
  77. template<typename _Tp, typename _Up>
  78. _GLIBCXX14_CONSTEXPR
  79. inline int
  80. __memcmp(const _Tp* __first1, const _Up* __first2, size_t __num)
  81. {
  82. #if __cplusplus >= 201103L
  83. static_assert(sizeof(_Tp) == sizeof(_Up), "can be compared with memcmp");
  84. #endif
  85. #ifdef __cpp_lib_is_constant_evaluated
  86. if (std::is_constant_evaluated())
  87. {
  88. for(; __num > 0; ++__first1, ++__first2, --__num)
  89. if (*__first1 != *__first2)
  90. return *__first1 < *__first2 ? -1 : 1;
  91. return 0;
  92. }
  93. else
  94. #endif
  95. return __builtin_memcmp(__first1, __first2, sizeof(_Tp) * __num);
  96. }
  97. #if __cplusplus < 201103L
  98. // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
  99. // nutshell, we are partially implementing the resolution of DR 187,
  100. // when it's safe, i.e., the value_types are equal.
  101. template<bool _BoolType>
  102. struct __iter_swap
  103. {
  104. template<typename _ForwardIterator1, typename _ForwardIterator2>
  105. static void
  106. iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
  107. {
  108. typedef typename iterator_traits<_ForwardIterator1>::value_type
  109. _ValueType1;
  110. _ValueType1 __tmp = *__a;
  111. *__a = *__b;
  112. *__b = __tmp;
  113. }
  114. };
  115. template<>
  116. struct __iter_swap<true>
  117. {
  118. template<typename _ForwardIterator1, typename _ForwardIterator2>
  119. static void
  120. iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
  121. {
  122. swap(*__a, *__b);
  123. }
  124. };
  125. #endif // C++03
  126. /**
  127. * @brief Swaps the contents of two iterators.
  128. * @ingroup mutating_algorithms
  129. * @param __a An iterator.
  130. * @param __b Another iterator.
  131. * @return Nothing.
  132. *
  133. * This function swaps the values pointed to by two iterators, not the
  134. * iterators themselves.
  135. */
  136. template<typename _ForwardIterator1, typename _ForwardIterator2>
  137. _GLIBCXX20_CONSTEXPR
  138. inline void
  139. iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
  140. {
  141. // concept requirements
  142. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  143. _ForwardIterator1>)
  144. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  145. _ForwardIterator2>)
  146. #if __cplusplus < 201103L
  147. typedef typename iterator_traits<_ForwardIterator1>::value_type
  148. _ValueType1;
  149. typedef typename iterator_traits<_ForwardIterator2>::value_type
  150. _ValueType2;
  151. __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
  152. _ValueType2>)
  153. __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
  154. _ValueType1>)
  155. typedef typename iterator_traits<_ForwardIterator1>::reference
  156. _ReferenceType1;
  157. typedef typename iterator_traits<_ForwardIterator2>::reference
  158. _ReferenceType2;
  159. std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
  160. && __are_same<_ValueType1&, _ReferenceType1>::__value
  161. && __are_same<_ValueType2&, _ReferenceType2>::__value>::
  162. iter_swap(__a, __b);
  163. #else
  164. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  165. // 187. iter_swap underspecified
  166. swap(*__a, *__b);
  167. #endif
  168. }
  169. /**
  170. * @brief Swap the elements of two sequences.
  171. * @ingroup mutating_algorithms
  172. * @param __first1 A forward iterator.
  173. * @param __last1 A forward iterator.
  174. * @param __first2 A forward iterator.
  175. * @return An iterator equal to @p first2+(last1-first1).
  176. *
  177. * Swaps each element in the range @p [first1,last1) with the
  178. * corresponding element in the range @p [first2,(last1-first1)).
  179. * The ranges must not overlap.
  180. */
  181. template<typename _ForwardIterator1, typename _ForwardIterator2>
  182. _GLIBCXX20_CONSTEXPR
  183. _ForwardIterator2
  184. swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  185. _ForwardIterator2 __first2)
  186. {
  187. // concept requirements
  188. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  189. _ForwardIterator1>)
  190. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  191. _ForwardIterator2>)
  192. __glibcxx_requires_valid_range(__first1, __last1);
  193. for (; __first1 != __last1; ++__first1, (void)++__first2)
  194. std::iter_swap(__first1, __first2);
  195. return __first2;
  196. }
  197. /**
  198. * @brief This does what you think it does.
  199. * @ingroup sorting_algorithms
  200. * @param __a A thing of arbitrary type.
  201. * @param __b Another thing of arbitrary type.
  202. * @return The lesser of the parameters.
  203. *
  204. * This is the simple classic generic implementation. It will work on
  205. * temporary expressions, since they are only evaluated once, unlike a
  206. * preprocessor macro.
  207. */
  208. template<typename _Tp>
  209. _GLIBCXX14_CONSTEXPR
  210. inline const _Tp&
  211. min(const _Tp& __a, const _Tp& __b)
  212. {
  213. // concept requirements
  214. __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  215. //return __b < __a ? __b : __a;
  216. if (__b < __a)
  217. return __b;
  218. return __a;
  219. }
  220. /**
  221. * @brief This does what you think it does.
  222. * @ingroup sorting_algorithms
  223. * @param __a A thing of arbitrary type.
  224. * @param __b Another thing of arbitrary type.
  225. * @return The greater of the parameters.
  226. *
  227. * This is the simple classic generic implementation. It will work on
  228. * temporary expressions, since they are only evaluated once, unlike a
  229. * preprocessor macro.
  230. */
  231. template<typename _Tp>
  232. _GLIBCXX14_CONSTEXPR
  233. inline const _Tp&
  234. max(const _Tp& __a, const _Tp& __b)
  235. {
  236. // concept requirements
  237. __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  238. //return __a < __b ? __b : __a;
  239. if (__a < __b)
  240. return __b;
  241. return __a;
  242. }
  243. /**
  244. * @brief This does what you think it does.
  245. * @ingroup sorting_algorithms
  246. * @param __a A thing of arbitrary type.
  247. * @param __b Another thing of arbitrary type.
  248. * @param __comp A @link comparison_functors comparison functor@endlink.
  249. * @return The lesser of the parameters.
  250. *
  251. * This will work on temporary expressions, since they are only evaluated
  252. * once, unlike a preprocessor macro.
  253. */
  254. template<typename _Tp, typename _Compare>
  255. _GLIBCXX14_CONSTEXPR
  256. inline const _Tp&
  257. min(const _Tp& __a, const _Tp& __b, _Compare __comp)
  258. {
  259. //return __comp(__b, __a) ? __b : __a;
  260. if (__comp(__b, __a))
  261. return __b;
  262. return __a;
  263. }
  264. /**
  265. * @brief This does what you think it does.
  266. * @ingroup sorting_algorithms
  267. * @param __a A thing of arbitrary type.
  268. * @param __b Another thing of arbitrary type.
  269. * @param __comp A @link comparison_functors comparison functor@endlink.
  270. * @return The greater of the parameters.
  271. *
  272. * This will work on temporary expressions, since they are only evaluated
  273. * once, unlike a preprocessor macro.
  274. */
  275. template<typename _Tp, typename _Compare>
  276. _GLIBCXX14_CONSTEXPR
  277. inline const _Tp&
  278. max(const _Tp& __a, const _Tp& __b, _Compare __comp)
  279. {
  280. //return __comp(__a, __b) ? __b : __a;
  281. if (__comp(__a, __b))
  282. return __b;
  283. return __a;
  284. }
  285. // Fallback implementation of the function in bits/stl_iterator.h used to
  286. // remove the __normal_iterator wrapper. See copy, fill, ...
  287. template<typename _Iterator>
  288. _GLIBCXX20_CONSTEXPR
  289. inline _Iterator
  290. __niter_base(_Iterator __it)
  291. _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)
  292. { return __it; }
  293. template<typename _Ite, typename _Seq>
  294. _Ite
  295. __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
  296. std::random_access_iterator_tag>&);
  297. // Reverse the __niter_base transformation to get a
  298. // __normal_iterator back again (this assumes that __normal_iterator
  299. // is only used to wrap random access iterators, like pointers).
  300. template<typename _From, typename _To>
  301. _GLIBCXX20_CONSTEXPR
  302. inline _From
  303. __niter_wrap(_From __from, _To __res)
  304. { return __from + (__res - std::__niter_base(__from)); }
  305. // No need to wrap, iterator already has the right type.
  306. template<typename _Iterator>
  307. _GLIBCXX20_CONSTEXPR
  308. inline _Iterator
  309. __niter_wrap(const _Iterator&, _Iterator __res)
  310. { return __res; }
  311. // All of these auxiliary structs serve two purposes. (1) Replace
  312. // calls to copy with memmove whenever possible. (Memmove, not memcpy,
  313. // because the input and output ranges are permitted to overlap.)
  314. // (2) If we're using random access iterators, then write the loop as
  315. // a for loop with an explicit count.
  316. template<bool _IsMove, bool _IsSimple, typename _Category>
  317. struct __copy_move
  318. {
  319. template<typename _II, typename _OI>
  320. _GLIBCXX20_CONSTEXPR
  321. static _OI
  322. __copy_m(_II __first, _II __last, _OI __result)
  323. {
  324. for (; __first != __last; ++__result, (void)++__first)
  325. *__result = *__first;
  326. return __result;
  327. }
  328. };
  329. #if __cplusplus >= 201103L
  330. template<typename _Category>
  331. struct __copy_move<true, false, _Category>
  332. {
  333. template<typename _II, typename _OI>
  334. _GLIBCXX20_CONSTEXPR
  335. static _OI
  336. __copy_m(_II __first, _II __last, _OI __result)
  337. {
  338. for (; __first != __last; ++__result, (void)++__first)
  339. *__result = std::move(*__first);
  340. return __result;
  341. }
  342. };
  343. #endif
  344. template<>
  345. struct __copy_move<false, false, random_access_iterator_tag>
  346. {
  347. template<typename _II, typename _OI>
  348. _GLIBCXX20_CONSTEXPR
  349. static _OI
  350. __copy_m(_II __first, _II __last, _OI __result)
  351. {
  352. typedef typename iterator_traits<_II>::difference_type _Distance;
  353. for(_Distance __n = __last - __first; __n > 0; --__n)
  354. {
  355. *__result = *__first;
  356. ++__first;
  357. ++__result;
  358. }
  359. return __result;
  360. }
  361. };
  362. #if __cplusplus >= 201103L
  363. template<>
  364. struct __copy_move<true, false, random_access_iterator_tag>
  365. {
  366. template<typename _II, typename _OI>
  367. _GLIBCXX20_CONSTEXPR
  368. static _OI
  369. __copy_m(_II __first, _II __last, _OI __result)
  370. {
  371. typedef typename iterator_traits<_II>::difference_type _Distance;
  372. for(_Distance __n = __last - __first; __n > 0; --__n)
  373. {
  374. *__result = std::move(*__first);
  375. ++__first;
  376. ++__result;
  377. }
  378. return __result;
  379. }
  380. };
  381. #endif
  382. template<bool _IsMove>
  383. struct __copy_move<_IsMove, true, random_access_iterator_tag>
  384. {
  385. template<typename _Tp>
  386. _GLIBCXX20_CONSTEXPR
  387. static _Tp*
  388. __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
  389. {
  390. #if __cplusplus >= 201103L
  391. using __assignable = __conditional_t<_IsMove,
  392. is_move_assignable<_Tp>,
  393. is_copy_assignable<_Tp>>;
  394. // trivial types can have deleted assignment
  395. static_assert( __assignable::value, "type must be assignable" );
  396. #endif
  397. const ptrdiff_t _Num = __last - __first;
  398. if (_Num)
  399. __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
  400. return __result + _Num;
  401. }
  402. };
  403. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  404. template<typename _Tp, typename _Ref, typename _Ptr>
  405. struct _Deque_iterator;
  406. struct _Bit_iterator;
  407. _GLIBCXX_END_NAMESPACE_CONTAINER
  408. // Helpers for streambuf iterators (either istream or ostream).
  409. // NB: avoid including <iosfwd>, relatively large.
  410. template<typename _CharT>
  411. struct char_traits;
  412. template<typename _CharT, typename _Traits>
  413. class istreambuf_iterator;
  414. template<typename _CharT, typename _Traits>
  415. class ostreambuf_iterator;
  416. template<bool _IsMove, typename _CharT>
  417. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  418. ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
  419. __copy_move_a2(_CharT*, _CharT*,
  420. ostreambuf_iterator<_CharT, char_traits<_CharT> >);
  421. template<bool _IsMove, typename _CharT>
  422. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  423. ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
  424. __copy_move_a2(const _CharT*, const _CharT*,
  425. ostreambuf_iterator<_CharT, char_traits<_CharT> >);
  426. template<bool _IsMove, typename _CharT>
  427. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  428. _CharT*>::__type
  429. __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
  430. istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
  431. template<bool _IsMove, typename _CharT>
  432. typename __gnu_cxx::__enable_if<
  433. __is_char<_CharT>::__value,
  434. _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*> >::__type
  435. __copy_move_a2(
  436. istreambuf_iterator<_CharT, char_traits<_CharT> >,
  437. istreambuf_iterator<_CharT, char_traits<_CharT> >,
  438. _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*>);
  439. template<bool _IsMove, typename _II, typename _OI>
  440. _GLIBCXX20_CONSTEXPR
  441. inline _OI
  442. __copy_move_a2(_II __first, _II __last, _OI __result)
  443. {
  444. typedef typename iterator_traits<_II>::iterator_category _Category;
  445. #ifdef __cpp_lib_is_constant_evaluated
  446. if (std::is_constant_evaluated())
  447. return std::__copy_move<_IsMove, false, _Category>::
  448. __copy_m(__first, __last, __result);
  449. #endif
  450. return std::__copy_move<_IsMove, __memcpyable<_OI, _II>::__value,
  451. _Category>::__copy_m(__first, __last, __result);
  452. }
  453. template<bool _IsMove,
  454. typename _Tp, typename _Ref, typename _Ptr, typename _OI>
  455. _OI
  456. __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
  457. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
  458. _OI);
  459. template<bool _IsMove,
  460. typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
  461. _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
  462. __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
  463. _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
  464. _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
  465. template<bool _IsMove, typename _II, typename _Tp>
  466. typename __gnu_cxx::__enable_if<
  467. __is_random_access_iter<_II>::__value,
  468. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
  469. __copy_move_a1(_II, _II, _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
  470. template<bool _IsMove, typename _II, typename _OI>
  471. _GLIBCXX20_CONSTEXPR
  472. inline _OI
  473. __copy_move_a1(_II __first, _II __last, _OI __result)
  474. { return std::__copy_move_a2<_IsMove>(__first, __last, __result); }
  475. template<bool _IsMove, typename _II, typename _OI>
  476. _GLIBCXX20_CONSTEXPR
  477. inline _OI
  478. __copy_move_a(_II __first, _II __last, _OI __result)
  479. {
  480. return std::__niter_wrap(__result,
  481. std::__copy_move_a1<_IsMove>(std::__niter_base(__first),
  482. std::__niter_base(__last),
  483. std::__niter_base(__result)));
  484. }
  485. template<bool _IsMove,
  486. typename _Ite, typename _Seq, typename _Cat, typename _OI>
  487. _OI
  488. __copy_move_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
  489. const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
  490. _OI);
  491. template<bool _IsMove,
  492. typename _II, typename _Ite, typename _Seq, typename _Cat>
  493. __gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
  494. __copy_move_a(_II, _II,
  495. const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
  496. template<bool _IsMove,
  497. typename _IIte, typename _ISeq, typename _ICat,
  498. typename _OIte, typename _OSeq, typename _OCat>
  499. ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>
  500. __copy_move_a(const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
  501. const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
  502. const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
  503. template<typename _InputIterator, typename _Size, typename _OutputIterator>
  504. _GLIBCXX20_CONSTEXPR
  505. _OutputIterator
  506. __copy_n_a(_InputIterator __first, _Size __n, _OutputIterator __result,
  507. bool)
  508. {
  509. if (__n > 0)
  510. {
  511. while (true)
  512. {
  513. *__result = *__first;
  514. ++__result;
  515. if (--__n > 0)
  516. ++__first;
  517. else
  518. break;
  519. }
  520. }
  521. return __result;
  522. }
  523. template<typename _CharT, typename _Size>
  524. typename __gnu_cxx::__enable_if<
  525. __is_char<_CharT>::__value, _CharT*>::__type
  526. __copy_n_a(istreambuf_iterator<_CharT, char_traits<_CharT> >,
  527. _Size, _CharT*, bool);
  528. template<typename _CharT, typename _Size>
  529. typename __gnu_cxx::__enable_if<
  530. __is_char<_CharT>::__value,
  531. _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*> >::__type
  532. __copy_n_a(istreambuf_iterator<_CharT, char_traits<_CharT> >, _Size,
  533. _GLIBCXX_STD_C::_Deque_iterator<_CharT, _CharT&, _CharT*>,
  534. bool);
  535. /**
  536. * @brief Copies the range [first,last) into result.
  537. * @ingroup mutating_algorithms
  538. * @param __first An input iterator.
  539. * @param __last An input iterator.
  540. * @param __result An output iterator.
  541. * @return result + (last - first)
  542. *
  543. * This inline function will boil down to a call to @c memmove whenever
  544. * possible. Failing that, if random access iterators are passed, then the
  545. * loop count will be known (and therefore a candidate for compiler
  546. * optimizations such as unrolling). Result may not be contained within
  547. * [first,last); the copy_backward function should be used instead.
  548. *
  549. * Note that the end of the output range is permitted to be contained
  550. * within [first,last).
  551. */
  552. template<typename _II, typename _OI>
  553. _GLIBCXX20_CONSTEXPR
  554. inline _OI
  555. copy(_II __first, _II __last, _OI __result)
  556. {
  557. // concept requirements
  558. __glibcxx_function_requires(_InputIteratorConcept<_II>)
  559. __glibcxx_function_requires(_OutputIteratorConcept<_OI,
  560. typename iterator_traits<_II>::reference>)
  561. __glibcxx_requires_can_increment_range(__first, __last, __result);
  562. return std::__copy_move_a<__is_move_iterator<_II>::__value>
  563. (std::__miter_base(__first), std::__miter_base(__last), __result);
  564. }
  565. #if __cplusplus >= 201103L
  566. /**
  567. * @brief Moves the range [first,last) into result.
  568. * @ingroup mutating_algorithms
  569. * @param __first An input iterator.
  570. * @param __last An input iterator.
  571. * @param __result An output iterator.
  572. * @return result + (last - first)
  573. *
  574. * This inline function will boil down to a call to @c memmove whenever
  575. * possible. Failing that, if random access iterators are passed, then the
  576. * loop count will be known (and therefore a candidate for compiler
  577. * optimizations such as unrolling). Result may not be contained within
  578. * [first,last); the move_backward function should be used instead.
  579. *
  580. * Note that the end of the output range is permitted to be contained
  581. * within [first,last).
  582. */
  583. template<typename _II, typename _OI>
  584. _GLIBCXX20_CONSTEXPR
  585. inline _OI
  586. move(_II __first, _II __last, _OI __result)
  587. {
  588. // concept requirements
  589. __glibcxx_function_requires(_InputIteratorConcept<_II>)
  590. __glibcxx_function_requires(_OutputIteratorConcept<_OI,
  591. typename iterator_traits<_II>::value_type&&>)
  592. __glibcxx_requires_can_increment_range(__first, __last, __result);
  593. return std::__copy_move_a<true>(std::__miter_base(__first),
  594. std::__miter_base(__last), __result);
  595. }
  596. #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
  597. #else
  598. #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
  599. #endif
  600. template<bool _IsMove, bool _IsSimple, typename _Category>
  601. struct __copy_move_backward
  602. {
  603. template<typename _BI1, typename _BI2>
  604. _GLIBCXX20_CONSTEXPR
  605. static _BI2
  606. __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
  607. {
  608. while (__first != __last)
  609. *--__result = *--__last;
  610. return __result;
  611. }
  612. };
  613. #if __cplusplus >= 201103L
  614. template<typename _Category>
  615. struct __copy_move_backward<true, false, _Category>
  616. {
  617. template<typename _BI1, typename _BI2>
  618. _GLIBCXX20_CONSTEXPR
  619. static _BI2
  620. __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
  621. {
  622. while (__first != __last)
  623. *--__result = std::move(*--__last);
  624. return __result;
  625. }
  626. };
  627. #endif
  628. template<>
  629. struct __copy_move_backward<false, false, random_access_iterator_tag>
  630. {
  631. template<typename _BI1, typename _BI2>
  632. _GLIBCXX20_CONSTEXPR
  633. static _BI2
  634. __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
  635. {
  636. typename iterator_traits<_BI1>::difference_type
  637. __n = __last - __first;
  638. for (; __n > 0; --__n)
  639. *--__result = *--__last;
  640. return __result;
  641. }
  642. };
  643. #if __cplusplus >= 201103L
  644. template<>
  645. struct __copy_move_backward<true, false, random_access_iterator_tag>
  646. {
  647. template<typename _BI1, typename _BI2>
  648. _GLIBCXX20_CONSTEXPR
  649. static _BI2
  650. __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
  651. {
  652. typename iterator_traits<_BI1>::difference_type
  653. __n = __last - __first;
  654. for (; __n > 0; --__n)
  655. *--__result = std::move(*--__last);
  656. return __result;
  657. }
  658. };
  659. #endif
  660. template<bool _IsMove>
  661. struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
  662. {
  663. template<typename _Tp>
  664. _GLIBCXX20_CONSTEXPR
  665. static _Tp*
  666. __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
  667. {
  668. #if __cplusplus >= 201103L
  669. using __assignable = __conditional_t<_IsMove,
  670. is_move_assignable<_Tp>,
  671. is_copy_assignable<_Tp>>;
  672. // trivial types can have deleted assignment
  673. static_assert( __assignable::value, "type must be assignable" );
  674. #endif
  675. const ptrdiff_t _Num = __last - __first;
  676. if (_Num)
  677. __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
  678. return __result - _Num;
  679. }
  680. };
  681. template<bool _IsMove, typename _BI1, typename _BI2>
  682. _GLIBCXX20_CONSTEXPR
  683. inline _BI2
  684. __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
  685. {
  686. typedef typename iterator_traits<_BI1>::iterator_category _Category;
  687. #ifdef __cpp_lib_is_constant_evaluated
  688. if (std::is_constant_evaluated())
  689. return std::__copy_move_backward<_IsMove, false, _Category>::
  690. __copy_move_b(__first, __last, __result);
  691. #endif
  692. return std::__copy_move_backward<_IsMove,
  693. __memcpyable<_BI2, _BI1>::__value,
  694. _Category>::__copy_move_b(__first,
  695. __last,
  696. __result);
  697. }
  698. template<bool _IsMove, typename _BI1, typename _BI2>
  699. _GLIBCXX20_CONSTEXPR
  700. inline _BI2
  701. __copy_move_backward_a1(_BI1 __first, _BI1 __last, _BI2 __result)
  702. { return std::__copy_move_backward_a2<_IsMove>(__first, __last, __result); }
  703. template<bool _IsMove,
  704. typename _Tp, typename _Ref, typename _Ptr, typename _OI>
  705. _OI
  706. __copy_move_backward_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
  707. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
  708. _OI);
  709. template<bool _IsMove,
  710. typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
  711. _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
  712. __copy_move_backward_a1(
  713. _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
  714. _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
  715. _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
  716. template<bool _IsMove, typename _II, typename _Tp>
  717. typename __gnu_cxx::__enable_if<
  718. __is_random_access_iter<_II>::__value,
  719. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
  720. __copy_move_backward_a1(_II, _II,
  721. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
  722. template<bool _IsMove, typename _II, typename _OI>
  723. _GLIBCXX20_CONSTEXPR
  724. inline _OI
  725. __copy_move_backward_a(_II __first, _II __last, _OI __result)
  726. {
  727. return std::__niter_wrap(__result,
  728. std::__copy_move_backward_a1<_IsMove>
  729. (std::__niter_base(__first), std::__niter_base(__last),
  730. std::__niter_base(__result)));
  731. }
  732. template<bool _IsMove,
  733. typename _Ite, typename _Seq, typename _Cat, typename _OI>
  734. _OI
  735. __copy_move_backward_a(
  736. const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
  737. const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
  738. _OI);
  739. template<bool _IsMove,
  740. typename _II, typename _Ite, typename _Seq, typename _Cat>
  741. __gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
  742. __copy_move_backward_a(_II, _II,
  743. const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
  744. template<bool _IsMove,
  745. typename _IIte, typename _ISeq, typename _ICat,
  746. typename _OIte, typename _OSeq, typename _OCat>
  747. ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>
  748. __copy_move_backward_a(
  749. const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
  750. const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
  751. const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
  752. /**
  753. * @brief Copies the range [first,last) into result.
  754. * @ingroup mutating_algorithms
  755. * @param __first A bidirectional iterator.
  756. * @param __last A bidirectional iterator.
  757. * @param __result A bidirectional iterator.
  758. * @return result - (last - first)
  759. *
  760. * The function has the same effect as copy, but starts at the end of the
  761. * range and works its way to the start, returning the start of the result.
  762. * This inline function will boil down to a call to @c memmove whenever
  763. * possible. Failing that, if random access iterators are passed, then the
  764. * loop count will be known (and therefore a candidate for compiler
  765. * optimizations such as unrolling).
  766. *
  767. * Result may not be in the range (first,last]. Use copy instead. Note
  768. * that the start of the output range may overlap [first,last).
  769. */
  770. template<typename _BI1, typename _BI2>
  771. _GLIBCXX20_CONSTEXPR
  772. inline _BI2
  773. copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
  774. {
  775. // concept requirements
  776. __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
  777. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
  778. __glibcxx_function_requires(_OutputIteratorConcept<_BI2,
  779. typename iterator_traits<_BI1>::reference>)
  780. __glibcxx_requires_can_decrement_range(__first, __last, __result);
  781. return std::__copy_move_backward_a<__is_move_iterator<_BI1>::__value>
  782. (std::__miter_base(__first), std::__miter_base(__last), __result);
  783. }
  784. #if __cplusplus >= 201103L
  785. /**
  786. * @brief Moves the range [first,last) into result.
  787. * @ingroup mutating_algorithms
  788. * @param __first A bidirectional iterator.
  789. * @param __last A bidirectional iterator.
  790. * @param __result A bidirectional iterator.
  791. * @return result - (last - first)
  792. *
  793. * The function has the same effect as move, but starts at the end of the
  794. * range and works its way to the start, returning the start of the result.
  795. * This inline function will boil down to a call to @c memmove whenever
  796. * possible. Failing that, if random access iterators are passed, then the
  797. * loop count will be known (and therefore a candidate for compiler
  798. * optimizations such as unrolling).
  799. *
  800. * Result may not be in the range (first,last]. Use move instead. Note
  801. * that the start of the output range may overlap [first,last).
  802. */
  803. template<typename _BI1, typename _BI2>
  804. _GLIBCXX20_CONSTEXPR
  805. inline _BI2
  806. move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
  807. {
  808. // concept requirements
  809. __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
  810. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
  811. __glibcxx_function_requires(_OutputIteratorConcept<_BI2,
  812. typename iterator_traits<_BI1>::value_type&&>)
  813. __glibcxx_requires_can_decrement_range(__first, __last, __result);
  814. return std::__copy_move_backward_a<true>(std::__miter_base(__first),
  815. std::__miter_base(__last),
  816. __result);
  817. }
  818. #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
  819. #else
  820. #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
  821. #endif
  822. template<typename _ForwardIterator, typename _Tp>
  823. _GLIBCXX20_CONSTEXPR
  824. inline typename
  825. __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
  826. __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
  827. const _Tp& __value)
  828. {
  829. for (; __first != __last; ++__first)
  830. *__first = __value;
  831. }
  832. template<typename _ForwardIterator, typename _Tp>
  833. _GLIBCXX20_CONSTEXPR
  834. inline typename
  835. __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
  836. __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
  837. const _Tp& __value)
  838. {
  839. const _Tp __tmp = __value;
  840. for (; __first != __last; ++__first)
  841. *__first = __tmp;
  842. }
  843. // Specialization: for char types we can use memset.
  844. template<typename _Tp>
  845. _GLIBCXX20_CONSTEXPR
  846. inline typename
  847. __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
  848. __fill_a1(_Tp* __first, _Tp* __last, const _Tp& __c)
  849. {
  850. const _Tp __tmp = __c;
  851. #if __cpp_lib_is_constant_evaluated
  852. if (std::is_constant_evaluated())
  853. {
  854. for (; __first != __last; ++__first)
  855. *__first = __tmp;
  856. return;
  857. }
  858. #endif
  859. if (const size_t __len = __last - __first)
  860. __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
  861. }
  862. template<typename _Ite, typename _Cont, typename _Tp>
  863. _GLIBCXX20_CONSTEXPR
  864. inline void
  865. __fill_a1(::__gnu_cxx::__normal_iterator<_Ite, _Cont> __first,
  866. ::__gnu_cxx::__normal_iterator<_Ite, _Cont> __last,
  867. const _Tp& __value)
  868. { std::__fill_a1(__first.base(), __last.base(), __value); }
  869. template<typename _Tp, typename _VTp>
  870. void
  871. __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
  872. const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
  873. const _VTp&);
  874. _GLIBCXX20_CONSTEXPR
  875. void
  876. __fill_a1(_GLIBCXX_STD_C::_Bit_iterator, _GLIBCXX_STD_C::_Bit_iterator,
  877. const bool&);
  878. template<typename _FIte, typename _Tp>
  879. _GLIBCXX20_CONSTEXPR
  880. inline void
  881. __fill_a(_FIte __first, _FIte __last, const _Tp& __value)
  882. { std::__fill_a1(__first, __last, __value); }
  883. template<typename _Ite, typename _Seq, typename _Cat, typename _Tp>
  884. void
  885. __fill_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
  886. const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
  887. const _Tp&);
  888. /**
  889. * @brief Fills the range [first,last) with copies of value.
  890. * @ingroup mutating_algorithms
  891. * @param __first A forward iterator.
  892. * @param __last A forward iterator.
  893. * @param __value A reference-to-const of arbitrary type.
  894. * @return Nothing.
  895. *
  896. * This function fills a range with copies of the same value. For char
  897. * types filling contiguous areas of memory, this becomes an inline call
  898. * to @c memset or @c wmemset.
  899. */
  900. template<typename _ForwardIterator, typename _Tp>
  901. _GLIBCXX20_CONSTEXPR
  902. inline void
  903. fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
  904. {
  905. // concept requirements
  906. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  907. _ForwardIterator>)
  908. __glibcxx_requires_valid_range(__first, __last);
  909. std::__fill_a(__first, __last, __value);
  910. }
  911. // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
  912. inline _GLIBCXX_CONSTEXPR int
  913. __size_to_integer(int __n) { return __n; }
  914. inline _GLIBCXX_CONSTEXPR unsigned
  915. __size_to_integer(unsigned __n) { return __n; }
  916. inline _GLIBCXX_CONSTEXPR long
  917. __size_to_integer(long __n) { return __n; }
  918. inline _GLIBCXX_CONSTEXPR unsigned long
  919. __size_to_integer(unsigned long __n) { return __n; }
  920. inline _GLIBCXX_CONSTEXPR long long
  921. __size_to_integer(long long __n) { return __n; }
  922. inline _GLIBCXX_CONSTEXPR unsigned long long
  923. __size_to_integer(unsigned long long __n) { return __n; }
  924. #if defined(__GLIBCXX_TYPE_INT_N_0)
  925. __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
  926. __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
  927. __extension__ inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_0
  928. __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
  929. #endif
  930. #if defined(__GLIBCXX_TYPE_INT_N_1)
  931. __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
  932. __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
  933. __extension__ inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_1
  934. __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
  935. #endif
  936. #if defined(__GLIBCXX_TYPE_INT_N_2)
  937. __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
  938. __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
  939. __extension__ inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_2
  940. __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
  941. #endif
  942. #if defined(__GLIBCXX_TYPE_INT_N_3)
  943. __extension__ inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_3
  944. __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
  945. __extension__ inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
  946. __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
  947. #endif
  948. inline _GLIBCXX_CONSTEXPR long long
  949. __size_to_integer(float __n) { return (long long)__n; }
  950. inline _GLIBCXX_CONSTEXPR long long
  951. __size_to_integer(double __n) { return (long long)__n; }
  952. inline _GLIBCXX_CONSTEXPR long long
  953. __size_to_integer(long double __n) { return (long long)__n; }
  954. #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
  955. __extension__ inline _GLIBCXX_CONSTEXPR long long
  956. __size_to_integer(__float128 __n) { return (long long)__n; }
  957. #endif
  958. template<typename _OutputIterator, typename _Size, typename _Tp>
  959. _GLIBCXX20_CONSTEXPR
  960. inline typename
  961. __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
  962. __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
  963. {
  964. for (; __n > 0; --__n, (void) ++__first)
  965. *__first = __value;
  966. return __first;
  967. }
  968. template<typename _OutputIterator, typename _Size, typename _Tp>
  969. _GLIBCXX20_CONSTEXPR
  970. inline typename
  971. __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
  972. __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
  973. {
  974. const _Tp __tmp = __value;
  975. for (; __n > 0; --__n, (void) ++__first)
  976. *__first = __tmp;
  977. return __first;
  978. }
  979. template<typename _Ite, typename _Seq, typename _Cat, typename _Size,
  980. typename _Tp>
  981. ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
  982. __fill_n_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
  983. _Size __n, const _Tp& __value,
  984. std::input_iterator_tag);
  985. template<typename _OutputIterator, typename _Size, typename _Tp>
  986. _GLIBCXX20_CONSTEXPR
  987. inline _OutputIterator
  988. __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
  989. std::output_iterator_tag)
  990. {
  991. #if __cplusplus >= 201103L
  992. static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
  993. #endif
  994. return __fill_n_a1(__first, __n, __value);
  995. }
  996. template<typename _OutputIterator, typename _Size, typename _Tp>
  997. _GLIBCXX20_CONSTEXPR
  998. inline _OutputIterator
  999. __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
  1000. std::input_iterator_tag)
  1001. {
  1002. #if __cplusplus >= 201103L
  1003. static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
  1004. #endif
  1005. return __fill_n_a1(__first, __n, __value);
  1006. }
  1007. template<typename _OutputIterator, typename _Size, typename _Tp>
  1008. _GLIBCXX20_CONSTEXPR
  1009. inline _OutputIterator
  1010. __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
  1011. std::random_access_iterator_tag)
  1012. {
  1013. #if __cplusplus >= 201103L
  1014. static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
  1015. #endif
  1016. if (__n <= 0)
  1017. return __first;
  1018. __glibcxx_requires_can_increment(__first, __n);
  1019. std::__fill_a(__first, __first + __n, __value);
  1020. return __first + __n;
  1021. }
  1022. /**
  1023. * @brief Fills the range [first,first+n) with copies of value.
  1024. * @ingroup mutating_algorithms
  1025. * @param __first An output iterator.
  1026. * @param __n The count of copies to perform.
  1027. * @param __value A reference-to-const of arbitrary type.
  1028. * @return The iterator at first+n.
  1029. *
  1030. * This function fills a range with copies of the same value. For char
  1031. * types filling contiguous areas of memory, this becomes an inline call
  1032. * to @c memset or @c wmemset.
  1033. *
  1034. * If @p __n is negative, the function does nothing.
  1035. */
  1036. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1037. // DR 865. More algorithms that throw away information
  1038. // DR 426. search_n(), fill_n(), and generate_n() with negative n
  1039. template<typename _OI, typename _Size, typename _Tp>
  1040. _GLIBCXX20_CONSTEXPR
  1041. inline _OI
  1042. fill_n(_OI __first, _Size __n, const _Tp& __value)
  1043. {
  1044. // concept requirements
  1045. __glibcxx_function_requires(_OutputIteratorConcept<_OI, const _Tp&>)
  1046. return std::__fill_n_a(__first, std::__size_to_integer(__n), __value,
  1047. std::__iterator_category(__first));
  1048. }
  1049. template<bool _BoolType>
  1050. struct __equal
  1051. {
  1052. template<typename _II1, typename _II2>
  1053. _GLIBCXX20_CONSTEXPR
  1054. static bool
  1055. equal(_II1 __first1, _II1 __last1, _II2 __first2)
  1056. {
  1057. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  1058. if (!(*__first1 == *__first2))
  1059. return false;
  1060. return true;
  1061. }
  1062. };
  1063. template<>
  1064. struct __equal<true>
  1065. {
  1066. template<typename _Tp>
  1067. _GLIBCXX20_CONSTEXPR
  1068. static bool
  1069. equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
  1070. {
  1071. if (const size_t __len = (__last1 - __first1))
  1072. return !std::__memcmp(__first1, __first2, __len);
  1073. return true;
  1074. }
  1075. };
  1076. template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
  1077. typename __gnu_cxx::__enable_if<
  1078. __is_random_access_iter<_II>::__value, bool>::__type
  1079. __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
  1080. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
  1081. _II);
  1082. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1083. typename _Tp2, typename _Ref2, typename _Ptr2>
  1084. bool
  1085. __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1086. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1087. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
  1088. template<typename _II, typename _Tp, typename _Ref, typename _Ptr>
  1089. typename __gnu_cxx::__enable_if<
  1090. __is_random_access_iter<_II>::__value, bool>::__type
  1091. __equal_aux1(_II, _II,
  1092. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>);
  1093. template<typename _II1, typename _II2>
  1094. _GLIBCXX20_CONSTEXPR
  1095. inline bool
  1096. __equal_aux1(_II1 __first1, _II1 __last1, _II2 __first2)
  1097. {
  1098. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  1099. const bool __simple = ((__is_integer<_ValueType1>::__value
  1100. || __is_pointer<_ValueType1>::__value)
  1101. && __memcmpable<_II1, _II2>::__value);
  1102. return std::__equal<__simple>::equal(__first1, __last1, __first2);
  1103. }
  1104. template<typename _II1, typename _II2>
  1105. _GLIBCXX20_CONSTEXPR
  1106. inline bool
  1107. __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
  1108. {
  1109. return std::__equal_aux1(std::__niter_base(__first1),
  1110. std::__niter_base(__last1),
  1111. std::__niter_base(__first2));
  1112. }
  1113. template<typename _II1, typename _Seq1, typename _Cat1, typename _II2>
  1114. bool
  1115. __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
  1116. const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
  1117. _II2);
  1118. template<typename _II1, typename _II2, typename _Seq2, typename _Cat2>
  1119. bool
  1120. __equal_aux(_II1, _II1,
  1121. const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
  1122. template<typename _II1, typename _Seq1, typename _Cat1,
  1123. typename _II2, typename _Seq2, typename _Cat2>
  1124. bool
  1125. __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
  1126. const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
  1127. const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
  1128. template<typename, typename>
  1129. struct __lc_rai
  1130. {
  1131. template<typename _II1, typename _II2>
  1132. _GLIBCXX20_CONSTEXPR
  1133. static _II1
  1134. __newlast1(_II1, _II1 __last1, _II2, _II2)
  1135. { return __last1; }
  1136. template<typename _II>
  1137. _GLIBCXX20_CONSTEXPR
  1138. static bool
  1139. __cnd2(_II __first, _II __last)
  1140. { return __first != __last; }
  1141. };
  1142. template<>
  1143. struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
  1144. {
  1145. template<typename _RAI1, typename _RAI2>
  1146. _GLIBCXX20_CONSTEXPR
  1147. static _RAI1
  1148. __newlast1(_RAI1 __first1, _RAI1 __last1,
  1149. _RAI2 __first2, _RAI2 __last2)
  1150. {
  1151. const typename iterator_traits<_RAI1>::difference_type
  1152. __diff1 = __last1 - __first1;
  1153. const typename iterator_traits<_RAI2>::difference_type
  1154. __diff2 = __last2 - __first2;
  1155. return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
  1156. }
  1157. template<typename _RAI>
  1158. static _GLIBCXX20_CONSTEXPR bool
  1159. __cnd2(_RAI, _RAI)
  1160. { return true; }
  1161. };
  1162. template<typename _II1, typename _II2, typename _Compare>
  1163. _GLIBCXX20_CONSTEXPR
  1164. bool
  1165. __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
  1166. _II2 __first2, _II2 __last2,
  1167. _Compare __comp)
  1168. {
  1169. typedef typename iterator_traits<_II1>::iterator_category _Category1;
  1170. typedef typename iterator_traits<_II2>::iterator_category _Category2;
  1171. typedef std::__lc_rai<_Category1, _Category2> __rai_type;
  1172. __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
  1173. for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
  1174. ++__first1, (void)++__first2)
  1175. {
  1176. if (__comp(__first1, __first2))
  1177. return true;
  1178. if (__comp(__first2, __first1))
  1179. return false;
  1180. }
  1181. return __first1 == __last1 && __first2 != __last2;
  1182. }
  1183. template<bool _BoolType>
  1184. struct __lexicographical_compare
  1185. {
  1186. template<typename _II1, typename _II2>
  1187. _GLIBCXX20_CONSTEXPR
  1188. static bool
  1189. __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1190. {
  1191. using __gnu_cxx::__ops::__iter_less_iter;
  1192. return std::__lexicographical_compare_impl(__first1, __last1,
  1193. __first2, __last2,
  1194. __iter_less_iter());
  1195. }
  1196. template<typename _II1, typename _II2>
  1197. _GLIBCXX20_CONSTEXPR
  1198. static int
  1199. __3way(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1200. {
  1201. while (__first1 != __last1)
  1202. {
  1203. if (__first2 == __last2)
  1204. return +1;
  1205. if (*__first1 < *__first2)
  1206. return -1;
  1207. if (*__first2 < *__first1)
  1208. return +1;
  1209. ++__first1;
  1210. ++__first2;
  1211. }
  1212. return int(__first2 == __last2) - 1;
  1213. }
  1214. };
  1215. template<>
  1216. struct __lexicographical_compare<true>
  1217. {
  1218. template<typename _Tp, typename _Up>
  1219. _GLIBCXX20_CONSTEXPR
  1220. static bool
  1221. __lc(const _Tp* __first1, const _Tp* __last1,
  1222. const _Up* __first2, const _Up* __last2)
  1223. { return __3way(__first1, __last1, __first2, __last2) < 0; }
  1224. template<typename _Tp, typename _Up>
  1225. _GLIBCXX20_CONSTEXPR
  1226. static ptrdiff_t
  1227. __3way(const _Tp* __first1, const _Tp* __last1,
  1228. const _Up* __first2, const _Up* __last2)
  1229. {
  1230. const size_t __len1 = __last1 - __first1;
  1231. const size_t __len2 = __last2 - __first2;
  1232. if (const size_t __len = std::min(__len1, __len2))
  1233. if (int __result = std::__memcmp(__first1, __first2, __len))
  1234. return __result;
  1235. return ptrdiff_t(__len1 - __len2);
  1236. }
  1237. };
  1238. template<typename _II1, typename _II2>
  1239. _GLIBCXX20_CONSTEXPR
  1240. inline bool
  1241. __lexicographical_compare_aux1(_II1 __first1, _II1 __last1,
  1242. _II2 __first2, _II2 __last2)
  1243. {
  1244. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  1245. typedef typename iterator_traits<_II2>::value_type _ValueType2;
  1246. const bool __simple =
  1247. (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
  1248. && __is_pointer<_II1>::__value
  1249. && __is_pointer<_II2>::__value
  1250. #if __cplusplus > 201703L && __cpp_lib_concepts
  1251. // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
  1252. // so __is_byte<T> could be true, but we can't use memcmp with
  1253. // volatile data.
  1254. && !is_volatile_v<remove_reference_t<iter_reference_t<_II1>>>
  1255. && !is_volatile_v<remove_reference_t<iter_reference_t<_II2>>>
  1256. #endif
  1257. );
  1258. return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
  1259. __first2, __last2);
  1260. }
  1261. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1262. typename _Tp2>
  1263. bool
  1264. __lexicographical_compare_aux1(
  1265. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1266. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1267. _Tp2*, _Tp2*);
  1268. template<typename _Tp1,
  1269. typename _Tp2, typename _Ref2, typename _Ptr2>
  1270. bool
  1271. __lexicographical_compare_aux1(_Tp1*, _Tp1*,
  1272. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>,
  1273. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
  1274. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1275. typename _Tp2, typename _Ref2, typename _Ptr2>
  1276. bool
  1277. __lexicographical_compare_aux1(
  1278. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1279. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1280. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>,
  1281. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
  1282. template<typename _II1, typename _II2>
  1283. _GLIBCXX20_CONSTEXPR
  1284. inline bool
  1285. __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
  1286. _II2 __first2, _II2 __last2)
  1287. {
  1288. return std::__lexicographical_compare_aux1(std::__niter_base(__first1),
  1289. std::__niter_base(__last1),
  1290. std::__niter_base(__first2),
  1291. std::__niter_base(__last2));
  1292. }
  1293. template<typename _Iter1, typename _Seq1, typename _Cat1,
  1294. typename _II2>
  1295. bool
  1296. __lexicographical_compare_aux(
  1297. const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
  1298. const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
  1299. _II2, _II2);
  1300. template<typename _II1,
  1301. typename _Iter2, typename _Seq2, typename _Cat2>
  1302. bool
  1303. __lexicographical_compare_aux(
  1304. _II1, _II1,
  1305. const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&,
  1306. const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&);
  1307. template<typename _Iter1, typename _Seq1, typename _Cat1,
  1308. typename _Iter2, typename _Seq2, typename _Cat2>
  1309. bool
  1310. __lexicographical_compare_aux(
  1311. const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
  1312. const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
  1313. const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&,
  1314. const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&);
  1315. template<typename _ForwardIterator, typename _Tp, typename _Compare>
  1316. _GLIBCXX20_CONSTEXPR
  1317. _ForwardIterator
  1318. __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
  1319. const _Tp& __val, _Compare __comp)
  1320. {
  1321. typedef typename iterator_traits<_ForwardIterator>::difference_type
  1322. _DistanceType;
  1323. _DistanceType __len = std::distance(__first, __last);
  1324. while (__len > 0)
  1325. {
  1326. _DistanceType __half = __len >> 1;
  1327. _ForwardIterator __middle = __first;
  1328. std::advance(__middle, __half);
  1329. if (__comp(__middle, __val))
  1330. {
  1331. __first = __middle;
  1332. ++__first;
  1333. __len = __len - __half - 1;
  1334. }
  1335. else
  1336. __len = __half;
  1337. }
  1338. return __first;
  1339. }
  1340. /**
  1341. * @brief Finds the first position in which @a val could be inserted
  1342. * without changing the ordering.
  1343. * @param __first An iterator.
  1344. * @param __last Another iterator.
  1345. * @param __val The search term.
  1346. * @return An iterator pointing to the first element <em>not less
  1347. * than</em> @a val, or end() if every element is less than
  1348. * @a val.
  1349. * @ingroup binary_search_algorithms
  1350. */
  1351. template<typename _ForwardIterator, typename _Tp>
  1352. _GLIBCXX20_CONSTEXPR
  1353. inline _ForwardIterator
  1354. lower_bound(_ForwardIterator __first, _ForwardIterator __last,
  1355. const _Tp& __val)
  1356. {
  1357. // concept requirements
  1358. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1359. __glibcxx_function_requires(_LessThanOpConcept<
  1360. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  1361. __glibcxx_requires_partitioned_lower(__first, __last, __val);
  1362. return std::__lower_bound(__first, __last, __val,
  1363. __gnu_cxx::__ops::__iter_less_val());
  1364. }
  1365. /// This is a helper function for the sort routines and for random.tcc.
  1366. // Precondition: __n > 0.
  1367. inline _GLIBCXX_CONSTEXPR int
  1368. __lg(int __n)
  1369. { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
  1370. inline _GLIBCXX_CONSTEXPR unsigned
  1371. __lg(unsigned __n)
  1372. { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
  1373. inline _GLIBCXX_CONSTEXPR long
  1374. __lg(long __n)
  1375. { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
  1376. inline _GLIBCXX_CONSTEXPR unsigned long
  1377. __lg(unsigned long __n)
  1378. { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
  1379. inline _GLIBCXX_CONSTEXPR long long
  1380. __lg(long long __n)
  1381. { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
  1382. inline _GLIBCXX_CONSTEXPR unsigned long long
  1383. __lg(unsigned long long __n)
  1384. { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
  1385. _GLIBCXX_BEGIN_NAMESPACE_ALGO
  1386. /**
  1387. * @brief Tests a range for element-wise equality.
  1388. * @ingroup non_mutating_algorithms
  1389. * @param __first1 An input iterator.
  1390. * @param __last1 An input iterator.
  1391. * @param __first2 An input iterator.
  1392. * @return A boolean true or false.
  1393. *
  1394. * This compares the elements of two ranges using @c == and returns true or
  1395. * false depending on whether all of the corresponding elements of the
  1396. * ranges are equal.
  1397. */
  1398. template<typename _II1, typename _II2>
  1399. _GLIBCXX20_CONSTEXPR
  1400. inline bool
  1401. equal(_II1 __first1, _II1 __last1, _II2 __first2)
  1402. {
  1403. // concept requirements
  1404. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1405. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1406. __glibcxx_function_requires(_EqualOpConcept<
  1407. typename iterator_traits<_II1>::value_type,
  1408. typename iterator_traits<_II2>::value_type>)
  1409. __glibcxx_requires_can_increment_range(__first1, __last1, __first2);
  1410. return std::__equal_aux(__first1, __last1, __first2);
  1411. }
  1412. /**
  1413. * @brief Tests a range for element-wise equality.
  1414. * @ingroup non_mutating_algorithms
  1415. * @param __first1 An input iterator.
  1416. * @param __last1 An input iterator.
  1417. * @param __first2 An input iterator.
  1418. * @param __binary_pred A binary predicate @link functors
  1419. * functor@endlink.
  1420. * @return A boolean true or false.
  1421. *
  1422. * This compares the elements of two ranges using the binary_pred
  1423. * parameter, and returns true or
  1424. * false depending on whether all of the corresponding elements of the
  1425. * ranges are equal.
  1426. */
  1427. template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
  1428. _GLIBCXX20_CONSTEXPR
  1429. inline bool
  1430. equal(_IIter1 __first1, _IIter1 __last1,
  1431. _IIter2 __first2, _BinaryPredicate __binary_pred)
  1432. {
  1433. // concept requirements
  1434. __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
  1435. __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
  1436. __glibcxx_requires_valid_range(__first1, __last1);
  1437. for (; __first1 != __last1; ++__first1, (void)++__first2)
  1438. if (!bool(__binary_pred(*__first1, *__first2)))
  1439. return false;
  1440. return true;
  1441. }
  1442. #if __cplusplus >= 201103L
  1443. // 4-iterator version of std::equal<It1, It2> for use in C++11.
  1444. template<typename _II1, typename _II2>
  1445. _GLIBCXX20_CONSTEXPR
  1446. inline bool
  1447. __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1448. {
  1449. using _RATag = random_access_iterator_tag;
  1450. using _Cat1 = typename iterator_traits<_II1>::iterator_category;
  1451. using _Cat2 = typename iterator_traits<_II2>::iterator_category;
  1452. using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
  1453. if (_RAIters())
  1454. {
  1455. auto __d1 = std::distance(__first1, __last1);
  1456. auto __d2 = std::distance(__first2, __last2);
  1457. if (__d1 != __d2)
  1458. return false;
  1459. return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
  1460. }
  1461. for (; __first1 != __last1 && __first2 != __last2;
  1462. ++__first1, (void)++__first2)
  1463. if (!(*__first1 == *__first2))
  1464. return false;
  1465. return __first1 == __last1 && __first2 == __last2;
  1466. }
  1467. // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
  1468. template<typename _II1, typename _II2, typename _BinaryPredicate>
  1469. _GLIBCXX20_CONSTEXPR
  1470. inline bool
  1471. __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
  1472. _BinaryPredicate __binary_pred)
  1473. {
  1474. using _RATag = random_access_iterator_tag;
  1475. using _Cat1 = typename iterator_traits<_II1>::iterator_category;
  1476. using _Cat2 = typename iterator_traits<_II2>::iterator_category;
  1477. using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
  1478. if (_RAIters())
  1479. {
  1480. auto __d1 = std::distance(__first1, __last1);
  1481. auto __d2 = std::distance(__first2, __last2);
  1482. if (__d1 != __d2)
  1483. return false;
  1484. return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
  1485. __binary_pred);
  1486. }
  1487. for (; __first1 != __last1 && __first2 != __last2;
  1488. ++__first1, (void)++__first2)
  1489. if (!bool(__binary_pred(*__first1, *__first2)))
  1490. return false;
  1491. return __first1 == __last1 && __first2 == __last2;
  1492. }
  1493. #endif // C++11
  1494. #if __cplusplus > 201103L
  1495. #define __cpp_lib_robust_nonmodifying_seq_ops 201304L
  1496. /**
  1497. * @brief Tests a range for element-wise equality.
  1498. * @ingroup non_mutating_algorithms
  1499. * @param __first1 An input iterator.
  1500. * @param __last1 An input iterator.
  1501. * @param __first2 An input iterator.
  1502. * @param __last2 An input iterator.
  1503. * @return A boolean true or false.
  1504. *
  1505. * This compares the elements of two ranges using @c == and returns true or
  1506. * false depending on whether all of the corresponding elements of the
  1507. * ranges are equal.
  1508. */
  1509. template<typename _II1, typename _II2>
  1510. _GLIBCXX20_CONSTEXPR
  1511. inline bool
  1512. equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1513. {
  1514. // concept requirements
  1515. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1516. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1517. __glibcxx_function_requires(_EqualOpConcept<
  1518. typename iterator_traits<_II1>::value_type,
  1519. typename iterator_traits<_II2>::value_type>)
  1520. __glibcxx_requires_valid_range(__first1, __last1);
  1521. __glibcxx_requires_valid_range(__first2, __last2);
  1522. return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2);
  1523. }
  1524. /**
  1525. * @brief Tests a range for element-wise equality.
  1526. * @ingroup non_mutating_algorithms
  1527. * @param __first1 An input iterator.
  1528. * @param __last1 An input iterator.
  1529. * @param __first2 An input iterator.
  1530. * @param __last2 An input iterator.
  1531. * @param __binary_pred A binary predicate @link functors
  1532. * functor@endlink.
  1533. * @return A boolean true or false.
  1534. *
  1535. * This compares the elements of two ranges using the binary_pred
  1536. * parameter, and returns true or
  1537. * false depending on whether all of the corresponding elements of the
  1538. * ranges are equal.
  1539. */
  1540. template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
  1541. _GLIBCXX20_CONSTEXPR
  1542. inline bool
  1543. equal(_IIter1 __first1, _IIter1 __last1,
  1544. _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
  1545. {
  1546. // concept requirements
  1547. __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
  1548. __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
  1549. __glibcxx_requires_valid_range(__first1, __last1);
  1550. __glibcxx_requires_valid_range(__first2, __last2);
  1551. return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2,
  1552. __binary_pred);
  1553. }
  1554. #endif // C++14
  1555. /**
  1556. * @brief Performs @b dictionary comparison on ranges.
  1557. * @ingroup sorting_algorithms
  1558. * @param __first1 An input iterator.
  1559. * @param __last1 An input iterator.
  1560. * @param __first2 An input iterator.
  1561. * @param __last2 An input iterator.
  1562. * @return A boolean true or false.
  1563. *
  1564. * <em>Returns true if the sequence of elements defined by the range
  1565. * [first1,last1) is lexicographically less than the sequence of elements
  1566. * defined by the range [first2,last2). Returns false otherwise.</em>
  1567. * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
  1568. * then this is an inline call to @c memcmp.
  1569. */
  1570. template<typename _II1, typename _II2>
  1571. _GLIBCXX20_CONSTEXPR
  1572. inline bool
  1573. lexicographical_compare(_II1 __first1, _II1 __last1,
  1574. _II2 __first2, _II2 __last2)
  1575. {
  1576. #ifdef _GLIBCXX_CONCEPT_CHECKS
  1577. // concept requirements
  1578. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  1579. typedef typename iterator_traits<_II2>::value_type _ValueType2;
  1580. #endif
  1581. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1582. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1583. __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
  1584. __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
  1585. __glibcxx_requires_valid_range(__first1, __last1);
  1586. __glibcxx_requires_valid_range(__first2, __last2);
  1587. return std::__lexicographical_compare_aux(__first1, __last1,
  1588. __first2, __last2);
  1589. }
  1590. /**
  1591. * @brief Performs @b dictionary comparison on ranges.
  1592. * @ingroup sorting_algorithms
  1593. * @param __first1 An input iterator.
  1594. * @param __last1 An input iterator.
  1595. * @param __first2 An input iterator.
  1596. * @param __last2 An input iterator.
  1597. * @param __comp A @link comparison_functors comparison functor@endlink.
  1598. * @return A boolean true or false.
  1599. *
  1600. * The same as the four-parameter @c lexicographical_compare, but uses the
  1601. * comp parameter instead of @c <.
  1602. */
  1603. template<typename _II1, typename _II2, typename _Compare>
  1604. _GLIBCXX20_CONSTEXPR
  1605. inline bool
  1606. lexicographical_compare(_II1 __first1, _II1 __last1,
  1607. _II2 __first2, _II2 __last2, _Compare __comp)
  1608. {
  1609. // concept requirements
  1610. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1611. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1612. __glibcxx_requires_valid_range(__first1, __last1);
  1613. __glibcxx_requires_valid_range(__first2, __last2);
  1614. return std::__lexicographical_compare_impl
  1615. (__first1, __last1, __first2, __last2,
  1616. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  1617. }
  1618. #if __cpp_lib_three_way_comparison
  1619. // Iter points to a contiguous range of unsigned narrow character type
  1620. // or std::byte, suitable for comparison by memcmp.
  1621. template<typename _Iter>
  1622. concept __is_byte_iter = contiguous_iterator<_Iter>
  1623. && __is_memcmp_ordered<iter_value_t<_Iter>>::__value;
  1624. // Return a struct with two members, initialized to the smaller of x and y
  1625. // (or x if they compare equal) and the result of the comparison x <=> y.
  1626. template<typename _Tp>
  1627. constexpr auto
  1628. __min_cmp(_Tp __x, _Tp __y)
  1629. {
  1630. struct _Res {
  1631. _Tp _M_min;
  1632. decltype(__x <=> __y) _M_cmp;
  1633. };
  1634. auto __c = __x <=> __y;
  1635. if (__c > 0)
  1636. return _Res{__y, __c};
  1637. return _Res{__x, __c};
  1638. }
  1639. /**
  1640. * @brief Performs dictionary comparison on ranges.
  1641. * @ingroup sorting_algorithms
  1642. * @param __first1 An input iterator.
  1643. * @param __last1 An input iterator.
  1644. * @param __first2 An input iterator.
  1645. * @param __last2 An input iterator.
  1646. * @param __comp A @link comparison_functors comparison functor@endlink.
  1647. * @return The comparison category that `__comp(*__first1, *__first2)`
  1648. * returns.
  1649. */
  1650. template<typename _InputIter1, typename _InputIter2, typename _Comp>
  1651. constexpr auto
  1652. lexicographical_compare_three_way(_InputIter1 __first1,
  1653. _InputIter1 __last1,
  1654. _InputIter2 __first2,
  1655. _InputIter2 __last2,
  1656. _Comp __comp)
  1657. -> decltype(__comp(*__first1, *__first2))
  1658. {
  1659. // concept requirements
  1660. __glibcxx_function_requires(_InputIteratorConcept<_InputIter1>)
  1661. __glibcxx_function_requires(_InputIteratorConcept<_InputIter2>)
  1662. __glibcxx_requires_valid_range(__first1, __last1);
  1663. __glibcxx_requires_valid_range(__first2, __last2);
  1664. using _Cat = decltype(__comp(*__first1, *__first2));
  1665. static_assert(same_as<common_comparison_category_t<_Cat>, _Cat>);
  1666. if (!std::__is_constant_evaluated())
  1667. if constexpr (same_as<_Comp, __detail::_Synth3way>
  1668. || same_as<_Comp, compare_three_way>)
  1669. if constexpr (__is_byte_iter<_InputIter1>)
  1670. if constexpr (__is_byte_iter<_InputIter2>)
  1671. {
  1672. const auto [__len, __lencmp] = _GLIBCXX_STD_A::
  1673. __min_cmp(__last1 - __first1, __last2 - __first2);
  1674. if (__len)
  1675. {
  1676. const auto __c
  1677. = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0;
  1678. if (__c != 0)
  1679. return __c;
  1680. }
  1681. return __lencmp;
  1682. }
  1683. while (__first1 != __last1)
  1684. {
  1685. if (__first2 == __last2)
  1686. return strong_ordering::greater;
  1687. if (auto __cmp = __comp(*__first1, *__first2); __cmp != 0)
  1688. return __cmp;
  1689. ++__first1;
  1690. ++__first2;
  1691. }
  1692. return (__first2 == __last2) <=> true; // See PR 94006
  1693. }
  1694. template<typename _InputIter1, typename _InputIter2>
  1695. constexpr auto
  1696. lexicographical_compare_three_way(_InputIter1 __first1,
  1697. _InputIter1 __last1,
  1698. _InputIter2 __first2,
  1699. _InputIter2 __last2)
  1700. {
  1701. return _GLIBCXX_STD_A::
  1702. lexicographical_compare_three_way(__first1, __last1, __first2, __last2,
  1703. compare_three_way{});
  1704. }
  1705. #endif // three_way_comparison
  1706. template<typename _InputIterator1, typename _InputIterator2,
  1707. typename _BinaryPredicate>
  1708. _GLIBCXX20_CONSTEXPR
  1709. pair<_InputIterator1, _InputIterator2>
  1710. __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1711. _InputIterator2 __first2, _BinaryPredicate __binary_pred)
  1712. {
  1713. while (__first1 != __last1 && __binary_pred(__first1, __first2))
  1714. {
  1715. ++__first1;
  1716. ++__first2;
  1717. }
  1718. return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
  1719. }
  1720. /**
  1721. * @brief Finds the places in ranges which don't match.
  1722. * @ingroup non_mutating_algorithms
  1723. * @param __first1 An input iterator.
  1724. * @param __last1 An input iterator.
  1725. * @param __first2 An input iterator.
  1726. * @return A pair of iterators pointing to the first mismatch.
  1727. *
  1728. * This compares the elements of two ranges using @c == and returns a pair
  1729. * of iterators. The first iterator points into the first range, the
  1730. * second iterator points into the second range, and the elements pointed
  1731. * to by the iterators are not equal.
  1732. */
  1733. template<typename _InputIterator1, typename _InputIterator2>
  1734. _GLIBCXX20_CONSTEXPR
  1735. inline pair<_InputIterator1, _InputIterator2>
  1736. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1737. _InputIterator2 __first2)
  1738. {
  1739. // concept requirements
  1740. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1741. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1742. __glibcxx_function_requires(_EqualOpConcept<
  1743. typename iterator_traits<_InputIterator1>::value_type,
  1744. typename iterator_traits<_InputIterator2>::value_type>)
  1745. __glibcxx_requires_valid_range(__first1, __last1);
  1746. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
  1747. __gnu_cxx::__ops::__iter_equal_to_iter());
  1748. }
  1749. /**
  1750. * @brief Finds the places in ranges which don't match.
  1751. * @ingroup non_mutating_algorithms
  1752. * @param __first1 An input iterator.
  1753. * @param __last1 An input iterator.
  1754. * @param __first2 An input iterator.
  1755. * @param __binary_pred A binary predicate @link functors
  1756. * functor@endlink.
  1757. * @return A pair of iterators pointing to the first mismatch.
  1758. *
  1759. * This compares the elements of two ranges using the binary_pred
  1760. * parameter, and returns a pair
  1761. * of iterators. The first iterator points into the first range, the
  1762. * second iterator points into the second range, and the elements pointed
  1763. * to by the iterators are not equal.
  1764. */
  1765. template<typename _InputIterator1, typename _InputIterator2,
  1766. typename _BinaryPredicate>
  1767. _GLIBCXX20_CONSTEXPR
  1768. inline pair<_InputIterator1, _InputIterator2>
  1769. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1770. _InputIterator2 __first2, _BinaryPredicate __binary_pred)
  1771. {
  1772. // concept requirements
  1773. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1774. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1775. __glibcxx_requires_valid_range(__first1, __last1);
  1776. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
  1777. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
  1778. }
  1779. #if __cplusplus > 201103L
  1780. template<typename _InputIterator1, typename _InputIterator2,
  1781. typename _BinaryPredicate>
  1782. _GLIBCXX20_CONSTEXPR
  1783. pair<_InputIterator1, _InputIterator2>
  1784. __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1785. _InputIterator2 __first2, _InputIterator2 __last2,
  1786. _BinaryPredicate __binary_pred)
  1787. {
  1788. while (__first1 != __last1 && __first2 != __last2
  1789. && __binary_pred(__first1, __first2))
  1790. {
  1791. ++__first1;
  1792. ++__first2;
  1793. }
  1794. return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
  1795. }
  1796. /**
  1797. * @brief Finds the places in ranges which don't match.
  1798. * @ingroup non_mutating_algorithms
  1799. * @param __first1 An input iterator.
  1800. * @param __last1 An input iterator.
  1801. * @param __first2 An input iterator.
  1802. * @param __last2 An input iterator.
  1803. * @return A pair of iterators pointing to the first mismatch.
  1804. *
  1805. * This compares the elements of two ranges using @c == and returns a pair
  1806. * of iterators. The first iterator points into the first range, the
  1807. * second iterator points into the second range, and the elements pointed
  1808. * to by the iterators are not equal.
  1809. */
  1810. template<typename _InputIterator1, typename _InputIterator2>
  1811. _GLIBCXX20_CONSTEXPR
  1812. inline pair<_InputIterator1, _InputIterator2>
  1813. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1814. _InputIterator2 __first2, _InputIterator2 __last2)
  1815. {
  1816. // concept requirements
  1817. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1818. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1819. __glibcxx_function_requires(_EqualOpConcept<
  1820. typename iterator_traits<_InputIterator1>::value_type,
  1821. typename iterator_traits<_InputIterator2>::value_type>)
  1822. __glibcxx_requires_valid_range(__first1, __last1);
  1823. __glibcxx_requires_valid_range(__first2, __last2);
  1824. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
  1825. __gnu_cxx::__ops::__iter_equal_to_iter());
  1826. }
  1827. /**
  1828. * @brief Finds the places in ranges which don't match.
  1829. * @ingroup non_mutating_algorithms
  1830. * @param __first1 An input iterator.
  1831. * @param __last1 An input iterator.
  1832. * @param __first2 An input iterator.
  1833. * @param __last2 An input iterator.
  1834. * @param __binary_pred A binary predicate @link functors
  1835. * functor@endlink.
  1836. * @return A pair of iterators pointing to the first mismatch.
  1837. *
  1838. * This compares the elements of two ranges using the binary_pred
  1839. * parameter, and returns a pair
  1840. * of iterators. The first iterator points into the first range, the
  1841. * second iterator points into the second range, and the elements pointed
  1842. * to by the iterators are not equal.
  1843. */
  1844. template<typename _InputIterator1, typename _InputIterator2,
  1845. typename _BinaryPredicate>
  1846. _GLIBCXX20_CONSTEXPR
  1847. inline pair<_InputIterator1, _InputIterator2>
  1848. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1849. _InputIterator2 __first2, _InputIterator2 __last2,
  1850. _BinaryPredicate __binary_pred)
  1851. {
  1852. // concept requirements
  1853. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1854. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1855. __glibcxx_requires_valid_range(__first1, __last1);
  1856. __glibcxx_requires_valid_range(__first2, __last2);
  1857. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
  1858. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
  1859. }
  1860. #endif
  1861. _GLIBCXX_END_NAMESPACE_ALGO
  1862. /// This is an overload used by find algos for the Input Iterator case.
  1863. template<typename _InputIterator, typename _Predicate>
  1864. _GLIBCXX20_CONSTEXPR
  1865. inline _InputIterator
  1866. __find_if(_InputIterator __first, _InputIterator __last,
  1867. _Predicate __pred, input_iterator_tag)
  1868. {
  1869. while (__first != __last && !__pred(__first))
  1870. ++__first;
  1871. return __first;
  1872. }
  1873. /// This is an overload used by find algos for the RAI case.
  1874. template<typename _RandomAccessIterator, typename _Predicate>
  1875. _GLIBCXX20_CONSTEXPR
  1876. _RandomAccessIterator
  1877. __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
  1878. _Predicate __pred, random_access_iterator_tag)
  1879. {
  1880. typename iterator_traits<_RandomAccessIterator>::difference_type
  1881. __trip_count = (__last - __first) >> 2;
  1882. for (; __trip_count > 0; --__trip_count)
  1883. {
  1884. if (__pred(__first))
  1885. return __first;
  1886. ++__first;
  1887. if (__pred(__first))
  1888. return __first;
  1889. ++__first;
  1890. if (__pred(__first))
  1891. return __first;
  1892. ++__first;
  1893. if (__pred(__first))
  1894. return __first;
  1895. ++__first;
  1896. }
  1897. switch (__last - __first)
  1898. {
  1899. case 3:
  1900. if (__pred(__first))
  1901. return __first;
  1902. ++__first;
  1903. // FALLTHRU
  1904. case 2:
  1905. if (__pred(__first))
  1906. return __first;
  1907. ++__first;
  1908. // FALLTHRU
  1909. case 1:
  1910. if (__pred(__first))
  1911. return __first;
  1912. ++__first;
  1913. // FALLTHRU
  1914. case 0:
  1915. default:
  1916. return __last;
  1917. }
  1918. }
  1919. template<typename _Iterator, typename _Predicate>
  1920. _GLIBCXX20_CONSTEXPR
  1921. inline _Iterator
  1922. __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
  1923. {
  1924. return __find_if(__first, __last, __pred,
  1925. std::__iterator_category(__first));
  1926. }
  1927. template<typename _InputIterator, typename _Predicate>
  1928. _GLIBCXX20_CONSTEXPR
  1929. typename iterator_traits<_InputIterator>::difference_type
  1930. __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  1931. {
  1932. typename iterator_traits<_InputIterator>::difference_type __n = 0;
  1933. for (; __first != __last; ++__first)
  1934. if (__pred(__first))
  1935. ++__n;
  1936. return __n;
  1937. }
  1938. template<typename _ForwardIterator, typename _Predicate>
  1939. _GLIBCXX20_CONSTEXPR
  1940. _ForwardIterator
  1941. __remove_if(_ForwardIterator __first, _ForwardIterator __last,
  1942. _Predicate __pred)
  1943. {
  1944. __first = std::__find_if(__first, __last, __pred);
  1945. if (__first == __last)
  1946. return __first;
  1947. _ForwardIterator __result = __first;
  1948. ++__first;
  1949. for (; __first != __last; ++__first)
  1950. if (!__pred(__first))
  1951. {
  1952. *__result = _GLIBCXX_MOVE(*__first);
  1953. ++__result;
  1954. }
  1955. return __result;
  1956. }
  1957. #if __cplusplus >= 201103L
  1958. template<typename _ForwardIterator1, typename _ForwardIterator2,
  1959. typename _BinaryPredicate>
  1960. _GLIBCXX20_CONSTEXPR
  1961. bool
  1962. __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1963. _ForwardIterator2 __first2, _BinaryPredicate __pred)
  1964. {
  1965. // Efficiently compare identical prefixes: O(N) if sequences
  1966. // have the same elements in the same order.
  1967. for (; __first1 != __last1; ++__first1, (void)++__first2)
  1968. if (!__pred(__first1, __first2))
  1969. break;
  1970. if (__first1 == __last1)
  1971. return true;
  1972. // Establish __last2 assuming equal ranges by iterating over the
  1973. // rest of the list.
  1974. _ForwardIterator2 __last2 = __first2;
  1975. std::advance(__last2, std::distance(__first1, __last1));
  1976. for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
  1977. {
  1978. if (__scan != std::__find_if(__first1, __scan,
  1979. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
  1980. continue; // We've seen this one before.
  1981. auto __matches
  1982. = std::__count_if(__first2, __last2,
  1983. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
  1984. if (0 == __matches ||
  1985. std::__count_if(__scan, __last1,
  1986. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
  1987. != __matches)
  1988. return false;
  1989. }
  1990. return true;
  1991. }
  1992. /**
  1993. * @brief Checks whether a permutation of the second sequence is equal
  1994. * to the first sequence.
  1995. * @ingroup non_mutating_algorithms
  1996. * @param __first1 Start of first range.
  1997. * @param __last1 End of first range.
  1998. * @param __first2 Start of second range.
  1999. * @return true if there exists a permutation of the elements in the range
  2000. * [__first2, __first2 + (__last1 - __first1)), beginning with
  2001. * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
  2002. * returns true; otherwise, returns false.
  2003. */
  2004. template<typename _ForwardIterator1, typename _ForwardIterator2>
  2005. _GLIBCXX20_CONSTEXPR
  2006. inline bool
  2007. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  2008. _ForwardIterator2 __first2)
  2009. {
  2010. // concept requirements
  2011. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
  2012. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
  2013. __glibcxx_function_requires(_EqualOpConcept<
  2014. typename iterator_traits<_ForwardIterator1>::value_type,
  2015. typename iterator_traits<_ForwardIterator2>::value_type>)
  2016. __glibcxx_requires_valid_range(__first1, __last1);
  2017. return std::__is_permutation(__first1, __last1, __first2,
  2018. __gnu_cxx::__ops::__iter_equal_to_iter());
  2019. }
  2020. #endif // C++11
  2021. _GLIBCXX_END_NAMESPACE_VERSION
  2022. } // namespace std
  2023. // NB: This file is included within many other C++ includes, as a way
  2024. // of getting the base algorithms. So, make sure that parallel bits
  2025. // come in too if requested.
  2026. #ifdef _GLIBCXX_PARALLEL
  2027. # include <parallel/algobase.h>
  2028. #endif
  2029. #endif