stl_algobase.h 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  1. // Core algorithmic facilities -*- C++ -*-
  2. // Copyright (C) 2001-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. /*
  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<_IsMove,
  392. is_move_assignable<_Tp>,
  393. is_copy_assignable<_Tp>>;
  394. // trivial types can have deleted assignment
  395. static_assert( __assignable::type::value, "type is not 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>::value_type>)
  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<_IsMove,
  670. is_move_assignable<_Tp>,
  671. is_copy_assignable<_Tp>>;
  672. // trivial types can have deleted assignment
  673. static_assert( __assignable::type::value, "type is not 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(_ConvertibleConcept<
  779. typename iterator_traits<_BI1>::value_type,
  780. typename iterator_traits<_BI2>::value_type>)
  781. __glibcxx_requires_can_decrement_range(__first, __last, __result);
  782. return std::__copy_move_backward_a<__is_move_iterator<_BI1>::__value>
  783. (std::__miter_base(__first), std::__miter_base(__last), __result);
  784. }
  785. #if __cplusplus >= 201103L
  786. /**
  787. * @brief Moves the range [first,last) into result.
  788. * @ingroup mutating_algorithms
  789. * @param __first A bidirectional iterator.
  790. * @param __last A bidirectional iterator.
  791. * @param __result A bidirectional iterator.
  792. * @return result - (last - first)
  793. *
  794. * The function has the same effect as move, but starts at the end of the
  795. * range and works its way to the start, returning the start of the result.
  796. * This inline function will boil down to a call to @c memmove whenever
  797. * possible. Failing that, if random access iterators are passed, then the
  798. * loop count will be known (and therefore a candidate for compiler
  799. * optimizations such as unrolling).
  800. *
  801. * Result may not be in the range (first,last]. Use move instead. Note
  802. * that the start of the output range may overlap [first,last).
  803. */
  804. template<typename _BI1, typename _BI2>
  805. _GLIBCXX20_CONSTEXPR
  806. inline _BI2
  807. move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
  808. {
  809. // concept requirements
  810. __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
  811. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
  812. __glibcxx_function_requires(_ConvertibleConcept<
  813. typename iterator_traits<_BI1>::value_type,
  814. typename iterator_traits<_BI2>::value_type>)
  815. __glibcxx_requires_can_decrement_range(__first, __last, __result);
  816. return std::__copy_move_backward_a<true>(std::__miter_base(__first),
  817. std::__miter_base(__last),
  818. __result);
  819. }
  820. #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
  821. #else
  822. #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
  823. #endif
  824. template<typename _ForwardIterator, typename _Tp>
  825. _GLIBCXX20_CONSTEXPR
  826. inline typename
  827. __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
  828. __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
  829. const _Tp& __value)
  830. {
  831. for (; __first != __last; ++__first)
  832. *__first = __value;
  833. }
  834. template<typename _ForwardIterator, typename _Tp>
  835. _GLIBCXX20_CONSTEXPR
  836. inline typename
  837. __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
  838. __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
  839. const _Tp& __value)
  840. {
  841. const _Tp __tmp = __value;
  842. for (; __first != __last; ++__first)
  843. *__first = __tmp;
  844. }
  845. // Specialization: for char types we can use memset.
  846. template<typename _Tp>
  847. _GLIBCXX20_CONSTEXPR
  848. inline typename
  849. __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
  850. __fill_a1(_Tp* __first, _Tp* __last, const _Tp& __c)
  851. {
  852. const _Tp __tmp = __c;
  853. #if __cpp_lib_is_constant_evaluated
  854. if (std::is_constant_evaluated())
  855. {
  856. for (; __first != __last; ++__first)
  857. *__first = __tmp;
  858. return;
  859. }
  860. #endif
  861. if (const size_t __len = __last - __first)
  862. __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
  863. }
  864. template<typename _Ite, typename _Cont, typename _Tp>
  865. _GLIBCXX20_CONSTEXPR
  866. inline void
  867. __fill_a1(::__gnu_cxx::__normal_iterator<_Ite, _Cont> __first,
  868. ::__gnu_cxx::__normal_iterator<_Ite, _Cont> __last,
  869. const _Tp& __value)
  870. { std::__fill_a1(__first.base(), __last.base(), __value); }
  871. template<typename _Tp, typename _VTp>
  872. void
  873. __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
  874. const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
  875. const _VTp&);
  876. void
  877. __fill_a1(_GLIBCXX_STD_C::_Bit_iterator, _GLIBCXX_STD_C::_Bit_iterator,
  878. const bool&);
  879. template<typename _FIte, typename _Tp>
  880. _GLIBCXX20_CONSTEXPR
  881. inline void
  882. __fill_a(_FIte __first, _FIte __last, const _Tp& __value)
  883. { std::__fill_a1(__first, __last, __value); }
  884. template<typename _Ite, typename _Seq, typename _Cat, typename _Tp>
  885. void
  886. __fill_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
  887. const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
  888. const _Tp&);
  889. /**
  890. * @brief Fills the range [first,last) with copies of value.
  891. * @ingroup mutating_algorithms
  892. * @param __first A forward iterator.
  893. * @param __last A forward iterator.
  894. * @param __value A reference-to-const of arbitrary type.
  895. * @return Nothing.
  896. *
  897. * This function fills a range with copies of the same value. For char
  898. * types filling contiguous areas of memory, this becomes an inline call
  899. * to @c memset or @c wmemset.
  900. */
  901. template<typename _ForwardIterator, typename _Tp>
  902. _GLIBCXX20_CONSTEXPR
  903. inline void
  904. fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
  905. {
  906. // concept requirements
  907. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  908. _ForwardIterator>)
  909. __glibcxx_requires_valid_range(__first, __last);
  910. std::__fill_a(__first, __last, __value);
  911. }
  912. // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
  913. inline _GLIBCXX_CONSTEXPR int
  914. __size_to_integer(int __n) { return __n; }
  915. inline _GLIBCXX_CONSTEXPR unsigned
  916. __size_to_integer(unsigned __n) { return __n; }
  917. inline _GLIBCXX_CONSTEXPR long
  918. __size_to_integer(long __n) { return __n; }
  919. inline _GLIBCXX_CONSTEXPR unsigned long
  920. __size_to_integer(unsigned long __n) { return __n; }
  921. inline _GLIBCXX_CONSTEXPR long long
  922. __size_to_integer(long long __n) { return __n; }
  923. inline _GLIBCXX_CONSTEXPR unsigned long long
  924. __size_to_integer(unsigned long long __n) { return __n; }
  925. #if defined(__GLIBCXX_TYPE_INT_N_0)
  926. inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
  927. __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
  928. inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_0
  929. __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
  930. #endif
  931. #if defined(__GLIBCXX_TYPE_INT_N_1)
  932. inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
  933. __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
  934. inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_1
  935. __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
  936. #endif
  937. #if defined(__GLIBCXX_TYPE_INT_N_2)
  938. inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
  939. __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
  940. inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_2
  941. __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
  942. #endif
  943. #if defined(__GLIBCXX_TYPE_INT_N_3)
  944. inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_3
  945. __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
  946. inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
  947. __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
  948. #endif
  949. inline _GLIBCXX_CONSTEXPR long long
  950. __size_to_integer(float __n) { return (long long)__n; }
  951. inline _GLIBCXX_CONSTEXPR long long
  952. __size_to_integer(double __n) { return (long long)__n; }
  953. inline _GLIBCXX_CONSTEXPR long long
  954. __size_to_integer(long double __n) { return (long long)__n; }
  955. #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
  956. inline _GLIBCXX_CONSTEXPR long long
  957. __size_to_integer(__float128 __n) { return (long long)__n; }
  958. #endif
  959. template<typename _OutputIterator, typename _Size, typename _Tp>
  960. _GLIBCXX20_CONSTEXPR
  961. inline typename
  962. __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
  963. __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
  964. {
  965. for (; __n > 0; --__n, (void) ++__first)
  966. *__first = __value;
  967. return __first;
  968. }
  969. template<typename _OutputIterator, typename _Size, typename _Tp>
  970. _GLIBCXX20_CONSTEXPR
  971. inline typename
  972. __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
  973. __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
  974. {
  975. const _Tp __tmp = __value;
  976. for (; __n > 0; --__n, (void) ++__first)
  977. *__first = __tmp;
  978. return __first;
  979. }
  980. template<typename _Ite, typename _Seq, typename _Cat, typename _Size,
  981. typename _Tp>
  982. ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
  983. __fill_n_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
  984. _Size __n, const _Tp& __value,
  985. std::input_iterator_tag);
  986. template<typename _OutputIterator, typename _Size, typename _Tp>
  987. _GLIBCXX20_CONSTEXPR
  988. inline _OutputIterator
  989. __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
  990. std::output_iterator_tag)
  991. {
  992. #if __cplusplus >= 201103L
  993. static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
  994. #endif
  995. return __fill_n_a1(__first, __n, __value);
  996. }
  997. template<typename _OutputIterator, typename _Size, typename _Tp>
  998. _GLIBCXX20_CONSTEXPR
  999. inline _OutputIterator
  1000. __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
  1001. std::input_iterator_tag)
  1002. {
  1003. #if __cplusplus >= 201103L
  1004. static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
  1005. #endif
  1006. return __fill_n_a1(__first, __n, __value);
  1007. }
  1008. template<typename _OutputIterator, typename _Size, typename _Tp>
  1009. _GLIBCXX20_CONSTEXPR
  1010. inline _OutputIterator
  1011. __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
  1012. std::random_access_iterator_tag)
  1013. {
  1014. #if __cplusplus >= 201103L
  1015. static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
  1016. #endif
  1017. if (__n <= 0)
  1018. return __first;
  1019. __glibcxx_requires_can_increment(__first, __n);
  1020. std::__fill_a(__first, __first + __n, __value);
  1021. return __first + __n;
  1022. }
  1023. /**
  1024. * @brief Fills the range [first,first+n) with copies of value.
  1025. * @ingroup mutating_algorithms
  1026. * @param __first An output iterator.
  1027. * @param __n The count of copies to perform.
  1028. * @param __value A reference-to-const of arbitrary type.
  1029. * @return The iterator at first+n.
  1030. *
  1031. * This function fills a range with copies of the same value. For char
  1032. * types filling contiguous areas of memory, this becomes an inline call
  1033. * to @c memset or @c wmemset.
  1034. *
  1035. * If @p __n is negative, the function does nothing.
  1036. */
  1037. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1038. // DR 865. More algorithms that throw away information
  1039. // DR 426. search_n(), fill_n(), and generate_n() with negative n
  1040. template<typename _OI, typename _Size, typename _Tp>
  1041. _GLIBCXX20_CONSTEXPR
  1042. inline _OI
  1043. fill_n(_OI __first, _Size __n, const _Tp& __value)
  1044. {
  1045. // concept requirements
  1046. __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
  1047. return std::__fill_n_a(__first, std::__size_to_integer(__n), __value,
  1048. std::__iterator_category(__first));
  1049. }
  1050. template<bool _BoolType>
  1051. struct __equal
  1052. {
  1053. template<typename _II1, typename _II2>
  1054. _GLIBCXX20_CONSTEXPR
  1055. static bool
  1056. equal(_II1 __first1, _II1 __last1, _II2 __first2)
  1057. {
  1058. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  1059. if (!(*__first1 == *__first2))
  1060. return false;
  1061. return true;
  1062. }
  1063. };
  1064. template<>
  1065. struct __equal<true>
  1066. {
  1067. template<typename _Tp>
  1068. _GLIBCXX20_CONSTEXPR
  1069. static bool
  1070. equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
  1071. {
  1072. if (const size_t __len = (__last1 - __first1))
  1073. return !std::__memcmp(__first1, __first2, __len);
  1074. return true;
  1075. }
  1076. };
  1077. template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
  1078. typename __gnu_cxx::__enable_if<
  1079. __is_random_access_iter<_II>::__value, bool>::__type
  1080. __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
  1081. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
  1082. _II);
  1083. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1084. typename _Tp2, typename _Ref2, typename _Ptr2>
  1085. bool
  1086. __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1087. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1088. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
  1089. template<typename _II, typename _Tp, typename _Ref, typename _Ptr>
  1090. typename __gnu_cxx::__enable_if<
  1091. __is_random_access_iter<_II>::__value, bool>::__type
  1092. __equal_aux1(_II, _II,
  1093. _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>);
  1094. template<typename _II1, typename _II2>
  1095. _GLIBCXX20_CONSTEXPR
  1096. inline bool
  1097. __equal_aux1(_II1 __first1, _II1 __last1, _II2 __first2)
  1098. {
  1099. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  1100. const bool __simple = ((__is_integer<_ValueType1>::__value
  1101. || __is_pointer<_ValueType1>::__value)
  1102. && __memcmpable<_II1, _II2>::__value);
  1103. return std::__equal<__simple>::equal(__first1, __last1, __first2);
  1104. }
  1105. template<typename _II1, typename _II2>
  1106. _GLIBCXX20_CONSTEXPR
  1107. inline bool
  1108. __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
  1109. {
  1110. return std::__equal_aux1(std::__niter_base(__first1),
  1111. std::__niter_base(__last1),
  1112. std::__niter_base(__first2));
  1113. }
  1114. template<typename _II1, typename _Seq1, typename _Cat1, typename _II2>
  1115. bool
  1116. __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
  1117. const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
  1118. _II2);
  1119. template<typename _II1, typename _II2, typename _Seq2, typename _Cat2>
  1120. bool
  1121. __equal_aux(_II1, _II1,
  1122. const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
  1123. template<typename _II1, typename _Seq1, typename _Cat1,
  1124. typename _II2, typename _Seq2, typename _Cat2>
  1125. bool
  1126. __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
  1127. const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
  1128. const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
  1129. template<typename, typename>
  1130. struct __lc_rai
  1131. {
  1132. template<typename _II1, typename _II2>
  1133. _GLIBCXX20_CONSTEXPR
  1134. static _II1
  1135. __newlast1(_II1, _II1 __last1, _II2, _II2)
  1136. { return __last1; }
  1137. template<typename _II>
  1138. _GLIBCXX20_CONSTEXPR
  1139. static bool
  1140. __cnd2(_II __first, _II __last)
  1141. { return __first != __last; }
  1142. };
  1143. template<>
  1144. struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
  1145. {
  1146. template<typename _RAI1, typename _RAI2>
  1147. _GLIBCXX20_CONSTEXPR
  1148. static _RAI1
  1149. __newlast1(_RAI1 __first1, _RAI1 __last1,
  1150. _RAI2 __first2, _RAI2 __last2)
  1151. {
  1152. const typename iterator_traits<_RAI1>::difference_type
  1153. __diff1 = __last1 - __first1;
  1154. const typename iterator_traits<_RAI2>::difference_type
  1155. __diff2 = __last2 - __first2;
  1156. return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
  1157. }
  1158. template<typename _RAI>
  1159. static _GLIBCXX20_CONSTEXPR bool
  1160. __cnd2(_RAI, _RAI)
  1161. { return true; }
  1162. };
  1163. template<typename _II1, typename _II2, typename _Compare>
  1164. _GLIBCXX20_CONSTEXPR
  1165. bool
  1166. __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
  1167. _II2 __first2, _II2 __last2,
  1168. _Compare __comp)
  1169. {
  1170. typedef typename iterator_traits<_II1>::iterator_category _Category1;
  1171. typedef typename iterator_traits<_II2>::iterator_category _Category2;
  1172. typedef std::__lc_rai<_Category1, _Category2> __rai_type;
  1173. __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
  1174. for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
  1175. ++__first1, (void)++__first2)
  1176. {
  1177. if (__comp(__first1, __first2))
  1178. return true;
  1179. if (__comp(__first2, __first1))
  1180. return false;
  1181. }
  1182. return __first1 == __last1 && __first2 != __last2;
  1183. }
  1184. template<bool _BoolType>
  1185. struct __lexicographical_compare
  1186. {
  1187. template<typename _II1, typename _II2>
  1188. _GLIBCXX20_CONSTEXPR
  1189. static bool
  1190. __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1191. {
  1192. using __gnu_cxx::__ops::__iter_less_iter;
  1193. return std::__lexicographical_compare_impl(__first1, __last1,
  1194. __first2, __last2,
  1195. __iter_less_iter());
  1196. }
  1197. template<typename _II1, typename _II2>
  1198. _GLIBCXX20_CONSTEXPR
  1199. static int
  1200. __3way(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1201. {
  1202. while (__first1 != __last1)
  1203. {
  1204. if (__first2 == __last2)
  1205. return +1;
  1206. if (*__first1 < *__first2)
  1207. return -1;
  1208. if (*__first2 < *__first1)
  1209. return +1;
  1210. ++__first1;
  1211. ++__first2;
  1212. }
  1213. return int(__first2 == __last2) - 1;
  1214. }
  1215. };
  1216. template<>
  1217. struct __lexicographical_compare<true>
  1218. {
  1219. template<typename _Tp, typename _Up>
  1220. _GLIBCXX20_CONSTEXPR
  1221. static bool
  1222. __lc(const _Tp* __first1, const _Tp* __last1,
  1223. const _Up* __first2, const _Up* __last2)
  1224. { return __3way(__first1, __last1, __first2, __last2) < 0; }
  1225. template<typename _Tp, typename _Up>
  1226. _GLIBCXX20_CONSTEXPR
  1227. static ptrdiff_t
  1228. __3way(const _Tp* __first1, const _Tp* __last1,
  1229. const _Up* __first2, const _Up* __last2)
  1230. {
  1231. const size_t __len1 = __last1 - __first1;
  1232. const size_t __len2 = __last2 - __first2;
  1233. if (const size_t __len = std::min(__len1, __len2))
  1234. if (int __result = std::__memcmp(__first1, __first2, __len))
  1235. return __result;
  1236. return ptrdiff_t(__len1 - __len2);
  1237. }
  1238. };
  1239. template<typename _II1, typename _II2>
  1240. _GLIBCXX20_CONSTEXPR
  1241. inline bool
  1242. __lexicographical_compare_aux1(_II1 __first1, _II1 __last1,
  1243. _II2 __first2, _II2 __last2)
  1244. {
  1245. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  1246. typedef typename iterator_traits<_II2>::value_type _ValueType2;
  1247. const bool __simple =
  1248. (__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
  1249. && __is_pointer<_II1>::__value
  1250. && __is_pointer<_II2>::__value
  1251. #if __cplusplus > 201703L && __cpp_lib_concepts
  1252. // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
  1253. // so __is_byte<T> could be true, but we can't use memcmp with
  1254. // volatile data.
  1255. && !is_volatile_v<remove_reference_t<iter_reference_t<_II1>>>
  1256. && !is_volatile_v<remove_reference_t<iter_reference_t<_II2>>>
  1257. #endif
  1258. );
  1259. return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
  1260. __first2, __last2);
  1261. }
  1262. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1263. typename _Tp2>
  1264. bool
  1265. __lexicographical_compare_aux1(
  1266. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1267. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1268. _Tp2*, _Tp2*);
  1269. template<typename _Tp1,
  1270. typename _Tp2, typename _Ref2, typename _Ptr2>
  1271. bool
  1272. __lexicographical_compare_aux1(_Tp1*, _Tp1*,
  1273. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>,
  1274. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
  1275. template<typename _Tp1, typename _Ref1, typename _Ptr1,
  1276. typename _Tp2, typename _Ref2, typename _Ptr2>
  1277. bool
  1278. __lexicographical_compare_aux1(
  1279. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1280. _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
  1281. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>,
  1282. _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
  1283. template<typename _II1, typename _II2>
  1284. _GLIBCXX20_CONSTEXPR
  1285. inline bool
  1286. __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
  1287. _II2 __first2, _II2 __last2)
  1288. {
  1289. return std::__lexicographical_compare_aux1(std::__niter_base(__first1),
  1290. std::__niter_base(__last1),
  1291. std::__niter_base(__first2),
  1292. std::__niter_base(__last2));
  1293. }
  1294. template<typename _Iter1, typename _Seq1, typename _Cat1,
  1295. typename _II2>
  1296. bool
  1297. __lexicographical_compare_aux(
  1298. const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
  1299. const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
  1300. _II2, _II2);
  1301. template<typename _II1,
  1302. typename _Iter2, typename _Seq2, typename _Cat2>
  1303. bool
  1304. __lexicographical_compare_aux(
  1305. _II1, _II1,
  1306. const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&,
  1307. const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&);
  1308. template<typename _Iter1, typename _Seq1, typename _Cat1,
  1309. typename _Iter2, typename _Seq2, typename _Cat2>
  1310. bool
  1311. __lexicographical_compare_aux(
  1312. const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
  1313. const ::__gnu_debug::_Safe_iterator<_Iter1, _Seq1, _Cat1>&,
  1314. const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&,
  1315. const ::__gnu_debug::_Safe_iterator<_Iter2, _Seq2, _Cat2>&);
  1316. template<typename _ForwardIterator, typename _Tp, typename _Compare>
  1317. _GLIBCXX20_CONSTEXPR
  1318. _ForwardIterator
  1319. __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
  1320. const _Tp& __val, _Compare __comp)
  1321. {
  1322. typedef typename iterator_traits<_ForwardIterator>::difference_type
  1323. _DistanceType;
  1324. _DistanceType __len = std::distance(__first, __last);
  1325. while (__len > 0)
  1326. {
  1327. _DistanceType __half = __len >> 1;
  1328. _ForwardIterator __middle = __first;
  1329. std::advance(__middle, __half);
  1330. if (__comp(__middle, __val))
  1331. {
  1332. __first = __middle;
  1333. ++__first;
  1334. __len = __len - __half - 1;
  1335. }
  1336. else
  1337. __len = __half;
  1338. }
  1339. return __first;
  1340. }
  1341. /**
  1342. * @brief Finds the first position in which @a val could be inserted
  1343. * without changing the ordering.
  1344. * @param __first An iterator.
  1345. * @param __last Another iterator.
  1346. * @param __val The search term.
  1347. * @return An iterator pointing to the first element <em>not less
  1348. * than</em> @a val, or end() if every element is less than
  1349. * @a val.
  1350. * @ingroup binary_search_algorithms
  1351. */
  1352. template<typename _ForwardIterator, typename _Tp>
  1353. _GLIBCXX20_CONSTEXPR
  1354. inline _ForwardIterator
  1355. lower_bound(_ForwardIterator __first, _ForwardIterator __last,
  1356. const _Tp& __val)
  1357. {
  1358. // concept requirements
  1359. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  1360. __glibcxx_function_requires(_LessThanOpConcept<
  1361. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  1362. __glibcxx_requires_partitioned_lower(__first, __last, __val);
  1363. return std::__lower_bound(__first, __last, __val,
  1364. __gnu_cxx::__ops::__iter_less_val());
  1365. }
  1366. /// This is a helper function for the sort routines and for random.tcc.
  1367. // Precondition: __n > 0.
  1368. inline _GLIBCXX_CONSTEXPR int
  1369. __lg(int __n)
  1370. { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
  1371. inline _GLIBCXX_CONSTEXPR unsigned
  1372. __lg(unsigned __n)
  1373. { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
  1374. inline _GLIBCXX_CONSTEXPR long
  1375. __lg(long __n)
  1376. { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
  1377. inline _GLIBCXX_CONSTEXPR unsigned long
  1378. __lg(unsigned long __n)
  1379. { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
  1380. inline _GLIBCXX_CONSTEXPR long long
  1381. __lg(long long __n)
  1382. { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
  1383. inline _GLIBCXX_CONSTEXPR unsigned long long
  1384. __lg(unsigned long long __n)
  1385. { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
  1386. _GLIBCXX_BEGIN_NAMESPACE_ALGO
  1387. /**
  1388. * @brief Tests a range for element-wise equality.
  1389. * @ingroup non_mutating_algorithms
  1390. * @param __first1 An input iterator.
  1391. * @param __last1 An input iterator.
  1392. * @param __first2 An input iterator.
  1393. * @return A boolean true or false.
  1394. *
  1395. * This compares the elements of two ranges using @c == and returns true or
  1396. * false depending on whether all of the corresponding elements of the
  1397. * ranges are equal.
  1398. */
  1399. template<typename _II1, typename _II2>
  1400. _GLIBCXX20_CONSTEXPR
  1401. inline bool
  1402. equal(_II1 __first1, _II1 __last1, _II2 __first2)
  1403. {
  1404. // concept requirements
  1405. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1406. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1407. __glibcxx_function_requires(_EqualOpConcept<
  1408. typename iterator_traits<_II1>::value_type,
  1409. typename iterator_traits<_II2>::value_type>)
  1410. __glibcxx_requires_can_increment_range(__first1, __last1, __first2);
  1411. return std::__equal_aux(__first1, __last1, __first2);
  1412. }
  1413. /**
  1414. * @brief Tests a range for element-wise equality.
  1415. * @ingroup non_mutating_algorithms
  1416. * @param __first1 An input iterator.
  1417. * @param __last1 An input iterator.
  1418. * @param __first2 An input iterator.
  1419. * @param __binary_pred A binary predicate @link functors
  1420. * functor@endlink.
  1421. * @return A boolean true or false.
  1422. *
  1423. * This compares the elements of two ranges using the binary_pred
  1424. * parameter, and returns true or
  1425. * false depending on whether all of the corresponding elements of the
  1426. * ranges are equal.
  1427. */
  1428. template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
  1429. _GLIBCXX20_CONSTEXPR
  1430. inline bool
  1431. equal(_IIter1 __first1, _IIter1 __last1,
  1432. _IIter2 __first2, _BinaryPredicate __binary_pred)
  1433. {
  1434. // concept requirements
  1435. __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
  1436. __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
  1437. __glibcxx_requires_valid_range(__first1, __last1);
  1438. for (; __first1 != __last1; ++__first1, (void)++__first2)
  1439. if (!bool(__binary_pred(*__first1, *__first2)))
  1440. return false;
  1441. return true;
  1442. }
  1443. #if __cplusplus >= 201103L
  1444. // 4-iterator version of std::equal<It1, It2> for use in C++11.
  1445. template<typename _II1, typename _II2>
  1446. _GLIBCXX20_CONSTEXPR
  1447. inline bool
  1448. __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1449. {
  1450. using _RATag = random_access_iterator_tag;
  1451. using _Cat1 = typename iterator_traits<_II1>::iterator_category;
  1452. using _Cat2 = typename iterator_traits<_II2>::iterator_category;
  1453. using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
  1454. if (_RAIters())
  1455. {
  1456. auto __d1 = std::distance(__first1, __last1);
  1457. auto __d2 = std::distance(__first2, __last2);
  1458. if (__d1 != __d2)
  1459. return false;
  1460. return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
  1461. }
  1462. for (; __first1 != __last1 && __first2 != __last2;
  1463. ++__first1, (void)++__first2)
  1464. if (!(*__first1 == *__first2))
  1465. return false;
  1466. return __first1 == __last1 && __first2 == __last2;
  1467. }
  1468. // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
  1469. template<typename _II1, typename _II2, typename _BinaryPredicate>
  1470. _GLIBCXX20_CONSTEXPR
  1471. inline bool
  1472. __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
  1473. _BinaryPredicate __binary_pred)
  1474. {
  1475. using _RATag = random_access_iterator_tag;
  1476. using _Cat1 = typename iterator_traits<_II1>::iterator_category;
  1477. using _Cat2 = typename iterator_traits<_II2>::iterator_category;
  1478. using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
  1479. if (_RAIters())
  1480. {
  1481. auto __d1 = std::distance(__first1, __last1);
  1482. auto __d2 = std::distance(__first2, __last2);
  1483. if (__d1 != __d2)
  1484. return false;
  1485. return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
  1486. __binary_pred);
  1487. }
  1488. for (; __first1 != __last1 && __first2 != __last2;
  1489. ++__first1, (void)++__first2)
  1490. if (!bool(__binary_pred(*__first1, *__first2)))
  1491. return false;
  1492. return __first1 == __last1 && __first2 == __last2;
  1493. }
  1494. #endif // C++11
  1495. #if __cplusplus > 201103L
  1496. #define __cpp_lib_robust_nonmodifying_seq_ops 201304
  1497. /**
  1498. * @brief Tests a range for element-wise equality.
  1499. * @ingroup non_mutating_algorithms
  1500. * @param __first1 An input iterator.
  1501. * @param __last1 An input iterator.
  1502. * @param __first2 An input iterator.
  1503. * @param __last2 An input iterator.
  1504. * @return A boolean true or false.
  1505. *
  1506. * This compares the elements of two ranges using @c == and returns true or
  1507. * false depending on whether all of the corresponding elements of the
  1508. * ranges are equal.
  1509. */
  1510. template<typename _II1, typename _II2>
  1511. _GLIBCXX20_CONSTEXPR
  1512. inline bool
  1513. equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1514. {
  1515. // concept requirements
  1516. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1517. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1518. __glibcxx_function_requires(_EqualOpConcept<
  1519. typename iterator_traits<_II1>::value_type,
  1520. typename iterator_traits<_II2>::value_type>)
  1521. __glibcxx_requires_valid_range(__first1, __last1);
  1522. __glibcxx_requires_valid_range(__first2, __last2);
  1523. return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2);
  1524. }
  1525. /**
  1526. * @brief Tests a range for element-wise equality.
  1527. * @ingroup non_mutating_algorithms
  1528. * @param __first1 An input iterator.
  1529. * @param __last1 An input iterator.
  1530. * @param __first2 An input iterator.
  1531. * @param __last2 An input iterator.
  1532. * @param __binary_pred A binary predicate @link functors
  1533. * functor@endlink.
  1534. * @return A boolean true or false.
  1535. *
  1536. * This compares the elements of two ranges using the binary_pred
  1537. * parameter, and returns true or
  1538. * false depending on whether all of the corresponding elements of the
  1539. * ranges are equal.
  1540. */
  1541. template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
  1542. _GLIBCXX20_CONSTEXPR
  1543. inline bool
  1544. equal(_IIter1 __first1, _IIter1 __last1,
  1545. _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
  1546. {
  1547. // concept requirements
  1548. __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
  1549. __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
  1550. __glibcxx_requires_valid_range(__first1, __last1);
  1551. __glibcxx_requires_valid_range(__first2, __last2);
  1552. return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2,
  1553. __binary_pred);
  1554. }
  1555. #endif // C++14
  1556. /**
  1557. * @brief Performs @b dictionary comparison on ranges.
  1558. * @ingroup sorting_algorithms
  1559. * @param __first1 An input iterator.
  1560. * @param __last1 An input iterator.
  1561. * @param __first2 An input iterator.
  1562. * @param __last2 An input iterator.
  1563. * @return A boolean true or false.
  1564. *
  1565. * <em>Returns true if the sequence of elements defined by the range
  1566. * [first1,last1) is lexicographically less than the sequence of elements
  1567. * defined by the range [first2,last2). Returns false otherwise.</em>
  1568. * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
  1569. * then this is an inline call to @c memcmp.
  1570. */
  1571. template<typename _II1, typename _II2>
  1572. _GLIBCXX20_CONSTEXPR
  1573. inline bool
  1574. lexicographical_compare(_II1 __first1, _II1 __last1,
  1575. _II2 __first2, _II2 __last2)
  1576. {
  1577. #ifdef _GLIBCXX_CONCEPT_CHECKS
  1578. // concept requirements
  1579. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  1580. typedef typename iterator_traits<_II2>::value_type _ValueType2;
  1581. #endif
  1582. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1583. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1584. __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
  1585. __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
  1586. __glibcxx_requires_valid_range(__first1, __last1);
  1587. __glibcxx_requires_valid_range(__first2, __last2);
  1588. return std::__lexicographical_compare_aux(__first1, __last1,
  1589. __first2, __last2);
  1590. }
  1591. /**
  1592. * @brief Performs @b dictionary comparison on ranges.
  1593. * @ingroup sorting_algorithms
  1594. * @param __first1 An input iterator.
  1595. * @param __last1 An input iterator.
  1596. * @param __first2 An input iterator.
  1597. * @param __last2 An input iterator.
  1598. * @param __comp A @link comparison_functors comparison functor@endlink.
  1599. * @return A boolean true or false.
  1600. *
  1601. * The same as the four-parameter @c lexicographical_compare, but uses the
  1602. * comp parameter instead of @c <.
  1603. */
  1604. template<typename _II1, typename _II2, typename _Compare>
  1605. _GLIBCXX20_CONSTEXPR
  1606. inline bool
  1607. lexicographical_compare(_II1 __first1, _II1 __last1,
  1608. _II2 __first2, _II2 __last2, _Compare __comp)
  1609. {
  1610. // concept requirements
  1611. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1612. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1613. __glibcxx_requires_valid_range(__first1, __last1);
  1614. __glibcxx_requires_valid_range(__first2, __last2);
  1615. return std::__lexicographical_compare_impl
  1616. (__first1, __last1, __first2, __last2,
  1617. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  1618. }
  1619. #if __cpp_lib_three_way_comparison
  1620. // Iter points to a contiguous range of unsigned narrow character type
  1621. // or std::byte, suitable for comparison by memcmp.
  1622. template<typename _Iter>
  1623. concept __is_byte_iter = contiguous_iterator<_Iter>
  1624. && __is_memcmp_ordered<iter_value_t<_Iter>>::__value;
  1625. // Return a struct with two members, initialized to the smaller of x and y
  1626. // (or x if they compare equal) and the result of the comparison x <=> y.
  1627. template<typename _Tp>
  1628. constexpr auto
  1629. __min_cmp(_Tp __x, _Tp __y)
  1630. {
  1631. struct _Res {
  1632. _Tp _M_min;
  1633. decltype(__x <=> __y) _M_cmp;
  1634. };
  1635. auto __c = __x <=> __y;
  1636. if (__c > 0)
  1637. return _Res{__y, __c};
  1638. return _Res{__x, __c};
  1639. }
  1640. /**
  1641. * @brief Performs dictionary comparison on ranges.
  1642. * @ingroup sorting_algorithms
  1643. * @param __first1 An input iterator.
  1644. * @param __last1 An input iterator.
  1645. * @param __first2 An input iterator.
  1646. * @param __last2 An input iterator.
  1647. * @param __comp A @link comparison_functors comparison functor@endlink.
  1648. * @return The comparison category that `__comp(*__first1, *__first2)`
  1649. * returns.
  1650. */
  1651. template<typename _InputIter1, typename _InputIter2, typename _Comp>
  1652. constexpr auto
  1653. lexicographical_compare_three_way(_InputIter1 __first1,
  1654. _InputIter1 __last1,
  1655. _InputIter2 __first2,
  1656. _InputIter2 __last2,
  1657. _Comp __comp)
  1658. -> decltype(__comp(*__first1, *__first2))
  1659. {
  1660. // concept requirements
  1661. __glibcxx_function_requires(_InputIteratorConcept<_InputIter1>)
  1662. __glibcxx_function_requires(_InputIteratorConcept<_InputIter2>)
  1663. __glibcxx_requires_valid_range(__first1, __last1);
  1664. __glibcxx_requires_valid_range(__first2, __last2);
  1665. #if __cpp_lib_is_constant_evaluated
  1666. using _Cat = decltype(__comp(*__first1, *__first2));
  1667. static_assert(same_as<common_comparison_category_t<_Cat>, _Cat>);
  1668. if (!std::is_constant_evaluated())
  1669. if constexpr (same_as<_Comp, __detail::_Synth3way>
  1670. || same_as<_Comp, compare_three_way>)
  1671. if constexpr (__is_byte_iter<_InputIter1>)
  1672. if constexpr (__is_byte_iter<_InputIter2>)
  1673. {
  1674. const auto [__len, __lencmp] = _GLIBCXX_STD_A::
  1675. __min_cmp(__last1 - __first1, __last2 - __first2);
  1676. if (__len)
  1677. {
  1678. const auto __c
  1679. = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0;
  1680. if (__c != 0)
  1681. return __c;
  1682. }
  1683. return __lencmp;
  1684. }
  1685. #endif // is_constant_evaluated
  1686. while (__first1 != __last1)
  1687. {
  1688. if (__first2 == __last2)
  1689. return strong_ordering::greater;
  1690. if (auto __cmp = __comp(*__first1, *__first2); __cmp != 0)
  1691. return __cmp;
  1692. ++__first1;
  1693. ++__first2;
  1694. }
  1695. return (__first2 == __last2) <=> true; // See PR 94006
  1696. }
  1697. template<typename _InputIter1, typename _InputIter2>
  1698. constexpr auto
  1699. lexicographical_compare_three_way(_InputIter1 __first1,
  1700. _InputIter1 __last1,
  1701. _InputIter2 __first2,
  1702. _InputIter2 __last2)
  1703. {
  1704. return _GLIBCXX_STD_A::
  1705. lexicographical_compare_three_way(__first1, __last1, __first2, __last2,
  1706. compare_three_way{});
  1707. }
  1708. #endif // three_way_comparison
  1709. template<typename _InputIterator1, typename _InputIterator2,
  1710. typename _BinaryPredicate>
  1711. _GLIBCXX20_CONSTEXPR
  1712. pair<_InputIterator1, _InputIterator2>
  1713. __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1714. _InputIterator2 __first2, _BinaryPredicate __binary_pred)
  1715. {
  1716. while (__first1 != __last1 && __binary_pred(__first1, __first2))
  1717. {
  1718. ++__first1;
  1719. ++__first2;
  1720. }
  1721. return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
  1722. }
  1723. /**
  1724. * @brief Finds the places in ranges which don't match.
  1725. * @ingroup non_mutating_algorithms
  1726. * @param __first1 An input iterator.
  1727. * @param __last1 An input iterator.
  1728. * @param __first2 An input iterator.
  1729. * @return A pair of iterators pointing to the first mismatch.
  1730. *
  1731. * This compares the elements of two ranges using @c == and returns a pair
  1732. * of iterators. The first iterator points into the first range, the
  1733. * second iterator points into the second range, and the elements pointed
  1734. * to by the iterators are not equal.
  1735. */
  1736. template<typename _InputIterator1, typename _InputIterator2>
  1737. _GLIBCXX20_CONSTEXPR
  1738. inline pair<_InputIterator1, _InputIterator2>
  1739. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1740. _InputIterator2 __first2)
  1741. {
  1742. // concept requirements
  1743. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1744. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1745. __glibcxx_function_requires(_EqualOpConcept<
  1746. typename iterator_traits<_InputIterator1>::value_type,
  1747. typename iterator_traits<_InputIterator2>::value_type>)
  1748. __glibcxx_requires_valid_range(__first1, __last1);
  1749. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
  1750. __gnu_cxx::__ops::__iter_equal_to_iter());
  1751. }
  1752. /**
  1753. * @brief Finds the places in ranges which don't match.
  1754. * @ingroup non_mutating_algorithms
  1755. * @param __first1 An input iterator.
  1756. * @param __last1 An input iterator.
  1757. * @param __first2 An input iterator.
  1758. * @param __binary_pred A binary predicate @link functors
  1759. * functor@endlink.
  1760. * @return A pair of iterators pointing to the first mismatch.
  1761. *
  1762. * This compares the elements of two ranges using the binary_pred
  1763. * parameter, and returns a pair
  1764. * of iterators. The first iterator points into the first range, the
  1765. * second iterator points into the second range, and the elements pointed
  1766. * to by the iterators are not equal.
  1767. */
  1768. template<typename _InputIterator1, typename _InputIterator2,
  1769. typename _BinaryPredicate>
  1770. _GLIBCXX20_CONSTEXPR
  1771. inline pair<_InputIterator1, _InputIterator2>
  1772. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1773. _InputIterator2 __first2, _BinaryPredicate __binary_pred)
  1774. {
  1775. // concept requirements
  1776. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1777. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1778. __glibcxx_requires_valid_range(__first1, __last1);
  1779. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
  1780. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
  1781. }
  1782. #if __cplusplus > 201103L
  1783. template<typename _InputIterator1, typename _InputIterator2,
  1784. typename _BinaryPredicate>
  1785. _GLIBCXX20_CONSTEXPR
  1786. pair<_InputIterator1, _InputIterator2>
  1787. __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1788. _InputIterator2 __first2, _InputIterator2 __last2,
  1789. _BinaryPredicate __binary_pred)
  1790. {
  1791. while (__first1 != __last1 && __first2 != __last2
  1792. && __binary_pred(__first1, __first2))
  1793. {
  1794. ++__first1;
  1795. ++__first2;
  1796. }
  1797. return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
  1798. }
  1799. /**
  1800. * @brief Finds the places in ranges which don't match.
  1801. * @ingroup non_mutating_algorithms
  1802. * @param __first1 An input iterator.
  1803. * @param __last1 An input iterator.
  1804. * @param __first2 An input iterator.
  1805. * @param __last2 An input iterator.
  1806. * @return A pair of iterators pointing to the first mismatch.
  1807. *
  1808. * This compares the elements of two ranges using @c == and returns a pair
  1809. * of iterators. The first iterator points into the first range, the
  1810. * second iterator points into the second range, and the elements pointed
  1811. * to by the iterators are not equal.
  1812. */
  1813. template<typename _InputIterator1, typename _InputIterator2>
  1814. _GLIBCXX20_CONSTEXPR
  1815. inline pair<_InputIterator1, _InputIterator2>
  1816. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1817. _InputIterator2 __first2, _InputIterator2 __last2)
  1818. {
  1819. // concept requirements
  1820. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1821. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1822. __glibcxx_function_requires(_EqualOpConcept<
  1823. typename iterator_traits<_InputIterator1>::value_type,
  1824. typename iterator_traits<_InputIterator2>::value_type>)
  1825. __glibcxx_requires_valid_range(__first1, __last1);
  1826. __glibcxx_requires_valid_range(__first2, __last2);
  1827. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
  1828. __gnu_cxx::__ops::__iter_equal_to_iter());
  1829. }
  1830. /**
  1831. * @brief Finds the places in ranges which don't match.
  1832. * @ingroup non_mutating_algorithms
  1833. * @param __first1 An input iterator.
  1834. * @param __last1 An input iterator.
  1835. * @param __first2 An input iterator.
  1836. * @param __last2 An input iterator.
  1837. * @param __binary_pred A binary predicate @link functors
  1838. * functor@endlink.
  1839. * @return A pair of iterators pointing to the first mismatch.
  1840. *
  1841. * This compares the elements of two ranges using the binary_pred
  1842. * parameter, and returns a pair
  1843. * of iterators. The first iterator points into the first range, the
  1844. * second iterator points into the second range, and the elements pointed
  1845. * to by the iterators are not equal.
  1846. */
  1847. template<typename _InputIterator1, typename _InputIterator2,
  1848. typename _BinaryPredicate>
  1849. _GLIBCXX20_CONSTEXPR
  1850. inline pair<_InputIterator1, _InputIterator2>
  1851. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1852. _InputIterator2 __first2, _InputIterator2 __last2,
  1853. _BinaryPredicate __binary_pred)
  1854. {
  1855. // concept requirements
  1856. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1857. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1858. __glibcxx_requires_valid_range(__first1, __last1);
  1859. __glibcxx_requires_valid_range(__first2, __last2);
  1860. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
  1861. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
  1862. }
  1863. #endif
  1864. _GLIBCXX_END_NAMESPACE_ALGO
  1865. /// This is an overload used by find algos for the Input Iterator case.
  1866. template<typename _InputIterator, typename _Predicate>
  1867. _GLIBCXX20_CONSTEXPR
  1868. inline _InputIterator
  1869. __find_if(_InputIterator __first, _InputIterator __last,
  1870. _Predicate __pred, input_iterator_tag)
  1871. {
  1872. while (__first != __last && !__pred(__first))
  1873. ++__first;
  1874. return __first;
  1875. }
  1876. /// This is an overload used by find algos for the RAI case.
  1877. template<typename _RandomAccessIterator, typename _Predicate>
  1878. _GLIBCXX20_CONSTEXPR
  1879. _RandomAccessIterator
  1880. __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
  1881. _Predicate __pred, random_access_iterator_tag)
  1882. {
  1883. typename iterator_traits<_RandomAccessIterator>::difference_type
  1884. __trip_count = (__last - __first) >> 2;
  1885. for (; __trip_count > 0; --__trip_count)
  1886. {
  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. if (__pred(__first))
  1897. return __first;
  1898. ++__first;
  1899. }
  1900. switch (__last - __first)
  1901. {
  1902. case 3:
  1903. if (__pred(__first))
  1904. return __first;
  1905. ++__first;
  1906. // FALLTHRU
  1907. case 2:
  1908. if (__pred(__first))
  1909. return __first;
  1910. ++__first;
  1911. // FALLTHRU
  1912. case 1:
  1913. if (__pred(__first))
  1914. return __first;
  1915. ++__first;
  1916. // FALLTHRU
  1917. case 0:
  1918. default:
  1919. return __last;
  1920. }
  1921. }
  1922. template<typename _Iterator, typename _Predicate>
  1923. _GLIBCXX20_CONSTEXPR
  1924. inline _Iterator
  1925. __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
  1926. {
  1927. return __find_if(__first, __last, __pred,
  1928. std::__iterator_category(__first));
  1929. }
  1930. template<typename _InputIterator, typename _Predicate>
  1931. _GLIBCXX20_CONSTEXPR
  1932. typename iterator_traits<_InputIterator>::difference_type
  1933. __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
  1934. {
  1935. typename iterator_traits<_InputIterator>::difference_type __n = 0;
  1936. for (; __first != __last; ++__first)
  1937. if (__pred(__first))
  1938. ++__n;
  1939. return __n;
  1940. }
  1941. #if __cplusplus >= 201103L
  1942. template<typename _ForwardIterator1, typename _ForwardIterator2,
  1943. typename _BinaryPredicate>
  1944. _GLIBCXX20_CONSTEXPR
  1945. bool
  1946. __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1947. _ForwardIterator2 __first2, _BinaryPredicate __pred)
  1948. {
  1949. // Efficiently compare identical prefixes: O(N) if sequences
  1950. // have the same elements in the same order.
  1951. for (; __first1 != __last1; ++__first1, (void)++__first2)
  1952. if (!__pred(__first1, __first2))
  1953. break;
  1954. if (__first1 == __last1)
  1955. return true;
  1956. // Establish __last2 assuming equal ranges by iterating over the
  1957. // rest of the list.
  1958. _ForwardIterator2 __last2 = __first2;
  1959. std::advance(__last2, std::distance(__first1, __last1));
  1960. for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
  1961. {
  1962. if (__scan != std::__find_if(__first1, __scan,
  1963. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
  1964. continue; // We've seen this one before.
  1965. auto __matches
  1966. = std::__count_if(__first2, __last2,
  1967. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
  1968. if (0 == __matches ||
  1969. std::__count_if(__scan, __last1,
  1970. __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
  1971. != __matches)
  1972. return false;
  1973. }
  1974. return true;
  1975. }
  1976. /**
  1977. * @brief Checks whether a permutation of the second sequence is equal
  1978. * to the first sequence.
  1979. * @ingroup non_mutating_algorithms
  1980. * @param __first1 Start of first range.
  1981. * @param __last1 End of first range.
  1982. * @param __first2 Start of second range.
  1983. * @return true if there exists a permutation of the elements in the range
  1984. * [__first2, __first2 + (__last1 - __first1)), beginning with
  1985. * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
  1986. * returns true; otherwise, returns false.
  1987. */
  1988. template<typename _ForwardIterator1, typename _ForwardIterator2>
  1989. _GLIBCXX20_CONSTEXPR
  1990. inline bool
  1991. is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  1992. _ForwardIterator2 __first2)
  1993. {
  1994. // concept requirements
  1995. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
  1996. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
  1997. __glibcxx_function_requires(_EqualOpConcept<
  1998. typename iterator_traits<_ForwardIterator1>::value_type,
  1999. typename iterator_traits<_ForwardIterator2>::value_type>)
  2000. __glibcxx_requires_valid_range(__first1, __last1);
  2001. return std::__is_permutation(__first1, __last1, __first2,
  2002. __gnu_cxx::__ops::__iter_equal_to_iter());
  2003. }
  2004. #endif // C++11
  2005. _GLIBCXX_END_NAMESPACE_VERSION
  2006. } // namespace std
  2007. // NB: This file is included within many other C++ includes, as a way
  2008. // of getting the base algorithms. So, make sure that parallel bits
  2009. // come in too if requested.
  2010. #ifdef _GLIBCXX_PARALLEL
  2011. # include <parallel/algobase.h>
  2012. #endif
  2013. #endif