stl_iterator.h 68 KB

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