13#ifndef dealii_array_view_h
14#define dealii_array_view_h
24#include <boost/container/small_vector.hpp>
36template <
int N,
typename T>
39template <
typename number>
84template <
typename ElementType,
typename MemorySpaceType = MemorySpace::Host>
150 template <
typename OtherElementType,
151 typename = std::enable_if_t<
152 std::is_same_v<ElementType, const OtherElementType> &&
153 !std::is_same_v<OtherElementType, const ElementType>>>
162#ifdef DEAL_II_HAVE_CXX20
180 template <
typename ContiguousContainer>
181 ArrayView(
const ContiguousContainer &container)
182 requires(std::is_same_v<
183 std::remove_const_t<ElementType>,
184 std::remove_const_t<typename ContiguousContainer::value_type>> &&
185 std::is_const_v<ElementType> &&
205 template <
typename ContiguousContainer>
206 ArrayView(ContiguousContainer &container)
207 requires(std::is_same_v<
208 std::remove_const_t<ElementType>,
209 std::remove_const_t<typename ContiguousContainer::value_type>> &&
232 template <
typename ContiguousContainer,
233 typename =
decltype(std::data(std::declval<ContiguousContainer>())),
234 typename =
decltype(std::size(std::declval<ContiguousContainer>())),
235 typename = std::enable_if_t<
236 std::is_same_v<std::remove_const_t<ElementType>,
238 typename ContiguousContainer::value_type>> &&
239 std::is_const_v<ElementType>>>
259 template <
typename ContiguousContainer,
260 typename =
decltype(std::data(std::declval<ContiguousContainer>())),
261 typename =
decltype(std::size(std::declval<ContiguousContainer>())),
262 typename = std::enable_if_t<std::is_same_v<
263 std::remove_const_t<ElementType>,
264 std::remove_const_t<typename ContiguousContainer::value_type>>>>
283 template <std::
size_t N>
329 ArrayView(
const std::initializer_list<std::remove_const_t<value_type>>
351 template <
typename OtherElementType,
352 typename = std::enable_if_t<
353 std::is_same_v<ElementType, const OtherElementType> &&
354 !std::is_same_v<OtherElementType, const ElementType>>>
508 operator[](const
std::
size_t i) const;
522 friend class
ArrayView<const ElementType, MemorySpaceType>;
530template <typename ElementType, typename MemorySpaceType>
538template <
typename ElementType,
typename MemorySpaceType>
555template <
typename ElementType,
typename MemorySpaceType>
558 const std::size_t n_elements)
563 this->starting_element = starting_element;
565 this->starting_element =
nullptr;
569 this->starting_element = starting_element;
571 this->n_elements = n_elements;
576template <
typename ElementType,
typename MemorySpaceType>
578 : starting_element(&element)
584template <
typename ElementType,
typename MemorySpaceType>
585template <
typename OtherElementType,
typename>
588 : starting_element(view.starting_element)
589 , n_elements(view.n_elements)
593#ifdef DEAL_II_HAVE_CXX20
595template <
typename ElementType,
typename MemorySpaceType>
596template <
typename ContiguousContainer>
598 const ContiguousContainer &container)
599 requires(std::is_same_v<
600 std::remove_const_t<ElementType>,
601 std::remove_const_t<typename ContiguousContainer::value_type>> &&
602 std::is_const_v<ElementType> &&
610template <
typename ElementType,
typename MemorySpaceType>
611template <
typename ContiguousContainer>
613 ContiguousContainer &container)
614 requires(std::is_same_v<
615 std::remove_const_t<ElementType>,
616 std::remove_const_t<typename ContiguousContainer::value_type>> &&
625template <
typename ElementType,
typename MemorySpaceType>
626template <
typename ContiguousContainer,
typename,
typename,
typename>
628 const ContiguousContainer &container)
635template <
typename ElementType,
typename MemorySpaceType>
636template <
typename ContiguousContainer,
typename,
typename,
typename>
638 ContiguousContainer &container)
647template <
typename ElementType,
typename MemorySpaceType>
648template <std::
size_t N>
650 ElementType (&array)[N])
655template <
typename ElementType,
typename MemorySpaceType>
657 const std::initializer_list<std::remove_const_t<value_type>> &initializer)
665template <
typename ElementType,
typename MemorySpaceType>
666template <
typename OtherElementType,
typename>
678template <
typename ElementType,
typename MemorySpaceType>
683 return (other_view.
data() == starting_element) &&
684 (other_view.
size() == n_elements);
689template <
typename ElementType,
typename MemorySpaceType>
692 const ArrayView<std::remove_const_t<value_type>, MemorySpaceType> &other_view)
695 return (other_view.data() == starting_element) &&
696 (other_view.size() == n_elements);
701template <
typename ElementType,
typename MemorySpaceType>
706 return !(*
this == other_view);
711template <
typename ElementType,
typename MemorySpaceType>
719 return starting_element;
724template <
typename ElementType,
typename MemorySpaceType>
727 const ArrayView<std::remove_const_t<value_type>, MemorySpaceType> &other_view)
730 return !(*
this == other_view);
735template <
typename ElementType,
typename MemorySpaceType>
744template <
typename ElementType,
typename MemorySpaceType>
748 return n_elements == 0;
753template <
typename ElementType,
typename MemorySpaceType>
757 return starting_element;
762template <
typename ElementType,
typename MemorySpaceType>
766 return starting_element + n_elements;
771template <
typename ElementType,
typename MemorySpaceType>
775 return starting_element;
780template <
typename ElementType,
typename MemorySpaceType>
784 return starting_element + n_elements;
789template <
typename ElementType,
typename MemorySpaceType>
795 return *(starting_element + i);
805template <
typename ElementType, std::
size_t str
ide = 1>
854 data() const noexcept;
869 operator[](const
std::
size_t i) const;
886template <typename ElementType,
std::
size_t stride>
892 return *(this->starting_element + stride * i);
897template <
typename ElementType, std::
size_t str
ide>
909template <
typename ElementType, std::
size_t str
ide>
918template <
typename ElementType, std::
size_t str
ide>
927template <
typename ElementType, std::
size_t str
ide>
940 namespace ArrayViewHelper
947 template <
class Iterator>
949 is_contiguous(
const Iterator &
first,
const Iterator &last)
951 const auto n = std::distance(
first, last);
952 for (std::decay_t<
decltype(n)> i = 0; i < n; ++i)
953 if (std::addressof(*(std::next(
first, i))) !=
954 std::next(std::addressof(*
first), i))
972 is_contiguous(T *, T *)
998template <
typename Iterator,
typename MemorySpaceType = MemorySpace::Host>
1000 std::remove_reference_t<typename std::iterator_traits<Iterator>::reference>,
1005 std::is_same_v<typename std::iterator_traits<Iterator>::iterator_category,
1006 typename std::random_access_iterator_tag>
1007#ifdef DEAL_II_HAVE_CXX20
1009 std::is_same_v<typename std::iterator_traits<Iterator>::iterator_category,
1010 typename std::contiguous_iterator_tag>
1013 "The provided iterator needs to be a random access iterator.");
1016 "The beginning of the array view needs to be before the end."));
1018 ExcMessage(
"The provided range isn't contiguous in memory!"));
1021 std::remove_reference_t<typename std::iterator_traits<Iterator>::reference>,
1036template <
typename ElementType,
typename MemorySpaceType = MemorySpace::Host>
1042 "The beginning of the array view needs to be before the end."));
1058template <
typename Number,
typename MemorySpaceType>
1077template <
typename Number,
typename MemorySpaceType>
1099template <
typename ElementType,
int N>
1122template <
typename ElementType>
1145template <
typename ElementType>
1168template <
typename ElementType>
1191template <
typename ElementType>
1219template <
typename ElementType>
1222 const std::size_t starting_index,
1223 const std::size_t size_of_view)
1225 Assert(starting_index + size_of_view <= vector.size(),
1226 ExcMessage(
"The starting index and size of the view you want to "
1227 "create would lead to a view that extends beyond the end "
1228 "of the given vector."));
1253template <
typename ElementType>
1256 const std::size_t starting_index,
1257 const std::size_t size_of_view)
1259 Assert(starting_index + size_of_view <= vector.size(),
1260 ExcMessage(
"The starting index and size of the view you want to "
1261 "create would lead to a view that extends beyond the end "
1262 "of the given vector."));
1275template <
typename ElementType>
1291template <
typename ElementType>
1307template <
typename ElementType>
1310 const std::size_t starting_index,
1311 const std::size_t size_of_view)
1313 Assert(starting_index + size_of_view <= vector.
size(),
1314 ExcMessage(
"The starting index and size of the view you want to "
1315 "create would lead to a view that extends beyond the end "
1316 "of the given vector."));
1329template <
typename ElementType>
1332 const std::size_t starting_index,
1333 const std::size_t size_of_view)
1335 Assert(starting_index + size_of_view <= vector.
size(),
1336 ExcMessage(
"The starting index and size of the view you want to "
1337 "create would lead to a view that extends beyond the end "
1338 "of the given vector."));
1358template <
typename ElementType, std::
size_t N>
1381template <
typename ElementType, std::
size_t N>
1406template <
typename ElementType>
1433template <
typename ElementType>
1458template <
typename ElementType>
1483template <
typename ElementType>
1508template <
typename ElementType>
1533template <
typename ElementType>
1563template <
typename ElementType>
1568 const std::size_t size_of_view)
1572 Assert(starting_column + size_of_view <= table.size()[1],
1573 ExcMessage(
"The starting index and size of the view you want to "
1574 "create would lead to a view that extends beyond the end "
1575 "of a column of the given table."));
1600template <
typename ElementType>
1605 const std::size_t size_of_view)
1609 Assert(starting_column + size_of_view <= table.size()[1],
1610 ExcMessage(
"The starting index and size of the view you want to "
1611 "create would lead to a view that extends beyond the end "
1612 "of a column of the given table."));
1629template <
typename Container>
auto make_const_array_view(const Container &container) -> decltype(make_array_view(container))
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
ArrayView(ContiguousContainer &container)
ArrayView< const ElementType > make_array_view(const Vector< ElementType > &vector)
ArrayView(value_type &element)
ArrayView< ElementType, MemorySpaceType > operator=(const ArrayView< OtherElementType, MemorySpaceType > &view)
ArrayView< Number, MemorySpaceType > make_array_view(ArrayView< Number, MemorySpaceType > &array_view)
value_type & operator[](const std::size_t i) const
ArrayView< const ElementType > make_array_view(const LAPACKFullMatrix< ElementType > &matrix)
ArrayView< ElementType > make_array_view(std::array< ElementType, N > &array)
bool operator!=(const ArrayView< std::remove_const_t< value_type >, MemorySpaceType > &other_view) const
ArrayView< ElementType > make_array_view(LAPACKFullMatrix< ElementType > &matrix)
ArrayView< ElementType > make_array_view(ElementType(&array)[N])
ArrayView< ElementType, MemorySpaceType > make_array_view(ElementType *const begin, ElementType *const end)
const_iterator cend() const
ArrayView(const ContiguousContainer &container)
ArrayView< const ElementType > make_array_view(const std::vector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
bool operator==(const ArrayView< std::remove_const_t< value_type >, MemorySpaceType > &other_view) const
ArrayView< const ElementType > make_array_view(const Table< 2, ElementType > &table)
ArrayView< const ElementType > make_array_view(const Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row, const typename Table< 2, ElementType >::size_type starting_column, const std::size_t size_of_view)
ArrayView< ElementType > make_array_view(std::vector< ElementType > &vector)
bool operator==(const ArrayView< const value_type, MemorySpaceType > &other_view) const
ArrayView< ElementType, MemorySpaceType > & operator=(const ArrayView &view)=default
ArrayView< ElementType > make_array_view(std::vector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
bool operator!=(const ArrayView< const value_type, MemorySpaceType > &other_view) const
const_iterator cbegin() const
ArrayView(value_type(&array)[N])
value_type * data() const noexcept
ArrayView< ElementType > make_array_view(Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row, const typename Table< 2, ElementType >::size_type starting_column, const std::size_t size_of_view)
void reinit(value_type *starting_element, const std::size_t n_elements)
ArrayView< ElementType > make_array_view(Table< 2, ElementType > &table)
value_type * starting_element
ArrayView(const std::initializer_list< std::remove_const_t< value_type > > &initializer_list)
ArrayView(value_type *starting_element, const std::size_t n_elements)
ArrayView< const Number, MemorySpaceType > make_array_view(const ArrayView< Number, MemorySpaceType > &array_view)
ArrayView< const ElementType > make_array_view(const Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row)
ArrayView< const ElementType > make_array_view(const AlignedVector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
ArrayView< const ElementType > make_array_view(const AlignedVector< ElementType > &vector)
const ElementType * const_iterator
ArrayView< ElementType > make_array_view(Vector< ElementType > &vector)
ArrayView< ElementType > make_array_view(Table< 2, ElementType > &table, const typename Table< 2, ElementType >::size_type row)
ArrayView< const ElementType > make_array_view(const std::vector< ElementType > &vector)
ArrayView< ElementType > make_array_view(AlignedVector< ElementType > &vector, const std::size_t starting_index, const std::size_t size_of_view)
ArrayView< const ElementType > make_array_view(const std::array< ElementType, N > &array)
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
ArrayView(const ArrayView< OtherElementType, MemorySpaceType > &view)
ArrayView< ElementType > make_array_view(AlignedVector< ElementType > &vector)
ArrayView(const ArrayView &view)=default
StridedArrayView(value_type *starting_element, const std::size_t n_elements)
value_type * starting_element
value_type * data() const noexcept
virtual size_type size() const override
constexpr LibraryBuildMode library_build_mode
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_HOST_DEVICE
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::vector< index_type > data