xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/bits/range_access.h (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1*b1e83836Smrg // Range access functions for containers -*- C++ -*-
248fb7bfaSmrg 
3*b1e83836Smrg // Copyright (C) 2010-2022 Free Software Foundation, Inc.
448fb7bfaSmrg //
548fb7bfaSmrg // This file is part of the GNU ISO C++ Library.  This library is free
648fb7bfaSmrg // software; you can redistribute it and/or modify it under the
748fb7bfaSmrg // terms of the GNU General Public License as published by the
848fb7bfaSmrg // Free Software Foundation; either version 3, or (at your option)
948fb7bfaSmrg // any later version.
1048fb7bfaSmrg 
1148fb7bfaSmrg // This library is distributed in the hope that it will be useful,
1248fb7bfaSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
1348fb7bfaSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1448fb7bfaSmrg // GNU General Public License for more details.
1548fb7bfaSmrg 
1648fb7bfaSmrg // Under Section 7 of GPL version 3, you are granted additional
1748fb7bfaSmrg // permissions described in the GCC Runtime Library Exception, version
1848fb7bfaSmrg // 3.1, as published by the Free Software Foundation.
1948fb7bfaSmrg 
2048fb7bfaSmrg // You should have received a copy of the GNU General Public License and
2148fb7bfaSmrg // a copy of the GCC Runtime Library Exception along with this program;
2248fb7bfaSmrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2348fb7bfaSmrg // <http://www.gnu.org/licenses/>.
2448fb7bfaSmrg 
2548fb7bfaSmrg /** @file bits/range_access.h
2648fb7bfaSmrg  *  This is an internal header file, included by other library headers.
2748fb7bfaSmrg  *  Do not attempt to use it directly. @headername{iterator}
2848fb7bfaSmrg  */
2948fb7bfaSmrg 
3048fb7bfaSmrg #ifndef _GLIBCXX_RANGE_ACCESS_H
3148fb7bfaSmrg #define _GLIBCXX_RANGE_ACCESS_H 1
3248fb7bfaSmrg 
3348fb7bfaSmrg #pragma GCC system_header
3448fb7bfaSmrg 
3548fb7bfaSmrg #if __cplusplus >= 201103L
364d5abbe8Smrg #include <initializer_list>
37*b1e83836Smrg #include <type_traits>	    // common_type_t, make_signed_t
38*b1e83836Smrg #include <bits/stl_iterator.h> // reverse_iterator
39fb8a8121Smrg 
_GLIBCXX_VISIBILITY(default)4048fb7bfaSmrg namespace std _GLIBCXX_VISIBILITY(default)
4148fb7bfaSmrg {
4248fb7bfaSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
4348fb7bfaSmrg 
4448fb7bfaSmrg   /**
4548fb7bfaSmrg    *  @brief  Return an iterator pointing to the first element of
4648fb7bfaSmrg    *          the container.
4748fb7bfaSmrg    *  @param  __cont  Container.
4848fb7bfaSmrg    */
49f9a78e0eSmrg   template<typename _Container>
50*b1e83836Smrg     [[__nodiscard__]]
51b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
5248fb7bfaSmrg     begin(_Container& __cont) -> decltype(__cont.begin())
5348fb7bfaSmrg     { return __cont.begin(); }
5448fb7bfaSmrg 
5548fb7bfaSmrg   /**
5648fb7bfaSmrg    *  @brief  Return an iterator pointing to the first element of
5748fb7bfaSmrg    *          the const container.
5848fb7bfaSmrg    *  @param  __cont  Container.
5948fb7bfaSmrg    */
60f9a78e0eSmrg   template<typename _Container>
61*b1e83836Smrg     [[__nodiscard__]]
62b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
6348fb7bfaSmrg     begin(const _Container& __cont) -> decltype(__cont.begin())
6448fb7bfaSmrg     { return __cont.begin(); }
6548fb7bfaSmrg 
6648fb7bfaSmrg   /**
6748fb7bfaSmrg    *  @brief  Return an iterator pointing to one past the last element of
6848fb7bfaSmrg    *          the container.
6948fb7bfaSmrg    *  @param  __cont  Container.
7048fb7bfaSmrg    */
71f9a78e0eSmrg   template<typename _Container>
72*b1e83836Smrg     [[__nodiscard__]]
73b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
7448fb7bfaSmrg     end(_Container& __cont) -> decltype(__cont.end())
7548fb7bfaSmrg     { return __cont.end(); }
7648fb7bfaSmrg 
7748fb7bfaSmrg   /**
7848fb7bfaSmrg    *  @brief  Return an iterator pointing to one past the last element of
7948fb7bfaSmrg    *          the const container.
8048fb7bfaSmrg    *  @param  __cont  Container.
8148fb7bfaSmrg    */
82f9a78e0eSmrg   template<typename _Container>
83*b1e83836Smrg     [[__nodiscard__]]
84b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
8548fb7bfaSmrg     end(const _Container& __cont) -> decltype(__cont.end())
8648fb7bfaSmrg     { return __cont.end(); }
8748fb7bfaSmrg 
8848fb7bfaSmrg   /**
8948fb7bfaSmrg    *  @brief  Return an iterator pointing to the first element of the array.
9048fb7bfaSmrg    *  @param  __arr  Array.
9148fb7bfaSmrg    */
92f9a78e0eSmrg   template<typename _Tp, size_t _Nm>
93*b1e83836Smrg     [[__nodiscard__]]
944d5abbe8Smrg     inline _GLIBCXX14_CONSTEXPR _Tp*
95fb8a8121Smrg     begin(_Tp (&__arr)[_Nm]) noexcept
9648fb7bfaSmrg     { return __arr; }
9748fb7bfaSmrg 
9848fb7bfaSmrg   /**
9948fb7bfaSmrg    *  @brief  Return an iterator pointing to one past the last element
10048fb7bfaSmrg    *          of the array.
10148fb7bfaSmrg    *  @param  __arr  Array.
10248fb7bfaSmrg    */
103f9a78e0eSmrg   template<typename _Tp, size_t _Nm>
104*b1e83836Smrg     [[__nodiscard__]]
1054d5abbe8Smrg     inline _GLIBCXX14_CONSTEXPR _Tp*
106fb8a8121Smrg     end(_Tp (&__arr)[_Nm]) noexcept
10748fb7bfaSmrg     { return __arr + _Nm; }
10848fb7bfaSmrg 
1094d5abbe8Smrg #if __cplusplus >= 201402L
1104d5abbe8Smrg 
1114d5abbe8Smrg   template<typename _Tp> class valarray;
1124d5abbe8Smrg   // These overloads must be declared for cbegin and cend to use them.
1137d4dc15bSmrg   template<typename _Tp> _Tp* begin(valarray<_Tp>&) noexcept;
1147d4dc15bSmrg   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
1157d4dc15bSmrg   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
1167d4dc15bSmrg   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
1174d5abbe8Smrg 
1184d5abbe8Smrg   /**
1194d5abbe8Smrg    *  @brief  Return an iterator pointing to the first element of
1204d5abbe8Smrg    *          the const container.
1214d5abbe8Smrg    *  @param  __cont  Container.
1224d5abbe8Smrg    */
123f9a78e0eSmrg   template<typename _Container>
124*b1e83836Smrg     [[__nodiscard__]]
125*b1e83836Smrg     constexpr auto
1264d5abbe8Smrg     cbegin(const _Container& __cont) noexcept(noexcept(std::begin(__cont)))
1274d5abbe8Smrg       -> decltype(std::begin(__cont))
1284d5abbe8Smrg     { return std::begin(__cont); }
1294d5abbe8Smrg 
1304d5abbe8Smrg   /**
1314d5abbe8Smrg    *  @brief  Return an iterator pointing to one past the last element of
1324d5abbe8Smrg    *          the const container.
1334d5abbe8Smrg    *  @param  __cont  Container.
1344d5abbe8Smrg    */
135f9a78e0eSmrg   template<typename _Container>
136*b1e83836Smrg     [[__nodiscard__]]
137*b1e83836Smrg     constexpr auto
1384d5abbe8Smrg     cend(const _Container& __cont) noexcept(noexcept(std::end(__cont)))
1394d5abbe8Smrg       -> decltype(std::end(__cont))
1404d5abbe8Smrg     { return std::end(__cont); }
1414d5abbe8Smrg 
1424d5abbe8Smrg   /**
1434d5abbe8Smrg    *  @brief  Return a reverse iterator pointing to the last element of
1444d5abbe8Smrg    *          the container.
1454d5abbe8Smrg    *  @param  __cont  Container.
1464d5abbe8Smrg    */
147f9a78e0eSmrg   template<typename _Container>
148*b1e83836Smrg     [[__nodiscard__]]
149b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
1504d5abbe8Smrg     rbegin(_Container& __cont) -> decltype(__cont.rbegin())
1514d5abbe8Smrg     { return __cont.rbegin(); }
1524d5abbe8Smrg 
1534d5abbe8Smrg   /**
1544d5abbe8Smrg    *  @brief  Return a reverse iterator pointing to the last element of
1554d5abbe8Smrg    *          the const container.
1564d5abbe8Smrg    *  @param  __cont  Container.
1574d5abbe8Smrg    */
158f9a78e0eSmrg   template<typename _Container>
159*b1e83836Smrg     [[__nodiscard__]]
160b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
1614d5abbe8Smrg     rbegin(const _Container& __cont) -> decltype(__cont.rbegin())
1624d5abbe8Smrg     { return __cont.rbegin(); }
1634d5abbe8Smrg 
1644d5abbe8Smrg   /**
1654d5abbe8Smrg    *  @brief  Return a reverse iterator pointing one past the first element of
1664d5abbe8Smrg    *          the container.
1674d5abbe8Smrg    *  @param  __cont  Container.
1684d5abbe8Smrg    */
169f9a78e0eSmrg   template<typename _Container>
170*b1e83836Smrg     [[__nodiscard__]]
171b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
1724d5abbe8Smrg     rend(_Container& __cont) -> decltype(__cont.rend())
1734d5abbe8Smrg     { return __cont.rend(); }
1744d5abbe8Smrg 
1754d5abbe8Smrg   /**
1764d5abbe8Smrg    *  @brief  Return a reverse iterator pointing one past the first element of
1774d5abbe8Smrg    *          the const container.
1784d5abbe8Smrg    *  @param  __cont  Container.
1794d5abbe8Smrg    */
180f9a78e0eSmrg   template<typename _Container>
181*b1e83836Smrg     [[__nodiscard__]]
182b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
1834d5abbe8Smrg     rend(const _Container& __cont) -> decltype(__cont.rend())
1844d5abbe8Smrg     { return __cont.rend(); }
1854d5abbe8Smrg 
1864d5abbe8Smrg   /**
1874d5abbe8Smrg    *  @brief  Return a reverse iterator pointing to the last element of
1884d5abbe8Smrg    *          the array.
1894d5abbe8Smrg    *  @param  __arr  Array.
1904d5abbe8Smrg    */
191f9a78e0eSmrg   template<typename _Tp, size_t _Nm>
192*b1e83836Smrg     [[__nodiscard__]]
193b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*>
194fb8a8121Smrg     rbegin(_Tp (&__arr)[_Nm]) noexcept
1954d5abbe8Smrg     { return reverse_iterator<_Tp*>(__arr + _Nm); }
1964d5abbe8Smrg 
1974d5abbe8Smrg   /**
1984d5abbe8Smrg    *  @brief  Return a reverse iterator pointing one past the first element of
1994d5abbe8Smrg    *          the array.
2004d5abbe8Smrg    *  @param  __arr  Array.
2014d5abbe8Smrg    */
202f9a78e0eSmrg   template<typename _Tp, size_t _Nm>
203*b1e83836Smrg     [[__nodiscard__]]
204b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*>
205fb8a8121Smrg     rend(_Tp (&__arr)[_Nm]) noexcept
2064d5abbe8Smrg     { return reverse_iterator<_Tp*>(__arr); }
2074d5abbe8Smrg 
2084d5abbe8Smrg   /**
2094d5abbe8Smrg    *  @brief  Return a reverse iterator pointing to the last element of
2104d5abbe8Smrg    *          the initializer_list.
2114d5abbe8Smrg    *  @param  __il  initializer_list.
2124d5abbe8Smrg    */
213f9a78e0eSmrg   template<typename _Tp>
214*b1e83836Smrg     [[__nodiscard__]]
215b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*>
216fb8a8121Smrg     rbegin(initializer_list<_Tp> __il) noexcept
2174d5abbe8Smrg     { return reverse_iterator<const _Tp*>(__il.end()); }
2184d5abbe8Smrg 
2194d5abbe8Smrg   /**
2204d5abbe8Smrg    *  @brief  Return a reverse iterator pointing one past the first element of
2214d5abbe8Smrg    *          the initializer_list.
2224d5abbe8Smrg    *  @param  __il  initializer_list.
2234d5abbe8Smrg    */
224f9a78e0eSmrg   template<typename _Tp>
225*b1e83836Smrg     [[__nodiscard__]]
226b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*>
227fb8a8121Smrg     rend(initializer_list<_Tp> __il) noexcept
2284d5abbe8Smrg     { return reverse_iterator<const _Tp*>(__il.begin()); }
2294d5abbe8Smrg 
2304d5abbe8Smrg   /**
2314d5abbe8Smrg    *  @brief  Return a reverse iterator pointing to the last element of
2324d5abbe8Smrg    *          the const container.
2334d5abbe8Smrg    *  @param  __cont  Container.
2344d5abbe8Smrg    */
235f9a78e0eSmrg   template<typename _Container>
236*b1e83836Smrg     [[__nodiscard__]]
237b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
2384d5abbe8Smrg     crbegin(const _Container& __cont) -> decltype(std::rbegin(__cont))
2394d5abbe8Smrg     { return std::rbegin(__cont); }
2404d5abbe8Smrg 
2414d5abbe8Smrg   /**
2424d5abbe8Smrg    *  @brief  Return a reverse iterator pointing one past the first element of
2434d5abbe8Smrg    *          the const container.
2444d5abbe8Smrg    *  @param  __cont  Container.
2454d5abbe8Smrg    */
246f9a78e0eSmrg   template<typename _Container>
247*b1e83836Smrg     [[__nodiscard__]]
248b17d1066Smrg     inline _GLIBCXX17_CONSTEXPR auto
2494d5abbe8Smrg     crend(const _Container& __cont) -> decltype(std::rend(__cont))
2504d5abbe8Smrg     { return std::rend(__cont); }
2514d5abbe8Smrg 
2524d5abbe8Smrg #endif // C++14
2534d5abbe8Smrg 
254b17d1066Smrg #if __cplusplus >= 201703L
255*b1e83836Smrg #define __cpp_lib_nonmember_container_access 201411L
256f9a78e0eSmrg 
257f9a78e0eSmrg   /**
258f9a78e0eSmrg    *  @brief  Return the size of a container.
259f9a78e0eSmrg    *  @param  __cont  Container.
260f9a78e0eSmrg    */
261f9a78e0eSmrg   template <typename _Container>
262*b1e83836Smrg     [[nodiscard]]
263f9a78e0eSmrg     constexpr auto
264b17d1066Smrg     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
265b17d1066Smrg     -> decltype(__cont.size())
266f9a78e0eSmrg     { return __cont.size(); }
267f9a78e0eSmrg 
268f9a78e0eSmrg   /**
269f9a78e0eSmrg    *  @brief  Return the size of an array.
270f9a78e0eSmrg    */
271f9a78e0eSmrg   template <typename _Tp, size_t _Nm>
272*b1e83836Smrg     [[nodiscard]]
273f9a78e0eSmrg     constexpr size_t
274fb8a8121Smrg     size(const _Tp (&)[_Nm]) noexcept
275f9a78e0eSmrg     { return _Nm; }
276f9a78e0eSmrg 
277f9a78e0eSmrg   /**
278f9a78e0eSmrg    *  @brief  Return whether a container is empty.
279f9a78e0eSmrg    *  @param  __cont  Container.
280f9a78e0eSmrg    */
281f9a78e0eSmrg   template <typename _Container>
282a3e9eb18Smrg     [[nodiscard]] constexpr auto
283b17d1066Smrg     empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
284b17d1066Smrg     -> decltype(__cont.empty())
285f9a78e0eSmrg     { return __cont.empty(); }
286f9a78e0eSmrg 
287f9a78e0eSmrg   /**
288f9a78e0eSmrg    *  @brief  Return whether an array is empty (always false).
289f9a78e0eSmrg    */
290f9a78e0eSmrg   template <typename _Tp, size_t _Nm>
291a3e9eb18Smrg     [[nodiscard]] constexpr bool
292fb8a8121Smrg     empty(const _Tp (&)[_Nm]) noexcept
293f9a78e0eSmrg     { return false; }
294f9a78e0eSmrg 
295f9a78e0eSmrg   /**
296f9a78e0eSmrg    *  @brief  Return whether an initializer_list is empty.
297f9a78e0eSmrg    *  @param  __il  Initializer list.
298f9a78e0eSmrg    */
299f9a78e0eSmrg   template <typename _Tp>
300a3e9eb18Smrg     [[nodiscard]] constexpr bool
301f9a78e0eSmrg     empty(initializer_list<_Tp> __il) noexcept
302f9a78e0eSmrg     { return __il.size() == 0;}
303f9a78e0eSmrg 
304f9a78e0eSmrg   /**
305f9a78e0eSmrg    *  @brief  Return the data pointer of a container.
306f9a78e0eSmrg    *  @param  __cont  Container.
307f9a78e0eSmrg    */
308f9a78e0eSmrg   template <typename _Container>
309*b1e83836Smrg     [[nodiscard]]
310f9a78e0eSmrg     constexpr auto
311b17d1066Smrg     data(_Container& __cont) noexcept(noexcept(__cont.data()))
312b17d1066Smrg     -> decltype(__cont.data())
313f9a78e0eSmrg     { return __cont.data(); }
314f9a78e0eSmrg 
315f9a78e0eSmrg   /**
316f9a78e0eSmrg    *  @brief  Return the data pointer of a const container.
317f9a78e0eSmrg    *  @param  __cont  Container.
318f9a78e0eSmrg    */
319f9a78e0eSmrg   template <typename _Container>
320*b1e83836Smrg     [[nodiscard]]
321f9a78e0eSmrg     constexpr auto
322b17d1066Smrg     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
323b17d1066Smrg     -> decltype(__cont.data())
324f9a78e0eSmrg     { return __cont.data(); }
325f9a78e0eSmrg 
326f9a78e0eSmrg   /**
327f9a78e0eSmrg    *  @brief  Return the data pointer of an array.
328f9a78e0eSmrg    *  @param  __array  Array.
329f9a78e0eSmrg    */
330f9a78e0eSmrg   template <typename _Tp, size_t _Nm>
331*b1e83836Smrg     [[nodiscard]]
332f9a78e0eSmrg     constexpr _Tp*
333f9a78e0eSmrg     data(_Tp (&__array)[_Nm]) noexcept
334f9a78e0eSmrg     { return __array; }
335f9a78e0eSmrg 
336f9a78e0eSmrg   /**
337f9a78e0eSmrg    *  @brief  Return the data pointer of an initializer list.
338f9a78e0eSmrg    *  @param  __il  Initializer list.
339f9a78e0eSmrg    */
340f9a78e0eSmrg   template <typename _Tp>
341*b1e83836Smrg     [[nodiscard]]
342f9a78e0eSmrg     constexpr const _Tp*
343f9a78e0eSmrg     data(initializer_list<_Tp> __il) noexcept
344f9a78e0eSmrg     { return __il.begin(); }
345f9a78e0eSmrg 
346fb8a8121Smrg #if __cplusplus > 201703L
347fb8a8121Smrg #define __cpp_lib_ssize 201902L
348fb8a8121Smrg   template<typename _Container>
349*b1e83836Smrg     [[nodiscard]]
350fb8a8121Smrg     constexpr auto
351fb8a8121Smrg     ssize(const _Container& __cont)
352fb8a8121Smrg     noexcept(noexcept(__cont.size()))
353fb8a8121Smrg     -> common_type_t<ptrdiff_t, make_signed_t<decltype(__cont.size())>>
354fb8a8121Smrg     {
355fb8a8121Smrg       using type = make_signed_t<decltype(__cont.size())>;
356fb8a8121Smrg       return static_cast<common_type_t<ptrdiff_t, type>>(__cont.size());
357fb8a8121Smrg     }
358fb8a8121Smrg 
359fb8a8121Smrg   template<typename _Tp, ptrdiff_t _Num>
360*b1e83836Smrg     [[nodiscard]]
361fb8a8121Smrg     constexpr ptrdiff_t
362fb8a8121Smrg     ssize(const _Tp (&)[_Num]) noexcept
363fb8a8121Smrg     { return _Num; }
364fb8a8121Smrg #endif // C++20
365*b1e83836Smrg 
366*b1e83836Smrg #endif // C++17
36748fb7bfaSmrg _GLIBCXX_END_NAMESPACE_VERSION
36848fb7bfaSmrg } // namespace
36948fb7bfaSmrg 
37048fb7bfaSmrg #endif // C++11
37148fb7bfaSmrg #endif // _GLIBCXX_RANGE_ACCESS_H
372