stl_iterator.h 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  1. // Iterators -*- C++ -*-
  2. // Copyright (C) 2001-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996-1998
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_iterator.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{iterator}
  48. *
  49. * This file implements reverse_iterator, back_insert_iterator,
  50. * front_insert_iterator, insert_iterator, __normal_iterator, and their
  51. * supporting functions and overloaded operators.
  52. */
  53. #ifndef _STL_ITERATOR_H
  54. #define _STL_ITERATOR_H 1
  55. #include <bits/cpp_type_traits.h>
  56. #include <bits/stl_iterator_base_types.h>
  57. #include <ext/type_traits.h>
  58. #include <bits/move.h>
  59. #include <bits/ptr_traits.h>
  60. #if __cplusplus >= 201103L
  61. # include <type_traits>
  62. #endif
  63. #if __cplusplus > 201703L
  64. # define __cpp_lib_array_constexpr 201811L
  65. # define __cpp_lib_constexpr_iterator 201811L
  66. #elif __cplusplus == 201703L
  67. # define __cpp_lib_array_constexpr 201803L
  68. #endif
  69. #if __cplusplus > 201703L
  70. # include <compare>
  71. # include <new>
  72. # include <bits/exception_defines.h>
  73. # include <bits/iterator_concepts.h>
  74. #endif
  75. namespace std _GLIBCXX_VISIBILITY(default)
  76. {
  77. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  78. /**
  79. * @addtogroup iterators
  80. * @{
  81. */
  82. #if __cplusplus > 201703L && __cpp_lib_concepts
  83. namespace __detail
  84. {
  85. // Weaken iterator_category _Cat to _Limit if it is derived from that,
  86. // otherwise use _Otherwise.
  87. template<typename _Cat, typename _Limit, typename _Otherwise = _Cat>
  88. using __clamp_iter_cat
  89. = conditional_t<derived_from<_Cat, _Limit>, _Limit, _Otherwise>;
  90. }
  91. #endif
  92. // 24.4.1 Reverse iterators
  93. /**
  94. * Bidirectional and random access iterators have corresponding reverse
  95. * %iterator adaptors that iterate through the data structure in the
  96. * opposite direction. They have the same signatures as the corresponding
  97. * iterators. The fundamental relation between a reverse %iterator and its
  98. * corresponding %iterator @c i is established by the identity:
  99. * @code
  100. * &*(reverse_iterator(i)) == &*(i - 1)
  101. * @endcode
  102. *
  103. * <em>This mapping is dictated by the fact that while there is always a
  104. * pointer past the end of an array, there might not be a valid pointer
  105. * before the beginning of an array.</em> [24.4.1]/1,2
  106. *
  107. * Reverse iterators can be tricky and surprising at first. Their
  108. * semantics make sense, however, and the trickiness is a side effect of
  109. * the requirement that the iterators must be safe.
  110. */
  111. template<typename _Iterator>
  112. class reverse_iterator
  113. : public iterator<typename iterator_traits<_Iterator>::iterator_category,
  114. typename iterator_traits<_Iterator>::value_type,
  115. typename iterator_traits<_Iterator>::difference_type,
  116. typename iterator_traits<_Iterator>::pointer,
  117. typename iterator_traits<_Iterator>::reference>
  118. {
  119. template<typename _Iter>
  120. friend class reverse_iterator;
  121. #if __cpp_lib_concepts
  122. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  123. // 3435. three_way_comparable_with<reverse_iterator<int*>, [...]>
  124. template<typename _Iter>
  125. static constexpr bool __convertible = !is_same_v<_Iter, _Iterator>
  126. && convertible_to<const _Iter&, _Iterator>;
  127. #endif
  128. protected:
  129. _Iterator current;
  130. typedef iterator_traits<_Iterator> __traits_type;
  131. public:
  132. typedef _Iterator iterator_type;
  133. typedef typename __traits_type::difference_type difference_type;
  134. typedef typename __traits_type::pointer pointer;
  135. typedef typename __traits_type::reference reference;
  136. #if __cplusplus > 201703L && __cpp_lib_concepts
  137. using iterator_concept
  138. = conditional_t<random_access_iterator<_Iterator>,
  139. random_access_iterator_tag,
  140. bidirectional_iterator_tag>;
  141. using iterator_category
  142. = __detail::__clamp_iter_cat<typename __traits_type::iterator_category,
  143. random_access_iterator_tag>;
  144. #endif
  145. /**
  146. * The default constructor value-initializes member @p current.
  147. * If it is a pointer, that means it is zero-initialized.
  148. */
  149. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  150. // 235 No specification of default ctor for reverse_iterator
  151. // 1012. reverse_iterator default ctor should value initialize
  152. _GLIBCXX17_CONSTEXPR
  153. reverse_iterator() : current() { }
  154. /**
  155. * This %iterator will move in the opposite direction that @p x does.
  156. */
  157. explicit _GLIBCXX17_CONSTEXPR
  158. reverse_iterator(iterator_type __x) : current(__x) { }
  159. /**
  160. * The copy constructor is normal.
  161. */
  162. _GLIBCXX17_CONSTEXPR
  163. reverse_iterator(const reverse_iterator& __x)
  164. : current(__x.current) { }
  165. #if __cplusplus >= 201103L
  166. reverse_iterator& operator=(const reverse_iterator&) = default;
  167. #endif
  168. /**
  169. * A %reverse_iterator across other types can be copied if the
  170. * underlying %iterator can be converted to the type of @c current.
  171. */
  172. template<typename _Iter>
  173. #if __cpp_lib_concepts
  174. requires __convertible<_Iter>
  175. #endif
  176. _GLIBCXX17_CONSTEXPR
  177. reverse_iterator(const reverse_iterator<_Iter>& __x)
  178. : current(__x.current) { }
  179. #if __cplusplus >= 201103L
  180. template<typename _Iter>
  181. #if __cpp_lib_concepts
  182. requires __convertible<_Iter>
  183. && assignable_from<_Iterator&, const _Iter&>
  184. #endif
  185. _GLIBCXX17_CONSTEXPR
  186. reverse_iterator&
  187. operator=(const reverse_iterator<_Iter>& __x)
  188. {
  189. current = __x.current;
  190. return *this;
  191. }
  192. #endif
  193. /**
  194. * @return @c current, the %iterator used for underlying work.
  195. */
  196. _GLIBCXX17_CONSTEXPR iterator_type
  197. base() const
  198. { return current; }
  199. /**
  200. * @return A reference to the value at @c --current
  201. *
  202. * This requires that @c --current is dereferenceable.
  203. *
  204. * @warning This implementation requires that for an iterator of the
  205. * underlying iterator type, @c x, a reference obtained by
  206. * @c *x remains valid after @c x has been modified or
  207. * destroyed. This is a bug: http://gcc.gnu.org/PR51823
  208. */
  209. _GLIBCXX17_CONSTEXPR reference
  210. operator*() const
  211. {
  212. _Iterator __tmp = current;
  213. return *--__tmp;
  214. }
  215. /**
  216. * @return A pointer to the value at @c --current
  217. *
  218. * This requires that @c --current is dereferenceable.
  219. */
  220. _GLIBCXX17_CONSTEXPR pointer
  221. operator->() const
  222. #if __cplusplus > 201703L && __cpp_concepts >= 201907L
  223. requires is_pointer_v<_Iterator>
  224. || requires(const _Iterator __i) { __i.operator->(); }
  225. #endif
  226. {
  227. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  228. // 1052. operator-> should also support smart pointers
  229. _Iterator __tmp = current;
  230. --__tmp;
  231. return _S_to_pointer(__tmp);
  232. }
  233. /**
  234. * @return @c *this
  235. *
  236. * Decrements the underlying iterator.
  237. */
  238. _GLIBCXX17_CONSTEXPR reverse_iterator&
  239. operator++()
  240. {
  241. --current;
  242. return *this;
  243. }
  244. /**
  245. * @return The original value of @c *this
  246. *
  247. * Decrements the underlying iterator.
  248. */
  249. _GLIBCXX17_CONSTEXPR reverse_iterator
  250. operator++(int)
  251. {
  252. reverse_iterator __tmp = *this;
  253. --current;
  254. return __tmp;
  255. }
  256. /**
  257. * @return @c *this
  258. *
  259. * Increments the underlying iterator.
  260. */
  261. _GLIBCXX17_CONSTEXPR reverse_iterator&
  262. operator--()
  263. {
  264. ++current;
  265. return *this;
  266. }
  267. /**
  268. * @return A reverse_iterator with the previous value of @c *this
  269. *
  270. * Increments the underlying iterator.
  271. */
  272. _GLIBCXX17_CONSTEXPR reverse_iterator
  273. operator--(int)
  274. {
  275. reverse_iterator __tmp = *this;
  276. ++current;
  277. return __tmp;
  278. }
  279. /**
  280. * @return A reverse_iterator that refers to @c current - @a __n
  281. *
  282. * The underlying iterator must be a Random Access Iterator.
  283. */
  284. _GLIBCXX17_CONSTEXPR reverse_iterator
  285. operator+(difference_type __n) const
  286. { return reverse_iterator(current - __n); }
  287. /**
  288. * @return *this
  289. *
  290. * Moves the underlying iterator backwards @a __n steps.
  291. * The underlying iterator must be a Random Access Iterator.
  292. */
  293. _GLIBCXX17_CONSTEXPR reverse_iterator&
  294. operator+=(difference_type __n)
  295. {
  296. current -= __n;
  297. return *this;
  298. }
  299. /**
  300. * @return A reverse_iterator that refers to @c current - @a __n
  301. *
  302. * The underlying iterator must be a Random Access Iterator.
  303. */
  304. _GLIBCXX17_CONSTEXPR reverse_iterator
  305. operator-(difference_type __n) const
  306. { return reverse_iterator(current + __n); }
  307. /**
  308. * @return *this
  309. *
  310. * Moves the underlying iterator forwards @a __n steps.
  311. * The underlying iterator must be a Random Access Iterator.
  312. */
  313. _GLIBCXX17_CONSTEXPR reverse_iterator&
  314. operator-=(difference_type __n)
  315. {
  316. current += __n;
  317. return *this;
  318. }
  319. /**
  320. * @return The value at @c current - @a __n - 1
  321. *
  322. * The underlying iterator must be a Random Access Iterator.
  323. */
  324. _GLIBCXX17_CONSTEXPR reference
  325. operator[](difference_type __n) const
  326. { return *(*this + __n); }
  327. #if __cplusplus > 201703L && __cpp_lib_concepts
  328. friend constexpr iter_rvalue_reference_t<_Iterator>
  329. iter_move(const reverse_iterator& __i)
  330. noexcept(is_nothrow_copy_constructible_v<_Iterator>
  331. && noexcept(ranges::iter_move(--std::declval<_Iterator&>())))
  332. {
  333. auto __tmp = __i.base();
  334. return ranges::iter_move(--__tmp);
  335. }
  336. template<indirectly_swappable<_Iterator> _Iter2>
  337. friend constexpr void
  338. iter_swap(const reverse_iterator& __x,
  339. const reverse_iterator<_Iter2>& __y)
  340. noexcept(is_nothrow_copy_constructible_v<_Iterator>
  341. && is_nothrow_copy_constructible_v<_Iter2>
  342. && noexcept(ranges::iter_swap(--std::declval<_Iterator&>(),
  343. --std::declval<_Iter2&>())))
  344. {
  345. auto __xtmp = __x.base();
  346. auto __ytmp = __y.base();
  347. ranges::iter_swap(--__xtmp, --__ytmp);
  348. }
  349. #endif
  350. private:
  351. template<typename _Tp>
  352. static _GLIBCXX17_CONSTEXPR _Tp*
  353. _S_to_pointer(_Tp* __p)
  354. { return __p; }
  355. template<typename _Tp>
  356. static _GLIBCXX17_CONSTEXPR pointer
  357. _S_to_pointer(_Tp __t)
  358. { return __t.operator->(); }
  359. };
  360. ///@{
  361. /**
  362. * @param __x A %reverse_iterator.
  363. * @param __y A %reverse_iterator.
  364. * @return A simple bool.
  365. *
  366. * Reverse iterators forward comparisons to their underlying base()
  367. * iterators.
  368. *
  369. */
  370. #if __cplusplus <= 201703L || ! defined __cpp_lib_concepts
  371. template<typename _Iterator>
  372. inline _GLIBCXX17_CONSTEXPR bool
  373. operator==(const reverse_iterator<_Iterator>& __x,
  374. const reverse_iterator<_Iterator>& __y)
  375. { return __x.base() == __y.base(); }
  376. template<typename _Iterator>
  377. inline _GLIBCXX17_CONSTEXPR bool
  378. operator<(const reverse_iterator<_Iterator>& __x,
  379. const reverse_iterator<_Iterator>& __y)
  380. { return __y.base() < __x.base(); }
  381. template<typename _Iterator>
  382. inline _GLIBCXX17_CONSTEXPR bool
  383. operator!=(const reverse_iterator<_Iterator>& __x,
  384. const reverse_iterator<_Iterator>& __y)
  385. { return !(__x == __y); }
  386. template<typename _Iterator>
  387. inline _GLIBCXX17_CONSTEXPR bool
  388. operator>(const reverse_iterator<_Iterator>& __x,
  389. const reverse_iterator<_Iterator>& __y)
  390. { return __y < __x; }
  391. template<typename _Iterator>
  392. inline _GLIBCXX17_CONSTEXPR bool
  393. operator<=(const reverse_iterator<_Iterator>& __x,
  394. const reverse_iterator<_Iterator>& __y)
  395. { return !(__y < __x); }
  396. template<typename _Iterator>
  397. inline _GLIBCXX17_CONSTEXPR bool
  398. operator>=(const reverse_iterator<_Iterator>& __x,
  399. const reverse_iterator<_Iterator>& __y)
  400. { return !(__x < __y); }
  401. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  402. // DR 280. Comparison of reverse_iterator to const reverse_iterator.
  403. template<typename _IteratorL, typename _IteratorR>
  404. inline _GLIBCXX17_CONSTEXPR bool
  405. operator==(const reverse_iterator<_IteratorL>& __x,
  406. const reverse_iterator<_IteratorR>& __y)
  407. { return __x.base() == __y.base(); }
  408. template<typename _IteratorL, typename _IteratorR>
  409. inline _GLIBCXX17_CONSTEXPR bool
  410. operator<(const reverse_iterator<_IteratorL>& __x,
  411. const reverse_iterator<_IteratorR>& __y)
  412. { return __x.base() > __y.base(); }
  413. template<typename _IteratorL, typename _IteratorR>
  414. inline _GLIBCXX17_CONSTEXPR bool
  415. operator!=(const reverse_iterator<_IteratorL>& __x,
  416. const reverse_iterator<_IteratorR>& __y)
  417. { return __x.base() != __y.base(); }
  418. template<typename _IteratorL, typename _IteratorR>
  419. inline _GLIBCXX17_CONSTEXPR bool
  420. operator>(const reverse_iterator<_IteratorL>& __x,
  421. const reverse_iterator<_IteratorR>& __y)
  422. { return __x.base() < __y.base(); }
  423. template<typename _IteratorL, typename _IteratorR>
  424. inline _GLIBCXX17_CONSTEXPR bool
  425. operator<=(const reverse_iterator<_IteratorL>& __x,
  426. const reverse_iterator<_IteratorR>& __y)
  427. { return __x.base() >= __y.base(); }
  428. template<typename _IteratorL, typename _IteratorR>
  429. inline _GLIBCXX17_CONSTEXPR bool
  430. operator>=(const reverse_iterator<_IteratorL>& __x,
  431. const reverse_iterator<_IteratorR>& __y)
  432. { return __x.base() <= __y.base(); }
  433. #else // C++20
  434. template<typename _IteratorL, typename _IteratorR>
  435. constexpr bool
  436. operator==(const reverse_iterator<_IteratorL>& __x,
  437. const reverse_iterator<_IteratorR>& __y)
  438. requires requires { { __x.base() == __y.base() } -> convertible_to<bool>; }
  439. { return __x.base() == __y.base(); }
  440. template<typename _IteratorL, typename _IteratorR>
  441. constexpr bool
  442. operator!=(const reverse_iterator<_IteratorL>& __x,
  443. const reverse_iterator<_IteratorR>& __y)
  444. requires requires { { __x.base() != __y.base() } -> convertible_to<bool>; }
  445. { return __x.base() != __y.base(); }
  446. template<typename _IteratorL, typename _IteratorR>
  447. constexpr bool
  448. operator<(const reverse_iterator<_IteratorL>& __x,
  449. const reverse_iterator<_IteratorR>& __y)
  450. requires requires { { __x.base() > __y.base() } -> convertible_to<bool>; }
  451. { return __x.base() > __y.base(); }
  452. template<typename _IteratorL, typename _IteratorR>
  453. constexpr bool
  454. operator>(const reverse_iterator<_IteratorL>& __x,
  455. const reverse_iterator<_IteratorR>& __y)
  456. requires requires { { __x.base() < __y.base() } -> convertible_to<bool>; }
  457. { return __x.base() < __y.base(); }
  458. template<typename _IteratorL, typename _IteratorR>
  459. constexpr bool
  460. operator<=(const reverse_iterator<_IteratorL>& __x,
  461. const reverse_iterator<_IteratorR>& __y)
  462. requires requires { { __x.base() >= __y.base() } -> convertible_to<bool>; }
  463. { return __x.base() >= __y.base(); }
  464. template<typename _IteratorL, typename _IteratorR>
  465. constexpr bool
  466. operator>=(const reverse_iterator<_IteratorL>& __x,
  467. const reverse_iterator<_IteratorR>& __y)
  468. requires requires { { __x.base() <= __y.base() } -> convertible_to<bool>; }
  469. { return __x.base() <= __y.base(); }
  470. template<typename _IteratorL,
  471. three_way_comparable_with<_IteratorL> _IteratorR>
  472. constexpr compare_three_way_result_t<_IteratorL, _IteratorR>
  473. operator<=>(const reverse_iterator<_IteratorL>& __x,
  474. const reverse_iterator<_IteratorR>& __y)
  475. { return __y.base() <=> __x.base(); }
  476. #endif // C++20
  477. ///@}
  478. #if __cplusplus < 201103L
  479. template<typename _Iterator>
  480. inline typename reverse_iterator<_Iterator>::difference_type
  481. operator-(const reverse_iterator<_Iterator>& __x,
  482. const reverse_iterator<_Iterator>& __y)
  483. { return __y.base() - __x.base(); }
  484. template<typename _IteratorL, typename _IteratorR>
  485. inline typename reverse_iterator<_IteratorL>::difference_type
  486. operator-(const reverse_iterator<_IteratorL>& __x,
  487. const reverse_iterator<_IteratorR>& __y)
  488. { return __y.base() - __x.base(); }
  489. #else
  490. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  491. // DR 685. reverse_iterator/move_iterator difference has invalid signatures
  492. template<typename _IteratorL, typename _IteratorR>
  493. inline _GLIBCXX17_CONSTEXPR auto
  494. operator-(const reverse_iterator<_IteratorL>& __x,
  495. const reverse_iterator<_IteratorR>& __y)
  496. -> decltype(__y.base() - __x.base())
  497. { return __y.base() - __x.base(); }
  498. #endif
  499. template<typename _Iterator>
  500. inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator>
  501. operator+(typename reverse_iterator<_Iterator>::difference_type __n,
  502. const reverse_iterator<_Iterator>& __x)
  503. { return reverse_iterator<_Iterator>(__x.base() - __n); }
  504. #if __cplusplus >= 201103L
  505. // Same as C++14 make_reverse_iterator but used in C++11 mode too.
  506. template<typename _Iterator>
  507. inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator>
  508. __make_reverse_iterator(_Iterator __i)
  509. { return reverse_iterator<_Iterator>(__i); }
  510. # if __cplusplus >= 201402L
  511. # define __cpp_lib_make_reverse_iterator 201402
  512. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  513. // DR 2285. make_reverse_iterator
  514. /// Generator function for reverse_iterator.
  515. template<typename _Iterator>
  516. inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator>
  517. make_reverse_iterator(_Iterator __i)
  518. { return reverse_iterator<_Iterator>(__i); }
  519. # if __cplusplus > 201703L && defined __cpp_lib_concepts
  520. template<typename _Iterator1, typename _Iterator2>
  521. requires (!sized_sentinel_for<_Iterator1, _Iterator2>)
  522. inline constexpr bool
  523. disable_sized_sentinel_for<reverse_iterator<_Iterator1>,
  524. reverse_iterator<_Iterator2>> = true;
  525. # endif // C++20
  526. # endif // C++14
  527. template<typename _Iterator>
  528. _GLIBCXX20_CONSTEXPR
  529. auto
  530. __niter_base(reverse_iterator<_Iterator> __it)
  531. -> decltype(__make_reverse_iterator(__niter_base(__it.base())))
  532. { return __make_reverse_iterator(__niter_base(__it.base())); }
  533. template<typename _Iterator>
  534. struct __is_move_iterator<reverse_iterator<_Iterator> >
  535. : __is_move_iterator<_Iterator>
  536. { };
  537. template<typename _Iterator>
  538. _GLIBCXX20_CONSTEXPR
  539. auto
  540. __miter_base(reverse_iterator<_Iterator> __it)
  541. -> decltype(__make_reverse_iterator(__miter_base(__it.base())))
  542. { return __make_reverse_iterator(__miter_base(__it.base())); }
  543. #endif // C++11
  544. // 24.4.2.2.1 back_insert_iterator
  545. /**
  546. * @brief Turns assignment into insertion.
  547. *
  548. * These are output iterators, constructed from a container-of-T.
  549. * Assigning a T to the iterator appends it to the container using
  550. * push_back.
  551. *
  552. * Tip: Using the back_inserter function to create these iterators can
  553. * save typing.
  554. */
  555. template<typename _Container>
  556. class back_insert_iterator
  557. : public iterator<output_iterator_tag, void, void, void, void>
  558. {
  559. protected:
  560. _Container* container;
  561. public:
  562. /// A nested typedef for the type of whatever container you used.
  563. typedef _Container container_type;
  564. #if __cplusplus > 201703L
  565. using difference_type = ptrdiff_t;
  566. constexpr back_insert_iterator() noexcept : container(nullptr) { }
  567. #endif
  568. /// The only way to create this %iterator is with a container.
  569. explicit _GLIBCXX20_CONSTEXPR
  570. back_insert_iterator(_Container& __x)
  571. : container(std::__addressof(__x)) { }
  572. /**
  573. * @param __value An instance of whatever type
  574. * container_type::const_reference is; presumably a
  575. * reference-to-const T for container<T>.
  576. * @return This %iterator, for chained operations.
  577. *
  578. * This kind of %iterator doesn't really have a @a position in the
  579. * container (you can think of the position as being permanently at
  580. * the end, if you like). Assigning a value to the %iterator will
  581. * always append the value to the end of the container.
  582. */
  583. #if __cplusplus < 201103L
  584. back_insert_iterator&
  585. operator=(typename _Container::const_reference __value)
  586. {
  587. container->push_back(__value);
  588. return *this;
  589. }
  590. #else
  591. _GLIBCXX20_CONSTEXPR
  592. back_insert_iterator&
  593. operator=(const typename _Container::value_type& __value)
  594. {
  595. container->push_back(__value);
  596. return *this;
  597. }
  598. _GLIBCXX20_CONSTEXPR
  599. back_insert_iterator&
  600. operator=(typename _Container::value_type&& __value)
  601. {
  602. container->push_back(std::move(__value));
  603. return *this;
  604. }
  605. #endif
  606. /// Simply returns *this.
  607. _GLIBCXX20_CONSTEXPR
  608. back_insert_iterator&
  609. operator*()
  610. { return *this; }
  611. /// Simply returns *this. (This %iterator does not @a move.)
  612. _GLIBCXX20_CONSTEXPR
  613. back_insert_iterator&
  614. operator++()
  615. { return *this; }
  616. /// Simply returns *this. (This %iterator does not @a move.)
  617. _GLIBCXX20_CONSTEXPR
  618. back_insert_iterator
  619. operator++(int)
  620. { return *this; }
  621. };
  622. /**
  623. * @param __x A container of arbitrary type.
  624. * @return An instance of back_insert_iterator working on @p __x.
  625. *
  626. * This wrapper function helps in creating back_insert_iterator instances.
  627. * Typing the name of the %iterator requires knowing the precise full
  628. * type of the container, which can be tedious and impedes generic
  629. * programming. Using this function lets you take advantage of automatic
  630. * template parameter deduction, making the compiler match the correct
  631. * types for you.
  632. */
  633. template<typename _Container>
  634. _GLIBCXX20_CONSTEXPR
  635. inline back_insert_iterator<_Container>
  636. back_inserter(_Container& __x)
  637. { return back_insert_iterator<_Container>(__x); }
  638. /**
  639. * @brief Turns assignment into insertion.
  640. *
  641. * These are output iterators, constructed from a container-of-T.
  642. * Assigning a T to the iterator prepends it to the container using
  643. * push_front.
  644. *
  645. * Tip: Using the front_inserter function to create these iterators can
  646. * save typing.
  647. */
  648. template<typename _Container>
  649. class front_insert_iterator
  650. : public iterator<output_iterator_tag, void, void, void, void>
  651. {
  652. protected:
  653. _Container* container;
  654. public:
  655. /// A nested typedef for the type of whatever container you used.
  656. typedef _Container container_type;
  657. #if __cplusplus > 201703L
  658. using difference_type = ptrdiff_t;
  659. constexpr front_insert_iterator() noexcept : container(nullptr) { }
  660. #endif
  661. /// The only way to create this %iterator is with a container.
  662. explicit _GLIBCXX20_CONSTEXPR
  663. front_insert_iterator(_Container& __x)
  664. : container(std::__addressof(__x)) { }
  665. /**
  666. * @param __value An instance of whatever type
  667. * container_type::const_reference is; presumably a
  668. * reference-to-const T for container<T>.
  669. * @return This %iterator, for chained operations.
  670. *
  671. * This kind of %iterator doesn't really have a @a position in the
  672. * container (you can think of the position as being permanently at
  673. * the front, if you like). Assigning a value to the %iterator will
  674. * always prepend the value to the front of the container.
  675. */
  676. #if __cplusplus < 201103L
  677. front_insert_iterator&
  678. operator=(typename _Container::const_reference __value)
  679. {
  680. container->push_front(__value);
  681. return *this;
  682. }
  683. #else
  684. _GLIBCXX20_CONSTEXPR
  685. front_insert_iterator&
  686. operator=(const typename _Container::value_type& __value)
  687. {
  688. container->push_front(__value);
  689. return *this;
  690. }
  691. _GLIBCXX20_CONSTEXPR
  692. front_insert_iterator&
  693. operator=(typename _Container::value_type&& __value)
  694. {
  695. container->push_front(std::move(__value));
  696. return *this;
  697. }
  698. #endif
  699. /// Simply returns *this.
  700. _GLIBCXX20_CONSTEXPR
  701. front_insert_iterator&
  702. operator*()
  703. { return *this; }
  704. /// Simply returns *this. (This %iterator does not @a move.)
  705. _GLIBCXX20_CONSTEXPR
  706. front_insert_iterator&
  707. operator++()
  708. { return *this; }
  709. /// Simply returns *this. (This %iterator does not @a move.)
  710. _GLIBCXX20_CONSTEXPR
  711. front_insert_iterator
  712. operator++(int)
  713. { return *this; }
  714. };
  715. /**
  716. * @param __x A container of arbitrary type.
  717. * @return An instance of front_insert_iterator working on @p x.
  718. *
  719. * This wrapper function helps in creating front_insert_iterator instances.
  720. * Typing the name of the %iterator requires knowing the precise full
  721. * type of the container, which can be tedious and impedes generic
  722. * programming. Using this function lets you take advantage of automatic
  723. * template parameter deduction, making the compiler match the correct
  724. * types for you.
  725. */
  726. template<typename _Container>
  727. _GLIBCXX20_CONSTEXPR
  728. inline front_insert_iterator<_Container>
  729. front_inserter(_Container& __x)
  730. { return front_insert_iterator<_Container>(__x); }
  731. /**
  732. * @brief Turns assignment into insertion.
  733. *
  734. * These are output iterators, constructed from a container-of-T.
  735. * Assigning a T to the iterator inserts it in the container at the
  736. * %iterator's position, rather than overwriting the value at that
  737. * position.
  738. *
  739. * (Sequences will actually insert a @e copy of the value before the
  740. * %iterator's position.)
  741. *
  742. * Tip: Using the inserter function to create these iterators can
  743. * save typing.
  744. */
  745. template<typename _Container>
  746. class insert_iterator
  747. : public iterator<output_iterator_tag, void, void, void, void>
  748. {
  749. #if __cplusplus > 201703L && defined __cpp_lib_concepts
  750. using _Iter = std::__detail::__range_iter_t<_Container>;
  751. protected:
  752. _Container* container = nullptr;
  753. _Iter iter = _Iter();
  754. #else
  755. typedef typename _Container::iterator _Iter;
  756. protected:
  757. _Container* container;
  758. _Iter iter;
  759. #endif
  760. public:
  761. /// A nested typedef for the type of whatever container you used.
  762. typedef _Container container_type;
  763. #if __cplusplus > 201703L && defined __cpp_lib_concepts
  764. using difference_type = ptrdiff_t;
  765. insert_iterator() = default;
  766. #endif
  767. /**
  768. * The only way to create this %iterator is with a container and an
  769. * initial position (a normal %iterator into the container).
  770. */
  771. _GLIBCXX20_CONSTEXPR
  772. insert_iterator(_Container& __x, _Iter __i)
  773. : container(std::__addressof(__x)), iter(__i) {}
  774. /**
  775. * @param __value An instance of whatever type
  776. * container_type::const_reference is; presumably a
  777. * reference-to-const T for container<T>.
  778. * @return This %iterator, for chained operations.
  779. *
  780. * This kind of %iterator maintains its own position in the
  781. * container. Assigning a value to the %iterator will insert the
  782. * value into the container at the place before the %iterator.
  783. *
  784. * The position is maintained such that subsequent assignments will
  785. * insert values immediately after one another. For example,
  786. * @code
  787. * // vector v contains A and Z
  788. *
  789. * insert_iterator i (v, ++v.begin());
  790. * i = 1;
  791. * i = 2;
  792. * i = 3;
  793. *
  794. * // vector v contains A, 1, 2, 3, and Z
  795. * @endcode
  796. */
  797. #if __cplusplus < 201103L
  798. insert_iterator&
  799. operator=(typename _Container::const_reference __value)
  800. {
  801. iter = container->insert(iter, __value);
  802. ++iter;
  803. return *this;
  804. }
  805. #else
  806. _GLIBCXX20_CONSTEXPR
  807. insert_iterator&
  808. operator=(const typename _Container::value_type& __value)
  809. {
  810. iter = container->insert(iter, __value);
  811. ++iter;
  812. return *this;
  813. }
  814. _GLIBCXX20_CONSTEXPR
  815. insert_iterator&
  816. operator=(typename _Container::value_type&& __value)
  817. {
  818. iter = container->insert(iter, std::move(__value));
  819. ++iter;
  820. return *this;
  821. }
  822. #endif
  823. /// Simply returns *this.
  824. _GLIBCXX20_CONSTEXPR
  825. insert_iterator&
  826. operator*()
  827. { return *this; }
  828. /// Simply returns *this. (This %iterator does not @a move.)
  829. _GLIBCXX20_CONSTEXPR
  830. insert_iterator&
  831. operator++()
  832. { return *this; }
  833. /// Simply returns *this. (This %iterator does not @a move.)
  834. _GLIBCXX20_CONSTEXPR
  835. insert_iterator&
  836. operator++(int)
  837. { return *this; }
  838. };
  839. /**
  840. * @param __x A container of arbitrary type.
  841. * @param __i An iterator into the container.
  842. * @return An instance of insert_iterator working on @p __x.
  843. *
  844. * This wrapper function helps in creating insert_iterator instances.
  845. * Typing the name of the %iterator requires knowing the precise full
  846. * type of the container, which can be tedious and impedes generic
  847. * programming. Using this function lets you take advantage of automatic
  848. * template parameter deduction, making the compiler match the correct
  849. * types for you.
  850. */
  851. #if __cplusplus > 201703L && defined __cpp_lib_concepts
  852. template<typename _Container>
  853. constexpr insert_iterator<_Container>
  854. inserter(_Container& __x, std::__detail::__range_iter_t<_Container> __i)
  855. { return insert_iterator<_Container>(__x, __i); }
  856. #else
  857. template<typename _Container>
  858. inline insert_iterator<_Container>
  859. inserter(_Container& __x, typename _Container::iterator __i)
  860. { return insert_iterator<_Container>(__x, __i); }
  861. #endif
  862. /// @} group iterators
  863. _GLIBCXX_END_NAMESPACE_VERSION
  864. } // namespace
  865. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  866. {
  867. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  868. // This iterator adapter is @a normal in the sense that it does not
  869. // change the semantics of any of the operators of its iterator
  870. // parameter. Its primary purpose is to convert an iterator that is
  871. // not a class, e.g. a pointer, into an iterator that is a class.
  872. // The _Container parameter exists solely so that different containers
  873. // using this template can instantiate different types, even if the
  874. // _Iterator parameter is the same.
  875. template<typename _Iterator, typename _Container>
  876. class __normal_iterator
  877. {
  878. protected:
  879. _Iterator _M_current;
  880. typedef std::iterator_traits<_Iterator> __traits_type;
  881. public:
  882. typedef _Iterator iterator_type;
  883. typedef typename __traits_type::iterator_category iterator_category;
  884. typedef typename __traits_type::value_type value_type;
  885. typedef typename __traits_type::difference_type difference_type;
  886. typedef typename __traits_type::reference reference;
  887. typedef typename __traits_type::pointer pointer;
  888. #if __cplusplus > 201703L && __cpp_lib_concepts
  889. using iterator_concept = std::__detail::__iter_concept<_Iterator>;
  890. #endif
  891. _GLIBCXX_CONSTEXPR __normal_iterator() _GLIBCXX_NOEXCEPT
  892. : _M_current(_Iterator()) { }
  893. explicit _GLIBCXX20_CONSTEXPR
  894. __normal_iterator(const _Iterator& __i) _GLIBCXX_NOEXCEPT
  895. : _M_current(__i) { }
  896. // Allow iterator to const_iterator conversion
  897. template<typename _Iter>
  898. _GLIBCXX20_CONSTEXPR
  899. __normal_iterator(const __normal_iterator<_Iter,
  900. typename __enable_if<
  901. (std::__are_same<_Iter, typename _Container::pointer>::__value),
  902. _Container>::__type>& __i) _GLIBCXX_NOEXCEPT
  903. : _M_current(__i.base()) { }
  904. // Forward iterator requirements
  905. _GLIBCXX20_CONSTEXPR
  906. reference
  907. operator*() const _GLIBCXX_NOEXCEPT
  908. { return *_M_current; }
  909. _GLIBCXX20_CONSTEXPR
  910. pointer
  911. operator->() const _GLIBCXX_NOEXCEPT
  912. { return _M_current; }
  913. _GLIBCXX20_CONSTEXPR
  914. __normal_iterator&
  915. operator++() _GLIBCXX_NOEXCEPT
  916. {
  917. ++_M_current;
  918. return *this;
  919. }
  920. _GLIBCXX20_CONSTEXPR
  921. __normal_iterator
  922. operator++(int) _GLIBCXX_NOEXCEPT
  923. { return __normal_iterator(_M_current++); }
  924. // Bidirectional iterator requirements
  925. _GLIBCXX20_CONSTEXPR
  926. __normal_iterator&
  927. operator--() _GLIBCXX_NOEXCEPT
  928. {
  929. --_M_current;
  930. return *this;
  931. }
  932. _GLIBCXX20_CONSTEXPR
  933. __normal_iterator
  934. operator--(int) _GLIBCXX_NOEXCEPT
  935. { return __normal_iterator(_M_current--); }
  936. // Random access iterator requirements
  937. _GLIBCXX20_CONSTEXPR
  938. reference
  939. operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
  940. { return _M_current[__n]; }
  941. _GLIBCXX20_CONSTEXPR
  942. __normal_iterator&
  943. operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
  944. { _M_current += __n; return *this; }
  945. _GLIBCXX20_CONSTEXPR
  946. __normal_iterator
  947. operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
  948. { return __normal_iterator(_M_current + __n); }
  949. _GLIBCXX20_CONSTEXPR
  950. __normal_iterator&
  951. operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
  952. { _M_current -= __n; return *this; }
  953. _GLIBCXX20_CONSTEXPR
  954. __normal_iterator
  955. operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
  956. { return __normal_iterator(_M_current - __n); }
  957. _GLIBCXX20_CONSTEXPR
  958. const _Iterator&
  959. base() const _GLIBCXX_NOEXCEPT
  960. { return _M_current; }
  961. };
  962. // Note: In what follows, the left- and right-hand-side iterators are
  963. // allowed to vary in types (conceptually in cv-qualification) so that
  964. // comparison between cv-qualified and non-cv-qualified iterators be
  965. // valid. However, the greedy and unfriendly operators in std::rel_ops
  966. // will make overload resolution ambiguous (when in scope) if we don't
  967. // provide overloads whose operands are of the same type. Can someone
  968. // remind me what generic programming is about? -- Gaby
  969. #if __cpp_lib_three_way_comparison
  970. template<typename _IteratorL, typename _IteratorR, typename _Container>
  971. requires requires (_IteratorL __lhs, _IteratorR __rhs)
  972. { { __lhs == __rhs } -> std::convertible_to<bool>; }
  973. constexpr bool
  974. operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
  975. const __normal_iterator<_IteratorR, _Container>& __rhs)
  976. noexcept(noexcept(__lhs.base() == __rhs.base()))
  977. { return __lhs.base() == __rhs.base(); }
  978. template<typename _IteratorL, typename _IteratorR, typename _Container>
  979. constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL>
  980. operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
  981. const __normal_iterator<_IteratorR, _Container>& __rhs)
  982. noexcept(noexcept(std::__detail::__synth3way(__lhs.base(), __rhs.base())))
  983. { return std::__detail::__synth3way(__lhs.base(), __rhs.base()); }
  984. #else
  985. // Forward iterator requirements
  986. template<typename _IteratorL, typename _IteratorR, typename _Container>
  987. _GLIBCXX20_CONSTEXPR
  988. inline bool
  989. operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
  990. const __normal_iterator<_IteratorR, _Container>& __rhs)
  991. _GLIBCXX_NOEXCEPT
  992. { return __lhs.base() == __rhs.base(); }
  993. template<typename _Iterator, typename _Container>
  994. _GLIBCXX20_CONSTEXPR
  995. inline bool
  996. operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
  997. const __normal_iterator<_Iterator, _Container>& __rhs)
  998. _GLIBCXX_NOEXCEPT
  999. { return __lhs.base() == __rhs.base(); }
  1000. template<typename _IteratorL, typename _IteratorR, typename _Container>
  1001. _GLIBCXX20_CONSTEXPR
  1002. inline bool
  1003. operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
  1004. const __normal_iterator<_IteratorR, _Container>& __rhs)
  1005. _GLIBCXX_NOEXCEPT
  1006. { return __lhs.base() != __rhs.base(); }
  1007. template<typename _Iterator, typename _Container>
  1008. _GLIBCXX20_CONSTEXPR
  1009. inline bool
  1010. operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
  1011. const __normal_iterator<_Iterator, _Container>& __rhs)
  1012. _GLIBCXX_NOEXCEPT
  1013. { return __lhs.base() != __rhs.base(); }
  1014. // Random access iterator requirements
  1015. template<typename _IteratorL, typename _IteratorR, typename _Container>
  1016. inline bool
  1017. operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
  1018. const __normal_iterator<_IteratorR, _Container>& __rhs)
  1019. _GLIBCXX_NOEXCEPT
  1020. { return __lhs.base() < __rhs.base(); }
  1021. template<typename _Iterator, typename _Container>
  1022. _GLIBCXX20_CONSTEXPR
  1023. inline bool
  1024. operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
  1025. const __normal_iterator<_Iterator, _Container>& __rhs)
  1026. _GLIBCXX_NOEXCEPT
  1027. { return __lhs.base() < __rhs.base(); }
  1028. template<typename _IteratorL, typename _IteratorR, typename _Container>
  1029. inline bool
  1030. operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
  1031. const __normal_iterator<_IteratorR, _Container>& __rhs)
  1032. _GLIBCXX_NOEXCEPT
  1033. { return __lhs.base() > __rhs.base(); }
  1034. template<typename _Iterator, typename _Container>
  1035. _GLIBCXX20_CONSTEXPR
  1036. inline bool
  1037. operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
  1038. const __normal_iterator<_Iterator, _Container>& __rhs)
  1039. _GLIBCXX_NOEXCEPT
  1040. { return __lhs.base() > __rhs.base(); }
  1041. template<typename _IteratorL, typename _IteratorR, typename _Container>
  1042. inline bool
  1043. operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
  1044. const __normal_iterator<_IteratorR, _Container>& __rhs)
  1045. _GLIBCXX_NOEXCEPT
  1046. { return __lhs.base() <= __rhs.base(); }
  1047. template<typename _Iterator, typename _Container>
  1048. _GLIBCXX20_CONSTEXPR
  1049. inline bool
  1050. operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
  1051. const __normal_iterator<_Iterator, _Container>& __rhs)
  1052. _GLIBCXX_NOEXCEPT
  1053. { return __lhs.base() <= __rhs.base(); }
  1054. template<typename _IteratorL, typename _IteratorR, typename _Container>
  1055. inline bool
  1056. operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
  1057. const __normal_iterator<_IteratorR, _Container>& __rhs)
  1058. _GLIBCXX_NOEXCEPT
  1059. { return __lhs.base() >= __rhs.base(); }
  1060. template<typename _Iterator, typename _Container>
  1061. _GLIBCXX20_CONSTEXPR
  1062. inline bool
  1063. operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
  1064. const __normal_iterator<_Iterator, _Container>& __rhs)
  1065. _GLIBCXX_NOEXCEPT
  1066. { return __lhs.base() >= __rhs.base(); }
  1067. #endif // three-way comparison
  1068. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1069. // According to the resolution of DR179 not only the various comparison
  1070. // operators but also operator- must accept mixed iterator/const_iterator
  1071. // parameters.
  1072. template<typename _IteratorL, typename _IteratorR, typename _Container>
  1073. #if __cplusplus >= 201103L
  1074. // DR 685.
  1075. _GLIBCXX20_CONSTEXPR
  1076. inline auto
  1077. operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
  1078. const __normal_iterator<_IteratorR, _Container>& __rhs) noexcept
  1079. -> decltype(__lhs.base() - __rhs.base())
  1080. #else
  1081. inline typename __normal_iterator<_IteratorL, _Container>::difference_type
  1082. operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
  1083. const __normal_iterator<_IteratorR, _Container>& __rhs)
  1084. #endif
  1085. { return __lhs.base() - __rhs.base(); }
  1086. template<typename _Iterator, typename _Container>
  1087. _GLIBCXX20_CONSTEXPR
  1088. inline typename __normal_iterator<_Iterator, _Container>::difference_type
  1089. operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
  1090. const __normal_iterator<_Iterator, _Container>& __rhs)
  1091. _GLIBCXX_NOEXCEPT
  1092. { return __lhs.base() - __rhs.base(); }
  1093. template<typename _Iterator, typename _Container>
  1094. _GLIBCXX20_CONSTEXPR
  1095. inline __normal_iterator<_Iterator, _Container>
  1096. operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
  1097. __n, const __normal_iterator<_Iterator, _Container>& __i)
  1098. _GLIBCXX_NOEXCEPT
  1099. { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
  1100. _GLIBCXX_END_NAMESPACE_VERSION
  1101. } // namespace
  1102. namespace std _GLIBCXX_VISIBILITY(default)
  1103. {
  1104. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1105. template<typename _Iterator, typename _Container>
  1106. _GLIBCXX20_CONSTEXPR
  1107. _Iterator
  1108. __niter_base(__gnu_cxx::__normal_iterator<_Iterator, _Container> __it)
  1109. _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)
  1110. { return __it.base(); }
  1111. #if __cplusplus >= 201103L
  1112. /**
  1113. * @addtogroup iterators
  1114. * @{
  1115. */
  1116. #if __cplusplus > 201703L && __cpp_lib_concepts
  1117. template<semiregular _Sent>
  1118. class move_sentinel
  1119. {
  1120. public:
  1121. constexpr
  1122. move_sentinel()
  1123. noexcept(is_nothrow_default_constructible_v<_Sent>)
  1124. : _M_last() { }
  1125. constexpr explicit
  1126. move_sentinel(_Sent __s)
  1127. noexcept(is_nothrow_move_constructible_v<_Sent>)
  1128. : _M_last(std::move(__s)) { }
  1129. template<typename _S2> requires convertible_to<const _S2&, _Sent>
  1130. constexpr
  1131. move_sentinel(const move_sentinel<_S2>& __s)
  1132. noexcept(is_nothrow_constructible_v<_Sent, const _S2&>)
  1133. : _M_last(__s.base())
  1134. { }
  1135. template<typename _S2> requires assignable_from<_Sent&, const _S2&>
  1136. constexpr move_sentinel&
  1137. operator=(const move_sentinel<_S2>& __s)
  1138. noexcept(is_nothrow_assignable_v<_Sent, const _S2&>)
  1139. {
  1140. _M_last = __s.base();
  1141. return *this;
  1142. }
  1143. constexpr _Sent
  1144. base() const
  1145. noexcept(is_nothrow_copy_constructible_v<_Sent>)
  1146. { return _M_last; }
  1147. private:
  1148. _Sent _M_last;
  1149. };
  1150. #endif // C++20
  1151. namespace __detail
  1152. {
  1153. #if __cplusplus > 201703L && __cpp_lib_concepts
  1154. template<typename _Iterator>
  1155. struct __move_iter_cat
  1156. { };
  1157. template<typename _Iterator>
  1158. requires requires { typename iterator_traits<_Iterator>::iterator_category; }
  1159. struct __move_iter_cat<_Iterator>
  1160. {
  1161. using iterator_category
  1162. = __clamp_iter_cat<typename iterator_traits<_Iterator>::iterator_category,
  1163. random_access_iterator_tag>;
  1164. };
  1165. #endif
  1166. }
  1167. // 24.4.3 Move iterators
  1168. /**
  1169. * Class template move_iterator is an iterator adapter with the same
  1170. * behavior as the underlying iterator except that its dereference
  1171. * operator implicitly converts the value returned by the underlying
  1172. * iterator's dereference operator to an rvalue reference. Some
  1173. * generic algorithms can be called with move iterators to replace
  1174. * copying with moving.
  1175. */
  1176. template<typename _Iterator>
  1177. class move_iterator
  1178. #if __cplusplus > 201703L && __cpp_lib_concepts
  1179. : public __detail::__move_iter_cat<_Iterator>
  1180. #endif
  1181. {
  1182. _Iterator _M_current;
  1183. using __traits_type = iterator_traits<_Iterator>;
  1184. #if ! (__cplusplus > 201703L && __cpp_lib_concepts)
  1185. using __base_ref = typename __traits_type::reference;
  1186. #endif
  1187. template<typename _Iter2>
  1188. friend class move_iterator;
  1189. #if __cpp_lib_concepts
  1190. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1191. // 3435. three_way_comparable_with<reverse_iterator<int*>, [...]>
  1192. template<typename _Iter2>
  1193. static constexpr bool __convertible = !is_same_v<_Iter2, _Iterator>
  1194. && convertible_to<const _Iter2&, _Iterator>;
  1195. #endif
  1196. public:
  1197. using iterator_type = _Iterator;
  1198. #if __cplusplus > 201703L && __cpp_lib_concepts
  1199. using iterator_concept = input_iterator_tag;
  1200. // iterator_category defined in __move_iter_cat
  1201. using value_type = iter_value_t<_Iterator>;
  1202. using difference_type = iter_difference_t<_Iterator>;
  1203. using pointer = _Iterator;
  1204. using reference = iter_rvalue_reference_t<_Iterator>;
  1205. #else
  1206. typedef typename __traits_type::iterator_category iterator_category;
  1207. typedef typename __traits_type::value_type value_type;
  1208. typedef typename __traits_type::difference_type difference_type;
  1209. // NB: DR 680.
  1210. typedef _Iterator pointer;
  1211. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1212. // 2106. move_iterator wrapping iterators returning prvalues
  1213. typedef typename conditional<is_reference<__base_ref>::value,
  1214. typename remove_reference<__base_ref>::type&&,
  1215. __base_ref>::type reference;
  1216. #endif
  1217. _GLIBCXX17_CONSTEXPR
  1218. move_iterator()
  1219. : _M_current() { }
  1220. explicit _GLIBCXX17_CONSTEXPR
  1221. move_iterator(iterator_type __i)
  1222. : _M_current(std::move(__i)) { }
  1223. template<typename _Iter>
  1224. #if __cpp_lib_concepts
  1225. requires __convertible<_Iter>
  1226. #endif
  1227. _GLIBCXX17_CONSTEXPR
  1228. move_iterator(const move_iterator<_Iter>& __i)
  1229. : _M_current(__i._M_current) { }
  1230. template<typename _Iter>
  1231. #if __cpp_lib_concepts
  1232. requires __convertible<_Iter>
  1233. && assignable_from<_Iterator&, const _Iter&>
  1234. #endif
  1235. _GLIBCXX17_CONSTEXPR
  1236. move_iterator& operator=(const move_iterator<_Iter>& __i)
  1237. {
  1238. _M_current = __i._M_current;
  1239. return *this;
  1240. }
  1241. #if __cplusplus <= 201703L
  1242. _GLIBCXX17_CONSTEXPR iterator_type
  1243. base() const
  1244. { return _M_current; }
  1245. #else
  1246. constexpr iterator_type
  1247. base() const &
  1248. #if __cpp_lib_concepts
  1249. requires copy_constructible<iterator_type>
  1250. #endif
  1251. { return _M_current; }
  1252. constexpr iterator_type
  1253. base() &&
  1254. { return std::move(_M_current); }
  1255. #endif
  1256. _GLIBCXX17_CONSTEXPR reference
  1257. operator*() const
  1258. #if __cplusplus > 201703L && __cpp_lib_concepts
  1259. { return ranges::iter_move(_M_current); }
  1260. #else
  1261. { return static_cast<reference>(*_M_current); }
  1262. #endif
  1263. _GLIBCXX17_CONSTEXPR pointer
  1264. operator->() const
  1265. { return _M_current; }
  1266. _GLIBCXX17_CONSTEXPR move_iterator&
  1267. operator++()
  1268. {
  1269. ++_M_current;
  1270. return *this;
  1271. }
  1272. _GLIBCXX17_CONSTEXPR move_iterator
  1273. operator++(int)
  1274. {
  1275. move_iterator __tmp = *this;
  1276. ++_M_current;
  1277. return __tmp;
  1278. }
  1279. #if __cpp_lib_concepts
  1280. constexpr void
  1281. operator++(int) requires (!forward_iterator<_Iterator>)
  1282. { ++_M_current; }
  1283. #endif
  1284. _GLIBCXX17_CONSTEXPR move_iterator&
  1285. operator--()
  1286. {
  1287. --_M_current;
  1288. return *this;
  1289. }
  1290. _GLIBCXX17_CONSTEXPR move_iterator
  1291. operator--(int)
  1292. {
  1293. move_iterator __tmp = *this;
  1294. --_M_current;
  1295. return __tmp;
  1296. }
  1297. _GLIBCXX17_CONSTEXPR move_iterator
  1298. operator+(difference_type __n) const
  1299. { return move_iterator(_M_current + __n); }
  1300. _GLIBCXX17_CONSTEXPR move_iterator&
  1301. operator+=(difference_type __n)
  1302. {
  1303. _M_current += __n;
  1304. return *this;
  1305. }
  1306. _GLIBCXX17_CONSTEXPR move_iterator
  1307. operator-(difference_type __n) const
  1308. { return move_iterator(_M_current - __n); }
  1309. _GLIBCXX17_CONSTEXPR move_iterator&
  1310. operator-=(difference_type __n)
  1311. {
  1312. _M_current -= __n;
  1313. return *this;
  1314. }
  1315. _GLIBCXX17_CONSTEXPR reference
  1316. operator[](difference_type __n) const
  1317. #if __cplusplus > 201703L && __cpp_lib_concepts
  1318. { return ranges::iter_move(_M_current + __n); }
  1319. #else
  1320. { return std::move(_M_current[__n]); }
  1321. #endif
  1322. #if __cplusplus > 201703L && __cpp_lib_concepts
  1323. template<sentinel_for<_Iterator> _Sent>
  1324. friend constexpr bool
  1325. operator==(const move_iterator& __x, const move_sentinel<_Sent>& __y)
  1326. { return __x.base() == __y.base(); }
  1327. template<sized_sentinel_for<_Iterator> _Sent>
  1328. friend constexpr iter_difference_t<_Iterator>
  1329. operator-(const move_sentinel<_Sent>& __x, const move_iterator& __y)
  1330. { return __x.base() - __y.base(); }
  1331. template<sized_sentinel_for<_Iterator> _Sent>
  1332. friend constexpr iter_difference_t<_Iterator>
  1333. operator-(const move_iterator& __x, const move_sentinel<_Sent>& __y)
  1334. { return __x.base() - __y.base(); }
  1335. friend constexpr iter_rvalue_reference_t<_Iterator>
  1336. iter_move(const move_iterator& __i)
  1337. noexcept(noexcept(ranges::iter_move(__i._M_current)))
  1338. { return ranges::iter_move(__i._M_current); }
  1339. template<indirectly_swappable<_Iterator> _Iter2>
  1340. friend constexpr void
  1341. iter_swap(const move_iterator& __x, const move_iterator<_Iter2>& __y)
  1342. noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
  1343. { return ranges::iter_swap(__x._M_current, __y._M_current); }
  1344. #endif // C++20
  1345. };
  1346. template<typename _IteratorL, typename _IteratorR>
  1347. inline _GLIBCXX17_CONSTEXPR bool
  1348. operator==(const move_iterator<_IteratorL>& __x,
  1349. const move_iterator<_IteratorR>& __y)
  1350. #if __cplusplus > 201703L && __cpp_lib_concepts
  1351. requires requires { { __x.base() == __y.base() } -> convertible_to<bool>; }
  1352. #endif
  1353. { return __x.base() == __y.base(); }
  1354. #if __cpp_lib_three_way_comparison
  1355. template<typename _IteratorL,
  1356. three_way_comparable_with<_IteratorL> _IteratorR>
  1357. constexpr compare_three_way_result_t<_IteratorL, _IteratorR>
  1358. operator<=>(const move_iterator<_IteratorL>& __x,
  1359. const move_iterator<_IteratorR>& __y)
  1360. { return __x.base() <=> __y.base(); }
  1361. #else
  1362. template<typename _IteratorL, typename _IteratorR>
  1363. inline _GLIBCXX17_CONSTEXPR bool
  1364. operator!=(const move_iterator<_IteratorL>& __x,
  1365. const move_iterator<_IteratorR>& __y)
  1366. { return !(__x == __y); }
  1367. #endif
  1368. template<typename _IteratorL, typename _IteratorR>
  1369. inline _GLIBCXX17_CONSTEXPR bool
  1370. operator<(const move_iterator<_IteratorL>& __x,
  1371. const move_iterator<_IteratorR>& __y)
  1372. #if __cplusplus > 201703L && __cpp_lib_concepts
  1373. requires requires { { __x.base() < __y.base() } -> convertible_to<bool>; }
  1374. #endif
  1375. { return __x.base() < __y.base(); }
  1376. template<typename _IteratorL, typename _IteratorR>
  1377. inline _GLIBCXX17_CONSTEXPR bool
  1378. operator<=(const move_iterator<_IteratorL>& __x,
  1379. const move_iterator<_IteratorR>& __y)
  1380. #if __cplusplus > 201703L && __cpp_lib_concepts
  1381. requires requires { { __y.base() < __x.base() } -> convertible_to<bool>; }
  1382. #endif
  1383. { return !(__y < __x); }
  1384. template<typename _IteratorL, typename _IteratorR>
  1385. inline _GLIBCXX17_CONSTEXPR bool
  1386. operator>(const move_iterator<_IteratorL>& __x,
  1387. const move_iterator<_IteratorR>& __y)
  1388. #if __cplusplus > 201703L && __cpp_lib_concepts
  1389. requires requires { { __y.base() < __x.base() } -> convertible_to<bool>; }
  1390. #endif
  1391. { return __y < __x; }
  1392. template<typename _IteratorL, typename _IteratorR>
  1393. inline _GLIBCXX17_CONSTEXPR bool
  1394. operator>=(const move_iterator<_IteratorL>& __x,
  1395. const move_iterator<_IteratorR>& __y)
  1396. #if __cplusplus > 201703L && __cpp_lib_concepts
  1397. requires requires { { __x.base() < __y.base() } -> convertible_to<bool>; }
  1398. #endif
  1399. { return !(__x < __y); }
  1400. #if ! (__cplusplus > 201703L && __cpp_lib_concepts)
  1401. // Note: See __normal_iterator operators note from Gaby to understand
  1402. // why we have these extra overloads for some move_iterator operators.
  1403. // These extra overloads are not needed in C++20, because the ones above
  1404. // are constrained with a requires-clause and so overload resolution will
  1405. // prefer them to greedy unconstrained function templates.
  1406. template<typename _Iterator>
  1407. inline _GLIBCXX17_CONSTEXPR bool
  1408. operator==(const move_iterator<_Iterator>& __x,
  1409. const move_iterator<_Iterator>& __y)
  1410. { return __x.base() == __y.base(); }
  1411. template<typename _Iterator>
  1412. inline _GLIBCXX17_CONSTEXPR bool
  1413. operator!=(const move_iterator<_Iterator>& __x,
  1414. const move_iterator<_Iterator>& __y)
  1415. { return !(__x == __y); }
  1416. template<typename _Iterator>
  1417. inline _GLIBCXX17_CONSTEXPR bool
  1418. operator<(const move_iterator<_Iterator>& __x,
  1419. const move_iterator<_Iterator>& __y)
  1420. { return __x.base() < __y.base(); }
  1421. template<typename _Iterator>
  1422. inline _GLIBCXX17_CONSTEXPR bool
  1423. operator<=(const move_iterator<_Iterator>& __x,
  1424. const move_iterator<_Iterator>& __y)
  1425. { return !(__y < __x); }
  1426. template<typename _Iterator>
  1427. inline _GLIBCXX17_CONSTEXPR bool
  1428. operator>(const move_iterator<_Iterator>& __x,
  1429. const move_iterator<_Iterator>& __y)
  1430. { return __y < __x; }
  1431. template<typename _Iterator>
  1432. inline _GLIBCXX17_CONSTEXPR bool
  1433. operator>=(const move_iterator<_Iterator>& __x,
  1434. const move_iterator<_Iterator>& __y)
  1435. { return !(__x < __y); }
  1436. #endif // ! C++20
  1437. // DR 685.
  1438. template<typename _IteratorL, typename _IteratorR>
  1439. inline _GLIBCXX17_CONSTEXPR auto
  1440. operator-(const move_iterator<_IteratorL>& __x,
  1441. const move_iterator<_IteratorR>& __y)
  1442. -> decltype(__x.base() - __y.base())
  1443. { return __x.base() - __y.base(); }
  1444. template<typename _Iterator>
  1445. inline _GLIBCXX17_CONSTEXPR move_iterator<_Iterator>
  1446. operator+(typename move_iterator<_Iterator>::difference_type __n,
  1447. const move_iterator<_Iterator>& __x)
  1448. { return __x + __n; }
  1449. template<typename _Iterator>
  1450. inline _GLIBCXX17_CONSTEXPR move_iterator<_Iterator>
  1451. make_move_iterator(_Iterator __i)
  1452. { return move_iterator<_Iterator>(std::move(__i)); }
  1453. template<typename _Iterator, typename _ReturnType
  1454. = typename conditional<__move_if_noexcept_cond
  1455. <typename iterator_traits<_Iterator>::value_type>::value,
  1456. _Iterator, move_iterator<_Iterator>>::type>
  1457. inline _GLIBCXX17_CONSTEXPR _ReturnType
  1458. __make_move_if_noexcept_iterator(_Iterator __i)
  1459. { return _ReturnType(__i); }
  1460. // Overload for pointers that matches std::move_if_noexcept more closely,
  1461. // returning a constant iterator when we don't want to move.
  1462. template<typename _Tp, typename _ReturnType
  1463. = typename conditional<__move_if_noexcept_cond<_Tp>::value,
  1464. const _Tp*, move_iterator<_Tp*>>::type>
  1465. inline _GLIBCXX17_CONSTEXPR _ReturnType
  1466. __make_move_if_noexcept_iterator(_Tp* __i)
  1467. { return _ReturnType(__i); }
  1468. #if __cplusplus > 201703L && __cpp_lib_concepts
  1469. // [iterators.common] Common iterators
  1470. namespace __detail
  1471. {
  1472. template<typename _It>
  1473. concept __common_iter_has_arrow = indirectly_readable<const _It>
  1474. && (requires(const _It& __it) { __it.operator->(); }
  1475. || is_reference_v<iter_reference_t<_It>>
  1476. || constructible_from<iter_value_t<_It>, iter_reference_t<_It>>);
  1477. template<typename _It>
  1478. concept __common_iter_use_postfix_proxy
  1479. = (!requires (_It& __i) { { *__i++ } -> __can_reference; })
  1480. && constructible_from<iter_value_t<_It>, iter_reference_t<_It>>;
  1481. } // namespace __detail
  1482. /// An iterator/sentinel adaptor for representing a non-common range.
  1483. template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
  1484. requires (!same_as<_It, _Sent>) && copyable<_It>
  1485. class common_iterator
  1486. {
  1487. template<typename _Tp, typename _Up>
  1488. static constexpr bool
  1489. _S_noexcept1()
  1490. {
  1491. if constexpr (is_trivially_default_constructible_v<_Tp>)
  1492. return is_nothrow_assignable_v<_Tp, _Up>;
  1493. else
  1494. return is_nothrow_constructible_v<_Tp, _Up>;
  1495. }
  1496. template<typename _It2, typename _Sent2>
  1497. static constexpr bool
  1498. _S_noexcept()
  1499. { return _S_noexcept1<_It, _It2>() && _S_noexcept1<_Sent, _Sent2>(); }
  1500. class __arrow_proxy
  1501. {
  1502. iter_value_t<_It> _M_keep;
  1503. __arrow_proxy(iter_reference_t<_It>&& __x)
  1504. : _M_keep(std::move(__x)) { }
  1505. friend class common_iterator;
  1506. public:
  1507. const iter_value_t<_It>*
  1508. operator->() const
  1509. { return std::__addressof(_M_keep); }
  1510. };
  1511. class __postfix_proxy
  1512. {
  1513. iter_value_t<_It> _M_keep;
  1514. __postfix_proxy(iter_reference_t<_It>&& __x)
  1515. : _M_keep(std::move(__x)) { }
  1516. friend class common_iterator;
  1517. public:
  1518. const iter_value_t<_It>&
  1519. operator*() const
  1520. { return _M_keep; }
  1521. };
  1522. public:
  1523. constexpr
  1524. common_iterator()
  1525. noexcept(is_nothrow_default_constructible_v<_It>)
  1526. : _M_it(), _M_index(0)
  1527. { }
  1528. constexpr
  1529. common_iterator(_It __i)
  1530. noexcept(is_nothrow_move_constructible_v<_It>)
  1531. : _M_it(std::move(__i)), _M_index(0)
  1532. { }
  1533. constexpr
  1534. common_iterator(_Sent __s)
  1535. noexcept(is_nothrow_move_constructible_v<_Sent>)
  1536. : _M_sent(std::move(__s)), _M_index(1)
  1537. { }
  1538. template<typename _It2, typename _Sent2>
  1539. requires convertible_to<const _It2&, _It>
  1540. && convertible_to<const _Sent2&, _Sent>
  1541. constexpr
  1542. common_iterator(const common_iterator<_It2, _Sent2>& __x)
  1543. noexcept(_S_noexcept<const _It2&, const _Sent2&>())
  1544. : _M_valueless(), _M_index(__x._M_index)
  1545. {
  1546. if (_M_index == 0)
  1547. {
  1548. if constexpr (is_trivially_default_constructible_v<_It>)
  1549. _M_it = std::move(__x._M_it);
  1550. else
  1551. ::new((void*)std::__addressof(_M_it)) _It(__x._M_it);
  1552. }
  1553. else if (_M_index == 1)
  1554. {
  1555. if constexpr (is_trivially_default_constructible_v<_Sent>)
  1556. _M_sent = std::move(__x._M_sent);
  1557. else
  1558. ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent);
  1559. }
  1560. }
  1561. constexpr
  1562. common_iterator(const common_iterator& __x)
  1563. noexcept(_S_noexcept<const _It&, const _Sent&>())
  1564. : _M_valueless(), _M_index(__x._M_index)
  1565. {
  1566. if (_M_index == 0)
  1567. {
  1568. if constexpr (is_trivially_default_constructible_v<_It>)
  1569. _M_it = std::move(__x._M_it);
  1570. else
  1571. ::new((void*)std::__addressof(_M_it)) _It(__x._M_it);
  1572. }
  1573. else if (_M_index == 1)
  1574. {
  1575. if constexpr (is_trivially_default_constructible_v<_Sent>)
  1576. _M_sent = std::move(__x._M_sent);
  1577. else
  1578. ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent);
  1579. }
  1580. }
  1581. common_iterator&
  1582. operator=(const common_iterator& __x)
  1583. noexcept(is_nothrow_copy_assignable_v<_It>
  1584. && is_nothrow_copy_assignable_v<_Sent>
  1585. && is_nothrow_copy_constructible_v<_It>
  1586. && is_nothrow_copy_constructible_v<_Sent>)
  1587. {
  1588. return this->operator=<_It, _Sent>(__x);
  1589. }
  1590. template<typename _It2, typename _Sent2>
  1591. requires convertible_to<const _It2&, _It>
  1592. && convertible_to<const _Sent2&, _Sent>
  1593. && assignable_from<_It&, const _It2&>
  1594. && assignable_from<_Sent&, const _Sent2&>
  1595. common_iterator&
  1596. operator=(const common_iterator<_It2, _Sent2>& __x)
  1597. noexcept(is_nothrow_constructible_v<_It, const _It2&>
  1598. && is_nothrow_constructible_v<_Sent, const _Sent2&>
  1599. && is_nothrow_assignable_v<_It, const _It2&>
  1600. && is_nothrow_assignable_v<_Sent, const _Sent2&>)
  1601. {
  1602. switch(_M_index << 2 | __x._M_index)
  1603. {
  1604. case 0b0000:
  1605. _M_it = __x._M_it;
  1606. break;
  1607. case 0b0101:
  1608. _M_sent = __x._M_sent;
  1609. break;
  1610. case 0b0001:
  1611. _M_it.~_It();
  1612. _M_index = -1;
  1613. [[fallthrough]];
  1614. case 0b1001:
  1615. ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent);
  1616. _M_index = 1;
  1617. break;
  1618. case 0b0100:
  1619. _M_sent.~_Sent();
  1620. _M_index = -1;
  1621. [[fallthrough]];
  1622. case 0b1000:
  1623. ::new((void*)std::__addressof(_M_it)) _It(__x._M_it);
  1624. _M_index = 0;
  1625. break;
  1626. default:
  1627. __glibcxx_assert(__x._M_has_value());
  1628. __builtin_unreachable();
  1629. }
  1630. return *this;
  1631. }
  1632. ~common_iterator()
  1633. {
  1634. switch (_M_index)
  1635. {
  1636. case 0:
  1637. _M_it.~_It();
  1638. break;
  1639. case 1:
  1640. _M_sent.~_Sent();
  1641. break;
  1642. }
  1643. }
  1644. decltype(auto)
  1645. operator*()
  1646. {
  1647. __glibcxx_assert(_M_index == 0);
  1648. return *_M_it;
  1649. }
  1650. decltype(auto)
  1651. operator*() const requires __detail::__dereferenceable<const _It>
  1652. {
  1653. __glibcxx_assert(_M_index == 0);
  1654. return *_M_it;
  1655. }
  1656. decltype(auto)
  1657. operator->() const requires __detail::__common_iter_has_arrow<_It>
  1658. {
  1659. __glibcxx_assert(_M_index == 0);
  1660. if constexpr (is_pointer_v<_It> || requires { _M_it.operator->(); })
  1661. return _M_it;
  1662. else if constexpr (is_reference_v<iter_reference_t<_It>>)
  1663. {
  1664. auto&& __tmp = *_M_it;
  1665. return std::__addressof(__tmp);
  1666. }
  1667. else
  1668. return __arrow_proxy{*_M_it};
  1669. }
  1670. common_iterator&
  1671. operator++()
  1672. {
  1673. __glibcxx_assert(_M_index == 0);
  1674. ++_M_it;
  1675. return *this;
  1676. }
  1677. decltype(auto)
  1678. operator++(int)
  1679. {
  1680. __glibcxx_assert(_M_index == 0);
  1681. if constexpr (forward_iterator<_It>)
  1682. {
  1683. common_iterator __tmp = *this;
  1684. ++*this;
  1685. return __tmp;
  1686. }
  1687. else if constexpr (!__detail::__common_iter_use_postfix_proxy<_It>)
  1688. return _M_it++;
  1689. else
  1690. {
  1691. __postfix_proxy __p(**this);
  1692. ++*this;
  1693. return __p;
  1694. }
  1695. }
  1696. template<typename _It2, sentinel_for<_It> _Sent2>
  1697. requires sentinel_for<_Sent, _It2>
  1698. friend bool
  1699. operator==(const common_iterator& __x,
  1700. const common_iterator<_It2, _Sent2>& __y)
  1701. {
  1702. switch(__x._M_index << 2 | __y._M_index)
  1703. {
  1704. case 0b0000:
  1705. case 0b0101:
  1706. return true;
  1707. case 0b0001:
  1708. return __x._M_it == __y._M_sent;
  1709. case 0b0100:
  1710. return __x._M_sent == __y._M_it;
  1711. default:
  1712. __glibcxx_assert(__x._M_has_value());
  1713. __glibcxx_assert(__y._M_has_value());
  1714. __builtin_unreachable();
  1715. }
  1716. }
  1717. template<typename _It2, sentinel_for<_It> _Sent2>
  1718. requires sentinel_for<_Sent, _It2> && equality_comparable_with<_It, _It2>
  1719. friend bool
  1720. operator==(const common_iterator& __x,
  1721. const common_iterator<_It2, _Sent2>& __y)
  1722. {
  1723. switch(__x._M_index << 2 | __y._M_index)
  1724. {
  1725. case 0b0101:
  1726. return true;
  1727. case 0b0000:
  1728. return __x._M_it == __y._M_it;
  1729. case 0b0001:
  1730. return __x._M_it == __y._M_sent;
  1731. case 0b0100:
  1732. return __x._M_sent == __y._M_it;
  1733. default:
  1734. __glibcxx_assert(__x._M_has_value());
  1735. __glibcxx_assert(__y._M_has_value());
  1736. __builtin_unreachable();
  1737. }
  1738. }
  1739. template<sized_sentinel_for<_It> _It2, sized_sentinel_for<_It> _Sent2>
  1740. requires sized_sentinel_for<_Sent, _It2>
  1741. friend iter_difference_t<_It2>
  1742. operator-(const common_iterator& __x,
  1743. const common_iterator<_It2, _Sent2>& __y)
  1744. {
  1745. switch(__x._M_index << 2 | __y._M_index)
  1746. {
  1747. case 0b0101:
  1748. return 0;
  1749. case 0b0000:
  1750. return __x._M_it - __y._M_it;
  1751. case 0b0001:
  1752. return __x._M_it - __y._M_sent;
  1753. case 0b0100:
  1754. return __x._M_sent - __y._M_it;
  1755. default:
  1756. __glibcxx_assert(__x._M_has_value());
  1757. __glibcxx_assert(__y._M_has_value());
  1758. __builtin_unreachable();
  1759. }
  1760. }
  1761. friend iter_rvalue_reference_t<_It>
  1762. iter_move(const common_iterator& __i)
  1763. noexcept(noexcept(ranges::iter_move(std::declval<const _It&>())))
  1764. requires input_iterator<_It>
  1765. {
  1766. __glibcxx_assert(__i._M_index == 0);
  1767. return ranges::iter_move(__i._M_it);
  1768. }
  1769. template<indirectly_swappable<_It> _It2, typename _Sent2>
  1770. friend void
  1771. iter_swap(const common_iterator& __x,
  1772. const common_iterator<_It2, _Sent2>& __y)
  1773. noexcept(noexcept(ranges::iter_swap(std::declval<const _It&>(),
  1774. std::declval<const _It2&>())))
  1775. {
  1776. __glibcxx_assert(__x._M_index == 0);
  1777. __glibcxx_assert(__y._M_index == 0);
  1778. return ranges::iter_swap(__x._M_it, __y._M_it);
  1779. }
  1780. private:
  1781. template<input_or_output_iterator _It2, sentinel_for<_It2> _Sent2>
  1782. friend class common_iterator;
  1783. bool _M_has_value() const noexcept { return _M_index < 2; }
  1784. union
  1785. {
  1786. _It _M_it;
  1787. _Sent _M_sent;
  1788. unsigned char _M_valueless;
  1789. };
  1790. unsigned char _M_index; // 0==_M_it, 1==_M_sent, 2==valueless
  1791. };
  1792. template<typename _It, typename _Sent>
  1793. struct incrementable_traits<common_iterator<_It, _Sent>>
  1794. {
  1795. using difference_type = iter_difference_t<_It>;
  1796. };
  1797. template<input_iterator _It, typename _Sent>
  1798. struct iterator_traits<common_iterator<_It, _Sent>>
  1799. {
  1800. private:
  1801. template<typename _Iter>
  1802. struct __ptr
  1803. {
  1804. using type = void;
  1805. };
  1806. template<typename _Iter>
  1807. requires __detail::__common_iter_has_arrow<_Iter>
  1808. struct __ptr<_Iter>
  1809. {
  1810. using _CIter = common_iterator<_Iter, _Sent>;
  1811. using type = decltype(std::declval<const _CIter&>().operator->());
  1812. };
  1813. static auto
  1814. _S_iter_cat()
  1815. {
  1816. using _Traits = iterator_traits<_It>;
  1817. if constexpr (requires { requires derived_from<typename _Traits::iterator_category,
  1818. forward_iterator_tag>; })
  1819. return forward_iterator_tag{};
  1820. else
  1821. return input_iterator_tag{};
  1822. }
  1823. public:
  1824. using iterator_concept = conditional_t<forward_iterator<_It>,
  1825. forward_iterator_tag, input_iterator_tag>;
  1826. using iterator_category = decltype(_S_iter_cat());
  1827. using value_type = iter_value_t<_It>;
  1828. using difference_type = iter_difference_t<_It>;
  1829. using pointer = typename __ptr<_It>::type;
  1830. using reference = iter_reference_t<_It>;
  1831. };
  1832. // [iterators.counted] Counted iterators
  1833. namespace __detail
  1834. {
  1835. template<typename _It>
  1836. struct __counted_iter_value_type
  1837. { };
  1838. template<indirectly_readable _It>
  1839. struct __counted_iter_value_type<_It>
  1840. { using value_type = iter_value_t<_It>; };
  1841. template<typename _It>
  1842. struct __counted_iter_concept
  1843. { };
  1844. template<typename _It>
  1845. requires requires { typename _It::iterator_concept; }
  1846. struct __counted_iter_concept<_It>
  1847. { using iterator_concept = typename _It::iterator_concept; };
  1848. template<typename _It>
  1849. struct __counted_iter_cat
  1850. { };
  1851. template<typename _It>
  1852. requires requires { typename _It::iterator_category; }
  1853. struct __counted_iter_cat<_It>
  1854. { using iterator_category = typename _It::iterator_category; };
  1855. }
  1856. /// An iterator adaptor that keeps track of the distance to the end.
  1857. template<input_or_output_iterator _It>
  1858. class counted_iterator
  1859. : public __detail::__counted_iter_value_type<_It>,
  1860. public __detail::__counted_iter_concept<_It>,
  1861. public __detail::__counted_iter_cat<_It>
  1862. {
  1863. public:
  1864. using iterator_type = _It;
  1865. // value_type defined in __counted_iter_value_type
  1866. using difference_type = iter_difference_t<_It>;
  1867. // iterator_concept defined in __counted_iter_concept
  1868. // iterator_category defined in __counted_iter_cat
  1869. constexpr counted_iterator() = default;
  1870. constexpr
  1871. counted_iterator(_It __i, iter_difference_t<_It> __n)
  1872. : _M_current(std::move(__i)), _M_length(__n)
  1873. { __glibcxx_assert(__n >= 0); }
  1874. template<typename _It2>
  1875. requires convertible_to<const _It2&, _It>
  1876. constexpr
  1877. counted_iterator(const counted_iterator<_It2>& __x)
  1878. : _M_current(__x._M_current), _M_length(__x._M_length)
  1879. { }
  1880. template<typename _It2>
  1881. requires assignable_from<_It&, const _It2&>
  1882. constexpr counted_iterator&
  1883. operator=(const counted_iterator<_It2>& __x)
  1884. {
  1885. _M_current = __x._M_current;
  1886. _M_length = __x._M_length;
  1887. return *this;
  1888. }
  1889. constexpr _It
  1890. base() const &
  1891. noexcept(is_nothrow_copy_constructible_v<_It>)
  1892. requires copy_constructible<_It>
  1893. { return _M_current; }
  1894. constexpr _It
  1895. base() &&
  1896. noexcept(is_nothrow_move_constructible_v<_It>)
  1897. { return std::move(_M_current); }
  1898. constexpr iter_difference_t<_It>
  1899. count() const noexcept { return _M_length; }
  1900. constexpr decltype(auto)
  1901. operator*()
  1902. noexcept(noexcept(*_M_current))
  1903. {
  1904. __glibcxx_assert( _M_length > 0 );
  1905. return *_M_current;
  1906. }
  1907. constexpr decltype(auto)
  1908. operator*() const
  1909. noexcept(noexcept(*_M_current))
  1910. requires __detail::__dereferenceable<const _It>
  1911. {
  1912. __glibcxx_assert( _M_length > 0 );
  1913. return *_M_current;
  1914. }
  1915. constexpr auto
  1916. operator->() const noexcept
  1917. requires contiguous_iterator<_It>
  1918. { return std::to_address(_M_current); }
  1919. constexpr counted_iterator&
  1920. operator++()
  1921. {
  1922. __glibcxx_assert(_M_length > 0);
  1923. ++_M_current;
  1924. --_M_length;
  1925. return *this;
  1926. }
  1927. decltype(auto)
  1928. operator++(int)
  1929. {
  1930. __glibcxx_assert(_M_length > 0);
  1931. --_M_length;
  1932. __try
  1933. {
  1934. return _M_current++;
  1935. } __catch(...) {
  1936. ++_M_length;
  1937. __throw_exception_again;
  1938. }
  1939. }
  1940. constexpr counted_iterator
  1941. operator++(int) requires forward_iterator<_It>
  1942. {
  1943. auto __tmp = *this;
  1944. ++*this;
  1945. return __tmp;
  1946. }
  1947. constexpr counted_iterator&
  1948. operator--() requires bidirectional_iterator<_It>
  1949. {
  1950. --_M_current;
  1951. ++_M_length;
  1952. return *this;
  1953. }
  1954. constexpr counted_iterator
  1955. operator--(int) requires bidirectional_iterator<_It>
  1956. {
  1957. auto __tmp = *this;
  1958. --*this;
  1959. return __tmp;
  1960. }
  1961. constexpr counted_iterator
  1962. operator+(iter_difference_t<_It> __n) const
  1963. requires random_access_iterator<_It>
  1964. { return counted_iterator(_M_current + __n, _M_length - __n); }
  1965. friend constexpr counted_iterator
  1966. operator+(iter_difference_t<_It> __n, const counted_iterator& __x)
  1967. requires random_access_iterator<_It>
  1968. { return __x + __n; }
  1969. constexpr counted_iterator&
  1970. operator+=(iter_difference_t<_It> __n)
  1971. requires random_access_iterator<_It>
  1972. {
  1973. __glibcxx_assert(__n <= _M_length);
  1974. _M_current += __n;
  1975. _M_length -= __n;
  1976. return *this;
  1977. }
  1978. constexpr counted_iterator
  1979. operator-(iter_difference_t<_It> __n) const
  1980. requires random_access_iterator<_It>
  1981. { return counted_iterator(_M_current - __n, _M_length + __n); }
  1982. template<common_with<_It> _It2>
  1983. friend constexpr iter_difference_t<_It2>
  1984. operator-(const counted_iterator& __x,
  1985. const counted_iterator<_It2>& __y)
  1986. { return __y._M_length - __x._M_length; }
  1987. friend constexpr iter_difference_t<_It>
  1988. operator-(const counted_iterator& __x, default_sentinel_t)
  1989. { return -__x._M_length; }
  1990. friend constexpr iter_difference_t<_It>
  1991. operator-(default_sentinel_t, const counted_iterator& __y)
  1992. { return __y._M_length; }
  1993. constexpr counted_iterator&
  1994. operator-=(iter_difference_t<_It> __n)
  1995. requires random_access_iterator<_It>
  1996. {
  1997. __glibcxx_assert(-__n <= _M_length);
  1998. _M_current -= __n;
  1999. _M_length += __n;
  2000. return *this;
  2001. }
  2002. constexpr decltype(auto)
  2003. operator[](iter_difference_t<_It> __n) const
  2004. noexcept(noexcept(_M_current[__n]))
  2005. requires random_access_iterator<_It>
  2006. {
  2007. __glibcxx_assert(__n < _M_length);
  2008. return _M_current[__n];
  2009. }
  2010. template<common_with<_It> _It2>
  2011. friend constexpr bool
  2012. operator==(const counted_iterator& __x,
  2013. const counted_iterator<_It2>& __y)
  2014. { return __x._M_length == __y._M_length; }
  2015. friend constexpr bool
  2016. operator==(const counted_iterator& __x, default_sentinel_t)
  2017. { return __x._M_length == 0; }
  2018. template<common_with<_It> _It2>
  2019. friend constexpr strong_ordering
  2020. operator<=>(const counted_iterator& __x,
  2021. const counted_iterator<_It2>& __y)
  2022. { return __y._M_length <=> __x._M_length; }
  2023. friend constexpr iter_rvalue_reference_t<_It>
  2024. iter_move(const counted_iterator& __i)
  2025. noexcept(noexcept(ranges::iter_move(__i._M_current)))
  2026. requires input_iterator<_It>
  2027. {
  2028. __glibcxx_assert( __i._M_length > 0 );
  2029. return ranges::iter_move(__i._M_current);
  2030. }
  2031. template<indirectly_swappable<_It> _It2>
  2032. friend constexpr void
  2033. iter_swap(const counted_iterator& __x,
  2034. const counted_iterator<_It2>& __y)
  2035. noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current)))
  2036. {
  2037. __glibcxx_assert( __x._M_length > 0 && __y._M_length > 0 );
  2038. ranges::iter_swap(__x._M_current, __y._M_current);
  2039. }
  2040. private:
  2041. template<input_or_output_iterator _It2> friend class counted_iterator;
  2042. _It _M_current = _It();
  2043. iter_difference_t<_It> _M_length = 0;
  2044. };
  2045. template<input_iterator _It>
  2046. requires same_as<__detail::__iter_traits<_It>, iterator_traits<_It>>
  2047. struct iterator_traits<counted_iterator<_It>> : iterator_traits<_It>
  2048. {
  2049. using pointer = conditional_t<contiguous_iterator<_It>,
  2050. add_pointer_t<iter_reference_t<_It>>,
  2051. void>;
  2052. };
  2053. #endif // C++20
  2054. /// @} group iterators
  2055. template<typename _Iterator>
  2056. auto
  2057. __niter_base(move_iterator<_Iterator> __it)
  2058. -> decltype(make_move_iterator(__niter_base(__it.base())))
  2059. { return make_move_iterator(__niter_base(__it.base())); }
  2060. template<typename _Iterator>
  2061. struct __is_move_iterator<move_iterator<_Iterator> >
  2062. {
  2063. enum { __value = 1 };
  2064. typedef __true_type __type;
  2065. };
  2066. template<typename _Iterator>
  2067. auto
  2068. __miter_base(move_iterator<_Iterator> __it)
  2069. -> decltype(__miter_base(__it.base()))
  2070. { return __miter_base(__it.base()); }
  2071. #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
  2072. #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
  2073. std::__make_move_if_noexcept_iterator(_Iter)
  2074. #else
  2075. #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
  2076. #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
  2077. #endif // C++11
  2078. #if __cpp_deduction_guides >= 201606
  2079. // These helper traits are used for deduction guides
  2080. // of associative containers.
  2081. template<typename _InputIterator>
  2082. using __iter_key_t = remove_const_t<
  2083. typename iterator_traits<_InputIterator>::value_type::first_type>;
  2084. template<typename _InputIterator>
  2085. using __iter_val_t =
  2086. typename iterator_traits<_InputIterator>::value_type::second_type;
  2087. template<typename _T1, typename _T2>
  2088. struct pair;
  2089. template<typename _InputIterator>
  2090. using __iter_to_alloc_t =
  2091. pair<add_const_t<__iter_key_t<_InputIterator>>,
  2092. __iter_val_t<_InputIterator>>;
  2093. #endif // __cpp_deduction_guides
  2094. _GLIBCXX_END_NAMESPACE_VERSION
  2095. } // namespace
  2096. #ifdef _GLIBCXX_DEBUG
  2097. # include <debug/stl_iterator.h>
  2098. #endif
  2099. #endif