OpenKalman
assign.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) 2024 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_ASSIGN_HPP
17 #define OPENKALMAN_ASSIGN_HPP
18 
19 namespace OpenKalman
20 {
21  namespace detail
22  {
23  template<typename M, typename Arg, typename...J>
24  static void copy_tensor_elements(M& m, Arg&& arg, std::index_sequence<>, J...j)
25  {
26  set_component(m, get_component(std::forward<Arg>(arg), j...), j...);
27  }
28 
29 
30  template<typename M, typename Arg, std::size_t I, std::size_t...Is, typename...J>
31  static void copy_tensor_elements(M& m, Arg&& arg, std::index_sequence<I, Is...>, J...j)
32  {
33  for (std::size_t i = 0; i < get_index_dimension_of<I>(arg); i++)
34  copy_tensor_elements(m, std::forward<Arg>(arg), std::index_sequence<Is...> {}, j..., i);
35  }
36  } // namespace detail
37 
38 
45 #ifdef __cpp_concepts
46  template<indexible To, vector_space_descriptors_may_match_with<To> From>
47 #else
48  template<typename To, typename From, std::enable_if_t<indexible<To> and vector_space_descriptors_may_match_with<From, To>, int> = 0>
49 #endif
50  constexpr To&&
51  assign(To&& a, From&& b)
52  {
53  if constexpr (interface::assign_defined_for<To, std::add_lvalue_reference_t<To>, From&&>)
54  {
56  }
57  else if constexpr (interface::assign_defined_for<To, std::add_lvalue_reference_t<To>, decltype(to_native_matrix<To>(std::declval<From&&>()))>)
58  {
59  interface::library_interface<std::decay_t<To>>::assign(a, to_native_matrix<To>(std::forward<From>(b)));
60  }
61  else if constexpr (std::is_assignable_v<std::add_lvalue_reference_t<To>, From&&>)
62  {
63  a = std::forward<From>(b);
64  }
65  else if constexpr (std::is_assignable_v<std::add_lvalue_reference_t<To>, decltype(to_native_matrix<To>(std::declval<From&&>()))>)
66  {
67  a = to_native_matrix<To>(std::forward<From>(b));
68  }
69  // \todo include the case where A is \ref directly_accessible
70  else
71  {
72  detail::copy_tensor_elements(a, std::forward<From>(b), std::make_index_sequence<index_count_v<To>>{});
73  }
74  return std::forward<To>(a);
75  }
76 
77 
78 } // namespace OpenKalman
79 
80 #endif //OPENKALMAN_ASSIGN_HPP
Arg && set_component(Arg &&arg, const scalar_type_of_t< Arg > &s, const Indices &indices)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: set_component.hpp:51
The root namespace for OpenKalman.
Definition: basics.hpp:34
An interface to various routines from the linear algebra library associated with indexible object T...
Definition: library_interface.hpp:37
constexpr To && assign(To &&a, From &&b)
Assign a writable object from an indexible object.
Definition: assign.hpp:51
decltype(auto) constexpr get_component(Arg &&arg, const Indices &indices)
Get a component of an object at a particular set of indices.
Definition: get_component.hpp:54