stl_algobase.h 49 KB

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