iterator 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. // -*- C++ -*-
  2. //===-------------------------- iterator ----------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_ITERATOR
  11. #define _LIBCPP_ITERATOR
  12. /*
  13. iterator synopsis
  14. namespace std
  15. {
  16. template<class Iterator>
  17. struct iterator_traits
  18. {
  19. typedef typename Iterator::difference_type difference_type;
  20. typedef typename Iterator::value_type value_type;
  21. typedef typename Iterator::pointer pointer;
  22. typedef typename Iterator::reference reference;
  23. typedef typename Iterator::iterator_category iterator_category;
  24. };
  25. template<class T>
  26. struct iterator_traits<T*>
  27. {
  28. typedef ptrdiff_t difference_type;
  29. typedef T value_type;
  30. typedef T* pointer;
  31. typedef T& reference;
  32. typedef random_access_iterator_tag iterator_category;
  33. };
  34. template<class T>
  35. struct iterator_traits<const T*>
  36. {
  37. typedef ptrdiff_t difference_type;
  38. typedef T value_type;
  39. typedef const T* pointer;
  40. typedef const T& reference;
  41. typedef random_access_iterator_tag iterator_category;
  42. };
  43. template<class Category, class T, class Distance = ptrdiff_t,
  44. class Pointer = T*, class Reference = T&>
  45. struct iterator
  46. {
  47. typedef T value_type;
  48. typedef Distance difference_type;
  49. typedef Pointer pointer;
  50. typedef Reference reference;
  51. typedef Category iterator_category;
  52. };
  53. struct input_iterator_tag {};
  54. struct output_iterator_tag {};
  55. struct forward_iterator_tag : public input_iterator_tag {};
  56. struct bidirectional_iterator_tag : public forward_iterator_tag {};
  57. struct random_access_iterator_tag : public bidirectional_iterator_tag {};
  58. // extension: second argument not conforming to C++03
  59. template <class InputIterator>
  60. void advance(InputIterator& i,
  61. typename iterator_traits<InputIterator>::difference_type n);
  62. template <class InputIterator>
  63. typename iterator_traits<InputIterator>::difference_type
  64. distance(InputIterator first, InputIterator last);
  65. template <class Iterator>
  66. class reverse_iterator
  67. : public iterator<typename iterator_traits<Iterator>::iterator_category,
  68. typename iterator_traits<Iterator>::value_type,
  69. typename iterator_traits<Iterator>::difference_type,
  70. typename iterator_traits<Iterator>::pointer,
  71. typename iterator_traits<Iterator>::reference>
  72. {
  73. protected:
  74. Iterator current;
  75. public:
  76. typedef Iterator iterator_type;
  77. typedef typename iterator_traits<Iterator>::difference_type difference_type;
  78. typedef typename iterator_traits<Iterator>::reference reference;
  79. typedef typename iterator_traits<Iterator>::pointer pointer;
  80. reverse_iterator();
  81. explicit reverse_iterator(Iterator x);
  82. template <class U> reverse_iterator(const reverse_iterator<U>& u);
  83. Iterator base() const;
  84. reference operator*() const;
  85. pointer operator->() const;
  86. reverse_iterator& operator++();
  87. reverse_iterator operator++(int);
  88. reverse_iterator& operator--();
  89. reverse_iterator operator--(int);
  90. reverse_iterator operator+ (difference_type n) const;
  91. reverse_iterator& operator+=(difference_type n);
  92. reverse_iterator operator- (difference_type n) const;
  93. reverse_iterator& operator-=(difference_type n);
  94. reference operator[](difference_type n) const;
  95. };
  96. template <class Iterator1, class Iterator2>
  97. bool
  98. operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
  99. template <class Iterator1, class Iterator2>
  100. bool
  101. operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
  102. template <class Iterator1, class Iterator2>
  103. bool
  104. operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
  105. template <class Iterator1, class Iterator2>
  106. bool
  107. operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
  108. template <class Iterator1, class Iterator2>
  109. bool
  110. operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
  111. template <class Iterator1, class Iterator2>
  112. bool
  113. operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
  114. template <class Iterator1, class Iterator2>
  115. auto
  116. operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
  117. -> decltype(__y.base() - __x.base());
  118. template <class Iterator>
  119. reverse_iterator<Iterator>
  120. operator+(typename reverse_iterator<Iterator>::difference_type n, const reverse_iterator<Iterator>& x);
  121. template <class Iterator> reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14
  122. template <class Container>
  123. class back_insert_iterator
  124. {
  125. protected:
  126. Container* container;
  127. public:
  128. typedef Container container_type;
  129. typedef void value_type;
  130. typedef void difference_type;
  131. typedef void reference;
  132. typedef void pointer;
  133. explicit back_insert_iterator(Container& x);
  134. back_insert_iterator& operator=(const typename Container::value_type& value);
  135. back_insert_iterator& operator*();
  136. back_insert_iterator& operator++();
  137. back_insert_iterator operator++(int);
  138. };
  139. template <class Container> back_insert_iterator<Container> back_inserter(Container& x);
  140. template <class Container>
  141. class front_insert_iterator
  142. {
  143. protected:
  144. Container* container;
  145. public:
  146. typedef Container container_type;
  147. typedef void value_type;
  148. typedef void difference_type;
  149. typedef void reference;
  150. typedef void pointer;
  151. explicit front_insert_iterator(Container& x);
  152. front_insert_iterator& operator=(const typename Container::value_type& value);
  153. front_insert_iterator& operator*();
  154. front_insert_iterator& operator++();
  155. front_insert_iterator operator++(int);
  156. };
  157. template <class Container> front_insert_iterator<Container> front_inserter(Container& x);
  158. template <class Container>
  159. class insert_iterator
  160. {
  161. protected:
  162. Container* container;
  163. typename Container::iterator iter;
  164. public:
  165. typedef Container container_type;
  166. typedef void value_type;
  167. typedef void difference_type;
  168. typedef void reference;
  169. typedef void pointer;
  170. insert_iterator(Container& x, typename Container::iterator i);
  171. insert_iterator& operator=(const typename Container::value_type& value);
  172. insert_iterator& operator*();
  173. insert_iterator& operator++();
  174. insert_iterator& operator++(int);
  175. };
  176. template <class Container, class Iterator>
  177. insert_iterator<Container> inserter(Container& x, Iterator i);
  178. template <class Iterator>
  179. class move_iterator {
  180. public:
  181. typedef Iterator iterator_type;
  182. typedef typename iterator_traits<Iterator>::difference_type difference_type;
  183. typedef Iterator pointer;
  184. typedef typename iterator_traits<Iterator>::value_type value_type;
  185. typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
  186. typedef value_type&& reference;
  187. move_iterator();
  188. explicit move_iterator(Iterator i);
  189. template <class U> move_iterator(const move_iterator<U>& u);
  190. template <class U> move_iterator& operator=(const move_iterator<U>& u);
  191. iterator_type base() const;
  192. reference operator*() const;
  193. pointer operator->() const;
  194. move_iterator& operator++();
  195. move_iterator operator++(int);
  196. move_iterator& operator--();
  197. move_iterator operator--(int);
  198. move_iterator operator+(difference_type n) const;
  199. move_iterator& operator+=(difference_type n);
  200. move_iterator operator-(difference_type n) const;
  201. move_iterator& operator-=(difference_type n);
  202. unspecified operator[](difference_type n) const;
  203. private:
  204. Iterator current; // exposition only
  205. };
  206. template <class Iterator1, class Iterator2>
  207. bool
  208. operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
  209. template <class Iterator1, class Iterator2>
  210. bool
  211. operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
  212. template <class Iterator1, class Iterator2>
  213. bool
  214. operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
  215. template <class Iterator1, class Iterator2>
  216. bool
  217. operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
  218. template <class Iterator1, class Iterator2>
  219. bool
  220. operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
  221. template <class Iterator1, class Iterator2>
  222. bool
  223. operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
  224. template <class Iterator1, class Iterator2>
  225. auto
  226. operator-(const move_iterator<Iterator1>& x,
  227. const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
  228. template <class Iterator>
  229. move_iterator<Iterator> operator+(typename move_iterator<Iterator>::difference_type n,
  230. const move_iterator<Iterator>& x);
  231. template <class Iterator>
  232. move_iterator<Iterator> make_move_iterator(const Iterator& i);
  233. template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
  234. class istream_iterator
  235. : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
  236. {
  237. public:
  238. typedef charT char_type;
  239. typedef traits traits_type;
  240. typedef basic_istream<charT,traits> istream_type;
  241. constexpr istream_iterator();
  242. istream_iterator(istream_type& s);
  243. istream_iterator(const istream_iterator& x);
  244. ~istream_iterator();
  245. const T& operator*() const;
  246. const T* operator->() const;
  247. istream_iterator& operator++();
  248. istream_iterator operator++(int);
  249. };
  250. template <class T, class charT, class traits, class Distance>
  251. bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
  252. const istream_iterator<T,charT,traits,Distance>& y);
  253. template <class T, class charT, class traits, class Distance>
  254. bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
  255. const istream_iterator<T,charT,traits,Distance>& y);
  256. template <class T, class charT = char, class traits = char_traits<charT> >
  257. class ostream_iterator
  258. : public iterator<output_iterator_tag, void, void, void ,void>
  259. {
  260. public:
  261. typedef charT char_type;
  262. typedef traits traits_type;
  263. typedef basic_ostream<charT,traits> ostream_type;
  264. ostream_iterator(ostream_type& s);
  265. ostream_iterator(ostream_type& s, const charT* delimiter);
  266. ostream_iterator(const ostream_iterator& x);
  267. ~ostream_iterator();
  268. ostream_iterator& operator=(const T& value);
  269. ostream_iterator& operator*();
  270. ostream_iterator& operator++();
  271. ostream_iterator& operator++(int);
  272. };
  273. template<class charT, class traits = char_traits<charT> >
  274. class istreambuf_iterator
  275. : public iterator<input_iterator_tag, charT,
  276. typename traits::off_type, unspecified,
  277. charT>
  278. {
  279. public:
  280. typedef charT char_type;
  281. typedef traits traits_type;
  282. typedef typename traits::int_type int_type;
  283. typedef basic_streambuf<charT,traits> streambuf_type;
  284. typedef basic_istream<charT,traits> istream_type;
  285. istreambuf_iterator() noexcept;
  286. istreambuf_iterator(istream_type& s) noexcept;
  287. istreambuf_iterator(streambuf_type* s) noexcept;
  288. istreambuf_iterator(a-private-type) noexcept;
  289. charT operator*() const;
  290. pointer operator->() const;
  291. istreambuf_iterator& operator++();
  292. a-private-type operator++(int);
  293. bool equal(const istreambuf_iterator& b) const;
  294. };
  295. template <class charT, class traits>
  296. bool operator==(const istreambuf_iterator<charT,traits>& a,
  297. const istreambuf_iterator<charT,traits>& b);
  298. template <class charT, class traits>
  299. bool operator!=(const istreambuf_iterator<charT,traits>& a,
  300. const istreambuf_iterator<charT,traits>& b);
  301. template <class charT, class traits = char_traits<charT> >
  302. class ostreambuf_iterator
  303. : public iterator<output_iterator_tag, void, void, void, void>
  304. {
  305. public:
  306. typedef charT char_type;
  307. typedef traits traits_type;
  308. typedef basic_streambuf<charT,traits> streambuf_type;
  309. typedef basic_ostream<charT,traits> ostream_type;
  310. ostreambuf_iterator(ostream_type& s) noexcept;
  311. ostreambuf_iterator(streambuf_type* s) noexcept;
  312. ostreambuf_iterator& operator=(charT c);
  313. ostreambuf_iterator& operator*();
  314. ostreambuf_iterator& operator++();
  315. ostreambuf_iterator& operator++(int);
  316. bool failed() const noexcept;
  317. };
  318. template <class C> auto begin(C& c) -> decltype(c.begin());
  319. template <class C> auto begin(const C& c) -> decltype(c.begin());
  320. template <class C> auto end(C& c) -> decltype(c.end());
  321. template <class C> auto end(const C& c) -> decltype(c.end());
  322. template <class T, size_t N> T* begin(T (&array)[N]);
  323. template <class T, size_t N> T* end(T (&array)[N]);
  324. template <class C> auto cbegin(const C& c) -> decltype(std::begin(c)); // C++14
  325. template <class C> auto cend(const C& c) -> decltype(std::end(c)); // C++14
  326. template <class C> auto rbegin(C& c) -> decltype(c.rbegin()); // C++14
  327. template <class C> auto rbegin(const C& c) -> decltype(c.rbegin()); // C++14
  328. template <class C> auto rend(C& c) -> decltype(c.rend()); // C++14
  329. template <class C> auto rend(const C& c) -> decltype(c.rend()); // C++14
  330. template <class E> reverse_iterator<const E*> rbegin(initializer_list<E> il); // C++14
  331. template <class E> reverse_iterator<const E*> rend(initializer_list<E> il); // C++14
  332. template <class T, size_t N> reverse_iterator<T*> rbegin(T (&array)[N]); // C++14
  333. template <class T, size_t N> reverse_iterator<T*> rend(T (&array)[N]); // C++14
  334. template <class C> auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
  335. template <class C> auto crend(const C& c) -> decltype(std::rend(c)); // C++14
  336. // 24.8, container access:
  337. template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
  338. template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
  339. template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
  340. template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
  341. template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
  342. template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
  343. template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
  344. template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
  345. template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
  346. } // std
  347. */
  348. #include <__config>
  349. #include <iosfwd> // for forward declarations of vector and string.
  350. #include <__functional_base>
  351. #include <type_traits>
  352. #include <cstddef>
  353. #include <initializer_list>
  354. #ifdef __APPLE__
  355. #include <Availability.h>
  356. #endif
  357. #include <__debug>
  358. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  359. #pragma GCC system_header
  360. #endif
  361. _LIBCPP_BEGIN_NAMESPACE_STD
  362. struct _LIBCPP_TYPE_VIS_ONLY input_iterator_tag {};
  363. struct _LIBCPP_TYPE_VIS_ONLY output_iterator_tag {};
  364. struct _LIBCPP_TYPE_VIS_ONLY forward_iterator_tag : public input_iterator_tag {};
  365. struct _LIBCPP_TYPE_VIS_ONLY bidirectional_iterator_tag : public forward_iterator_tag {};
  366. struct _LIBCPP_TYPE_VIS_ONLY random_access_iterator_tag : public bidirectional_iterator_tag {};
  367. template <class _Tp>
  368. struct __has_iterator_category
  369. {
  370. private:
  371. struct __two {char __lx; char __lxx;};
  372. template <class _Up> static __two __test(...);
  373. template <class _Up> static char __test(typename _Up::iterator_category* = 0);
  374. public:
  375. static const bool value = sizeof(__test<_Tp>(0)) == 1;
  376. };
  377. template <class _Iter, bool> struct __iterator_traits_impl {};
  378. template <class _Iter>
  379. struct __iterator_traits_impl<_Iter, true>
  380. {
  381. typedef typename _Iter::difference_type difference_type;
  382. typedef typename _Iter::value_type value_type;
  383. typedef typename _Iter::pointer pointer;
  384. typedef typename _Iter::reference reference;
  385. typedef typename _Iter::iterator_category iterator_category;
  386. };
  387. template <class _Iter, bool> struct __iterator_traits {};
  388. template <class _Iter>
  389. struct __iterator_traits<_Iter, true>
  390. : __iterator_traits_impl
  391. <
  392. _Iter,
  393. is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
  394. is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
  395. >
  396. {};
  397. // iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
  398. // exists. Else iterator_traits<Iterator> will be an empty class. This is a
  399. // conforming extension which allows some programs to compile and behave as
  400. // the client expects instead of failing at compile time.
  401. template <class _Iter>
  402. struct _LIBCPP_TYPE_VIS_ONLY iterator_traits
  403. : __iterator_traits<_Iter, __has_iterator_category<_Iter>::value> {};
  404. template<class _Tp>
  405. struct _LIBCPP_TYPE_VIS_ONLY iterator_traits<_Tp*>
  406. {
  407. typedef ptrdiff_t difference_type;
  408. typedef typename remove_const<_Tp>::type value_type;
  409. typedef _Tp* pointer;
  410. typedef _Tp& reference;
  411. typedef random_access_iterator_tag iterator_category;
  412. };
  413. template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
  414. struct __has_iterator_category_convertible_to
  415. : public integral_constant<bool, is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
  416. {};
  417. template <class _Tp, class _Up>
  418. struct __has_iterator_category_convertible_to<_Tp, _Up, false> : public false_type {};
  419. template <class _Tp>
  420. struct __is_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
  421. template <class _Tp>
  422. struct __is_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
  423. template <class _Tp>
  424. struct __is_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
  425. template <class _Tp>
  426. struct __is_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
  427. template <class _Tp>
  428. struct __is_exactly_input_iterator
  429. : public integral_constant<bool,
  430. __has_iterator_category_convertible_to<_Tp, input_iterator_tag>::value &&
  431. !__has_iterator_category_convertible_to<_Tp, forward_iterator_tag>::value> {};
  432. template<class _Category, class _Tp, class _Distance = ptrdiff_t,
  433. class _Pointer = _Tp*, class _Reference = _Tp&>
  434. struct _LIBCPP_TYPE_VIS_ONLY iterator
  435. {
  436. typedef _Tp value_type;
  437. typedef _Distance difference_type;
  438. typedef _Pointer pointer;
  439. typedef _Reference reference;
  440. typedef _Category iterator_category;
  441. };
  442. template <class _InputIter>
  443. inline _LIBCPP_INLINE_VISIBILITY
  444. void __advance(_InputIter& __i,
  445. typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
  446. {
  447. for (; __n > 0; --__n)
  448. ++__i;
  449. }
  450. template <class _BiDirIter>
  451. inline _LIBCPP_INLINE_VISIBILITY
  452. void __advance(_BiDirIter& __i,
  453. typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
  454. {
  455. if (__n >= 0)
  456. for (; __n > 0; --__n)
  457. ++__i;
  458. else
  459. for (; __n < 0; ++__n)
  460. --__i;
  461. }
  462. template <class _RandIter>
  463. inline _LIBCPP_INLINE_VISIBILITY
  464. void __advance(_RandIter& __i,
  465. typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
  466. {
  467. __i += __n;
  468. }
  469. template <class _InputIter>
  470. inline _LIBCPP_INLINE_VISIBILITY
  471. void advance(_InputIter& __i,
  472. typename iterator_traits<_InputIter>::difference_type __n)
  473. {
  474. __advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
  475. }
  476. template <class _InputIter>
  477. inline _LIBCPP_INLINE_VISIBILITY
  478. typename iterator_traits<_InputIter>::difference_type
  479. __distance(_InputIter __first, _InputIter __last, input_iterator_tag)
  480. {
  481. typename iterator_traits<_InputIter>::difference_type __r(0);
  482. for (; __first != __last; ++__first)
  483. ++__r;
  484. return __r;
  485. }
  486. template <class _RandIter>
  487. inline _LIBCPP_INLINE_VISIBILITY
  488. typename iterator_traits<_RandIter>::difference_type
  489. __distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
  490. {
  491. return __last - __first;
  492. }
  493. template <class _InputIter>
  494. inline _LIBCPP_INLINE_VISIBILITY
  495. typename iterator_traits<_InputIter>::difference_type
  496. distance(_InputIter __first, _InputIter __last)
  497. {
  498. return __distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
  499. }
  500. template <class _InputIter>
  501. inline _LIBCPP_INLINE_VISIBILITY
  502. _InputIter
  503. next(_InputIter __x,
  504. typename iterator_traits<_InputIter>::difference_type __n = 1,
  505. typename enable_if<__is_input_iterator<_InputIter>::value>::type* = 0)
  506. {
  507. _VSTD::advance(__x, __n);
  508. return __x;
  509. }
  510. template <class _BidiretionalIter>
  511. inline _LIBCPP_INLINE_VISIBILITY
  512. _BidiretionalIter
  513. prev(_BidiretionalIter __x,
  514. typename iterator_traits<_BidiretionalIter>::difference_type __n = 1,
  515. typename enable_if<__is_bidirectional_iterator<_BidiretionalIter>::value>::type* = 0)
  516. {
  517. _VSTD::advance(__x, -__n);
  518. return __x;
  519. }
  520. template <class _Iter>
  521. class _LIBCPP_TYPE_VIS_ONLY reverse_iterator
  522. : public iterator<typename iterator_traits<_Iter>::iterator_category,
  523. typename iterator_traits<_Iter>::value_type,
  524. typename iterator_traits<_Iter>::difference_type,
  525. typename iterator_traits<_Iter>::pointer,
  526. typename iterator_traits<_Iter>::reference>
  527. {
  528. private:
  529. #if !defined(_LIBCPP_SGX_CONFIG)
  530. mutable _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
  531. #endif // !defined(_LIBCPP_SGX_CONFIG)
  532. protected:
  533. _Iter current;
  534. public:
  535. typedef _Iter iterator_type;
  536. typedef typename iterator_traits<_Iter>::difference_type difference_type;
  537. typedef typename iterator_traits<_Iter>::reference reference;
  538. typedef typename iterator_traits<_Iter>::pointer pointer;
  539. _LIBCPP_INLINE_VISIBILITY reverse_iterator() : current() {}
  540. #if !defined(_LIBCPP_SGX_CONFIG)
  541. _LIBCPP_INLINE_VISIBILITY explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
  542. #else
  543. _LIBCPP_INLINE_VISIBILITY explicit reverse_iterator(_Iter __x) : current(__x) {}
  544. #endif // !defined(_LIBCPP_SGX_CONFIG)
  545. template <class _Up> _LIBCPP_INLINE_VISIBILITY reverse_iterator(const reverse_iterator<_Up>& __u)
  546. #if !defined(_LIBCPP_SGX_CONFIG)
  547. : __t(__u.base()), current(__u.base()) {}
  548. #else
  549. : current(__u.base()) {}
  550. #endif // !defined(_LIBCPP_SGX_CONFIG)
  551. _LIBCPP_INLINE_VISIBILITY _Iter base() const {return current;}
  552. _LIBCPP_INLINE_VISIBILITY reference operator*() const {_Iter __tmp = current; return *--__tmp;}
  553. _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return _VSTD::addressof(operator*());}
  554. _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator++() {--current; return *this;}
  555. _LIBCPP_INLINE_VISIBILITY reverse_iterator operator++(int)
  556. {reverse_iterator __tmp(*this); --current; return __tmp;}
  557. _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator--() {++current; return *this;}
  558. _LIBCPP_INLINE_VISIBILITY reverse_iterator operator--(int)
  559. {reverse_iterator __tmp(*this); ++current; return __tmp;}
  560. _LIBCPP_INLINE_VISIBILITY reverse_iterator operator+ (difference_type __n) const
  561. {return reverse_iterator(current - __n);}
  562. _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator+=(difference_type __n)
  563. {current -= __n; return *this;}
  564. _LIBCPP_INLINE_VISIBILITY reverse_iterator operator- (difference_type __n) const
  565. {return reverse_iterator(current + __n);}
  566. _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator-=(difference_type __n)
  567. {current += __n; return *this;}
  568. _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
  569. {return *(*this + __n);}
  570. };
  571. template <class _Iter1, class _Iter2>
  572. inline _LIBCPP_INLINE_VISIBILITY
  573. bool
  574. operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
  575. {
  576. return __x.base() == __y.base();
  577. }
  578. template <class _Iter1, class _Iter2>
  579. inline _LIBCPP_INLINE_VISIBILITY
  580. bool
  581. operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
  582. {
  583. return __x.base() > __y.base();
  584. }
  585. template <class _Iter1, class _Iter2>
  586. inline _LIBCPP_INLINE_VISIBILITY
  587. bool
  588. operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
  589. {
  590. return __x.base() != __y.base();
  591. }
  592. template <class _Iter1, class _Iter2>
  593. inline _LIBCPP_INLINE_VISIBILITY
  594. bool
  595. operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
  596. {
  597. return __x.base() < __y.base();
  598. }
  599. template <class _Iter1, class _Iter2>
  600. inline _LIBCPP_INLINE_VISIBILITY
  601. bool
  602. operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
  603. {
  604. return __x.base() <= __y.base();
  605. }
  606. template <class _Iter1, class _Iter2>
  607. inline _LIBCPP_INLINE_VISIBILITY
  608. bool
  609. operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
  610. {
  611. return __x.base() >= __y.base();
  612. }
  613. #ifndef _LIBCPP_CXX03_LANG
  614. template <class _Iter1, class _Iter2>
  615. inline _LIBCPP_INLINE_VISIBILITY
  616. auto
  617. operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
  618. -> decltype(__y.base() - __x.base())
  619. {
  620. return __y.base() - __x.base();
  621. }
  622. #else
  623. template <class _Iter1, class _Iter2>
  624. inline _LIBCPP_INLINE_VISIBILITY
  625. typename reverse_iterator<_Iter1>::difference_type
  626. operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
  627. {
  628. return __y.base() - __x.base();
  629. }
  630. #endif
  631. template <class _Iter>
  632. inline _LIBCPP_INLINE_VISIBILITY
  633. reverse_iterator<_Iter>
  634. operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
  635. {
  636. return reverse_iterator<_Iter>(__x.base() - __n);
  637. }
  638. #if _LIBCPP_STD_VER > 11
  639. template <class _Iter>
  640. inline _LIBCPP_INLINE_VISIBILITY
  641. reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
  642. {
  643. return reverse_iterator<_Iter>(__i);
  644. }
  645. #endif
  646. template <class _Container>
  647. class _LIBCPP_TYPE_VIS_ONLY back_insert_iterator
  648. : public iterator<output_iterator_tag,
  649. void,
  650. void,
  651. void,
  652. void>
  653. {
  654. protected:
  655. _Container* container;
  656. public:
  657. typedef _Container container_type;
  658. _LIBCPP_INLINE_VISIBILITY explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
  659. _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(const typename _Container::value_type& __value_)
  660. {container->push_back(__value_); return *this;}
  661. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  662. _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(typename _Container::value_type&& __value_)
  663. {container->push_back(_VSTD::move(__value_)); return *this;}
  664. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  665. _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator*() {return *this;}
  666. _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator++() {return *this;}
  667. _LIBCPP_INLINE_VISIBILITY back_insert_iterator operator++(int) {return *this;}
  668. };
  669. template <class _Container>
  670. inline _LIBCPP_INLINE_VISIBILITY
  671. back_insert_iterator<_Container>
  672. back_inserter(_Container& __x)
  673. {
  674. return back_insert_iterator<_Container>(__x);
  675. }
  676. template <class _Container>
  677. class _LIBCPP_TYPE_VIS_ONLY front_insert_iterator
  678. : public iterator<output_iterator_tag,
  679. void,
  680. void,
  681. void,
  682. void>
  683. {
  684. protected:
  685. _Container* container;
  686. public:
  687. typedef _Container container_type;
  688. _LIBCPP_INLINE_VISIBILITY explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
  689. _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(const typename _Container::value_type& __value_)
  690. {container->push_front(__value_); return *this;}
  691. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  692. _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(typename _Container::value_type&& __value_)
  693. {container->push_front(_VSTD::move(__value_)); return *this;}
  694. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  695. _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator*() {return *this;}
  696. _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator++() {return *this;}
  697. _LIBCPP_INLINE_VISIBILITY front_insert_iterator operator++(int) {return *this;}
  698. };
  699. template <class _Container>
  700. inline _LIBCPP_INLINE_VISIBILITY
  701. front_insert_iterator<_Container>
  702. front_inserter(_Container& __x)
  703. {
  704. return front_insert_iterator<_Container>(__x);
  705. }
  706. template <class _Container>
  707. class _LIBCPP_TYPE_VIS_ONLY insert_iterator
  708. : public iterator<output_iterator_tag,
  709. void,
  710. void,
  711. void,
  712. void>
  713. {
  714. protected:
  715. _Container* container;
  716. typename _Container::iterator iter;
  717. public:
  718. typedef _Container container_type;
  719. _LIBCPP_INLINE_VISIBILITY insert_iterator(_Container& __x, typename _Container::iterator __i)
  720. : container(_VSTD::addressof(__x)), iter(__i) {}
  721. _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(const typename _Container::value_type& __value_)
  722. {iter = container->insert(iter, __value_); ++iter; return *this;}
  723. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  724. _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(typename _Container::value_type&& __value_)
  725. {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
  726. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  727. _LIBCPP_INLINE_VISIBILITY insert_iterator& operator*() {return *this;}
  728. _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++() {return *this;}
  729. _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++(int) {return *this;}
  730. };
  731. template <class _Container>
  732. inline _LIBCPP_INLINE_VISIBILITY
  733. insert_iterator<_Container>
  734. inserter(_Container& __x, typename _Container::iterator __i)
  735. {
  736. return insert_iterator<_Container>(__x, __i);
  737. }
  738. #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  739. template <class _Tp, class _CharT = char,
  740. class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
  741. class _LIBCPP_TYPE_VIS_ONLY istream_iterator
  742. : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
  743. {
  744. public:
  745. typedef _CharT char_type;
  746. typedef _Traits traits_type;
  747. typedef basic_istream<_CharT,_Traits> istream_type;
  748. private:
  749. istream_type* __in_stream_;
  750. _Tp __value_;
  751. public:
  752. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(0), __value_() {}
  753. _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
  754. {
  755. if (!(*__in_stream_ >> __value_))
  756. __in_stream_ = 0;
  757. }
  758. _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
  759. _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
  760. _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
  761. {
  762. if (!(*__in_stream_ >> __value_))
  763. __in_stream_ = 0;
  764. return *this;
  765. }
  766. _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
  767. {istream_iterator __t(*this); ++(*this); return __t;}
  768. friend _LIBCPP_INLINE_VISIBILITY
  769. bool operator==(const istream_iterator& __x, const istream_iterator& __y)
  770. {return __x.__in_stream_ == __y.__in_stream_;}
  771. friend _LIBCPP_INLINE_VISIBILITY
  772. bool operator!=(const istream_iterator& __x, const istream_iterator& __y)
  773. {return !(__x == __y);}
  774. };
  775. template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
  776. class _LIBCPP_TYPE_VIS_ONLY ostream_iterator
  777. : public iterator<output_iterator_tag, void, void, void, void>
  778. {
  779. public:
  780. typedef _CharT char_type;
  781. typedef _Traits traits_type;
  782. typedef basic_ostream<_CharT,_Traits> ostream_type;
  783. private:
  784. ostream_type* __out_stream_;
  785. const char_type* __delim_;
  786. public:
  787. _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s)
  788. : __out_stream_(_VSTD::addressof(__s)), __delim_(0) {}
  789. _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter)
  790. : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
  791. _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
  792. {
  793. *__out_stream_ << __value_;
  794. if (__delim_)
  795. *__out_stream_ << __delim_;
  796. return *this;
  797. }
  798. _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
  799. _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
  800. _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
  801. };
  802. template<class _CharT, class _Traits>
  803. class _LIBCPP_TYPE_VIS_ONLY istreambuf_iterator
  804. : public iterator<input_iterator_tag, _CharT,
  805. typename _Traits::off_type, _CharT*,
  806. _CharT>
  807. {
  808. public:
  809. typedef _CharT char_type;
  810. typedef _Traits traits_type;
  811. typedef typename _Traits::int_type int_type;
  812. typedef basic_streambuf<_CharT,_Traits> streambuf_type;
  813. typedef basic_istream<_CharT,_Traits> istream_type;
  814. private:
  815. mutable streambuf_type* __sbuf_;
  816. class __proxy
  817. {
  818. char_type __keep_;
  819. streambuf_type* __sbuf_;
  820. _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
  821. : __keep_(__c), __sbuf_(__s) {}
  822. friend class istreambuf_iterator;
  823. public:
  824. _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
  825. };
  826. _LIBCPP_INLINE_VISIBILITY
  827. bool __test_for_eof() const
  828. {
  829. if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
  830. __sbuf_ = 0;
  831. return __sbuf_ == 0;
  832. }
  833. public:
  834. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(0) {}
  835. _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
  836. : __sbuf_(__s.rdbuf()) {}
  837. _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
  838. : __sbuf_(__s) {}
  839. _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
  840. : __sbuf_(__p.__sbuf_) {}
  841. _LIBCPP_INLINE_VISIBILITY char_type operator*() const
  842. {return static_cast<char_type>(__sbuf_->sgetc());}
  843. _LIBCPP_INLINE_VISIBILITY char_type* operator->() const {return nullptr;}
  844. _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
  845. {
  846. __sbuf_->sbumpc();
  847. return *this;
  848. }
  849. _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
  850. {
  851. return __proxy(__sbuf_->sbumpc(), __sbuf_);
  852. }
  853. _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
  854. {return __test_for_eof() == __b.__test_for_eof();}
  855. };
  856. template <class _CharT, class _Traits>
  857. inline _LIBCPP_INLINE_VISIBILITY
  858. bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
  859. const istreambuf_iterator<_CharT,_Traits>& __b)
  860. {return __a.equal(__b);}
  861. template <class _CharT, class _Traits>
  862. inline _LIBCPP_INLINE_VISIBILITY
  863. bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
  864. const istreambuf_iterator<_CharT,_Traits>& __b)
  865. {return !__a.equal(__b);}
  866. template <class _CharT, class _Traits>
  867. class _LIBCPP_TYPE_VIS_ONLY ostreambuf_iterator
  868. : public iterator<output_iterator_tag, void, void, void, void>
  869. {
  870. public:
  871. typedef _CharT char_type;
  872. typedef _Traits traits_type;
  873. typedef basic_streambuf<_CharT,_Traits> streambuf_type;
  874. typedef basic_ostream<_CharT,_Traits> ostream_type;
  875. private:
  876. streambuf_type* __sbuf_;
  877. public:
  878. _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
  879. : __sbuf_(__s.rdbuf()) {}
  880. _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
  881. : __sbuf_(__s) {}
  882. _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
  883. {
  884. if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
  885. __sbuf_ = 0;
  886. return *this;
  887. }
  888. _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
  889. _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
  890. _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
  891. _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == 0;}
  892. #if !defined(__APPLE__) || \
  893. (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
  894. (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
  895. template <class _Ch, class _Tr>
  896. friend
  897. _LIBCPP_HIDDEN
  898. ostreambuf_iterator<_Ch, _Tr>
  899. __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
  900. const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
  901. ios_base& __iob, _Ch __fl);
  902. #endif
  903. };
  904. #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  905. template <class _Iter>
  906. class _LIBCPP_TYPE_VIS_ONLY move_iterator
  907. {
  908. private:
  909. _Iter __i;
  910. public:
  911. typedef _Iter iterator_type;
  912. typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
  913. typedef typename iterator_traits<iterator_type>::value_type value_type;
  914. typedef typename iterator_traits<iterator_type>::difference_type difference_type;
  915. typedef iterator_type pointer;
  916. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  917. typedef typename iterator_traits<iterator_type>::reference __reference;
  918. typedef typename conditional<
  919. is_reference<__reference>::value,
  920. typename remove_reference<__reference>::type&&,
  921. __reference
  922. >::type reference;
  923. #else
  924. typedef typename iterator_traits<iterator_type>::reference reference;
  925. #endif
  926. _LIBCPP_INLINE_VISIBILITY move_iterator() : __i() {}
  927. _LIBCPP_INLINE_VISIBILITY explicit move_iterator(_Iter __x) : __i(__x) {}
  928. template <class _Up> _LIBCPP_INLINE_VISIBILITY move_iterator(const move_iterator<_Up>& __u)
  929. : __i(__u.base()) {}
  930. _LIBCPP_INLINE_VISIBILITY _Iter base() const {return __i;}
  931. _LIBCPP_INLINE_VISIBILITY reference operator*() const {
  932. return static_cast<reference>(*__i);
  933. }
  934. _LIBCPP_INLINE_VISIBILITY pointer operator->() const { return __i;}
  935. _LIBCPP_INLINE_VISIBILITY move_iterator& operator++() {++__i; return *this;}
  936. _LIBCPP_INLINE_VISIBILITY move_iterator operator++(int)
  937. {move_iterator __tmp(*this); ++__i; return __tmp;}
  938. _LIBCPP_INLINE_VISIBILITY move_iterator& operator--() {--__i; return *this;}
  939. _LIBCPP_INLINE_VISIBILITY move_iterator operator--(int)
  940. {move_iterator __tmp(*this); --__i; return __tmp;}
  941. _LIBCPP_INLINE_VISIBILITY move_iterator operator+ (difference_type __n) const
  942. {return move_iterator(__i + __n);}
  943. _LIBCPP_INLINE_VISIBILITY move_iterator& operator+=(difference_type __n)
  944. {__i += __n; return *this;}
  945. _LIBCPP_INLINE_VISIBILITY move_iterator operator- (difference_type __n) const
  946. {return move_iterator(__i - __n);}
  947. _LIBCPP_INLINE_VISIBILITY move_iterator& operator-=(difference_type __n)
  948. {__i -= __n; return *this;}
  949. _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
  950. {
  951. return static_cast<reference>(__i[__n]);
  952. }
  953. };
  954. template <class _Iter1, class _Iter2>
  955. inline _LIBCPP_INLINE_VISIBILITY
  956. bool
  957. operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
  958. {
  959. return __x.base() == __y.base();
  960. }
  961. template <class _Iter1, class _Iter2>
  962. inline _LIBCPP_INLINE_VISIBILITY
  963. bool
  964. operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
  965. {
  966. return __x.base() < __y.base();
  967. }
  968. template <class _Iter1, class _Iter2>
  969. inline _LIBCPP_INLINE_VISIBILITY
  970. bool
  971. operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
  972. {
  973. return __x.base() != __y.base();
  974. }
  975. template <class _Iter1, class _Iter2>
  976. inline _LIBCPP_INLINE_VISIBILITY
  977. bool
  978. operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
  979. {
  980. return __x.base() > __y.base();
  981. }
  982. template <class _Iter1, class _Iter2>
  983. inline _LIBCPP_INLINE_VISIBILITY
  984. bool
  985. operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
  986. {
  987. return __x.base() >= __y.base();
  988. }
  989. template <class _Iter1, class _Iter2>
  990. inline _LIBCPP_INLINE_VISIBILITY
  991. bool
  992. operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
  993. {
  994. return __x.base() <= __y.base();
  995. }
  996. #ifndef _LIBCPP_CXX03_LANG
  997. template <class _Iter1, class _Iter2>
  998. inline _LIBCPP_INLINE_VISIBILITY
  999. auto
  1000. operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
  1001. -> decltype(__x.base() - __y.base())
  1002. {
  1003. return __x.base() - __y.base();
  1004. }
  1005. #else
  1006. template <class _Iter1, class _Iter2>
  1007. inline _LIBCPP_INLINE_VISIBILITY
  1008. typename move_iterator<_Iter1>::difference_type
  1009. operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
  1010. {
  1011. return __x.base() - __y.base();
  1012. }
  1013. #endif
  1014. template <class _Iter>
  1015. inline _LIBCPP_INLINE_VISIBILITY
  1016. move_iterator<_Iter>
  1017. operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
  1018. {
  1019. return move_iterator<_Iter>(__x.base() + __n);
  1020. }
  1021. template <class _Iter>
  1022. inline _LIBCPP_INLINE_VISIBILITY
  1023. move_iterator<_Iter>
  1024. make_move_iterator(_Iter __i)
  1025. {
  1026. return move_iterator<_Iter>(__i);
  1027. }
  1028. // __wrap_iter
  1029. template <class _Iter> class __wrap_iter;
  1030. template <class _Iter1, class _Iter2>
  1031. _LIBCPP_INLINE_VISIBILITY
  1032. bool
  1033. operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1034. template <class _Iter1, class _Iter2>
  1035. _LIBCPP_INLINE_VISIBILITY
  1036. bool
  1037. operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1038. template <class _Iter1, class _Iter2>
  1039. _LIBCPP_INLINE_VISIBILITY
  1040. bool
  1041. operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1042. template <class _Iter1, class _Iter2>
  1043. _LIBCPP_INLINE_VISIBILITY
  1044. bool
  1045. operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1046. template <class _Iter1, class _Iter2>
  1047. _LIBCPP_INLINE_VISIBILITY
  1048. bool
  1049. operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1050. template <class _Iter1, class _Iter2>
  1051. _LIBCPP_INLINE_VISIBILITY
  1052. bool
  1053. operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1054. #ifndef _LIBCPP_CXX03_LANG
  1055. template <class _Iter1, class _Iter2>
  1056. _LIBCPP_INLINE_VISIBILITY
  1057. auto
  1058. operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1059. -> decltype(__x.base() - __y.base());
  1060. #else
  1061. template <class _Iter1, class _Iter2>
  1062. _LIBCPP_INLINE_VISIBILITY
  1063. typename __wrap_iter<_Iter1>::difference_type
  1064. operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1065. #endif
  1066. template <class _Iter>
  1067. _LIBCPP_INLINE_VISIBILITY
  1068. __wrap_iter<_Iter>
  1069. operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
  1070. template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY copy(_Ip, _Ip, _Op);
  1071. template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY copy_backward(_B1, _B1, _B2);
  1072. template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY move(_Ip, _Ip, _Op);
  1073. template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY move_backward(_B1, _B1, _B2);
  1074. template <class _Tp>
  1075. _LIBCPP_INLINE_VISIBILITY
  1076. typename enable_if
  1077. <
  1078. is_trivially_copy_assignable<_Tp>::value,
  1079. _Tp*
  1080. >::type
  1081. __unwrap_iter(__wrap_iter<_Tp*>);
  1082. template <class _Iter>
  1083. class __wrap_iter
  1084. {
  1085. public:
  1086. typedef _Iter iterator_type;
  1087. typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
  1088. typedef typename iterator_traits<iterator_type>::value_type value_type;
  1089. typedef typename iterator_traits<iterator_type>::difference_type difference_type;
  1090. typedef typename iterator_traits<iterator_type>::pointer pointer;
  1091. typedef typename iterator_traits<iterator_type>::reference reference;
  1092. private:
  1093. iterator_type __i;
  1094. public:
  1095. _LIBCPP_INLINE_VISIBILITY __wrap_iter() _NOEXCEPT
  1096. #if _LIBCPP_STD_VER > 11
  1097. : __i{}
  1098. #endif
  1099. {
  1100. #if _LIBCPP_DEBUG_LEVEL >= 2
  1101. __get_db()->__insert_i(this);
  1102. #endif
  1103. }
  1104. template <class _Up> _LIBCPP_INLINE_VISIBILITY __wrap_iter(const __wrap_iter<_Up>& __u,
  1105. typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0) _NOEXCEPT
  1106. : __i(__u.base())
  1107. {
  1108. #if _LIBCPP_DEBUG_LEVEL >= 2
  1109. __get_db()->__iterator_copy(this, &__u);
  1110. #endif
  1111. }
  1112. #if _LIBCPP_DEBUG_LEVEL >= 2
  1113. _LIBCPP_INLINE_VISIBILITY
  1114. __wrap_iter(const __wrap_iter& __x)
  1115. : __i(__x.base())
  1116. {
  1117. __get_db()->__iterator_copy(this, &__x);
  1118. }
  1119. _LIBCPP_INLINE_VISIBILITY
  1120. __wrap_iter& operator=(const __wrap_iter& __x)
  1121. {
  1122. if (this != &__x)
  1123. {
  1124. __get_db()->__iterator_copy(this, &__x);
  1125. __i = __x.__i;
  1126. }
  1127. return *this;
  1128. }
  1129. _LIBCPP_INLINE_VISIBILITY
  1130. ~__wrap_iter()
  1131. {
  1132. __get_db()->__erase_i(this);
  1133. }
  1134. #endif
  1135. _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT
  1136. {
  1137. #if _LIBCPP_DEBUG_LEVEL >= 2
  1138. _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
  1139. "Attempted to dereference a non-dereferenceable iterator");
  1140. #endif
  1141. return *__i;
  1142. }
  1143. _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT
  1144. {
  1145. #if _LIBCPP_DEBUG_LEVEL >= 2
  1146. _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
  1147. "Attempted to dereference a non-dereferenceable iterator");
  1148. #endif
  1149. return (pointer)_VSTD::addressof(*__i);
  1150. }
  1151. _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator++() _NOEXCEPT
  1152. {
  1153. #if _LIBCPP_DEBUG_LEVEL >= 2
  1154. _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
  1155. "Attempted to increment non-incrementable iterator");
  1156. #endif
  1157. ++__i;
  1158. return *this;
  1159. }
  1160. _LIBCPP_INLINE_VISIBILITY __wrap_iter operator++(int) _NOEXCEPT
  1161. {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
  1162. _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator--() _NOEXCEPT
  1163. {
  1164. #if _LIBCPP_DEBUG_LEVEL >= 2
  1165. _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
  1166. "Attempted to decrement non-decrementable iterator");
  1167. #endif
  1168. --__i;
  1169. return *this;
  1170. }
  1171. _LIBCPP_INLINE_VISIBILITY __wrap_iter operator--(int) _NOEXCEPT
  1172. {__wrap_iter __tmp(*this); --(*this); return __tmp;}
  1173. _LIBCPP_INLINE_VISIBILITY __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
  1174. {__wrap_iter __w(*this); __w += __n; return __w;}
  1175. _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
  1176. {
  1177. #if _LIBCPP_DEBUG_LEVEL >= 2
  1178. _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
  1179. "Attempted to add/subtract iterator outside of valid range");
  1180. #endif
  1181. __i += __n;
  1182. return *this;
  1183. }
  1184. _LIBCPP_INLINE_VISIBILITY __wrap_iter operator- (difference_type __n) const _NOEXCEPT
  1185. {return *this + (-__n);}
  1186. _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
  1187. {*this += -__n; return *this;}
  1188. _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const _NOEXCEPT
  1189. {
  1190. #if _LIBCPP_DEBUG_LEVEL >= 2
  1191. _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
  1192. "Attempted to subscript iterator outside of valid range");
  1193. #endif
  1194. return __i[__n];
  1195. }
  1196. _LIBCPP_INLINE_VISIBILITY iterator_type base() const _NOEXCEPT {return __i;}
  1197. private:
  1198. #if _LIBCPP_DEBUG_LEVEL >= 2
  1199. _LIBCPP_INLINE_VISIBILITY __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
  1200. {
  1201. __get_db()->__insert_ic(this, __p);
  1202. }
  1203. #else
  1204. _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
  1205. #endif
  1206. template <class _Up> friend class __wrap_iter;
  1207. template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
  1208. template <class _Tp, class _Alloc> friend class _LIBCPP_TYPE_VIS_ONLY vector;
  1209. template <class _Iter1, class _Iter2>
  1210. friend
  1211. bool
  1212. operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1213. template <class _Iter1, class _Iter2>
  1214. friend
  1215. bool
  1216. operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1217. template <class _Iter1, class _Iter2>
  1218. friend
  1219. bool
  1220. operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1221. template <class _Iter1, class _Iter2>
  1222. friend
  1223. bool
  1224. operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1225. template <class _Iter1, class _Iter2>
  1226. friend
  1227. bool
  1228. operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1229. template <class _Iter1, class _Iter2>
  1230. friend
  1231. bool
  1232. operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1233. #ifndef _LIBCPP_CXX03_LANG
  1234. template <class _Iter1, class _Iter2>
  1235. friend
  1236. auto
  1237. operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1238. -> decltype(__x.base() - __y.base());
  1239. #else
  1240. template <class _Iter1, class _Iter2>
  1241. friend
  1242. typename __wrap_iter<_Iter1>::difference_type
  1243. operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
  1244. #endif
  1245. template <class _Iter1>
  1246. friend
  1247. __wrap_iter<_Iter1>
  1248. operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
  1249. template <class _Ip, class _Op> friend _Op copy(_Ip, _Ip, _Op);
  1250. template <class _B1, class _B2> friend _B2 copy_backward(_B1, _B1, _B2);
  1251. template <class _Ip, class _Op> friend _Op move(_Ip, _Ip, _Op);
  1252. template <class _B1, class _B2> friend _B2 move_backward(_B1, _B1, _B2);
  1253. template <class _Tp>
  1254. friend
  1255. typename enable_if
  1256. <
  1257. is_trivially_copy_assignable<_Tp>::value,
  1258. _Tp*
  1259. >::type
  1260. __unwrap_iter(__wrap_iter<_Tp*>);
  1261. };
  1262. template <class _Iter1, class _Iter2>
  1263. inline _LIBCPP_INLINE_VISIBILITY
  1264. bool
  1265. operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1266. {
  1267. return __x.base() == __y.base();
  1268. }
  1269. template <class _Iter1, class _Iter2>
  1270. inline _LIBCPP_INLINE_VISIBILITY
  1271. bool
  1272. operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1273. {
  1274. #if _LIBCPP_DEBUG_LEVEL >= 2
  1275. _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
  1276. "Attempted to compare incomparable iterators");
  1277. #endif
  1278. return __x.base() < __y.base();
  1279. }
  1280. template <class _Iter1, class _Iter2>
  1281. inline _LIBCPP_INLINE_VISIBILITY
  1282. bool
  1283. operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1284. {
  1285. return !(__x == __y);
  1286. }
  1287. template <class _Iter1, class _Iter2>
  1288. inline _LIBCPP_INLINE_VISIBILITY
  1289. bool
  1290. operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1291. {
  1292. return __y < __x;
  1293. }
  1294. template <class _Iter1, class _Iter2>
  1295. inline _LIBCPP_INLINE_VISIBILITY
  1296. bool
  1297. operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1298. {
  1299. return !(__x < __y);
  1300. }
  1301. template <class _Iter1, class _Iter2>
  1302. inline _LIBCPP_INLINE_VISIBILITY
  1303. bool
  1304. operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1305. {
  1306. return !(__y < __x);
  1307. }
  1308. template <class _Iter1>
  1309. inline _LIBCPP_INLINE_VISIBILITY
  1310. bool
  1311. operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
  1312. {
  1313. return !(__x == __y);
  1314. }
  1315. template <class _Iter1>
  1316. inline _LIBCPP_INLINE_VISIBILITY
  1317. bool
  1318. operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
  1319. {
  1320. return __y < __x;
  1321. }
  1322. template <class _Iter1>
  1323. inline _LIBCPP_INLINE_VISIBILITY
  1324. bool
  1325. operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
  1326. {
  1327. return !(__x < __y);
  1328. }
  1329. template <class _Iter1>
  1330. inline _LIBCPP_INLINE_VISIBILITY
  1331. bool
  1332. operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
  1333. {
  1334. return !(__y < __x);
  1335. }
  1336. #ifndef _LIBCPP_CXX03_LANG
  1337. template <class _Iter1, class _Iter2>
  1338. inline _LIBCPP_INLINE_VISIBILITY
  1339. auto
  1340. operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1341. -> decltype(__x.base() - __y.base())
  1342. {
  1343. #if _LIBCPP_DEBUG_LEVEL >= 2
  1344. _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
  1345. "Attempted to subtract incompatible iterators");
  1346. #endif
  1347. return __x.base() - __y.base();
  1348. }
  1349. #else
  1350. template <class _Iter1, class _Iter2>
  1351. inline _LIBCPP_INLINE_VISIBILITY
  1352. typename __wrap_iter<_Iter1>::difference_type
  1353. operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  1354. {
  1355. #if _LIBCPP_DEBUG_LEVEL >= 2
  1356. _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
  1357. "Attempted to subtract incompatible iterators");
  1358. #endif
  1359. return __x.base() - __y.base();
  1360. }
  1361. #endif
  1362. template <class _Iter>
  1363. inline _LIBCPP_INLINE_VISIBILITY
  1364. __wrap_iter<_Iter>
  1365. operator+(typename __wrap_iter<_Iter>::difference_type __n,
  1366. __wrap_iter<_Iter> __x) _NOEXCEPT
  1367. {
  1368. __x += __n;
  1369. return __x;
  1370. }
  1371. template <class _Iter>
  1372. struct __libcpp_is_trivial_iterator
  1373. : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
  1374. template <class _Iter>
  1375. struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
  1376. : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
  1377. template <class _Iter>
  1378. struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
  1379. : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
  1380. template <class _Iter>
  1381. struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
  1382. : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
  1383. template <class _Tp, size_t _Np>
  1384. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1385. _Tp*
  1386. begin(_Tp (&__array)[_Np])
  1387. {
  1388. return __array;
  1389. }
  1390. template <class _Tp, size_t _Np>
  1391. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1392. _Tp*
  1393. end(_Tp (&__array)[_Np])
  1394. {
  1395. return __array + _Np;
  1396. }
  1397. #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
  1398. template <class _Cp>
  1399. inline _LIBCPP_INLINE_VISIBILITY
  1400. auto
  1401. begin(_Cp& __c) -> decltype(__c.begin())
  1402. {
  1403. return __c.begin();
  1404. }
  1405. template <class _Cp>
  1406. inline _LIBCPP_INLINE_VISIBILITY
  1407. auto
  1408. begin(const _Cp& __c) -> decltype(__c.begin())
  1409. {
  1410. return __c.begin();
  1411. }
  1412. template <class _Cp>
  1413. inline _LIBCPP_INLINE_VISIBILITY
  1414. auto
  1415. end(_Cp& __c) -> decltype(__c.end())
  1416. {
  1417. return __c.end();
  1418. }
  1419. template <class _Cp>
  1420. inline _LIBCPP_INLINE_VISIBILITY
  1421. auto
  1422. end(const _Cp& __c) -> decltype(__c.end())
  1423. {
  1424. return __c.end();
  1425. }
  1426. #if _LIBCPP_STD_VER > 11
  1427. template <class _Tp, size_t _Np>
  1428. inline _LIBCPP_INLINE_VISIBILITY
  1429. reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
  1430. {
  1431. return reverse_iterator<_Tp*>(__array + _Np);
  1432. }
  1433. template <class _Tp, size_t _Np>
  1434. inline _LIBCPP_INLINE_VISIBILITY
  1435. reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
  1436. {
  1437. return reverse_iterator<_Tp*>(__array);
  1438. }
  1439. template <class _Ep>
  1440. inline _LIBCPP_INLINE_VISIBILITY
  1441. reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
  1442. {
  1443. return reverse_iterator<const _Ep*>(__il.end());
  1444. }
  1445. template <class _Ep>
  1446. inline _LIBCPP_INLINE_VISIBILITY
  1447. reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
  1448. {
  1449. return reverse_iterator<const _Ep*>(__il.begin());
  1450. }
  1451. template <class _Cp>
  1452. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1453. auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
  1454. {
  1455. return _VSTD::begin(__c);
  1456. }
  1457. template <class _Cp>
  1458. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  1459. auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
  1460. {
  1461. return _VSTD::end(__c);
  1462. }
  1463. template <class _Cp>
  1464. inline _LIBCPP_INLINE_VISIBILITY
  1465. auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
  1466. {
  1467. return __c.rbegin();
  1468. }
  1469. template <class _Cp>
  1470. inline _LIBCPP_INLINE_VISIBILITY
  1471. auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
  1472. {
  1473. return __c.rbegin();
  1474. }
  1475. template <class _Cp>
  1476. inline _LIBCPP_INLINE_VISIBILITY
  1477. auto rend(_Cp& __c) -> decltype(__c.rend())
  1478. {
  1479. return __c.rend();
  1480. }
  1481. template <class _Cp>
  1482. inline _LIBCPP_INLINE_VISIBILITY
  1483. auto rend(const _Cp& __c) -> decltype(__c.rend())
  1484. {
  1485. return __c.rend();
  1486. }
  1487. template <class _Cp>
  1488. inline _LIBCPP_INLINE_VISIBILITY
  1489. auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
  1490. {
  1491. return _VSTD::rbegin(__c);
  1492. }
  1493. template <class _Cp>
  1494. inline _LIBCPP_INLINE_VISIBILITY
  1495. auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
  1496. {
  1497. return _VSTD::rend(__c);
  1498. }
  1499. #endif
  1500. #else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
  1501. template <class _Cp>
  1502. inline _LIBCPP_INLINE_VISIBILITY
  1503. typename _Cp::iterator
  1504. begin(_Cp& __c)
  1505. {
  1506. return __c.begin();
  1507. }
  1508. template <class _Cp>
  1509. inline _LIBCPP_INLINE_VISIBILITY
  1510. typename _Cp::const_iterator
  1511. begin(const _Cp& __c)
  1512. {
  1513. return __c.begin();
  1514. }
  1515. template <class _Cp>
  1516. inline _LIBCPP_INLINE_VISIBILITY
  1517. typename _Cp::iterator
  1518. end(_Cp& __c)
  1519. {
  1520. return __c.end();
  1521. }
  1522. template <class _Cp>
  1523. inline _LIBCPP_INLINE_VISIBILITY
  1524. typename _Cp::const_iterator
  1525. end(const _Cp& __c)
  1526. {
  1527. return __c.end();
  1528. }
  1529. #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
  1530. #if _LIBCPP_STD_VER > 14
  1531. template <class _Cont>
  1532. constexpr auto size(const _Cont& __c) -> decltype(__c.size()) { return __c.size(); }
  1533. template <class _Tp, size_t _Sz>
  1534. constexpr size_t size(const _Tp (&__array)[_Sz]) noexcept { return _Sz; }
  1535. template <class _Cont>
  1536. constexpr auto empty(const _Cont& __c) -> decltype(__c.empty()) { return __c.empty(); }
  1537. template <class _Tp, size_t _Sz>
  1538. constexpr bool empty(const _Tp (&__array)[_Sz]) noexcept { return false; }
  1539. template <class _Ep>
  1540. constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
  1541. template <class _Cont> constexpr
  1542. auto data(_Cont& __c) -> decltype(__c.data()) { return __c.data(); }
  1543. template <class _Cont> constexpr
  1544. auto data(const _Cont& __c) -> decltype(__c.data()) { return __c.data(); }
  1545. template <class _Tp, size_t _Sz>
  1546. constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
  1547. template <class _Ep>
  1548. constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
  1549. #endif
  1550. _LIBCPP_END_NAMESPACE_STD
  1551. #endif // _LIBCPP_ITERATOR