Loading...
Searching...
No Matches
layout_right.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_STD_LAYOUT_RIGHT_HH
#define DUNE_COMMON_STD_LAYOUT_RIGHT_HH
#include <array>
#include <type_traits>
#include <dune/common/indices.hh>
#include <dune/common/std/no_unique_address.hh>
#include <dune/common/std/impl/fwd_layouts.hh>
namespace Dune::Std {
template <class Extents>
class layout_right::mapping
{
template <class> friend class mapping;
public:
using extents_type = Extents;
using size_type = typename extents_type::size_type;
using rank_type = typename extents_type::rank_type;
using index_type = typename extents_type::index_type;
using layout_type = layout_right;
constexpr mapping () noexcept = default;
constexpr mapping (const mapping&) noexcept = default;
constexpr mapping (const extents_type& e) noexcept
: extents_(e)
{}
template <class OtherExtents,
std::enable_if_t<std::is_constructible_v<extents_type, OtherExtents>, int> = 0>
#if __cpp_conditional_explicit >= 201806L
explicit(!std::is_convertible_v<OtherExtents, extents_type>)
#endif
constexpr mapping (const mapping<OtherExtents>& m) noexcept
: extents_(m.extents())
{}
template <class OtherExtents, class E = extents_type,
std::enable_if_t<(E::rank() <= 1), int> = 0,
std::enable_if_t<std::is_constructible_v<extents_type, OtherExtents>, int> = 0>
#if __cpp_conditional_explicit >= 201806L
explicit(!std::is_convertible_v<OtherExtents, extents_type>)
#endif
constexpr mapping (const layout_left::mapping<OtherExtents>& m) noexcept
: extents_(m.extents())
{}
template <class OtherExtents,
std::enable_if_t<std::is_constructible_v<extents_type, OtherExtents>, int> = 0>
#if __cpp_conditional_explicit >= 201806L
explicit(extents_type::rank() > 0)
#endif
constexpr mapping (const layout_stride::mapping<OtherExtents>& m)
: extents_(m.extents())
{
#ifndef NDEBUG
if constexpr(extents_type::rank() > 0) {
index_type prod = 1;
for (rank_type r = extents_type::rank()-1; r > 0; --r) {
assert(m.strides(r) == prod);
prod *= m.extents().extent(r);
}
assert(m.strides(0) == prod);
}
#endif
}
constexpr mapping& operator= (const mapping&) noexcept = default;
constexpr const extents_type& extents () const noexcept { return extents_; }
constexpr index_type required_span_size () const noexcept { return extents_.product(); }
template <class... Indices,
std::enable_if_t<(sizeof...(Indices) == extents_type::rank()), int> = 0,
std::enable_if_t<(std::is_convertible_v<Indices, index_type> && ...), int> = 0,
std::enable_if_t<(std::is_nothrow_constructible_v<Indices, index_type> && ...), int> = 0>
constexpr index_type operator() (Indices... ii) const noexcept
{
const std::array indices{index_type(std::move(ii))...};
index_type value = indices.front();
for (rank_type j = 0; j < extents_type::rank()-1; ++j) {
value = indices[j+1] + extents_.extent(j+1) * value;
}
return value;
}
constexpr index_type operator() () const noexcept
{
return 0;
}
static constexpr bool is_always_unique () noexcept { return true; }
static constexpr bool is_always_exhaustive () noexcept { return true; }
static constexpr bool is_always_strided () noexcept { return true; }
static constexpr bool is_unique () noexcept { return true; }
static constexpr bool is_exhaustive () noexcept { return true; }
static constexpr bool is_strided () noexcept { return true; }
template <class E = extents_type,
std::enable_if_t<(E::rank() > 0), int> = 0>
constexpr index_type stride (rank_type i) const noexcept
{
assert(i < extents_type::rank());
index_type prod = 1;
for (rank_type r = i+1; r < extents_type::rank(); ++r)
prod *= extents().extent(r);
return prod;
}
template <class OtherExtents>
friend constexpr bool operator== (const mapping& a, const mapping<OtherExtents>& b) noexcept
{
return a.extents_ == b.extents_;
}
private:
DUNE_NO_UNIQUE_ADDRESS extents_type extents_;
};
} // end namespace Dune::Std
#endif // DUNE_COMMON_STD_LAYOUT_RIGHT_HH
fwd_layouts.hh
no_unique_address.hh
DUNE_NO_UNIQUE_ADDRESS
#define DUNE_NO_UNIQUE_ADDRESS
Definition no_unique_address.hh:24
indices.hh
Dune::Indices
Namespace with predefined compile time indices for the range [0,19].
Definition indices.hh:50
Dune::Std
Namespace for features backported from new C++ standards.
Definition algorithm.hh:19
Dune::Std::layout_left::mapping
A layout mapping where the leftmost extent has stride 1.
Definition layout_left.hh:20
Dune::Std::layout_right
A layout where the rightmost extent has stride 1, and strides increase right-to-left as the product o...
Definition fwd_layouts.hh:30
Dune::Std::layout_right::mapping
A layout mapping where the rightmost extent has stride 1.
Definition layout_right.hh:20
Dune::Std::layout_right::mapping::extents
constexpr const extents_type & extents() const noexcept
Definition layout_right.hh:86
Dune::Std::layout_right::mapping::mapping
constexpr mapping(const mapping< OtherExtents > &m) noexcept
Construct the mapping from another mapping with a different extents type.
Definition layout_right.hh:47
Dune::Std::layout_right::mapping::operator()
constexpr index_type operator()() const noexcept
The default offset for rank-0 tensors is 0.
Definition layout_right.hh:105
Dune::Std::layout_right::mapping::layout_type
layout_right layout_type
Definition layout_right.hh:28
Dune::Std::layout_right::mapping::operator=
constexpr mapping & operator=(const mapping &) noexcept=default
Copy-assignment for the mapping.
Dune::Std::layout_right::mapping::required_span_size
constexpr index_type required_span_size() const noexcept
Definition layout_right.hh:87
Dune::Std::layout_right::mapping::mapping
constexpr mapping() noexcept=default
The default construction is possible for default constructible extents.
Dune::Std::layout_right::mapping::size_type
typename extents_type::size_type size_type
Definition layout_right.hh:25
Dune::Std::layout_right::mapping::extents_type
Extents extents_type
Definition layout_right.hh:24
Dune::Std::layout_right::mapping::is_always_unique
static constexpr bool is_always_unique() noexcept
Definition layout_right.hh:110
Dune::Std::layout_right::mapping::stride
constexpr index_type stride(rank_type i) const noexcept
The stride is the product of the extents E(n)*E(n-1)*...*E(i+1).
Definition layout_right.hh:121
Dune::Std::layout_right::mapping::is_exhaustive
static constexpr bool is_exhaustive() noexcept
Definition layout_right.hh:115
Dune::Std::layout_right::mapping::mapping
constexpr mapping(const layout_left::mapping< OtherExtents > &m) noexcept
Construct the mapping from a layout_left.
Definition layout_right.hh:58
Dune::Std::layout_right::mapping::is_strided
static constexpr bool is_strided() noexcept
Definition layout_right.hh:116
Dune::Std::layout_right::mapping::mapping
friend class mapping
Definition layout_right.hh:21
Dune::Std::layout_right::mapping::rank_type
typename extents_type::rank_type rank_type
Definition layout_right.hh:26
Dune::Std::layout_right::mapping::is_always_strided
static constexpr bool is_always_strided() noexcept
Definition layout_right.hh:112
Dune::Std::layout_right::mapping::is_unique
static constexpr bool is_unique() noexcept
Definition layout_right.hh:114
Dune::Std::layout_right::mapping::operator==
friend constexpr bool operator==(const mapping &a, const mapping< OtherExtents > &b) noexcept
Definition layout_right.hh:131
Dune::Std::layout_right::mapping::is_always_exhaustive
static constexpr bool is_always_exhaustive() noexcept
Definition layout_right.hh:111
Dune::Std::layout_right::mapping::index_type
typename extents_type::index_type index_type
Definition layout_right.hh:27
Dune::Std::layout_stride::mapping
A layout mapping where the strides are user-defined.
Definition layout_stride.hh:21
Dune::Std::layout_stride::mapping::extents
constexpr const extents_type & extents() const noexcept
Definition layout_stride.hh:87
Dune::Std::layout_stride::mapping::strides
constexpr const strides_type & strides() const noexcept
Get the array of all strides.
Definition layout_stride.hh:129