stl_iterator.h 78 KB

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