From: Daniel Arndt Date: Thu, 25 Jan 2018 09:05:29 +0000 (+0100) Subject: Reorder array_view.h and make_array_view for Tensor classes X-Git-Tag: v9.0.0-rc1~519^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0cadc94040931cb613650c2b722cf4111a8c2e1e;p=dealii.git Reorder array_view.h and make_array_view for Tensor classes --- diff --git a/doc/news/changes/minor/20180125DanielArndt-1 b/doc/news/changes/minor/20180125DanielArndt-1 new file mode 100644 index 0000000000..c124b17545 --- /dev/null +++ b/doc/news/changes/minor/20180125DanielArndt-1 @@ -0,0 +1,4 @@ +New: There are new overloads of make_array_view for Tensor, SymmetricTensor, +LAPACKFullMatrix, C-style array and Vector. +
+(Daniel Arndt, 2018/01/25) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 6c3259179c..33e0e08484 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include @@ -422,49 +424,206 @@ ArrayView::operator[](const std::size_t i) const +#ifndef DOXYGEN +namespace internal +{ + namespace ArrayViewHelper + { + template + bool is_contiguous(const Iterator &first, const Iterator &last) + { + const auto n = std::distance(first, last); + for (typename std::decay::type i = 0; i < n; ++i) + if (*(std::next(first, i)) != *(std::next(std::addressof(*first), i))) + return false; + return true; + } + } +} +#endif + + + + /** - * Create a view to an entire C-style array. This is equivalent to - * initializing an ArrayView object with a pointer to the first element and - * the size of the given argument. + * Create an ArrayView that takes a pair of iterators as arguments. The type + * of the ArrayView is inferred from the value type of the iterator (e.g., the + * view created from two const iterators will have a const type). * - * Whether the resulting ArrayView is writable or not depends on the - * ElementType being a const type or not. + * @warning The iterators @p begin and @p end must bound (in the usual half-open + * way) a contiguous in memory range of values. This function is intended for + * use with iterators into containers like + * boost::container::small_vector or std::vector and + * will not work correctly with, e.g., + * boost::container::stable_vector or std::deque. + * In debug mode, we check that the provided iterators represent contiguous + * memory indeed. * - * @param[in] array The C-style array for which we want to have an ArrayView - * object. The ArrayView corresponds to the entire vector. + * @relates ArrayView + */ +template +ArrayView::reference>::type> +make_array_view (const Iterator begin, const Iterator end) +{ + static_assert(std::is_same::iterator_category, + typename std::random_access_iterator_tag>::value, + "The provided iterator should be a random access iterator."); + Assert(begin <= end, + ExcMessage("The beginning of the array view should be before the end.")); + Assert(internal::ArrayViewHelper::is_contiguous(begin, end), + ExcMessage("The provided range isn't contiguous in memory!")); + // the reference type, not the value type, knows the constness of the iterator + return ArrayView::reference>::type> + (std::addressof(*begin), end - begin); +} + + + +/** + * Create a view from a pair of pointers. ElementType may be + * const-qualified. + * + * @warning The pointers @p begin and @p end must bound (in the usual + * half-open way) a contiguous in memory range of values. * * @relates ArrayView */ -template -inline +template ArrayView -make_array_view (ElementType (&array)[N]) +make_array_view (ElementType *const begin, ElementType *const end) { - return ArrayView (array, N); + Assert(begin <= end, + ExcMessage("The beginning of the array view should be before the end.")); + return ArrayView(begin, end - begin); } /** - * Create a view to an entire std::vector object. This is equivalent to + * Create a view to an entire Tensor object. This is equivalent to initializing + * an ArrayView object with a pointer to the first element and the size of the + * given argument. + * + * This function is used for @p const references to objects of Tensor type + * because they contain immutable elements. Consequently, the return type of + * this function is a view to a set of @p const objects. + * + * @param[in] vector The Tensor for which we want to have an array view + * object. The array view corresponds to the entire object but the + * order in which the entries are presented in the array is undefined and can + * not be relied upon. + * + * @relates ArrayView + */ +template +inline +ArrayView +make_array_view (const Tensor &tensor) +{ + return make_array_view (tensor.begin_raw(), tensor.end_raw()); +} + + + +/** + * Create a view to an entire Tensor object. This is equivalent to initializing + * an ArrayView object with a pointer to the first element and the size of the + * given argument. + * + * This function is used for non-@p const references to objects of Tensor type. + * Such objects contain elements that can be written to. Consequently, + * the return type of this function is a view to a set of writable objects. + * + * @param[in] vector The Tensor for which we want to have an array view + * object. The array view corresponds to the entire object but the + * order in which the entries are presented in the array is undefined and can + * not be relied upon. + * + * @relates ArrayView + */ +template +inline +ArrayView +make_array_view (Tensor &tensor) +{ + return make_array_view (tensor.begin_raw(), tensor.end_raw()); +} + + + +/** + * Create a view to an entire SymmetricTensor object. This is equivalent to + * initializing an ArrayView object with a pointer to the first element and the + * size of the given argument. + * + * This function is used for @p const references to objects of SymmetricTensor + * type because they contain immutable elements. Consequently, the return type + * of this function is a view to a set of @p const objects. + * + * @param[in] vector The SymmetricTensor for which we want to have an array + * view object. The array view corresponds to the entire object but + * the order in which the entries are presented in the array is undefined and + * can not be relied upon. + * + * @relates ArrayView + */ +template +inline +ArrayView +make_array_view (const SymmetricTensor &tensor) +{ + return make_array_view (tensor.begin_raw(), tensor.end_raw()); +} + + + +/** + * Create a view to an entire SymmetricTensor object. This is equivalent to + * initializing an ArrayView object with a pointer to the first element and the + * size of the given argument. + * + * This function is used for non-@p const references to objects of + * SymmetricTensor type. Such objects contain elements that can be written to. + * Consequently, the return type of this function is a view to a set of writable + * objects. + * + * @param[in] vector The SymmetricTensor for which we want to have an array view + * object. The array view corresponds to the entire object but the + * order in which the entries are presented in the array is undefined and can + * not be relied upon. + * + * @relates ArrayView + */ +template +inline +ArrayView +make_array_view (SymmetricTensor &tensor) +{ + return make_array_view (tensor.begin_raw(), tensor.end_raw()); +} + + + +/** + * Create a view to an entire C-style array. This is equivalent to * initializing an ArrayView object with a pointer to the first element and * the size of the given argument. * - * This function is used for non-@p const references to objects of vector - * type. Such objects contain elements that can be written to. Consequently, - * the return type of this function is a view to a set of writable objects. + * Whether the resulting ArrayView is writable or not depends on the + * ElementType being a const type or not. * - * @param[in] vector The vector for which we want to have an array view - * object. The array view corresponds to the entire vector. + * @param[in] array The C-style array for which we want to have an ArrayView + * object. The ArrayView corresponds to the entire vector. * * @relates ArrayView */ -template +template inline ArrayView -make_array_view (std::vector &vector) +make_array_view (ElementType (&array)[N]) { - return ArrayView (vector.data(), vector.size()); + return ArrayView (array, N); } @@ -492,6 +651,7 @@ make_array_view (Vector &vector) } + /** * Create a view to an entire Vector object. This is equivalent to * initializing an ArrayView object with a pointer to the first element and @@ -516,6 +676,30 @@ make_array_view (const Vector &vector) +/** + * Create a view to an entire std::vector object. This is equivalent to + * initializing an ArrayView object with a pointer to the first element and + * the size of the given argument. + * + * This function is used for non-@p const references to objects of vector + * type. Such objects contain elements that can be written to. Consequently, + * the return type of this function is a view to a set of writable objects. + * + * @param[in] vector The vector for which we want to have an array view + * object. The array view corresponds to the entire vector. + * + * @relates ArrayView + */ +template +inline +ArrayView +make_array_view (std::vector &vector) +{ + return ArrayView (vector.data(), vector.size()); +} + + + /** * Create a view to an entire std::vector object. This is equivalent to * initializing an ArrayView object with a pointer to the first element and @@ -573,76 +757,6 @@ make_array_view (std::vector &vector, return ArrayView (&vector[starting_index], size_of_view); } -#ifndef DOXYGEN -namespace internal -{ - namespace ArrayViewHelper - { - template - bool is_contiguous(const Iterator &first, const Iterator &last) - { - const auto n = std::distance(first, last); - for (typename std::decay::type i = 0; i < n; ++i) - if (*(std::next(first, i)) != *(std::next(std::addressof(*first), i))) - return false; - return true; - } - } -} -#endif - - -/** - * Create an ArrayView that takes a pair of iterators as arguments. The type - * of the ArrayView is inferred from the value type of the iterator (e.g., the - * view created from two const iterators will have a const type). - * - * @warning The iterators @p begin and @p end must bound (in the usual half-open - * way) a contiguous in memory range of values. This function is intended for - * use with iterators into containers like - * boost::container::small_vector or std::vector and - * will not work correctly with, e.g., - * boost::container::stable_vector or std::deque. - * In debug mode, we check that the provided iterators represent contiguous - * memory indeed. - * - * @relates ArrayView - */ -template -ArrayView::reference>::type> -make_array_view (const Iterator begin, const Iterator end) -{ - static_assert(std::is_same::iterator_category, - typename std::random_access_iterator_tag>::value, - "The provided iterator should be a random access iterator."); - Assert(begin <= end, - ExcMessage("The beginning of the array view should be before the end.")); - Assert(internal::ArrayViewHelper::is_contiguous(begin, end), - ExcMessage("The provided range isn't contiguous in memory!")); - // the reference type, not the value type, knows the constness of the iterator - return ArrayView::reference>::type> - (&*begin, end - begin); -} - -/** - * Create a view from a pair of pointers. ElementType may be - * const-qualified. - * - * @warning The pointers @p begin and @p end must bound (in the usual - * half-open way) a contiguous in memory range of values. - * - * @relates ArrayView - */ -template -ArrayView -make_array_view (ElementType *const begin, ElementType *const end) -{ - Assert(begin <= end, - ExcMessage("The beginning of the array view should be before the end.")); - return ArrayView(begin, end - begin); -} - /** @@ -785,6 +899,7 @@ make_array_view (LAPACKFullMatrix &matrix) } + /** * Create a view to an entire LAPACKFullMatrix object. This is equivalent to * initializing an ArrayView object with a pointer to the first element of the @@ -810,6 +925,7 @@ make_array_view (const LAPACKFullMatrix &matrix) } + /** * Create a view to an entire row of a Table<2> object. This is equivalent to * initializing an ArrayView object with a pointer to the first element of the