Horizon
operations.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2014-present
5 // Copyright Gonzalo Brito Gadeschi 2017
6 //
7 // Use, modification and distribution is subject to the
8 // Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
11 //
12 // Project home: https://github.com/ericniebler/range-v3
13 //
14 
15 #ifndef RANGES_V3_RANGE_OPERATIONS_HPP
16 #define RANGES_V3_RANGE_OPERATIONS_HPP
17 
18 #include <stdexcept>
19 
20 #include <range/v3/range_fwd.hpp>
21 
26 #include <range/v3/utility/static_const.hpp>
27 
28 #include <range/v3/detail/prologue.hpp>
29 
30 namespace ranges
31 {
35  struct at_fn
36  {
38  template(typename Rng)(
39  requires random_access_range<Rng> AND sized_range<Rng> AND
40  borrowed_range<Rng>)
41  constexpr range_reference_t<Rng> //
42  operator()(Rng && rng, range_difference_t<Rng> n) const
43  {
44  // Workaround https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67371 in GCC 5
45  check_throw(rng, n);
46  return ranges::begin(rng)[n];
47  }
48 
49  private:
50  template<typename Rng>
51  static constexpr void check_throw(Rng && rng, range_difference_t<Rng> n)
52  {
53  (n < 0 || n >= ranges::distance(rng)) ? throw std::out_of_range("ranges::at")
54  : void(0);
55  }
56  };
57 
63 
64 
67  struct index_fn
68  {
70  template(typename Rng, typename Int)(
71  requires random_access_range<Rng> AND integral<Int> AND borrowed_range<Rng>)
72  constexpr range_reference_t<Rng> operator()(Rng && rng, Int n) const //
73  {
74  using D = range_difference_t<Rng>;
75  RANGES_EXPECT(0 <= static_cast<D>(n));
76  RANGES_EXPECT(!(bool)sized_range<Rng> ||
77  static_cast<D>(n) < ranges::distance(rng));
78  return ranges::begin(rng)[static_cast<D>(n)];
79  }
80  };
81 
86  RANGES_INLINE_VARIABLE(index_fn, index)
87 
88 
89  struct back_fn
90  {
92  template(typename Rng)(
93  requires common_range<Rng> AND bidirectional_range<Rng> AND
94  borrowed_range<Rng>)
95  constexpr range_reference_t<Rng> operator()(Rng && rng) const
96  {
97  return *prev(end(rng));
98  }
99  };
100 
103  RANGES_INLINE_VARIABLE(back_fn, back)
104 
105 
106  struct front_fn
107  {
109  template(typename Rng)(
110  requires forward_range<Rng> AND borrowed_range<Rng>)
111  constexpr range_reference_t<Rng> operator()(Rng && rng) const
112  {
113  return *begin(rng);
114  }
115  };
116 
119  RANGES_INLINE_VARIABLE(front_fn, front)
120 } // namespace ranges
121 
122 #include <range/v3/detail/epilogue.hpp>
123 
124 #endif
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
_t< detail::back_< L > > back
Return the last element in meta::list L.
Definition: meta.hpp:2103
at_c< L, N::type::value > at
Return the N th element in the meta::list L.
Definition: meta.hpp:1969
_t< detail::front_< L > > front
Return the first element in meta::list L.
Definition: meta.hpp:2070
Checked indexed range access.
Definition: operations.hpp:36
Unchecked indexed range access.
Definition: operations.hpp:90
Definition: operations.hpp:107
Checked indexed range access.
Definition: operations.hpp:68