Merge pull request #190 from annagrin/dev/annagrin/remove_basic_span

Remove basic_span
This commit is contained in:
Neil MacIntosh 2015-11-13 13:31:45 -08:00
commit 252671257f
2 changed files with 397 additions and 331 deletions

View File

@ -1039,180 +1039,12 @@ namespace details
} // namespace details } // namespace details
template <typename ArrayView> template <typename Span>
class contiguous_span_iterator; class contiguous_span_iterator;
template <typename ArrayView> template <typename Span>
class general_span_iterator; class general_span_iterator;
enum class byte : std::uint8_t {}; enum class byte : std::uint8_t {};
template <typename ValueType, typename BoundsType>
class basic_span
{
public:
static const size_t rank = BoundsType::rank;
using bounds_type = BoundsType;
using size_type = typename bounds_type::size_type;
using index_type = typename bounds_type::index_type;
using value_type = ValueType;
using const_value_type = std::add_const_t<value_type>;
using pointer = ValueType*;
using reference = ValueType&;
using iterator = std::conditional_t<std::is_same<typename BoundsType::mapping_type, contiguous_mapping_tag>::value, contiguous_span_iterator<basic_span>, general_span_iterator<basic_span>>;
using const_iterator = std::conditional_t<std::is_same<typename BoundsType::mapping_type, contiguous_mapping_tag>::value, contiguous_span_iterator<basic_span<const_value_type, BoundsType>>, general_span_iterator<basic_span<const_value_type, BoundsType>>>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using sliced_type = std::conditional_t<rank == 1, value_type, basic_span<value_type, typename BoundsType::sliced_type>>;
private:
pointer m_pdata;
bounds_type m_bounds;
public:
constexpr bounds_type bounds() const noexcept
{
return m_bounds;
}
template <size_t Dim = 0>
constexpr size_type extent() const noexcept
{
static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
return m_bounds.template extent<Dim>();
}
constexpr size_type size() const noexcept
{
return m_bounds.size();
}
constexpr reference operator[](const index_type& idx) const
{
return m_pdata[m_bounds.linearize(idx)];
}
constexpr pointer data() const noexcept
{
return m_pdata;
}
template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
constexpr Ret operator[](size_type idx) const
{
fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
const size_type ridx = idx * m_bounds.stride();
fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
return Ret {m_pdata + ridx, m_bounds.slice()};
}
constexpr operator bool () const noexcept
{
return m_pdata != nullptr;
}
constexpr iterator begin() const
{
return iterator {this, true};
}
constexpr iterator end() const
{
return iterator {this, false};
}
constexpr const_iterator cbegin() const
{
return const_iterator {reinterpret_cast<const basic_span<const value_type, bounds_type> *>(this), true};
}
constexpr const_iterator cend() const
{
return const_iterator {reinterpret_cast<const basic_span<const value_type, bounds_type> *>(this), false};
}
constexpr reverse_iterator rbegin() const
{
return reverse_iterator {end()};
}
constexpr reverse_iterator rend() const
{
return reverse_iterator {begin()};
}
constexpr const_reverse_iterator crbegin() const
{
return const_reverse_iterator {cend()};
}
constexpr const_reverse_iterator crend() const
{
return const_reverse_iterator {cbegin()};
}
template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator== (const basic_span<OtherValueType, OtherBoundsType> & other) const noexcept
{
return m_bounds.size() == other.m_bounds.size() &&
(m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
}
template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator!= (const basic_span<OtherValueType, OtherBoundsType> & other) const noexcept
{
return !(*this == other);
}
template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator< (const basic_span<OtherValueType, OtherBoundsType> & other) const noexcept
{
return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
}
template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator<= (const basic_span<OtherValueType, OtherBoundsType> & other) const noexcept
{
return !(other < *this);
}
template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator> (const basic_span<OtherValueType, OtherBoundsType> & other) const noexcept
{
return (other < *this);
}
template <typename OtherValueType, typename OtherBoundsType, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator>= (const basic_span<OtherValueType, OtherBoundsType> & other) const noexcept
{
return !(*this < other);
}
public:
template <typename OtherValueType, typename OtherBounds,
typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value
&& std::is_convertible<OtherBounds, bounds_type>::value>>
constexpr basic_span(const basic_span<OtherValueType, OtherBounds> & other ) noexcept
: m_pdata(other.m_pdata), m_bounds(other.m_bounds)
{
}
protected:
constexpr basic_span(pointer data, bounds_type bound) noexcept
: m_pdata(data)
, m_bounds(std::move(bound))
{
fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
}
template <typename T>
constexpr basic_span(T *data, std::enable_if_t<std::is_same<value_type, std::remove_all_extents_t<T>>::value, bounds_type> bound) noexcept
: m_pdata(reinterpret_cast<pointer>(data))
, m_bounds(std::move(bound))
{
fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
}
template <typename DestBounds>
constexpr basic_span<value_type, DestBounds> as_span(const DestBounds &bounds)
{
details::verifyBoundsReshape(m_bounds, bounds);
return {m_pdata, bounds};
}
private:
friend iterator;
friend const_iterator;
template <typename ValueType2, typename BoundsType2>
friend class basic_span;
};
template <std::ptrdiff_t DimSize = dynamic_range> template <std::ptrdiff_t DimSize = dynamic_range>
struct dim struct dim
{ {
@ -1235,21 +1067,21 @@ class strided_span;
namespace details namespace details
{ {
template <typename T, typename = std::true_type> template <typename T, typename = std::true_type>
struct ArrayViewTypeTraits struct SpanTypeTraits
{ {
using value_type = T; using value_type = T;
using size_type = size_t; using size_type = size_t;
}; };
template <typename Traits> template <typename Traits>
struct ArrayViewTypeTraits<Traits, typename std::is_reference<typename Traits::span_traits &>::type> struct SpanTypeTraits<Traits, typename std::is_reference<typename Traits::span_traits &>::type>
{ {
using value_type = typename Traits::span_traits::value_type; using value_type = typename Traits::span_traits::value_type;
using size_type = typename Traits::span_traits::size_type; using size_type = typename Traits::span_traits::size_type;
}; };
template <typename T, std::ptrdiff_t... Ranks> template <typename T, std::ptrdiff_t... Ranks>
struct ArrayViewArrayTraits { struct SpanArrayTraits {
using type = span<T, Ranks...>; using type = span<T, Ranks...>;
using value_type = T; using value_type = T;
using bounds_type = static_bounds<Ranks...>; using bounds_type = static_bounds<Ranks...>;
@ -1257,7 +1089,7 @@ namespace details
using reference = T&; using reference = T&;
}; };
template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks> template <typename T, std::ptrdiff_t N, std::ptrdiff_t... Ranks>
struct ArrayViewArrayTraits<T[N], Ranks...> : ArrayViewArrayTraits<T, Ranks..., N> {}; struct SpanArrayTraits<T[N], Ranks...> : SpanArrayTraits<T, Ranks..., N> {};
template <typename BoundsType> template <typename BoundsType>
BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size
@ -1322,113 +1154,131 @@ namespace details
template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions> template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
class span : public basic_span <ValueType, static_bounds <FirstDimension, RestDimensions...>> class span
{ {
template <typename ValueType2, std::ptrdiff_t FirstDimension2, template <typename ValueType2, std::ptrdiff_t FirstDimension2, std::ptrdiff_t... RestDimensions2>
std::ptrdiff_t... RestDimensions2>
friend class span; friend class span;
using Base = basic_span<ValueType, static_bounds<FirstDimension, RestDimensions...>>; public:
using bounds_type = static_bounds<FirstDimension, RestDimensions...>;
static const size_t rank = bounds_type::rank;
using size_type = typename bounds_type::size_type;
using index_type = typename bounds_type::index_type;
using value_type = ValueType;
using const_value_type = std::add_const_t<value_type>;
using pointer = std::add_pointer_t<value_type>;
using reference = std::add_lvalue_reference_t<value_type>;
using iterator = contiguous_span_iterator<span>;
using const_span = span<const_value_type, FirstDimension, RestDimensions...>;
using const_iterator = contiguous_span_iterator<const_span>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using sliced_type = std::conditional_t<rank == 1, value_type, span<value_type, RestDimensions...>>;
private:
pointer m_pdata;
bounds_type m_bounds;
friend iterator;
friend const_iterator;
public: public:
using typename Base::bounds_type;
using typename Base::size_type;
using typename Base::pointer;
using typename Base::value_type;
using typename Base::index_type;
using typename Base::iterator;
using typename Base::const_iterator;
using typename Base::reference;
using Base::rank;
public: constexpr span(pointer data, bounds_type bounds) noexcept
// basic : m_pdata(data), m_bounds(std::move(bounds))
constexpr span(pointer ptr, size_type size) : Base(ptr, bounds_type{ size }) {
fail_fast_assert((m_bounds.size() > 0 && data != nullptr) || m_bounds.size() == 0);
}
constexpr span(pointer ptr, size_type size) noexcept
: span(ptr, bounds_type{ size })
{} {}
constexpr span(pointer ptr, bounds_type bounds) : Base(ptr, std::move(bounds)) constexpr span(std::nullptr_t) noexcept
: span(nullptr, bounds_type{})
{} {}
constexpr span(std::nullptr_t) : Base(nullptr, bounds_type{}) constexpr span(std::nullptr_t, size_type size) noexcept
{} : span(nullptr, bounds_type{})
constexpr span(std::nullptr_t, size_type size) : Base(nullptr, bounds_type{})
{ {
fail_fast_assert(size == 0); fail_fast_assert(size == 0);
} }
// default // default
template <std::ptrdiff_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>> template <std::ptrdiff_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>>
constexpr span() : Base(nullptr, bounds_type()) constexpr span() noexcept
: span(nullptr, bounds_type())
{} {}
// from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer) // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
template <typename T, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range> template <typename T, typename Helper = details::SpanArrayTraits<T, dynamic_range>,
/*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], typename Base::value_type (*)[]>::value>*/> typename Dummy = std::enable_if_t<std::is_same<value_type, std::remove_all_extents_t<T>>::value>
constexpr span(T* const& data, size_type size) : Base(data, typename Helper::bounds_type{size}) /*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], value_type (*)[]>::value>*/
>
constexpr span(T* const& data, size_type size) : span(reinterpret_cast<pointer>(data), typename Helper::bounds_type{size})
{} {}
// from n-dimensions static array // from n-dimensions static array
template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>, template <typename T, size_t N, typename Helper = details::SpanArrayTraits<T, N>,
typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value>> typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], value_type(*)[]>::value>
constexpr span (T (&arr)[N]) : Base(arr, typename Helper::bounds_type()) >
constexpr span (T (&arr)[N]) : span(reinterpret_cast<pointer>(arr), typename Helper::bounds_type())
{} {}
// from n-dimensions static array with size // from n-dimensions static array with size
template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>, template <typename T, size_t N, typename Helper = details::SpanArrayTraits<T, N>,
typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value> typename = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], value_type(*)[]>::value>
> >
constexpr span(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{size}) constexpr span(T(&arr)[N], size_type size) : span(arr, typename Helper::bounds_type{size})
{ {
fail_fast_assert(size <= N); fail_fast_assert(size <= N);
} }
// from std array // from std array
template <size_t N, template <size_t N,
typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value> typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, bounds_type>::value>
> >
constexpr span (std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>()) constexpr span (std::array<std::remove_const_t<value_type>, N> & arr) : span(arr.data(), static_bounds<N>())
{} {}
template <size_t N, template <size_t N,
typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, typename Base::bounds_type>::value typename Dummy = std::enable_if_t<std::is_convertible<static_bounds<N>, bounds_type>::value
&& std::is_const<value_type>::value> && std::is_const<value_type>::value>
> >
constexpr span (const std::array<std::remove_const_t<value_type>, N> & arr) : Base(arr.data(), static_bounds<N>()) constexpr span (const std::array<std::remove_const_t<value_type>, N> & arr) : span(arr.data(), static_bounds<N>())
{} {}
// from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity // from begin, end pointers. We don't provide iterator pair since no way to guarantee the contiguity
template <typename Ptr, template <typename Ptr,
typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value typename Dummy = std::enable_if_t<std::is_convertible<Ptr, pointer>::value
&& details::LessThan<Base::bounds_type::dynamic_rank, 2>::value> && details::LessThan<bounds_type::dynamic_rank, 2>::value>
> // remove literal 0 case > // remove literal 0 case
constexpr span (pointer begin, Ptr end) : Base(begin, details::newBoundsHelper<typename Base::bounds_type>(static_cast<pointer>(end) - begin)) constexpr span (pointer begin, Ptr end) : span(begin, details::newBoundsHelper<bounds_type>(static_cast<pointer>(end) - begin))
{} {}
// from containers. It must has .size() and .data() two function signatures // from containers. It must has .size() and .data() two function signatures
template <typename Cont, typename DataType = typename Cont::value_type, template <typename Cont, typename DataType = typename Cont::value_type,
typename Dummy = std::enable_if_t<!details::is_span<Cont>::value typename Dummy = std::enable_if_t<!details::is_span<Cont>::value
&& std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value && std::is_convertible<DataType (*)[], value_type (*)[]>::value
&& std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value> && std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
> >
constexpr span (Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size())) constexpr span (Cont& cont) : span(static_cast<pointer>(cont.data()), details::newBoundsHelper<bounds_type>(cont.size()))
{} {}
constexpr span(const span &) = default; constexpr span(const span &) = default;
// convertible // convertible
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, template <typename OtherValueType, std::ptrdiff_t... OtherDimensions,
typename BaseType = basic_span<ValueType, static_bounds<FirstDimension, RestDimensions...>>, typename OtherBounds = static_bounds<OtherDimensions...>,
typename OtherBaseType = basic_span<OtherValueType, static_bounds<OtherDimensions...>>, typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType, ValueType>::value && std::is_convertible<OtherBounds, bounds_type>::value>
typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
> >
constexpr span(const span<OtherValueType, OtherDimensions...> &av) constexpr span(const span<OtherValueType, OtherDimensions...>& other) noexcept
: Base(static_cast<const typename span<OtherValueType, OtherDimensions...>::Base&>(av)) : m_pdata(other.m_pdata), m_bounds(other.m_bounds)
{} {}
// reshape // reshape
// DimCount here is a workaround for a bug in MSVC 2015 // DimCount here is a workaround for a bug in MSVC 2015
template <typename... Dimensions2, size_t DimCount = sizeof...(Dimensions2), typename = std::enable_if_t<(DimCount > 0)>> template <typename... Dimensions2, size_t DimCount = sizeof...(Dimensions2), bool Enabled = (DimCount > 0), typename Dummy = std::enable_if_t<Enabled>>
constexpr span<ValueType, Dimensions2::value...> as_span(Dimensions2... dims) constexpr span<ValueType, Dimensions2::value...> as_span(Dimensions2... dims)
{ {
using BoundsType = typename span<ValueType, (Dimensions2::value)...>::bounds_type; using BoundsType = typename span<ValueType, (Dimensions2::value)...>::bounds_type;
@ -1454,18 +1304,18 @@ public:
// from bytes array // from bytes array
template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>> template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
constexpr auto as_span() const noexcept -> span<const U, (Base::bounds_type::static_size != dynamic_range ? static_cast<std::ptrdiff_t>(static_cast<size_t>(Base::bounds_type::static_size) / sizeof(U)) : dynamic_range)> constexpr auto as_span() const noexcept -> span<const U, (bounds_type::static_size != dynamic_range ? static_cast<std::ptrdiff_t>(static_cast<size_t>(bounds_type::static_size) / sizeof(U)) : dynamic_range)>
{ {
static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % static_cast<size_type>(sizeof(U)) == 0), static_assert(std::is_standard_layout<U>::value && (bounds_type::static_size == dynamic_range || bounds_type::static_size % static_cast<size_type>(sizeof(U)) == 0),
"Target type must be standard layout and its size must match the byte array size"); "Target type must be standard layout and its size must match the byte array size");
fail_fast_assert((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX); fail_fast_assert((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX);
return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) }; return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
} }
template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>> template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
constexpr auto as_span() const noexcept -> span<U, (Base::bounds_type::static_size != dynamic_range ? static_cast<ptrdiff_t>(static_cast<size_t>(Base::bounds_type::static_size) / sizeof(U)) : dynamic_range)> constexpr auto as_span() const noexcept -> span<U, (bounds_type::static_size != dynamic_range ? static_cast<ptrdiff_t>(static_cast<size_t>(bounds_type::static_size) / sizeof(U)) : dynamic_range)>
{ {
static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % static_cast<size_t>(sizeof(U)) == 0), static_assert(std::is_standard_layout<U>::value && (bounds_type::static_size == dynamic_range || bounds_type::static_size % static_cast<size_t>(sizeof(U)) == 0),
"Target type must be standard layout and its size must match the byte array size"); "Target type must be standard layout and its size must match the byte array size");
fail_fast_assert((this->bytes() % sizeof(U)) == 0); fail_fast_assert((this->bytes() % sizeof(U)) == 0);
return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) }; return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
@ -1536,30 +1386,130 @@ public:
} }
// section // section
constexpr strided_span<ValueType, rank> section(index_type origin, index_type extents) const constexpr strided_span<ValueType, rank> section(index_type origin, index_type extents) const noexcept
{ {
size_type size = this->bounds().total_size() - this->bounds().linearize(origin); size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(Base::bounds())} }; return{ &this->operator[](origin), size, strided_bounds<rank> {extents, details::make_stride(bounds())} };
} }
constexpr reference operator[](const index_type& idx) const constexpr reference operator[](const index_type& idx) const noexcept
{ {
return Base::operator[](idx); return m_pdata[m_bounds.linearize(idx)];
} }
template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>> template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
constexpr span<ValueType, RestDimensions...> operator[](size_type idx) const constexpr Ret operator[](size_type idx) const noexcept
{ {
auto ret = Base::operator[](idx); fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
return{ ret.data(), ret.bounds() }; const size_type ridx = idx * m_bounds.stride();
fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
return Ret{ m_pdata + ridx, m_bounds.slice() };
} }
using Base::operator==; constexpr bounds_type bounds() const noexcept
using Base::operator!=; {
using Base::operator<; return m_bounds;
using Base::operator<=; }
using Base::operator>;
using Base::operator>=; template <size_t Dim = 0>
constexpr size_type extent() const noexcept
{
static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
return m_bounds.template extent<Dim>();
}
constexpr size_type size() const noexcept
{
return m_bounds.size();
}
constexpr pointer data() const noexcept
{
return m_pdata;
}
constexpr operator bool() const noexcept
{
return m_pdata != nullptr;
}
constexpr iterator begin() const noexcept
{
return iterator{ this, true };
}
constexpr iterator end() const noexcept
{
return iterator{ this, false };
}
constexpr const_iterator cbegin() const noexcept
{
return const_iterator{ reinterpret_cast<const const_span*>(this), true };
}
constexpr const_iterator cend() const noexcept
{
return const_iterator{ reinterpret_cast<const const_span*>(this), false };
}
constexpr reverse_iterator rbegin() const noexcept
{
return reverse_iterator{ end() };
}
constexpr reverse_iterator rend() const noexcept
{
return reverse_iterator{ begin() };
}
constexpr const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator{ cend() };
}
constexpr const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator{ cbegin() };
}
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator== (const span<OtherValueType, OtherDimensions...> & other) const noexcept
{
return m_bounds.size() == other.m_bounds.size() &&
(m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
}
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator!= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
{
return !(*this == other);
}
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator< (const span<OtherValueType, OtherDimensions...> & other) const noexcept
{
return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
}
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator<= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
{
return !(other < *this);
}
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator> (const span<OtherValueType, OtherDimensions...> & other) const noexcept
{
return (other < *this);
}
template <typename OtherValueType, std::ptrdiff_t... OtherDimensions, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator>= (const span<OtherValueType, OtherDimensions...> & other) const noexcept
{
return !(*this < other);
}
}; };
template <typename T, std::ptrdiff_t... Dimensions> template <typename T, std::ptrdiff_t... Dimensions>
@ -1569,13 +1519,13 @@ constexpr auto as_span(T* const& ptr, dim<Dimensions>... args) -> span<std::remo
} }
template <typename T> template <typename T>
constexpr auto as_span (T* arr, std::ptrdiff_t len) -> typename details::ArrayViewArrayTraits<T, dynamic_range>::type constexpr auto as_span (T* arr, std::ptrdiff_t len) -> typename details::SpanArrayTraits<T, dynamic_range>::type
{ {
return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len}; return {reinterpret_cast<std::remove_all_extents_t<T>*>(arr), len};
} }
template <typename T, size_t N> template <typename T, size_t N>
constexpr auto as_span (T (&arr)[N]) -> typename details::ArrayViewArrayTraits<T, N>::type constexpr auto as_span (T (&arr)[N]) -> typename details::SpanArrayTraits<T, N>::type
{ {
return {arr}; return {arr};
} }
@ -1614,56 +1564,62 @@ constexpr auto as_span(Cont &&arr) -> std::enable_if_t<!details::is_span<std::de
span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete; span<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> = delete;
template <typename ValueType, size_t Rank> template <typename ValueType, size_t Rank>
class strided_span : public basic_span<ValueType, strided_bounds<Rank>> class strided_span
{ {
using Base = basic_span<ValueType, strided_bounds<Rank>>; public:
using bounds_type = strided_bounds<Rank>;
using size_type = typename bounds_type::size_type;
using index_type = typename bounds_type::index_type;
using value_type = ValueType;
using const_value_type = std::add_const_t<value_type>;
using pointer = std::add_pointer_t<value_type>;
using reference = std::add_lvalue_reference_t<value_type>;
using iterator = general_span_iterator<strided_span>;
using const_strided_span = strided_span<const_value_type, Rank>;
using const_iterator = general_span_iterator<const_strided_span>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using sliced_type = std::conditional_t<Rank == 1, value_type, strided_span<value_type, Rank-1>>;
template<typename OtherValue, size_t OtherRank> private:
pointer m_pdata;
bounds_type m_bounds;
friend iterator;
friend const_iterator;
template <typename OtherValueType, size_t OtherRank>
friend class strided_span; friend class strided_span;
public: public:
using Base::rank;
using typename Base::bounds_type;
using typename Base::size_type;
using typename Base::pointer;
using typename Base::value_type;
using typename Base::index_type;
using typename Base::iterator;
using typename Base::const_iterator;
using typename Base::reference;
// from static array of size N
template<size_type N>
strided_span(value_type(&values)[N], bounds_type bounds) : Base(values, std::move(bounds))
{
fail_fast_assert(this->bounds().total_size() <= N, "Bounds cross data boundaries");
}
// from raw data // from raw data
strided_span(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds)) constexpr strided_span(pointer ptr, size_type size, bounds_type bounds)
: m_pdata(ptr), m_bounds(std::move(bounds))
{ {
fail_fast_assert((m_bounds.size() > 0 && ptr != nullptr) || m_bounds.size() == 0);
fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries"); fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
} }
// from static array of size N
template<size_type N>
constexpr strided_span(value_type(&values)[N], bounds_type bounds) : strided_span(values, N, std::move(bounds))
{}
// from array view // from array view
template <std::ptrdiff_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>> template <std::ptrdiff_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
strided_span(span<ValueType, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds)) constexpr strided_span(span<ValueType, Dimensions...> av, bounds_type bounds) : strided_span(av.data(), av.bounds().total_size(), std::move(bounds))
{ {}
fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
}
// convertible // convertible
template <typename OtherValueType, template <typename OtherValueType,
typename BaseType = basic_span<ValueType, strided_bounds<Rank>>, typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType(*)[], value_type(*)[]>::value>
typename OtherBaseType = basic_span<OtherValueType, strided_bounds<Rank>>,
typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
> >
constexpr strided_span(const strided_span<OtherValueType, Rank> &av) : Base(static_cast<const typename strided_span<OtherValueType, Rank>::Base &>(av)) // static_cast is required constexpr strided_span(const strided_span<OtherValueType, Rank>& other)
: m_pdata(other.m_pdata), m_bounds(other.m_bounds)
{} {}
// convert from bytes // convert from bytes
template <typename OtherValueType> template <typename OtherValueType>
strided_span<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, rank> as_strided_span() const constexpr strided_span<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, Rank> as_strided_span() const
{ {
static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes"); static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type)); auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type));
@ -1672,79 +1628,188 @@ public:
return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} }; return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} };
} }
strided_span section(index_type origin, index_type extents) const constexpr strided_span section(index_type origin, index_type extents) const
{ {
size_type size = this->bounds().total_size() - this->bounds().linearize(origin); size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}}; return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(bounds())}};
} }
constexpr reference operator[](const index_type& idx) const constexpr reference operator[](const index_type& idx) const
{ {
return Base::operator[](idx); return m_pdata[m_bounds.linearize(idx)];
} }
template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>> template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
constexpr strided_span<value_type, rank-1> operator[](size_type idx) const constexpr Ret operator[](size_type idx) const
{ {
auto ret = Base::operator[](idx); fail_fast_assert(idx < m_bounds.size(), "index is out of bounds of the array");
return{ ret.data(), ret.bounds().total_size(), ret.bounds() }; const size_type ridx = idx * m_bounds.stride();
fail_fast_assert(ridx < m_bounds.total_size(), "index is out of bounds of the underlying data");
return{ m_pdata + ridx, m_bounds.slice().total_size(), m_bounds.slice() };
}
constexpr bounds_type bounds() const noexcept
{
return m_bounds;
}
template <size_t Dim = 0>
constexpr size_type extent() const noexcept
{
static_assert(Dim < Rank, "dimension should be less than Rank (dimension count starts from 0)");
return m_bounds.template extent<Dim>();
}
constexpr size_type size() const noexcept
{
return m_bounds.size();
}
constexpr pointer data() const noexcept
{
return m_pdata;
}
constexpr operator bool() const noexcept
{
return m_pdata != nullptr;
}
constexpr iterator begin() const
{
return iterator{ this, true };
}
constexpr iterator end() const
{
return iterator{ this, false };
}
constexpr const_iterator cbegin() const
{
return const_iterator{ reinterpret_cast<const const_strided_span*>(this), true };
}
constexpr const_iterator cend() const
{
return const_iterator{ reinterpret_cast<const const_strided_span*>(this), false };
}
constexpr reverse_iterator rbegin() const
{
return reverse_iterator{ end() };
}
constexpr reverse_iterator rend() const
{
return reverse_iterator{ begin() };
}
constexpr const_reverse_iterator crbegin() const
{
return const_reverse_iterator{ cend() };
}
constexpr const_reverse_iterator crend() const
{
return const_reverse_iterator{ cbegin() };
}
template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator== (const strided_span<OtherValueType, OtherRank>& other) const noexcept
{
return m_bounds.size() == other.m_bounds.size() &&
(m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin()));
}
template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator!= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
{
return !(*this == other);
}
template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator< (const strided_span<OtherValueType, OtherRank>& other) const noexcept
{
return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end());
}
template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator<= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
{
return !(other < *this);
}
template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator> (const strided_span<OtherValueType, OtherRank>& other) const noexcept
{
return (other < *this);
}
template <typename OtherValueType, std::ptrdiff_t OtherRank, typename Dummy = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>, std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator>= (const strided_span<OtherValueType, OtherRank>& other) const noexcept
{
return !(*this < other);
} }
private: private:
static index_type resize_extent(const index_type& extent, std::ptrdiff_t d) static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
{ {
fail_fast_assert(extent[rank - 1] >= d && (extent[rank-1] % d == 0), "The last dimension of the array needs to contain a multiple of new type elements"); fail_fast_assert(extent[Rank - 1] >= d && (extent[Rank-1] % d == 0), "The last dimension of the array needs to contain a multiple of new type elements");
index_type ret = extent; index_type ret = extent;
ret[rank - 1] /= d; ret[Rank - 1] /= d;
return ret; return ret;
} }
template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>> template <bool Enabled = (Rank == 1), typename Dummy = std::enable_if_t<Enabled>>
static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0) static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0)
{ {
fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); fail_fast_assert(strides[Rank - 1] == 1, "Only strided arrays with regular strides can be resized");
return strides; return strides;
} }
template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>> template <bool Enabled = (Rank > 1), typename Dummy = std::enable_if_t<Enabled>>
static index_type resize_stride(const index_type& strides, std::ptrdiff_t d) static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
{ {
fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); fail_fast_assert(strides[Rank - 1] == 1, "Only strided arrays with regular strides can be resized");
fail_fast_assert(strides[rank - 2] >= d && (strides[rank - 2] % d == 0), "The strides must have contiguous chunks of memory that can contain a multiple of new type elements"); fail_fast_assert(strides[Rank - 2] >= d && (strides[Rank - 2] % d == 0), "The strides must have contiguous chunks of memory that can contain a multiple of new type elements");
for (size_t i = rank - 1; i > 0; --i) for (size_t i = Rank - 1; i > 0; --i)
{
fail_fast_assert((strides[i - 1] >= strides[i]) && (strides[i - 1] % strides[i] == 0), "Only strided arrays with regular strides can be resized"); fail_fast_assert((strides[i - 1] >= strides[i]) && (strides[i - 1] % strides[i] == 0), "Only strided arrays with regular strides can be resized");
}
index_type ret = strides / d; index_type ret = strides / d;
ret[rank - 1] = 1; ret[Rank - 1] = 1;
return ret; return ret;
} }
}; };
template <typename ArrayView> template <class Span>
class contiguous_span_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type> class contiguous_span_iterator : public std::iterator<std::random_access_iterator_tag, typename Span::value_type>
{ {
using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>; using Base = std::iterator<std::random_access_iterator_tag, typename Span::value_type>;
public: public:
using typename Base::reference; using typename Base::reference;
using typename Base::pointer; using typename Base::pointer;
using typename Base::difference_type; using typename Base::difference_type;
private: private:
template <typename ValueType, typename Bounds> template <typename ValueType, std::ptrdiff_t FirstDimension, std::ptrdiff_t... RestDimensions>
friend class basic_span; friend class span;
pointer m_pdata; pointer m_pdata;
const ArrayView * m_validator; const Span* m_validator;
void validateThis() const void validateThis() const
{ {
fail_fast_assert(m_pdata >= m_validator->m_pdata && m_pdata < m_validator->m_pdata + m_validator->size(), "iterator is out of range of the array"); fail_fast_assert(m_pdata >= m_validator->m_pdata && m_pdata < m_validator->m_pdata + m_validator->size(), "iterator is out of range of the array");
} }
contiguous_span_iterator (const ArrayView *container, bool isbegin) : contiguous_span_iterator (const Span* container, bool isbegin) :
m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) {} m_pdata(isbegin ? container->m_pdata : container->m_pdata + container->size()), m_validator(container) {}
public: public:
reference operator*() const noexcept reference operator*() const noexcept
@ -1840,28 +1905,28 @@ public:
} }
}; };
template <typename ArrayView> template <typename Span>
contiguous_span_iterator<ArrayView> operator+(typename contiguous_span_iterator<ArrayView>::difference_type n, const contiguous_span_iterator<ArrayView>& rhs) noexcept contiguous_span_iterator<Span> operator+(typename contiguous_span_iterator<Span>::difference_type n, const contiguous_span_iterator<Span>& rhs) noexcept
{ {
return rhs + n; return rhs + n;
} }
template <typename ArrayView> template <typename Span>
class general_span_iterator : public std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type> class general_span_iterator : public std::iterator<std::random_access_iterator_tag, typename Span::value_type>
{ {
using Base = std::iterator<std::random_access_iterator_tag, typename ArrayView::value_type>; using Base = std::iterator<std::random_access_iterator_tag, typename Span::value_type>;
public: public:
using typename Base::reference; using typename Base::reference;
using typename Base::pointer; using typename Base::pointer;
using typename Base::difference_type; using typename Base::difference_type;
using typename Base::value_type; using typename Base::value_type;
private: private:
template <typename ValueType, typename Bounds> template <typename ValueType, size_t Rank>
friend class basic_span; friend class strided_span;
const ArrayView * m_container; const Span* m_container;
typename ArrayView::bounds_type::iterator m_itr; typename Span::bounds_type::iterator m_itr;
general_span_iterator(const ArrayView *container, bool isbegin) : general_span_iterator(const Span* container, bool isbegin) :
m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end()) m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
{} {}
public: public:
@ -1956,8 +2021,8 @@ public:
} }
}; };
template <typename ArrayView> template <typename Span>
general_span_iterator<ArrayView> operator+(typename general_span_iterator<ArrayView>::difference_type n, const general_span_iterator<ArrayView>& rhs) noexcept general_span_iterator<Span> operator+(typename general_span_iterator<Span>::difference_type n, const general_span_iterator<Span>& rhs) noexcept
{ {
return rhs + n; return rhs + n;
} }

View File

@ -323,7 +323,8 @@ SUITE(span_tests)
CHECK(sav[1] == 2); CHECK(sav[1] == 2);
#if _MSC_VER > 1800 #if _MSC_VER > 1800
strided_span<const int, 1> sav_c{ {src}, {2, 1} }; //strided_span<const int, 1> sav_c{ {src}, {2, 1} };
strided_span<const int, 1> sav_c{ span<const int>{src}, strided_bounds<1>{2, 1} };
#else #else
strided_span<const int, 1> sav_c{ span<const int>{src}, strided_bounds<1>{2, 1} }; strided_span<const int, 1> sav_c{ span<const int>{src}, strided_bounds<1>{2, 1} };
#endif #endif
@ -1405,7 +1406,7 @@ SUITE(span_tests)
} }
} }
TEST(custmized_span_size) TEST(customized_span_size)
{ {
double (*arr)[3][4] = new double[100][3][4]; double (*arr)[3][4] = new double[100][3][4];
span<double, dynamic_range, 3, 4> av1(arr, 10); span<double, dynamic_range, 3, 4> av1(arr, 10);