OpenKalman
set_slice.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) 2022-2023 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_SET_SLICE_HPP
17 #define OPENKALMAN_SET_SLICE_HPP
18 
19 namespace OpenKalman
20 {
21  namespace detail
22  {
23  template<typename Arg, typename Block, typename BeginTup, typename...J>
24  static void assign_slice_elements(Arg& arg, Block&& block, const BeginTup& begin_tup, std::index_sequence<>, J...j)
25  {
26  set_component(arg, get_component(std::forward<Block>(block), std::get<0>(j)...), std::get<1>(j)...);
27  }
28 
29 
30  template<typename Arg, typename Block, typename BeginTup, std::size_t I, std::size_t...Is, typename...J>
31  static void assign_slice_elements(Arg& arg, Block&& block, const BeginTup& begin_tup, std::index_sequence<I, Is...>, J...j)
32  {
33  for (std::size_t i = 0; i < get_index_dimension_of<I>(block); i++)
34  assign_slice_elements(arg, std::forward<Block>(block), begin_tup, std::index_sequence<Is...>{}, j...,
35  std::tuple{i, std::get<I>(begin_tup) + i});
36  }
37  } // namespace detail
38 
39 
49 #ifdef __cpp_concepts
50  template<writable Arg, indexible Block, values::index...Begin> requires (sizeof...(Begin) >= index_count_v<Arg>)
51 #else
52  template<typename Arg, typename Block, typename...Begin, std::enable_if_t<writable<Arg> and indexible<Block> and
53  (values::index<Begin> and ...) and (sizeof...(Begin) >= index_count<Arg>::value), int> = 0>
54 #endif
55  constexpr Arg&&
56  set_slice(Arg&& arg, Block&& block, const Begin&...begin)
57  {
58  std::index_sequence_for<Begin...> begin_seq;
59  internal::check_block_limits(begin_seq, begin_seq, arg, std::tuple{begin...});
60  internal::check_block_limits(begin_seq, begin_seq, arg, std::tuple{begin...},
61  std::apply([](auto&&...a) -> decltype(auto) {
62  return std::forward_as_tuple(get_dimension(std::forward<decltype(a)>(a))...);
63  }, all_vector_space_descriptors(block)));
64 
65  if constexpr (interface::set_slice_defined_for<Arg, Arg&, Block&&, const Begin&...>)
66  {
67  interface::library_interface<std::decay_t<Arg>>::set_slice(arg, std::forward<Block>(block), begin...);
68  }
69  else if constexpr (interface::set_slice_defined_for<Arg, Arg&, decltype(to_native_matrix<Arg>(std::declval<Block&&>())), const Begin&...>)
70  {
71  interface::library_interface<std::decay_t<Arg>>::set_slice(arg, to_native_matrix<Arg>(std::forward<Block>(block)), begin...);;
72  }
73  // \todo If arg is directly_accessible and the library interface is not defined, set the block based on the raw data.
74  else
75  {
76  std::make_index_sequence<sizeof...(Begin)> seq;
77  detail::assign_slice_elements(arg, std::forward<Block>(block), std::forward_as_tuple(begin...), seq);
78  }
79  return std::forward<Arg>(arg);
80  }
81 
82 
83 } // namespace OpenKalman
84 
85 #endif //OPENKALMAN_SET_SLICE_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
constexpr bool indexible
T is a generalized tensor type.
Definition: indexible.hpp:32
constexpr bool value
T is numerical value or is reducible to a numerical value.
Definition: value.hpp:31
decltype(auto) constexpr all_vector_space_descriptors(const T &t)
Return a collection of coordinates::pattern objects associated with T.
Definition: all_vector_space_descriptors.hpp:52
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 bool index
T is an index value.
Definition: index.hpp:56
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
constexpr Arg && set_slice(Arg &&arg, Block &&block, const Begin &...begin)
Assign an object to a particular slice of a matrix or tensor.
Definition: set_slice.hpp:56