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_COLLECTIONS_VIEWS_REPEAT_HPP
17 #define OPENKALMAN_COLLECTIONS_VIEWS_REPEAT_HPP
18 
19 #include "values/values.hpp"
20 #include "all.hpp"
21 
23 {
29  template<std::size_t N, typename T>
31  {
32 #ifdef __cpp_lib_concepts
33  constexpr repeat_tuple_view() requires std::default_initializable<T> = default;
34 #else
35  template<bool Enable = true, std::enable_if_t<Enable and stdex::default_initializable<T>, int> = 0>
36  constexpr repeat_tuple_view() {};
37 #endif
38 
39 
40 #ifdef __cpp_lib_concepts
41  template<typename Arg> requires std::constructible_from<T, Arg&&>
42 #else
43  template<typename Arg, std::enable_if_t<stdex::constructible_from<T, Arg&&>, int> = 0>
44 #endif
45  explicit constexpr repeat_tuple_view(Arg&& arg) : t {std::forward<Arg>(arg)} {}
46 
47 
51  constexpr T value() const { return t; }
52 
53 
57 #ifdef __cpp_explicit_this_parameter
58  template<std::size_t i>
59  constexpr decltype(auto)
60  get(this auto&& self) noexcept
61  {
62  static_assert(i < N, "Index out of range");
63  return std::forward<decltype(self)>(self).t;
64  }
65 #else
66  template<std::size_t i>
67  constexpr decltype(auto)
68  get() & { static_assert(i < N, "Index out of range"); return t; }
69 
71  template<std::size_t i>
72  constexpr decltype(auto)
73  get() const & { static_assert(i < N, "Index out of range"); return t; }
74 
76  template<std::size_t i>
77  constexpr decltype(auto)
78  get() && noexcept { static_assert(i < N, "Index out of range"); return std::move(*this).t; }
79 
81  template<std::size_t i>
82  constexpr decltype(auto)
83  get() const && noexcept { static_assert(i < N, "Index out of range"); return std::move(*this).t; }
84 #endif
85 
86  private:
87 
88  T t;
89  };
90 
91 }
92 
93 
94 namespace std
95 {
96  template<std::size_t N, typename T>
97  struct tuple_size<OpenKalman::collections::repeat_tuple_view<N, T>> : std::integral_constant<size_t, N> {};
98 
99 
100  template<std::size_t i, std::size_t N, typename T>
101  struct tuple_element<i, OpenKalman::collections::repeat_tuple_view<N, T>>
102  {
103  static_assert(i < N);
104  using type = T;
105  };
106 }
107 
108 
110 {
111  namespace detail
112  {
114  {
115 #ifdef __cpp_lib_ranges
116  template<std::move_constructible W, values::size Bound = values::unbounded_size_t>
117 #else
118  template<typename W, typename Bound = values::unbounded_size_t, typename = void>
119 #endif
120  constexpr auto
121  operator() [[nodiscard]] (W&& value, Bound&& bound = {}) const
122  {
123  if constexpr (stdex::same_as<Bound, values::unbounded_size_t>)
124  {
125  return stdex::ranges::views::repeat(std::forward<W>(value)) | all;
126  }
127  else if constexpr (values::fixed<Bound>)
128  {
129  if constexpr (values::fixed_value_of_v<Bound> == 1)
130  return stdex::ranges::views::single(std::forward<W>(value)) | all;
131  else
132  return repeat_tuple_view<values::fixed_value_of_v<Bound>, W> {std::forward<W>(value)} | all;
133  }
134  else
135  {
136  return stdex::ranges::views::repeat(std::forward<W>(value), std::forward<Bound>(bound)) | all;
137  }
138  }
139 
140  };
141 
142  }
143 
144 
148  inline constexpr detail::repeat_adaptor repeat;
149 
150 }
151 
152 
153 #endif
Namespace for collections.
Definition: collections.hpp:27
Header file for code relating to values (e.g., scalars and indices)
constexpr detail::repeat_adaptor repeat
a std::ranges::range_adaptor_closure for a set of repeated collection objects.
Definition: repeat.hpp:148
A uniformly_gettable view that replicates a particular value N number of times.
Definition: repeat.hpp:30
The root namespace for OpenKalman.
Definition: basics.hpp:34
decltype(auto) constexpr get() &
Get element i of a repeat_tuple_view.
Definition: repeat.hpp:68
Namespace for generalized views.
Definition: collections.hpp:33
constexpr T value() const
Definition: repeat.hpp:51