OpenKalman
iota.hpp
Go to the documentation of this file.
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2025 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
17 #ifndef OPENKALMAN_COLLECTIONS_VIEWS_IOTA_HPP
18 #define OPENKALMAN_COLLECTIONS_VIEWS_IOTA_HPP
19 
20 #include "values/values.hpp"
21 #include "generate.hpp"
22 
24 {
25  namespace detail
26  {
27  template<typename Start = std::integral_constant<std::size_t, 0>>
29  {
30  constexpr iota_generator() = default;
31 
32  explicit constexpr iota_generator(Start start) : start_ {std::move(start)} {};
33 
34 #ifdef __cpp_concepts
35  template<values::index I>
36 #else
37  template<typename I, std::enable_if_t<values::index<I>, int> = 0>
38 #endif
39  constexpr auto
40  operator() (I i) const
41  {
42  return values::operation(std::plus<std::size_t>{}, start_, std::move(i));
43  }
44 
45  private:
46 
47  Start start_;
48  };
49 
50  }
51 
52 
62 #ifdef __cpp_concepts
63  template<values::integral Start = std::integral_constant<std::size_t, 0>, values::size Size = values::unbounded_size_t>
64  requires (not values::index<Size> or std::convertible_to<values::value_type_of_t<Size>, values::value_type_of_t<Start>>) and
65  std::same_as<Start, std::remove_reference_t<Start>> and
66  std::same_as<Size, std::remove_reference_t<Size>>
67 #else
68  template<typename Start = std::integral_constant<std::size_t, 0>, typename Size = values::unbounded_size_t>
69 #endif
70  struct iota_view : generate_view<detail::iota_generator<Start>, Size>
71  {
72  private:
73 
75 
76  public:
77 
81 #ifdef __cpp_concepts
82  constexpr
83  iota_view(Start start, Size size) requires values::index<Size>
84 #else
85  template<bool Enable = true, std::enable_if_t<Enable and values::index<Size>, int> = 0>
86  constexpr iota_view(Start start, Size size)
87 #endif
88  : view_base {detail::iota_generator{std::move(start)}, std::move(size)} {}
89 
90 
94 #ifdef __cpp_concepts
95  explicit constexpr
96  iota_view(Start start) requires (not values::index<Size>)
97 #else
98  template<bool Enable = true, std::enable_if_t<Enable and not values::index<Size>, int> = 0>
99  explicit constexpr iota_view(Start start)
100 #endif
101  : view_base {detail::iota_generator{std::move(start)}} {}
102 
103 
107  constexpr
108  iota_view() noexcept = default;
109 
110  };
111 
112 
116  template<typename Start, typename Size>
117  iota_view(const Start&, const Size&) -> iota_view<Start, Size>;
118 
122  template<typename Start>
123  iota_view(const Start&) -> iota_view<Start>;
124 
125 } // OpenKalman::values
126 
127 
128 #ifdef __cpp_lib_ranges
129 namespace std::ranges
130 #else
131 namespace OpenKalman::stdex::ranges
132 #endif
133 {
134  template<typename Start, typename Size>
135  constexpr bool enable_borrowed_range<OpenKalman::collections::iota_view<Start, Size>> = true;
136 }
137 
138 
139 namespace std
140 {
141 #ifdef __cpp_concepts
142  template<typename Start, OpenKalman::values::fixed Size>
143  struct tuple_size<OpenKalman::collections::iota_view<Start, Size>>
144  : std::integral_constant<size_t, OpenKalman::values::fixed_value_of_v<Size>> {};
145 #else
146  template<typename Start, typename Size>
147  struct tuple_size<OpenKalman::collections::iota_view<Start, Size>>
148  : tuple_size<OpenKalman::collections::generate_view<OpenKalman::collections::detail::iota_generator<Start>, Size>> {};
149 #endif
150 
151 
152 #ifdef __cpp_concepts
153  template<std::size_t i, OpenKalman::values::fixed Start, typename Size>
154  struct tuple_element<i, OpenKalman::collections::iota_view<Start, Size>>
155  {
156  static_assert(not OpenKalman::values::fixed<Size> or requires { requires i < OpenKalman::values::fixed_value_of<Size>::value; });
157  using type = OpenKalman::values::operation_t<std::plus<>, Start, std::integral_constant<std::size_t, i>>;
158  };
159 #else
160  template<std::size_t i, typename Start, typename Size>
161  struct tuple_element<i, OpenKalman::collections::iota_view<Start, Size>>
162  : tuple_element<i, OpenKalman::collections::generate_view<OpenKalman::collections::detail::iota_generator<Start>, Size>> {};
163 #endif
164 
165 }
166 
167 
169 {
170  namespace detail
171  {
173  {
177 #ifdef __cpp_concepts
178  template<values::index Start, values::index Size> requires
179  std::convertible_to<values::value_type_of_t<Size>, values::value_type_of_t<Start>>
180 #else
181  template<typename Start, typename Size, std::enable_if_t<values::index<Start> and values::index<Size> and
182  stdex::convertible_to<values::value_type_of_t<Size>, values::value_type_of_t<Start>>, int> = 0>
183 #endif
184  constexpr auto
185  operator() (Start start, Size size) const
186  {
187  return iota_view<std::decay_t<Start>, std::decay_t<Size>>(std::move(start), std::move(size));
188  }
189 
190 
194 #ifdef __cpp_concepts
195  template<values::index Start>
196 #else
197  template<typename Start, std::enable_if_t<values::index<Start>, int> = 0>
198 #endif
199  constexpr auto
200  operator() (Start start) const
201  {
202  return iota_view<std::decay_t<Start>>(std::move(start));
203  }
204 
205 
209  constexpr auto
210  operator() () const
211  {
212  return iota_view<> {};
213  }
214 
215  };
216  }
217 
218 
225  inline constexpr detail::iota_adapter iota;
226 
227 }
228 
229 
230 #endif
constexpr detail::iota_adapter iota
a RangeAdapterObject associated with iota_view.
Definition: iota.hpp:225
Namespace for collections.
Definition: collections.hpp:27
Header file for code relating to values (e.g., scalars and indices)
typename value_type_of< T >::type value_type_of_t
Helper template for value_type_of.
Definition: value_type_of.hpp:52
constexpr bool value
T is a fixed or dynamic value that is reducible to a number.
Definition: value.hpp:45
An iota collection that is a std::range and may also be uniformly_gettable.
Definition: iota.hpp:70
Definition for collections::generate_view and collections::views::generate.
A collection_view created by lazily generating elements based on an index.
Definition: generate.hpp:39
constexpr bool size
T is either an index representing a size, or unbounded_size_t, which indicates that the size is unbou...
Definition: size.hpp:65
Namespace for generalized views.
Definition: collections.hpp:33
decltype(operation(std::declval< Operation && >(), std::declval< Args && >()...)) operation_t
The resulting type from an values::operation.
Definition: operation.hpp:115
iota_view(const Start &, const Size &) -> iota_view< Start, Size >
Deduction guide.
constexpr auto operation(Operation &&op, Args &&...args)
A potentially constant-evaluated operation involving some number of values.
Definition: operation.hpp:98
constexpr iota_view(Start start, Size size)
Construct from an initial value and size.
Definition: iota.hpp:86
constexpr iota_view(Start start)
Construct from an initial value, default-initializing Size.
Definition: iota.hpp:99