OpenKalman
repeat.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 
16 #ifndef OPENKALMAN_VIEWS_REPEAT_HPP
17 #define OPENKALMAN_VIEWS_REPEAT_HPP
18 
19 #include "values/concepts/size.hpp"
21 #include "all.hpp"
22 
24 {
30  template<std::size_t N, typename T>
32  {
33 #ifdef __cpp_lib_concepts
34  constexpr repeat_tuple_view() requires std::default_initializable<T> = default;
35 #else
36  template<typename aT = T, std::enable_if_t<std::is_default_constructible_v<aT>, int> = 0>
37  constexpr repeat_tuple_view() {};
38 #endif
39 
40 
41 #ifdef __cpp_lib_concepts
42  template<typename Arg> requires std::constructible_from<T, Arg&&>
43 #else
44  template<typename Arg, std::enable_if_t<std::is_constructible_v<T, Arg&&>, int> = 0>
45 #endif
46  explicit constexpr repeat_tuple_view(Arg&& arg) : t {std::forward<Arg>(arg)} {}
47 
48 
52  constexpr T value() const { return t; }
53 
54 
58  #ifdef __cpp_concepts
59  template<std::size_t i> requires (i < N)
60  #else
61  template<std::size_t i, std::enable_if_t<i < N, int> = 0>
62  #endif
63  friend constexpr T
64  get(const repeat_tuple_view& v)
65  {
66  return v.t;
67  }
68 
69 
73  #ifdef __cpp_concepts
74  template<size_t i> requires (i < N)
75  #else
76  template<size_t i, std::enable_if_t<i < N, int> = 0>
77  #endif
78  friend constexpr T
79  get(repeat_tuple_view&& v)
80  {
81  return std::move(v).t;
82  }
83 
84  private:
85 
86  T t;
87  };
88 
89 }
90 
91 
92 namespace std
93 {
94  template<std::size_t N, typename T>
95  struct tuple_size<OpenKalman::collections::repeat_tuple_view<N, T>> : std::integral_constant<size_t, N> {};
96 
97 
98  template<std::size_t i, std::size_t N, typename T>
99  struct tuple_element<i, OpenKalman::collections::repeat_tuple_view<N, T>>
100  {
101  static_assert(i < N);
102  using type = T;
103  };
104 } // namespace std
105 
106 
108 {
109  namespace detail
110  {
112  {
113 #ifdef __cpp_lib_ranges
114  template<std::move_constructible W, values::size Bound = std::unreachable_sentinel_t> requires
115  std::is_object_v<W> and std::same_as<W, std::remove_cv_t<W>>
116 #else
117  template<typename W, typename Bound = unreachable_sentinel_t, typename = void>
118 #endif
119  constexpr auto
120  operator() [[nodiscard]] (W&& value, Bound&& bound = {}) const
121  {
122  if constexpr (values::fixed<Bound>)
123  {
124  #ifdef __cpp_lib_ranges
125  namespace cv = std::ranges::views;
126  #else
127  namespace cv = ranges::views;
128  #endif
129  if constexpr (values::fixed_number_of_v<Bound> == 1) return cv::single(std::forward<W>(value)) | all;
130  else return repeat_tuple_view<values::fixed_number_of_v<Bound>, W> {std::forward<W>(value)} | all;
131  }
132  else
133  {
134 #ifdef __cpp_lib_ranges_repeat
135  namespace cv = std::ranges::views;
136 #else
137  namespace cv = ranges::views;
138 #endif
139  return cv::repeat(std::forward<W>(value), std::forward<Bound>(bound)) | all;
140  }
141  }
142  };
143 
144  }
145 
146 
150  inline constexpr detail::repeat_adaptor repeat;
151 
152 }
153 
154 
155 #endif
Namespace for collections.
Definition: collections.hpp:27
Definition for values::size.
constexpr detail::repeat_adaptor repeat
a std::ranges::range_adaptor_closure for a set of repeatenated collection objects.
Definition: repeat.hpp:150
A view of a tuple that replicates a particular value N number of times.
Definition: repeat.hpp:31
The root namespace for OpenKalman.
Definition: basics.hpp:34
Namespace for generalized views.
Definition: collections.hpp:33
Definition for ::fixed.
constexpr T value() const
Definition: repeat.hpp:52