range_access.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // <range_access.h> -*- C++ -*-
  2. // Copyright (C) 2010-2018 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. /** @file bits/range_access.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{iterator}
  23. */
  24. #ifndef _GLIBCXX_RANGE_ACCESS_H
  25. #define _GLIBCXX_RANGE_ACCESS_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus >= 201103L
  28. #include <initializer_list>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. /**
  33. * @brief Return an iterator pointing to the first element of
  34. * the container.
  35. * @param __cont Container.
  36. */
  37. template<typename _Container>
  38. inline _GLIBCXX17_CONSTEXPR auto
  39. begin(_Container& __cont) -> decltype(__cont.begin())
  40. { return __cont.begin(); }
  41. /**
  42. * @brief Return an iterator pointing to the first element of
  43. * the const container.
  44. * @param __cont Container.
  45. */
  46. template<typename _Container>
  47. inline _GLIBCXX17_CONSTEXPR auto
  48. begin(const _Container& __cont) -> decltype(__cont.begin())
  49. { return __cont.begin(); }
  50. /**
  51. * @brief Return an iterator pointing to one past the last element of
  52. * the container.
  53. * @param __cont Container.
  54. */
  55. template<typename _Container>
  56. inline _GLIBCXX17_CONSTEXPR auto
  57. end(_Container& __cont) -> decltype(__cont.end())
  58. { return __cont.end(); }
  59. /**
  60. * @brief Return an iterator pointing to one past the last element of
  61. * the const container.
  62. * @param __cont Container.
  63. */
  64. template<typename _Container>
  65. inline _GLIBCXX17_CONSTEXPR auto
  66. end(const _Container& __cont) -> decltype(__cont.end())
  67. { return __cont.end(); }
  68. /**
  69. * @brief Return an iterator pointing to the first element of the array.
  70. * @param __arr Array.
  71. */
  72. template<typename _Tp, size_t _Nm>
  73. inline _GLIBCXX14_CONSTEXPR _Tp*
  74. begin(_Tp (&__arr)[_Nm])
  75. { return __arr; }
  76. /**
  77. * @brief Return an iterator pointing to one past the last element
  78. * of the array.
  79. * @param __arr Array.
  80. */
  81. template<typename _Tp, size_t _Nm>
  82. inline _GLIBCXX14_CONSTEXPR _Tp*
  83. end(_Tp (&__arr)[_Nm])
  84. { return __arr + _Nm; }
  85. #if __cplusplus >= 201402L
  86. template<typename _Tp> class valarray;
  87. // These overloads must be declared for cbegin and cend to use them.
  88. template<typename _Tp> _Tp* begin(valarray<_Tp>&);
  89. template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
  90. template<typename _Tp> _Tp* end(valarray<_Tp>&);
  91. template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
  92. /**
  93. * @brief Return an iterator pointing to the first element of
  94. * the const container.
  95. * @param __cont Container.
  96. */
  97. template<typename _Container>
  98. inline constexpr auto
  99. cbegin(const _Container& __cont) noexcept(noexcept(std::begin(__cont)))
  100. -> decltype(std::begin(__cont))
  101. { return std::begin(__cont); }
  102. /**
  103. * @brief Return an iterator pointing to one past the last element of
  104. * the const container.
  105. * @param __cont Container.
  106. */
  107. template<typename _Container>
  108. inline constexpr auto
  109. cend(const _Container& __cont) noexcept(noexcept(std::end(__cont)))
  110. -> decltype(std::end(__cont))
  111. { return std::end(__cont); }
  112. /**
  113. * @brief Return a reverse iterator pointing to the last element of
  114. * the container.
  115. * @param __cont Container.
  116. */
  117. template<typename _Container>
  118. inline _GLIBCXX17_CONSTEXPR auto
  119. rbegin(_Container& __cont) -> decltype(__cont.rbegin())
  120. { return __cont.rbegin(); }
  121. /**
  122. * @brief Return a reverse iterator pointing to the last element of
  123. * the const container.
  124. * @param __cont Container.
  125. */
  126. template<typename _Container>
  127. inline _GLIBCXX17_CONSTEXPR auto
  128. rbegin(const _Container& __cont) -> decltype(__cont.rbegin())
  129. { return __cont.rbegin(); }
  130. /**
  131. * @brief Return a reverse iterator pointing one past the first element of
  132. * the container.
  133. * @param __cont Container.
  134. */
  135. template<typename _Container>
  136. inline _GLIBCXX17_CONSTEXPR auto
  137. rend(_Container& __cont) -> decltype(__cont.rend())
  138. { return __cont.rend(); }
  139. /**
  140. * @brief Return a reverse iterator pointing one past the first element of
  141. * the const container.
  142. * @param __cont Container.
  143. */
  144. template<typename _Container>
  145. inline _GLIBCXX17_CONSTEXPR auto
  146. rend(const _Container& __cont) -> decltype(__cont.rend())
  147. { return __cont.rend(); }
  148. /**
  149. * @brief Return a reverse iterator pointing to the last element of
  150. * the array.
  151. * @param __arr Array.
  152. */
  153. template<typename _Tp, size_t _Nm>
  154. inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*>
  155. rbegin(_Tp (&__arr)[_Nm])
  156. { return reverse_iterator<_Tp*>(__arr + _Nm); }
  157. /**
  158. * @brief Return a reverse iterator pointing one past the first element of
  159. * the array.
  160. * @param __arr Array.
  161. */
  162. template<typename _Tp, size_t _Nm>
  163. inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*>
  164. rend(_Tp (&__arr)[_Nm])
  165. { return reverse_iterator<_Tp*>(__arr); }
  166. /**
  167. * @brief Return a reverse iterator pointing to the last element of
  168. * the initializer_list.
  169. * @param __il initializer_list.
  170. */
  171. template<typename _Tp>
  172. inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*>
  173. rbegin(initializer_list<_Tp> __il)
  174. { return reverse_iterator<const _Tp*>(__il.end()); }
  175. /**
  176. * @brief Return a reverse iterator pointing one past the first element of
  177. * the initializer_list.
  178. * @param __il initializer_list.
  179. */
  180. template<typename _Tp>
  181. inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*>
  182. rend(initializer_list<_Tp> __il)
  183. { return reverse_iterator<const _Tp*>(__il.begin()); }
  184. /**
  185. * @brief Return a reverse iterator pointing to the last element of
  186. * the const container.
  187. * @param __cont Container.
  188. */
  189. template<typename _Container>
  190. inline _GLIBCXX17_CONSTEXPR auto
  191. crbegin(const _Container& __cont) -> decltype(std::rbegin(__cont))
  192. { return std::rbegin(__cont); }
  193. /**
  194. * @brief Return a reverse iterator pointing one past the first element of
  195. * the const container.
  196. * @param __cont Container.
  197. */
  198. template<typename _Container>
  199. inline _GLIBCXX17_CONSTEXPR auto
  200. crend(const _Container& __cont) -> decltype(std::rend(__cont))
  201. { return std::rend(__cont); }
  202. #endif // C++14
  203. #if __cplusplus >= 201703L
  204. #define __cpp_lib_nonmember_container_access 201411
  205. /**
  206. * @brief Return the size of a container.
  207. * @param __cont Container.
  208. */
  209. template <typename _Container>
  210. constexpr auto
  211. size(const _Container& __cont) noexcept(noexcept(__cont.size()))
  212. -> decltype(__cont.size())
  213. { return __cont.size(); }
  214. /**
  215. * @brief Return the size of an array.
  216. * @param __array Array.
  217. */
  218. template <typename _Tp, size_t _Nm>
  219. constexpr size_t
  220. size(const _Tp (&/*__array*/)[_Nm]) noexcept
  221. { return _Nm; }
  222. /**
  223. * @brief Return whether a container is empty.
  224. * @param __cont Container.
  225. */
  226. template <typename _Container>
  227. [[nodiscard]] constexpr auto
  228. empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
  229. -> decltype(__cont.empty())
  230. { return __cont.empty(); }
  231. /**
  232. * @brief Return whether an array is empty (always false).
  233. * @param __array Container.
  234. */
  235. template <typename _Tp, size_t _Nm>
  236. [[nodiscard]] constexpr bool
  237. empty(const _Tp (&/*__array*/)[_Nm]) noexcept
  238. { return false; }
  239. /**
  240. * @brief Return whether an initializer_list is empty.
  241. * @param __il Initializer list.
  242. */
  243. template <typename _Tp>
  244. [[nodiscard]] constexpr bool
  245. empty(initializer_list<_Tp> __il) noexcept
  246. { return __il.size() == 0;}
  247. /**
  248. * @brief Return the data pointer of a container.
  249. * @param __cont Container.
  250. */
  251. template <typename _Container>
  252. constexpr auto
  253. data(_Container& __cont) noexcept(noexcept(__cont.data()))
  254. -> decltype(__cont.data())
  255. { return __cont.data(); }
  256. /**
  257. * @brief Return the data pointer of a const container.
  258. * @param __cont Container.
  259. */
  260. template <typename _Container>
  261. constexpr auto
  262. data(const _Container& __cont) noexcept(noexcept(__cont.data()))
  263. -> decltype(__cont.data())
  264. { return __cont.data(); }
  265. /**
  266. * @brief Return the data pointer of an array.
  267. * @param __array Array.
  268. */
  269. template <typename _Tp, size_t _Nm>
  270. constexpr _Tp*
  271. data(_Tp (&__array)[_Nm]) noexcept
  272. { return __array; }
  273. /**
  274. * @brief Return the data pointer of an initializer list.
  275. * @param __il Initializer list.
  276. */
  277. template <typename _Tp>
  278. constexpr const _Tp*
  279. data(initializer_list<_Tp> __il) noexcept
  280. { return __il.begin(); }
  281. #endif // C++17
  282. _GLIBCXX_END_NAMESPACE_VERSION
  283. } // namespace
  284. #endif // C++11
  285. #endif // _GLIBCXX_RANGE_ACCESS_H