stl_algobase.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. // Core algorithmic facilities -*- C++ -*-
  2. // Copyright (C) 2001-2019 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. namespace std _GLIBCXX_VISIBILITY(default)
  68. {
  69. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  70. #if __cplusplus < 201103L
  71. // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
  72. // nutshell, we are partially implementing the resolution of DR 187,
  73. // when it's safe, i.e., the value_types are equal.
  74. template<bool _BoolType>
  75. struct __iter_swap
  76. {
  77. template<typename _ForwardIterator1, typename _ForwardIterator2>
  78. static void
  79. iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
  80. {
  81. typedef typename iterator_traits<_ForwardIterator1>::value_type
  82. _ValueType1;
  83. _ValueType1 __tmp = *__a;
  84. *__a = *__b;
  85. *__b = __tmp;
  86. }
  87. };
  88. template<>
  89. struct __iter_swap<true>
  90. {
  91. template<typename _ForwardIterator1, typename _ForwardIterator2>
  92. static void
  93. iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
  94. {
  95. swap(*__a, *__b);
  96. }
  97. };
  98. #endif
  99. /**
  100. * @brief Swaps the contents of two iterators.
  101. * @ingroup mutating_algorithms
  102. * @param __a An iterator.
  103. * @param __b Another iterator.
  104. * @return Nothing.
  105. *
  106. * This function swaps the values pointed to by two iterators, not the
  107. * iterators themselves.
  108. */
  109. template<typename _ForwardIterator1, typename _ForwardIterator2>
  110. inline void
  111. iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
  112. {
  113. // concept requirements
  114. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  115. _ForwardIterator1>)
  116. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  117. _ForwardIterator2>)
  118. #if __cplusplus < 201103L
  119. typedef typename iterator_traits<_ForwardIterator1>::value_type
  120. _ValueType1;
  121. typedef typename iterator_traits<_ForwardIterator2>::value_type
  122. _ValueType2;
  123. __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
  124. _ValueType2>)
  125. __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
  126. _ValueType1>)
  127. typedef typename iterator_traits<_ForwardIterator1>::reference
  128. _ReferenceType1;
  129. typedef typename iterator_traits<_ForwardIterator2>::reference
  130. _ReferenceType2;
  131. std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
  132. && __are_same<_ValueType1&, _ReferenceType1>::__value
  133. && __are_same<_ValueType2&, _ReferenceType2>::__value>::
  134. iter_swap(__a, __b);
  135. #else
  136. swap(*__a, *__b);
  137. #endif
  138. }
  139. /**
  140. * @brief Swap the elements of two sequences.
  141. * @ingroup mutating_algorithms
  142. * @param __first1 A forward iterator.
  143. * @param __last1 A forward iterator.
  144. * @param __first2 A forward iterator.
  145. * @return An iterator equal to @p first2+(last1-first1).
  146. *
  147. * Swaps each element in the range @p [first1,last1) with the
  148. * corresponding element in the range @p [first2,(last1-first1)).
  149. * The ranges must not overlap.
  150. */
  151. template<typename _ForwardIterator1, typename _ForwardIterator2>
  152. _ForwardIterator2
  153. swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
  154. _ForwardIterator2 __first2)
  155. {
  156. // concept requirements
  157. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  158. _ForwardIterator1>)
  159. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  160. _ForwardIterator2>)
  161. __glibcxx_requires_valid_range(__first1, __last1);
  162. for (; __first1 != __last1; ++__first1, (void)++__first2)
  163. std::iter_swap(__first1, __first2);
  164. return __first2;
  165. }
  166. /**
  167. * @brief This does what you think it does.
  168. * @ingroup sorting_algorithms
  169. * @param __a A thing of arbitrary type.
  170. * @param __b Another thing of arbitrary type.
  171. * @return The lesser of the parameters.
  172. *
  173. * This is the simple classic generic implementation. It will work on
  174. * temporary expressions, since they are only evaluated once, unlike a
  175. * preprocessor macro.
  176. */
  177. template<typename _Tp>
  178. _GLIBCXX14_CONSTEXPR
  179. inline const _Tp&
  180. min(const _Tp& __a, const _Tp& __b)
  181. {
  182. // concept requirements
  183. __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  184. //return __b < __a ? __b : __a;
  185. if (__b < __a)
  186. return __b;
  187. return __a;
  188. }
  189. /**
  190. * @brief This does what you think it does.
  191. * @ingroup sorting_algorithms
  192. * @param __a A thing of arbitrary type.
  193. * @param __b Another thing of arbitrary type.
  194. * @return The greater of the parameters.
  195. *
  196. * This is the simple classic generic implementation. It will work on
  197. * temporary expressions, since they are only evaluated once, unlike a
  198. * preprocessor macro.
  199. */
  200. template<typename _Tp>
  201. _GLIBCXX14_CONSTEXPR
  202. inline const _Tp&
  203. max(const _Tp& __a, const _Tp& __b)
  204. {
  205. // concept requirements
  206. __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
  207. //return __a < __b ? __b : __a;
  208. if (__a < __b)
  209. return __b;
  210. return __a;
  211. }
  212. /**
  213. * @brief This does what you think it does.
  214. * @ingroup sorting_algorithms
  215. * @param __a A thing of arbitrary type.
  216. * @param __b Another thing of arbitrary type.
  217. * @param __comp A @link comparison_functors comparison functor@endlink.
  218. * @return The lesser of the parameters.
  219. *
  220. * This will work on temporary expressions, since they are only evaluated
  221. * once, unlike a preprocessor macro.
  222. */
  223. template<typename _Tp, typename _Compare>
  224. _GLIBCXX14_CONSTEXPR
  225. inline const _Tp&
  226. min(const _Tp& __a, const _Tp& __b, _Compare __comp)
  227. {
  228. //return __comp(__b, __a) ? __b : __a;
  229. if (__comp(__b, __a))
  230. return __b;
  231. return __a;
  232. }
  233. /**
  234. * @brief This does what you think it does.
  235. * @ingroup sorting_algorithms
  236. * @param __a A thing of arbitrary type.
  237. * @param __b Another thing of arbitrary type.
  238. * @param __comp A @link comparison_functors comparison functor@endlink.
  239. * @return The greater of the parameters.
  240. *
  241. * This will work on temporary expressions, since they are only evaluated
  242. * once, unlike a preprocessor macro.
  243. */
  244. template<typename _Tp, typename _Compare>
  245. _GLIBCXX14_CONSTEXPR
  246. inline const _Tp&
  247. max(const _Tp& __a, const _Tp& __b, _Compare __comp)
  248. {
  249. //return __comp(__a, __b) ? __b : __a;
  250. if (__comp(__a, __b))
  251. return __b;
  252. return __a;
  253. }
  254. // Fallback implementation of the function in bits/stl_iterator.h used to
  255. // remove the __normal_iterator wrapper. See copy, fill, ...
  256. template<typename _Iterator>
  257. inline _Iterator
  258. __niter_base(_Iterator __it)
  259. _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)
  260. { return __it; }
  261. // Reverse the __niter_base transformation to get a
  262. // __normal_iterator back again (this assumes that __normal_iterator
  263. // is only used to wrap random access iterators, like pointers).
  264. template<typename _From, typename _To>
  265. inline _From
  266. __niter_wrap(_From __from, _To __res)
  267. { return __from + (__res - std::__niter_base(__from)); }
  268. // No need to wrap, iterator already has the right type.
  269. template<typename _Iterator>
  270. inline _Iterator
  271. __niter_wrap(const _Iterator&, _Iterator __res)
  272. { return __res; }
  273. // All of these auxiliary structs serve two purposes. (1) Replace
  274. // calls to copy with memmove whenever possible. (Memmove, not memcpy,
  275. // because the input and output ranges are permitted to overlap.)
  276. // (2) If we're using random access iterators, then write the loop as
  277. // a for loop with an explicit count.
  278. template<bool _IsMove, bool _IsSimple, typename _Category>
  279. struct __copy_move
  280. {
  281. template<typename _II, typename _OI>
  282. static _OI
  283. __copy_m(_II __first, _II __last, _OI __result)
  284. {
  285. for (; __first != __last; ++__result, (void)++__first)
  286. *__result = *__first;
  287. return __result;
  288. }
  289. };
  290. #if __cplusplus >= 201103L
  291. template<typename _Category>
  292. struct __copy_move<true, false, _Category>
  293. {
  294. template<typename _II, typename _OI>
  295. static _OI
  296. __copy_m(_II __first, _II __last, _OI __result)
  297. {
  298. for (; __first != __last; ++__result, (void)++__first)
  299. *__result = std::move(*__first);
  300. return __result;
  301. }
  302. };
  303. #endif
  304. template<>
  305. struct __copy_move<false, false, random_access_iterator_tag>
  306. {
  307. template<typename _II, typename _OI>
  308. static _OI
  309. __copy_m(_II __first, _II __last, _OI __result)
  310. {
  311. typedef typename iterator_traits<_II>::difference_type _Distance;
  312. for(_Distance __n = __last - __first; __n > 0; --__n)
  313. {
  314. *__result = *__first;
  315. ++__first;
  316. ++__result;
  317. }
  318. return __result;
  319. }
  320. };
  321. #if __cplusplus >= 201103L
  322. template<>
  323. struct __copy_move<true, false, random_access_iterator_tag>
  324. {
  325. template<typename _II, typename _OI>
  326. static _OI
  327. __copy_m(_II __first, _II __last, _OI __result)
  328. {
  329. typedef typename iterator_traits<_II>::difference_type _Distance;
  330. for(_Distance __n = __last - __first; __n > 0; --__n)
  331. {
  332. *__result = std::move(*__first);
  333. ++__first;
  334. ++__result;
  335. }
  336. return __result;
  337. }
  338. };
  339. #endif
  340. template<bool _IsMove>
  341. struct __copy_move<_IsMove, true, random_access_iterator_tag>
  342. {
  343. template<typename _Tp>
  344. static _Tp*
  345. __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
  346. {
  347. #if __cplusplus >= 201103L
  348. using __assignable = conditional<_IsMove,
  349. is_move_assignable<_Tp>,
  350. is_copy_assignable<_Tp>>;
  351. // trivial types can have deleted assignment
  352. static_assert( __assignable::type::value, "type is not assignable" );
  353. #endif
  354. const ptrdiff_t _Num = __last - __first;
  355. if (_Num)
  356. __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
  357. return __result + _Num;
  358. }
  359. };
  360. template<bool _IsMove, typename _II, typename _OI>
  361. inline _OI
  362. __copy_move_a(_II __first, _II __last, _OI __result)
  363. {
  364. typedef typename iterator_traits<_II>::value_type _ValueTypeI;
  365. typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
  366. typedef typename iterator_traits<_II>::iterator_category _Category;
  367. const bool __simple = (__is_trivially_copyable(_ValueTypeI)
  368. && __is_pointer<_II>::__value
  369. && __is_pointer<_OI>::__value
  370. && __are_same<_ValueTypeI, _ValueTypeO>::__value);
  371. return std::__copy_move<_IsMove, __simple,
  372. _Category>::__copy_m(__first, __last, __result);
  373. }
  374. // Helpers for streambuf iterators (either istream or ostream).
  375. // NB: avoid including <iosfwd>, relatively large.
  376. template<typename _CharT>
  377. struct char_traits;
  378. template<typename _CharT, typename _Traits>
  379. class istreambuf_iterator;
  380. template<typename _CharT, typename _Traits>
  381. class ostreambuf_iterator;
  382. template<bool _IsMove, typename _CharT>
  383. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  384. ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
  385. __copy_move_a2(_CharT*, _CharT*,
  386. ostreambuf_iterator<_CharT, char_traits<_CharT> >);
  387. template<bool _IsMove, typename _CharT>
  388. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  389. ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
  390. __copy_move_a2(const _CharT*, const _CharT*,
  391. ostreambuf_iterator<_CharT, char_traits<_CharT> >);
  392. template<bool _IsMove, typename _CharT>
  393. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
  394. _CharT*>::__type
  395. __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
  396. istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
  397. template<bool _IsMove, typename _II, typename _OI>
  398. inline _OI
  399. __copy_move_a2(_II __first, _II __last, _OI __result)
  400. {
  401. return std::__niter_wrap(__result,
  402. std::__copy_move_a<_IsMove>(std::__niter_base(__first),
  403. std::__niter_base(__last),
  404. std::__niter_base(__result)));
  405. }
  406. /**
  407. * @brief Copies the range [first,last) into result.
  408. * @ingroup mutating_algorithms
  409. * @param __first An input iterator.
  410. * @param __last An input iterator.
  411. * @param __result An output iterator.
  412. * @return result + (first - last)
  413. *
  414. * This inline function will boil down to a call to @c memmove whenever
  415. * possible. Failing that, if random access iterators are passed, then the
  416. * loop count will be known (and therefore a candidate for compiler
  417. * optimizations such as unrolling). Result may not be contained within
  418. * [first,last); the copy_backward function should be used instead.
  419. *
  420. * Note that the end of the output range is permitted to be contained
  421. * within [first,last).
  422. */
  423. template<typename _II, typename _OI>
  424. inline _OI
  425. copy(_II __first, _II __last, _OI __result)
  426. {
  427. // concept requirements
  428. __glibcxx_function_requires(_InputIteratorConcept<_II>)
  429. __glibcxx_function_requires(_OutputIteratorConcept<_OI,
  430. typename iterator_traits<_II>::value_type>)
  431. __glibcxx_requires_can_increment_range(__first, __last, __result);
  432. return std::__copy_move_a2<__is_move_iterator<_II>::__value>
  433. (std::__miter_base(__first), std::__miter_base(__last), __result);
  434. }
  435. #if __cplusplus >= 201103L
  436. /**
  437. * @brief Moves the range [first,last) into result.
  438. * @ingroup mutating_algorithms
  439. * @param __first An input iterator.
  440. * @param __last An input iterator.
  441. * @param __result An output iterator.
  442. * @return result + (first - last)
  443. *
  444. * This inline function will boil down to a call to @c memmove whenever
  445. * possible. Failing that, if random access iterators are passed, then the
  446. * loop count will be known (and therefore a candidate for compiler
  447. * optimizations such as unrolling). Result may not be contained within
  448. * [first,last); the move_backward function should be used instead.
  449. *
  450. * Note that the end of the output range is permitted to be contained
  451. * within [first,last).
  452. */
  453. template<typename _II, typename _OI>
  454. inline _OI
  455. move(_II __first, _II __last, _OI __result)
  456. {
  457. // concept requirements
  458. __glibcxx_function_requires(_InputIteratorConcept<_II>)
  459. __glibcxx_function_requires(_OutputIteratorConcept<_OI,
  460. typename iterator_traits<_II>::value_type>)
  461. __glibcxx_requires_can_increment_range(__first, __last, __result);
  462. return std::__copy_move_a2<true>(std::__miter_base(__first),
  463. std::__miter_base(__last), __result);
  464. }
  465. #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
  466. #else
  467. #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
  468. #endif
  469. template<bool, bool, typename>
  470. struct __copy_move_backward
  471. {
  472. template<typename _BI1, typename _BI2>
  473. static _BI2
  474. __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
  475. {
  476. while (__first != __last)
  477. *--__result = *--__last;
  478. return __result;
  479. }
  480. };
  481. #if __cplusplus >= 201103L
  482. template<typename _Category>
  483. struct __copy_move_backward<true, false, _Category>
  484. {
  485. template<typename _BI1, typename _BI2>
  486. static _BI2
  487. __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
  488. {
  489. while (__first != __last)
  490. *--__result = std::move(*--__last);
  491. return __result;
  492. }
  493. };
  494. #endif
  495. template<>
  496. struct __copy_move_backward<false, false, random_access_iterator_tag>
  497. {
  498. template<typename _BI1, typename _BI2>
  499. static _BI2
  500. __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
  501. {
  502. typename iterator_traits<_BI1>::difference_type __n;
  503. for (__n = __last - __first; __n > 0; --__n)
  504. *--__result = *--__last;
  505. return __result;
  506. }
  507. };
  508. #if __cplusplus >= 201103L
  509. template<>
  510. struct __copy_move_backward<true, false, random_access_iterator_tag>
  511. {
  512. template<typename _BI1, typename _BI2>
  513. static _BI2
  514. __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
  515. {
  516. typename iterator_traits<_BI1>::difference_type __n;
  517. for (__n = __last - __first; __n > 0; --__n)
  518. *--__result = std::move(*--__last);
  519. return __result;
  520. }
  521. };
  522. #endif
  523. template<bool _IsMove>
  524. struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
  525. {
  526. template<typename _Tp>
  527. static _Tp*
  528. __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
  529. {
  530. #if __cplusplus >= 201103L
  531. using __assignable = conditional<_IsMove,
  532. is_move_assignable<_Tp>,
  533. is_copy_assignable<_Tp>>;
  534. // trivial types can have deleted assignment
  535. static_assert( __assignable::type::value, "type is not assignable" );
  536. #endif
  537. const ptrdiff_t _Num = __last - __first;
  538. if (_Num)
  539. __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
  540. return __result - _Num;
  541. }
  542. };
  543. template<bool _IsMove, typename _BI1, typename _BI2>
  544. inline _BI2
  545. __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
  546. {
  547. typedef typename iterator_traits<_BI1>::value_type _ValueType1;
  548. typedef typename iterator_traits<_BI2>::value_type _ValueType2;
  549. typedef typename iterator_traits<_BI1>::iterator_category _Category;
  550. const bool __simple = (__is_trivially_copyable(_ValueType1)
  551. && __is_pointer<_BI1>::__value
  552. && __is_pointer<_BI2>::__value
  553. && __are_same<_ValueType1, _ValueType2>::__value);
  554. return std::__copy_move_backward<_IsMove, __simple,
  555. _Category>::__copy_move_b(__first,
  556. __last,
  557. __result);
  558. }
  559. template<bool _IsMove, typename _BI1, typename _BI2>
  560. inline _BI2
  561. __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
  562. {
  563. return std::__niter_wrap(__result,
  564. std::__copy_move_backward_a<_IsMove>
  565. (std::__niter_base(__first), std::__niter_base(__last),
  566. std::__niter_base(__result)));
  567. }
  568. /**
  569. * @brief Copies the range [first,last) into result.
  570. * @ingroup mutating_algorithms
  571. * @param __first A bidirectional iterator.
  572. * @param __last A bidirectional iterator.
  573. * @param __result A bidirectional iterator.
  574. * @return result - (first - last)
  575. *
  576. * The function has the same effect as copy, but starts at the end of the
  577. * range and works its way to the start, returning the start of the result.
  578. * This inline function will boil down to a call to @c memmove whenever
  579. * possible. Failing that, if random access iterators are passed, then the
  580. * loop count will be known (and therefore a candidate for compiler
  581. * optimizations such as unrolling).
  582. *
  583. * Result may not be in the range (first,last]. Use copy instead. Note
  584. * that the start of the output range may overlap [first,last).
  585. */
  586. template<typename _BI1, typename _BI2>
  587. inline _BI2
  588. copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
  589. {
  590. // concept requirements
  591. __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
  592. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
  593. __glibcxx_function_requires(_ConvertibleConcept<
  594. typename iterator_traits<_BI1>::value_type,
  595. typename iterator_traits<_BI2>::value_type>)
  596. __glibcxx_requires_can_decrement_range(__first, __last, __result);
  597. return std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
  598. (std::__miter_base(__first), std::__miter_base(__last), __result);
  599. }
  600. #if __cplusplus >= 201103L
  601. /**
  602. * @brief Moves the range [first,last) into result.
  603. * @ingroup mutating_algorithms
  604. * @param __first A bidirectional iterator.
  605. * @param __last A bidirectional iterator.
  606. * @param __result A bidirectional iterator.
  607. * @return result - (first - last)
  608. *
  609. * The function has the same effect as move, but starts at the end of the
  610. * range and works its way to the start, returning the start of the result.
  611. * This inline function will boil down to a call to @c memmove whenever
  612. * possible. Failing that, if random access iterators are passed, then the
  613. * loop count will be known (and therefore a candidate for compiler
  614. * optimizations such as unrolling).
  615. *
  616. * Result may not be in the range (first,last]. Use move instead. Note
  617. * that the start of the output range may overlap [first,last).
  618. */
  619. template<typename _BI1, typename _BI2>
  620. inline _BI2
  621. move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
  622. {
  623. // concept requirements
  624. __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
  625. __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
  626. __glibcxx_function_requires(_ConvertibleConcept<
  627. typename iterator_traits<_BI1>::value_type,
  628. typename iterator_traits<_BI2>::value_type>)
  629. __glibcxx_requires_can_decrement_range(__first, __last, __result);
  630. return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
  631. std::__miter_base(__last),
  632. __result);
  633. }
  634. #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
  635. #else
  636. #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
  637. #endif
  638. template<typename _ForwardIterator, typename _Tp>
  639. inline typename
  640. __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
  641. __fill_a(_ForwardIterator __first, _ForwardIterator __last,
  642. const _Tp& __value)
  643. {
  644. for (; __first != __last; ++__first)
  645. *__first = __value;
  646. }
  647. template<typename _ForwardIterator, typename _Tp>
  648. inline typename
  649. __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
  650. __fill_a(_ForwardIterator __first, _ForwardIterator __last,
  651. const _Tp& __value)
  652. {
  653. const _Tp __tmp = __value;
  654. for (; __first != __last; ++__first)
  655. *__first = __tmp;
  656. }
  657. // Specialization: for char types we can use memset.
  658. template<typename _Tp>
  659. inline typename
  660. __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
  661. __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
  662. {
  663. const _Tp __tmp = __c;
  664. if (const size_t __len = __last - __first)
  665. __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
  666. }
  667. /**
  668. * @brief Fills the range [first,last) with copies of value.
  669. * @ingroup mutating_algorithms
  670. * @param __first A forward iterator.
  671. * @param __last A forward iterator.
  672. * @param __value A reference-to-const of arbitrary type.
  673. * @return Nothing.
  674. *
  675. * This function fills a range with copies of the same value. For char
  676. * types filling contiguous areas of memory, this becomes an inline call
  677. * to @c memset or @c wmemset.
  678. */
  679. template<typename _ForwardIterator, typename _Tp>
  680. inline void
  681. fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
  682. {
  683. // concept requirements
  684. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  685. _ForwardIterator>)
  686. __glibcxx_requires_valid_range(__first, __last);
  687. std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
  688. __value);
  689. }
  690. template<typename _OutputIterator, typename _Size, typename _Tp>
  691. inline typename
  692. __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
  693. __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
  694. {
  695. for (__decltype(__n + 0) __niter = __n;
  696. __niter > 0; --__niter, (void) ++__first)
  697. *__first = __value;
  698. return __first;
  699. }
  700. template<typename _OutputIterator, typename _Size, typename _Tp>
  701. inline typename
  702. __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
  703. __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
  704. {
  705. const _Tp __tmp = __value;
  706. for (__decltype(__n + 0) __niter = __n;
  707. __niter > 0; --__niter, (void) ++__first)
  708. *__first = __tmp;
  709. return __first;
  710. }
  711. template<typename _Size, typename _Tp>
  712. inline typename
  713. __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
  714. __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
  715. {
  716. std::__fill_a(__first, __first + __n, __c);
  717. return __first + __n;
  718. }
  719. /**
  720. * @brief Fills the range [first,first+n) with copies of value.
  721. * @ingroup mutating_algorithms
  722. * @param __first An output iterator.
  723. * @param __n The count of copies to perform.
  724. * @param __value A reference-to-const of arbitrary type.
  725. * @return The iterator at first+n.
  726. *
  727. * This function fills a range with copies of the same value. For char
  728. * types filling contiguous areas of memory, this becomes an inline call
  729. * to @c memset or @ wmemset.
  730. *
  731. * _GLIBCXX_RESOLVE_LIB_DEFECTS
  732. * DR 865. More algorithms that throw away information
  733. */
  734. template<typename _OI, typename _Size, typename _Tp>
  735. inline _OI
  736. fill_n(_OI __first, _Size __n, const _Tp& __value)
  737. {
  738. // concept requirements
  739. __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
  740. __glibcxx_requires_can_increment(__first, __n);
  741. return std::__niter_wrap(__first,
  742. std::__fill_n_a(std::__niter_base(__first), __n, __value));
  743. }
  744. template<bool _BoolType>
  745. struct __equal
  746. {
  747. template<typename _II1, typename _II2>
  748. static bool
  749. equal(_II1 __first1, _II1 __last1, _II2 __first2)
  750. {
  751. for (; __first1 != __last1; ++__first1, (void) ++__first2)
  752. if (!(*__first1 == *__first2))
  753. return false;
  754. return true;
  755. }
  756. };
  757. template<>
  758. struct __equal<true>
  759. {
  760. template<typename _Tp>
  761. static bool
  762. equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
  763. {
  764. if (const size_t __len = (__last1 - __first1))
  765. return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) * __len);
  766. return true;
  767. }
  768. };
  769. template<typename _II1, typename _II2>
  770. inline bool
  771. __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
  772. {
  773. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  774. typedef typename iterator_traits<_II2>::value_type _ValueType2;
  775. const bool __simple = ((__is_integer<_ValueType1>::__value
  776. || __is_pointer<_ValueType1>::__value)
  777. && __is_pointer<_II1>::__value
  778. && __is_pointer<_II2>::__value
  779. && __are_same<_ValueType1, _ValueType2>::__value);
  780. return std::__equal<__simple>::equal(__first1, __last1, __first2);
  781. }
  782. template<typename, typename>
  783. struct __lc_rai
  784. {
  785. template<typename _II1, typename _II2>
  786. static _II1
  787. __newlast1(_II1, _II1 __last1, _II2, _II2)
  788. { return __last1; }
  789. template<typename _II>
  790. static bool
  791. __cnd2(_II __first, _II __last)
  792. { return __first != __last; }
  793. };
  794. template<>
  795. struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
  796. {
  797. template<typename _RAI1, typename _RAI2>
  798. static _RAI1
  799. __newlast1(_RAI1 __first1, _RAI1 __last1,
  800. _RAI2 __first2, _RAI2 __last2)
  801. {
  802. const typename iterator_traits<_RAI1>::difference_type
  803. __diff1 = __last1 - __first1;
  804. const typename iterator_traits<_RAI2>::difference_type
  805. __diff2 = __last2 - __first2;
  806. return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
  807. }
  808. template<typename _RAI>
  809. static bool
  810. __cnd2(_RAI, _RAI)
  811. { return true; }
  812. };
  813. template<typename _II1, typename _II2, typename _Compare>
  814. bool
  815. __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
  816. _II2 __first2, _II2 __last2,
  817. _Compare __comp)
  818. {
  819. typedef typename iterator_traits<_II1>::iterator_category _Category1;
  820. typedef typename iterator_traits<_II2>::iterator_category _Category2;
  821. typedef std::__lc_rai<_Category1, _Category2> __rai_type;
  822. __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
  823. for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
  824. ++__first1, (void)++__first2)
  825. {
  826. if (__comp(__first1, __first2))
  827. return true;
  828. if (__comp(__first2, __first1))
  829. return false;
  830. }
  831. return __first1 == __last1 && __first2 != __last2;
  832. }
  833. template<bool _BoolType>
  834. struct __lexicographical_compare
  835. {
  836. template<typename _II1, typename _II2>
  837. static bool __lc(_II1, _II1, _II2, _II2);
  838. };
  839. template<bool _BoolType>
  840. template<typename _II1, typename _II2>
  841. bool
  842. __lexicographical_compare<_BoolType>::
  843. __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  844. {
  845. return std::__lexicographical_compare_impl(__first1, __last1,
  846. __first2, __last2,
  847. __gnu_cxx::__ops::__iter_less_iter());
  848. }
  849. template<>
  850. struct __lexicographical_compare<true>
  851. {
  852. template<typename _Tp, typename _Up>
  853. static bool
  854. __lc(const _Tp* __first1, const _Tp* __last1,
  855. const _Up* __first2, const _Up* __last2)
  856. {
  857. const size_t __len1 = __last1 - __first1;
  858. const size_t __len2 = __last2 - __first2;
  859. if (const size_t __len = std::min(__len1, __len2))
  860. if (int __result = __builtin_memcmp(__first1, __first2, __len))
  861. return __result < 0;
  862. return __len1 < __len2;
  863. }
  864. };
  865. template<typename _II1, typename _II2>
  866. inline bool
  867. __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
  868. _II2 __first2, _II2 __last2)
  869. {
  870. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  871. typedef typename iterator_traits<_II2>::value_type _ValueType2;
  872. const bool __simple =
  873. (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
  874. && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
  875. && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
  876. && __is_pointer<_II1>::__value
  877. && __is_pointer<_II2>::__value);
  878. return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
  879. __first2, __last2);
  880. }
  881. template<typename _ForwardIterator, typename _Tp, typename _Compare>
  882. _ForwardIterator
  883. __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
  884. const _Tp& __val, _Compare __comp)
  885. {
  886. typedef typename iterator_traits<_ForwardIterator>::difference_type
  887. _DistanceType;
  888. _DistanceType __len = std::distance(__first, __last);
  889. while (__len > 0)
  890. {
  891. _DistanceType __half = __len >> 1;
  892. _ForwardIterator __middle = __first;
  893. std::advance(__middle, __half);
  894. if (__comp(__middle, __val))
  895. {
  896. __first = __middle;
  897. ++__first;
  898. __len = __len - __half - 1;
  899. }
  900. else
  901. __len = __half;
  902. }
  903. return __first;
  904. }
  905. /**
  906. * @brief Finds the first position in which @a val could be inserted
  907. * without changing the ordering.
  908. * @param __first An iterator.
  909. * @param __last Another iterator.
  910. * @param __val The search term.
  911. * @return An iterator pointing to the first element <em>not less
  912. * than</em> @a val, or end() if every element is less than
  913. * @a val.
  914. * @ingroup binary_search_algorithms
  915. */
  916. template<typename _ForwardIterator, typename _Tp>
  917. inline _ForwardIterator
  918. lower_bound(_ForwardIterator __first, _ForwardIterator __last,
  919. const _Tp& __val)
  920. {
  921. // concept requirements
  922. __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
  923. __glibcxx_function_requires(_LessThanOpConcept<
  924. typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
  925. __glibcxx_requires_partitioned_lower(__first, __last, __val);
  926. return std::__lower_bound(__first, __last, __val,
  927. __gnu_cxx::__ops::__iter_less_val());
  928. }
  929. /// This is a helper function for the sort routines and for random.tcc.
  930. // Precondition: __n > 0.
  931. inline _GLIBCXX_CONSTEXPR int
  932. __lg(int __n)
  933. { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
  934. inline _GLIBCXX_CONSTEXPR unsigned
  935. __lg(unsigned __n)
  936. { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
  937. inline _GLIBCXX_CONSTEXPR long
  938. __lg(long __n)
  939. { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
  940. inline _GLIBCXX_CONSTEXPR unsigned long
  941. __lg(unsigned long __n)
  942. { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
  943. inline _GLIBCXX_CONSTEXPR long long
  944. __lg(long long __n)
  945. { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
  946. inline _GLIBCXX_CONSTEXPR unsigned long long
  947. __lg(unsigned long long __n)
  948. { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
  949. _GLIBCXX_BEGIN_NAMESPACE_ALGO
  950. /**
  951. * @brief Tests a range for element-wise equality.
  952. * @ingroup non_mutating_algorithms
  953. * @param __first1 An input iterator.
  954. * @param __last1 An input iterator.
  955. * @param __first2 An input iterator.
  956. * @return A boolean true or false.
  957. *
  958. * This compares the elements of two ranges using @c == and returns true or
  959. * false depending on whether all of the corresponding elements of the
  960. * ranges are equal.
  961. */
  962. template<typename _II1, typename _II2>
  963. inline bool
  964. equal(_II1 __first1, _II1 __last1, _II2 __first2)
  965. {
  966. // concept requirements
  967. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  968. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  969. __glibcxx_function_requires(_EqualOpConcept<
  970. typename iterator_traits<_II1>::value_type,
  971. typename iterator_traits<_II2>::value_type>)
  972. __glibcxx_requires_can_increment_range(__first1, __last1, __first2);
  973. return std::__equal_aux(std::__niter_base(__first1),
  974. std::__niter_base(__last1),
  975. std::__niter_base(__first2));
  976. }
  977. /**
  978. * @brief Tests a range for element-wise equality.
  979. * @ingroup non_mutating_algorithms
  980. * @param __first1 An input iterator.
  981. * @param __last1 An input iterator.
  982. * @param __first2 An input iterator.
  983. * @param __binary_pred A binary predicate @link functors
  984. * functor@endlink.
  985. * @return A boolean true or false.
  986. *
  987. * This compares the elements of two ranges using the binary_pred
  988. * parameter, and returns true or
  989. * false depending on whether all of the corresponding elements of the
  990. * ranges are equal.
  991. */
  992. template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
  993. inline bool
  994. equal(_IIter1 __first1, _IIter1 __last1,
  995. _IIter2 __first2, _BinaryPredicate __binary_pred)
  996. {
  997. // concept requirements
  998. __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
  999. __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
  1000. __glibcxx_requires_valid_range(__first1, __last1);
  1001. for (; __first1 != __last1; ++__first1, (void)++__first2)
  1002. if (!bool(__binary_pred(*__first1, *__first2)))
  1003. return false;
  1004. return true;
  1005. }
  1006. #if __cplusplus >= 201103L
  1007. // 4-iterator version of std::equal<It1, It2> for use in C++11.
  1008. template<typename _II1, typename _II2>
  1009. inline bool
  1010. __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1011. {
  1012. using _RATag = random_access_iterator_tag;
  1013. using _Cat1 = typename iterator_traits<_II1>::iterator_category;
  1014. using _Cat2 = typename iterator_traits<_II2>::iterator_category;
  1015. using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
  1016. if (_RAIters())
  1017. {
  1018. auto __d1 = std::distance(__first1, __last1);
  1019. auto __d2 = std::distance(__first2, __last2);
  1020. if (__d1 != __d2)
  1021. return false;
  1022. return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
  1023. }
  1024. for (; __first1 != __last1 && __first2 != __last2;
  1025. ++__first1, (void)++__first2)
  1026. if (!(*__first1 == *__first2))
  1027. return false;
  1028. return __first1 == __last1 && __first2 == __last2;
  1029. }
  1030. // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
  1031. template<typename _II1, typename _II2, typename _BinaryPredicate>
  1032. inline bool
  1033. __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
  1034. _BinaryPredicate __binary_pred)
  1035. {
  1036. using _RATag = random_access_iterator_tag;
  1037. using _Cat1 = typename iterator_traits<_II1>::iterator_category;
  1038. using _Cat2 = typename iterator_traits<_II2>::iterator_category;
  1039. using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
  1040. if (_RAIters())
  1041. {
  1042. auto __d1 = std::distance(__first1, __last1);
  1043. auto __d2 = std::distance(__first2, __last2);
  1044. if (__d1 != __d2)
  1045. return false;
  1046. return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
  1047. __binary_pred);
  1048. }
  1049. for (; __first1 != __last1 && __first2 != __last2;
  1050. ++__first1, (void)++__first2)
  1051. if (!bool(__binary_pred(*__first1, *__first2)))
  1052. return false;
  1053. return __first1 == __last1 && __first2 == __last2;
  1054. }
  1055. #endif // C++11
  1056. #if __cplusplus > 201103L
  1057. #define __cpp_lib_robust_nonmodifying_seq_ops 201304
  1058. /**
  1059. * @brief Tests a range for element-wise equality.
  1060. * @ingroup non_mutating_algorithms
  1061. * @param __first1 An input iterator.
  1062. * @param __last1 An input iterator.
  1063. * @param __first2 An input iterator.
  1064. * @param __last2 An input iterator.
  1065. * @return A boolean true or false.
  1066. *
  1067. * This compares the elements of two ranges using @c == and returns true or
  1068. * false depending on whether all of the corresponding elements of the
  1069. * ranges are equal.
  1070. */
  1071. template<typename _II1, typename _II2>
  1072. inline bool
  1073. equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
  1074. {
  1075. // concept requirements
  1076. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1077. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1078. __glibcxx_function_requires(_EqualOpConcept<
  1079. typename iterator_traits<_II1>::value_type,
  1080. typename iterator_traits<_II2>::value_type>)
  1081. __glibcxx_requires_valid_range(__first1, __last1);
  1082. __glibcxx_requires_valid_range(__first2, __last2);
  1083. return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2);
  1084. }
  1085. /**
  1086. * @brief Tests a range for element-wise equality.
  1087. * @ingroup non_mutating_algorithms
  1088. * @param __first1 An input iterator.
  1089. * @param __last1 An input iterator.
  1090. * @param __first2 An input iterator.
  1091. * @param __last2 An input iterator.
  1092. * @param __binary_pred A binary predicate @link functors
  1093. * functor@endlink.
  1094. * @return A boolean true or false.
  1095. *
  1096. * This compares the elements of two ranges using the binary_pred
  1097. * parameter, and returns true or
  1098. * false depending on whether all of the corresponding elements of the
  1099. * ranges are equal.
  1100. */
  1101. template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
  1102. inline bool
  1103. equal(_IIter1 __first1, _IIter1 __last1,
  1104. _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
  1105. {
  1106. // concept requirements
  1107. __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
  1108. __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
  1109. __glibcxx_requires_valid_range(__first1, __last1);
  1110. __glibcxx_requires_valid_range(__first2, __last2);
  1111. return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2,
  1112. __binary_pred);
  1113. }
  1114. #endif // C++14
  1115. /**
  1116. * @brief Performs @b dictionary comparison on ranges.
  1117. * @ingroup sorting_algorithms
  1118. * @param __first1 An input iterator.
  1119. * @param __last1 An input iterator.
  1120. * @param __first2 An input iterator.
  1121. * @param __last2 An input iterator.
  1122. * @return A boolean true or false.
  1123. *
  1124. * <em>Returns true if the sequence of elements defined by the range
  1125. * [first1,last1) is lexicographically less than the sequence of elements
  1126. * defined by the range [first2,last2). Returns false otherwise.</em>
  1127. * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
  1128. * then this is an inline call to @c memcmp.
  1129. */
  1130. template<typename _II1, typename _II2>
  1131. inline bool
  1132. lexicographical_compare(_II1 __first1, _II1 __last1,
  1133. _II2 __first2, _II2 __last2)
  1134. {
  1135. #ifdef _GLIBCXX_CONCEPT_CHECKS
  1136. // concept requirements
  1137. typedef typename iterator_traits<_II1>::value_type _ValueType1;
  1138. typedef typename iterator_traits<_II2>::value_type _ValueType2;
  1139. #endif
  1140. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1141. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1142. __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
  1143. __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
  1144. __glibcxx_requires_valid_range(__first1, __last1);
  1145. __glibcxx_requires_valid_range(__first2, __last2);
  1146. return std::__lexicographical_compare_aux(std::__niter_base(__first1),
  1147. std::__niter_base(__last1),
  1148. std::__niter_base(__first2),
  1149. std::__niter_base(__last2));
  1150. }
  1151. /**
  1152. * @brief Performs @b dictionary comparison on ranges.
  1153. * @ingroup sorting_algorithms
  1154. * @param __first1 An input iterator.
  1155. * @param __last1 An input iterator.
  1156. * @param __first2 An input iterator.
  1157. * @param __last2 An input iterator.
  1158. * @param __comp A @link comparison_functors comparison functor@endlink.
  1159. * @return A boolean true or false.
  1160. *
  1161. * The same as the four-parameter @c lexicographical_compare, but uses the
  1162. * comp parameter instead of @c <.
  1163. */
  1164. template<typename _II1, typename _II2, typename _Compare>
  1165. inline bool
  1166. lexicographical_compare(_II1 __first1, _II1 __last1,
  1167. _II2 __first2, _II2 __last2, _Compare __comp)
  1168. {
  1169. // concept requirements
  1170. __glibcxx_function_requires(_InputIteratorConcept<_II1>)
  1171. __glibcxx_function_requires(_InputIteratorConcept<_II2>)
  1172. __glibcxx_requires_valid_range(__first1, __last1);
  1173. __glibcxx_requires_valid_range(__first2, __last2);
  1174. return std::__lexicographical_compare_impl
  1175. (__first1, __last1, __first2, __last2,
  1176. __gnu_cxx::__ops::__iter_comp_iter(__comp));
  1177. }
  1178. template<typename _InputIterator1, typename _InputIterator2,
  1179. typename _BinaryPredicate>
  1180. pair<_InputIterator1, _InputIterator2>
  1181. __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1182. _InputIterator2 __first2, _BinaryPredicate __binary_pred)
  1183. {
  1184. while (__first1 != __last1 && __binary_pred(__first1, __first2))
  1185. {
  1186. ++__first1;
  1187. ++__first2;
  1188. }
  1189. return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
  1190. }
  1191. /**
  1192. * @brief Finds the places in ranges which don't match.
  1193. * @ingroup non_mutating_algorithms
  1194. * @param __first1 An input iterator.
  1195. * @param __last1 An input iterator.
  1196. * @param __first2 An input iterator.
  1197. * @return A pair of iterators pointing to the first mismatch.
  1198. *
  1199. * This compares the elements of two ranges using @c == and returns a pair
  1200. * of iterators. The first iterator points into the first range, the
  1201. * second iterator points into the second range, and the elements pointed
  1202. * to by the iterators are not equal.
  1203. */
  1204. template<typename _InputIterator1, typename _InputIterator2>
  1205. inline pair<_InputIterator1, _InputIterator2>
  1206. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1207. _InputIterator2 __first2)
  1208. {
  1209. // concept requirements
  1210. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1211. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1212. __glibcxx_function_requires(_EqualOpConcept<
  1213. typename iterator_traits<_InputIterator1>::value_type,
  1214. typename iterator_traits<_InputIterator2>::value_type>)
  1215. __glibcxx_requires_valid_range(__first1, __last1);
  1216. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
  1217. __gnu_cxx::__ops::__iter_equal_to_iter());
  1218. }
  1219. /**
  1220. * @brief Finds the places in ranges which don't match.
  1221. * @ingroup non_mutating_algorithms
  1222. * @param __first1 An input iterator.
  1223. * @param __last1 An input iterator.
  1224. * @param __first2 An input iterator.
  1225. * @param __binary_pred A binary predicate @link functors
  1226. * functor@endlink.
  1227. * @return A pair of iterators pointing to the first mismatch.
  1228. *
  1229. * This compares the elements of two ranges using the binary_pred
  1230. * parameter, and returns a pair
  1231. * of iterators. The first iterator points into the first range, the
  1232. * second iterator points into the second range, and the elements pointed
  1233. * to by the iterators are not equal.
  1234. */
  1235. template<typename _InputIterator1, typename _InputIterator2,
  1236. typename _BinaryPredicate>
  1237. inline pair<_InputIterator1, _InputIterator2>
  1238. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1239. _InputIterator2 __first2, _BinaryPredicate __binary_pred)
  1240. {
  1241. // concept requirements
  1242. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1243. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1244. __glibcxx_requires_valid_range(__first1, __last1);
  1245. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
  1246. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
  1247. }
  1248. #if __cplusplus > 201103L
  1249. template<typename _InputIterator1, typename _InputIterator2,
  1250. typename _BinaryPredicate>
  1251. pair<_InputIterator1, _InputIterator2>
  1252. __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1253. _InputIterator2 __first2, _InputIterator2 __last2,
  1254. _BinaryPredicate __binary_pred)
  1255. {
  1256. while (__first1 != __last1 && __first2 != __last2
  1257. && __binary_pred(__first1, __first2))
  1258. {
  1259. ++__first1;
  1260. ++__first2;
  1261. }
  1262. return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
  1263. }
  1264. /**
  1265. * @brief Finds the places in ranges which don't match.
  1266. * @ingroup non_mutating_algorithms
  1267. * @param __first1 An input iterator.
  1268. * @param __last1 An input iterator.
  1269. * @param __first2 An input iterator.
  1270. * @param __last2 An input iterator.
  1271. * @return A pair of iterators pointing to the first mismatch.
  1272. *
  1273. * This compares the elements of two ranges using @c == and returns a pair
  1274. * of iterators. The first iterator points into the first range, the
  1275. * second iterator points into the second range, and the elements pointed
  1276. * to by the iterators are not equal.
  1277. */
  1278. template<typename _InputIterator1, typename _InputIterator2>
  1279. inline pair<_InputIterator1, _InputIterator2>
  1280. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1281. _InputIterator2 __first2, _InputIterator2 __last2)
  1282. {
  1283. // concept requirements
  1284. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1285. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1286. __glibcxx_function_requires(_EqualOpConcept<
  1287. typename iterator_traits<_InputIterator1>::value_type,
  1288. typename iterator_traits<_InputIterator2>::value_type>)
  1289. __glibcxx_requires_valid_range(__first1, __last1);
  1290. __glibcxx_requires_valid_range(__first2, __last2);
  1291. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
  1292. __gnu_cxx::__ops::__iter_equal_to_iter());
  1293. }
  1294. /**
  1295. * @brief Finds the places in ranges which don't match.
  1296. * @ingroup non_mutating_algorithms
  1297. * @param __first1 An input iterator.
  1298. * @param __last1 An input iterator.
  1299. * @param __first2 An input iterator.
  1300. * @param __last2 An input iterator.
  1301. * @param __binary_pred A binary predicate @link functors
  1302. * functor@endlink.
  1303. * @return A pair of iterators pointing to the first mismatch.
  1304. *
  1305. * This compares the elements of two ranges using the binary_pred
  1306. * parameter, and returns a pair
  1307. * of iterators. The first iterator points into the first range, the
  1308. * second iterator points into the second range, and the elements pointed
  1309. * to by the iterators are not equal.
  1310. */
  1311. template<typename _InputIterator1, typename _InputIterator2,
  1312. typename _BinaryPredicate>
  1313. inline pair<_InputIterator1, _InputIterator2>
  1314. mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
  1315. _InputIterator2 __first2, _InputIterator2 __last2,
  1316. _BinaryPredicate __binary_pred)
  1317. {
  1318. // concept requirements
  1319. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  1320. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  1321. __glibcxx_requires_valid_range(__first1, __last1);
  1322. __glibcxx_requires_valid_range(__first2, __last2);
  1323. return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
  1324. __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
  1325. }
  1326. #endif
  1327. _GLIBCXX_END_NAMESPACE_ALGO
  1328. _GLIBCXX_END_NAMESPACE_VERSION
  1329. } // namespace std
  1330. // NB: This file is included within many other C++ includes, as a way
  1331. // of getting the base algorithms. So, make sure that parallel bits
  1332. // come in too if requested.
  1333. #ifdef _GLIBCXX_PARALLEL
  1334. # include <parallel/algobase.h>
  1335. #endif
  1336. #endif