您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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