reverse_iterator.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma once
  2. /**
  3. * A constexpr std::reverse_iterator for C++11.
  4. * Implementation taken from libstdc++,
  5. * https://raw.githubusercontent.com/gcc-mirror/gcc/gcc-9_2_0-release/libstdc%2B%2B-v3/include/bits/stl_iterator.h
  6. * adapted to our code base and constexpr'ified.
  7. */
  8. // Copyright (C) 2001-2019 Free Software Foundation, Inc.
  9. //
  10. // This file is part of the GNU ISO C++ Library. This library is free
  11. // software; you can redistribute it and/or modify it under the
  12. // terms of the GNU General Public License as published by the
  13. // Free Software Foundation; either version 3, or (at your option)
  14. // any later version.
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. // Under Section 7 of GPL version 3, you are granted additional
  20. // permissions described in the GCC Runtime Library Exception, version
  21. // 3.1, as published by the Free Software Foundation.
  22. // You should have received a copy of the GNU General Public License and
  23. // a copy of the GCC Runtime Library Exception along with this program;
  24. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  25. // <http://www.gnu.org/licenses/>.
  26. /*
  27. *
  28. * Copyright (c) 1994
  29. * Hewlett-Packard Company
  30. *
  31. * Permission to use, copy, modify, distribute and sell this software
  32. * and its documentation for any purpose is hereby granted without fee,
  33. * provided that the above copyright notice appear in all copies and
  34. * that both that copyright notice and this permission notice appear
  35. * in supporting documentation. Hewlett-Packard Company makes no
  36. * representations about the suitability of this software for any
  37. * purpose. It is provided "as is" without express or implied warranty.
  38. *
  39. *
  40. * Copyright (c) 1996-1998
  41. * Silicon Graphics Computer Systems, Inc.
  42. *
  43. * Permission to use, copy, modify, distribute and sell this software
  44. * and its documentation for any purpose is hereby granted without fee,
  45. * provided that the above copyright notice appear in all copies and
  46. * that both that copyright notice and this permission notice appear
  47. * in supporting documentation. Silicon Graphics makes no
  48. * representations about the suitability of this software for any
  49. * purpose. It is provided "as is" without express or implied warranty.
  50. */
  51. #include <c10/util/C++17.h>
  52. #include <iterator>
  53. namespace c10 {
  54. template <typename _Iterator>
  55. class reverse_iterator {
  56. protected:
  57. _Iterator current;
  58. using __traits_type = std::iterator_traits<_Iterator>;
  59. public:
  60. using iterator_type = _Iterator;
  61. using value_type = typename __traits_type::value_type;
  62. using difference_type = typename __traits_type::difference_type;
  63. using pointer = typename __traits_type::pointer;
  64. using reference = typename __traits_type::reference;
  65. using iterator_category = typename __traits_type::iterator_category;
  66. constexpr reverse_iterator() : current() {}
  67. explicit constexpr reverse_iterator(iterator_type __x) : current(__x) {}
  68. constexpr reverse_iterator(const reverse_iterator& __x)
  69. : current(__x.current) {}
  70. constexpr reverse_iterator& operator=(const reverse_iterator& rhs) noexcept {
  71. current = rhs.current;
  72. return current;
  73. }
  74. template <typename _Iter>
  75. constexpr reverse_iterator(const reverse_iterator<_Iter>& __x)
  76. : current(__x.base()) {}
  77. constexpr iterator_type base() const {
  78. return current;
  79. }
  80. constexpr reference operator*() const {
  81. #if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
  82. _Iterator iter = current;
  83. return *--iter;
  84. #else
  85. // Only works for random access iterators if we're not C++14 :(
  86. return *(current - 1);
  87. #endif
  88. }
  89. constexpr pointer operator->() const {
  90. #if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
  91. _Iterator iter = current;
  92. return _S_to_pointer(--iter);
  93. #else
  94. // Only works for random access iterators if we're not C++14 :(
  95. return _S_to_pointer(current - 1);
  96. #endif
  97. }
  98. constexpr reverse_iterator& operator++() {
  99. --current;
  100. return *this;
  101. }
  102. constexpr reverse_iterator operator++(int) {
  103. reverse_iterator __tmp = *this;
  104. --current;
  105. return __tmp;
  106. }
  107. constexpr reverse_iterator& operator--() {
  108. ++current;
  109. return *this;
  110. }
  111. constexpr reverse_iterator operator--(int) {
  112. reverse_iterator __tmp = *this;
  113. ++current;
  114. return __tmp;
  115. }
  116. constexpr reverse_iterator operator+(difference_type __n) const {
  117. return reverse_iterator(current - __n);
  118. }
  119. constexpr reverse_iterator& operator+=(difference_type __n) {
  120. current -= __n;
  121. return *this;
  122. }
  123. constexpr reverse_iterator operator-(difference_type __n) const {
  124. return reverse_iterator(current + __n);
  125. }
  126. constexpr reverse_iterator& operator-=(difference_type __n) {
  127. current += __n;
  128. return *this;
  129. }
  130. constexpr reference operator[](difference_type __n) const {
  131. return *(*this + __n);
  132. }
  133. private:
  134. template <typename _Tp>
  135. static constexpr _Tp* _S_to_pointer(_Tp* __p) {
  136. return __p;
  137. }
  138. template <typename _Tp>
  139. static constexpr pointer _S_to_pointer(_Tp __t) {
  140. return __t.operator->();
  141. }
  142. };
  143. template <typename _Iterator>
  144. inline constexpr bool operator==(
  145. const reverse_iterator<_Iterator>& __x,
  146. const reverse_iterator<_Iterator>& __y) {
  147. return __x.base() == __y.base();
  148. }
  149. template <typename _Iterator>
  150. inline constexpr bool operator<(
  151. const reverse_iterator<_Iterator>& __x,
  152. const reverse_iterator<_Iterator>& __y) {
  153. return __y.base() < __x.base();
  154. }
  155. template <typename _Iterator>
  156. inline constexpr bool operator!=(
  157. const reverse_iterator<_Iterator>& __x,
  158. const reverse_iterator<_Iterator>& __y) {
  159. return !(__x == __y);
  160. }
  161. template <typename _Iterator>
  162. inline constexpr bool operator>(
  163. const reverse_iterator<_Iterator>& __x,
  164. const reverse_iterator<_Iterator>& __y) {
  165. return __y < __x;
  166. }
  167. template <typename _Iterator>
  168. inline constexpr bool operator<=(
  169. const reverse_iterator<_Iterator>& __x,
  170. const reverse_iterator<_Iterator>& __y) {
  171. return !(__y < __x);
  172. }
  173. template <typename _Iterator>
  174. inline constexpr bool operator>=(
  175. const reverse_iterator<_Iterator>& __x,
  176. const reverse_iterator<_Iterator>& __y) {
  177. return !(__x < __y);
  178. }
  179. template <typename _IteratorL, typename _IteratorR>
  180. inline constexpr bool operator==(
  181. const reverse_iterator<_IteratorL>& __x,
  182. const reverse_iterator<_IteratorR>& __y) {
  183. return __x.base() == __y.base();
  184. }
  185. template <typename _IteratorL, typename _IteratorR>
  186. inline constexpr bool operator<(
  187. const reverse_iterator<_IteratorL>& __x,
  188. const reverse_iterator<_IteratorR>& __y) {
  189. return __y.base() < __x.base();
  190. }
  191. template <typename _IteratorL, typename _IteratorR>
  192. inline constexpr bool operator!=(
  193. const reverse_iterator<_IteratorL>& __x,
  194. const reverse_iterator<_IteratorR>& __y) {
  195. return !(__x == __y);
  196. }
  197. template <typename _IteratorL, typename _IteratorR>
  198. inline constexpr bool operator>(
  199. const reverse_iterator<_IteratorL>& __x,
  200. const reverse_iterator<_IteratorR>& __y) {
  201. return __y < __x;
  202. }
  203. template <typename _IteratorL, typename _IteratorR>
  204. inline constexpr bool operator<=(
  205. const reverse_iterator<_IteratorL>& __x,
  206. const reverse_iterator<_IteratorR>& __y) {
  207. return !(__y < __x);
  208. }
  209. template <typename _IteratorL, typename _IteratorR>
  210. inline constexpr bool operator>=(
  211. const reverse_iterator<_IteratorL>& __x,
  212. const reverse_iterator<_IteratorR>& __y) {
  213. return !(__x < __y);
  214. }
  215. template <typename _IteratorL, typename _IteratorR>
  216. inline constexpr decltype(auto) operator-(
  217. const reverse_iterator<_IteratorL>& __x,
  218. const reverse_iterator<_IteratorR>& __y) {
  219. return __y.base() - __x.base();
  220. }
  221. template <typename _Iterator>
  222. inline constexpr reverse_iterator<_Iterator> operator+(
  223. typename reverse_iterator<_Iterator>::difference_type __n,
  224. const reverse_iterator<_Iterator>& __x) {
  225. return reverse_iterator<_Iterator>(__x.base() - __n);
  226. }
  227. template <typename _Iterator>
  228. inline constexpr reverse_iterator<_Iterator> __make_reverse_iterator(
  229. _Iterator __i) {
  230. return reverse_iterator<_Iterator>(__i);
  231. }
  232. template <typename _Iterator>
  233. inline constexpr reverse_iterator<_Iterator> make_reverse_iterator(
  234. _Iterator __i) {
  235. return reverse_iterator<_Iterator>(__i);
  236. }
  237. template <typename _Iterator>
  238. decltype(auto) __niter_base(reverse_iterator<_Iterator> __it) {
  239. return __make_reverse_iterator(__niter_base(__it.base()));
  240. }
  241. } // namespace c10