Merge pull request #7 from annagrin/dev/annagrin/strided_array_view_bugfixes

Fixes to strided_array_view and related classes.
This commit is contained in:
Neil MacIntosh 2015-09-14 22:27:48 -07:00
commit 652d886963
3 changed files with 1254 additions and 210 deletions

View File

@ -52,6 +52,11 @@
#endif // _NOEXCEPT #endif // _NOEXCEPT
#if _MSC_VER <= 1800
#pragma warning(push)
#pragma warning(disable: 4351) // warns about newly introduced aggregate initializer behavior
#endif // _MSC_VER <= 1800
namespace Guide { namespace Guide {
/* /*
@ -81,10 +86,14 @@ namespace details
using value_type = ValueType; using value_type = ValueType;
static const unsigned int rank = Rank; static const unsigned int rank = Rank;
_CONSTEXPR coordinate_facade() _NOEXCEPT _CONSTEXPR coordinate_facade() _NOEXCEPT
{
static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
}
_CONSTEXPR coordinate_facade(const value_type(&values)[rank]) _NOEXCEPT
{ {
static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade."); static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
for (unsigned int i = 0; i < rank; ++i) for (unsigned int i = 0; i < rank; ++i)
elems[i] = {}; elems[i] = values[i];
} }
_CONSTEXPR coordinate_facade(value_type e0) _NOEXCEPT _CONSTEXPR coordinate_facade(value_type e0) _NOEXCEPT
{ {
@ -96,7 +105,7 @@ namespace details
_CONSTEXPR coordinate_facade(std::initializer_list<value_type> il) _CONSTEXPR coordinate_facade(std::initializer_list<value_type> il)
{ {
static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade."); static_assert(std::is_base_of<coordinate_facade, ConcreteType>::value, "ConcreteType must be derived from coordinate_facade.");
fail_fast_assert(il.size() == rank); fail_fast_assert(il.size() == rank, "The size of the initializer list must match the rank of the array");
for (unsigned int i = 0; i < rank; ++i) for (unsigned int i = 0; i < rank; ++i)
{ {
elems[i] = begin(il)[i]; elems[i] = begin(il)[i];
@ -119,13 +128,13 @@ namespace details
// Preconditions: component_idx < rank // Preconditions: component_idx < rank
_CONSTEXPR reference operator[](unsigned int component_idx) _CONSTEXPR reference operator[](unsigned int component_idx)
{ {
fail_fast_assert(component_idx < rank); fail_fast_assert(component_idx < rank, "Component index must be less than rank");
return elems[component_idx]; return elems[component_idx];
} }
// Preconditions: component_idx < rank // Preconditions: component_idx < rank
_CONSTEXPR const_reference operator[](unsigned int component_idx) const _CONSTEXPR const_reference operator[](unsigned int component_idx) const
{ {
fail_fast_assert(component_idx < rank); fail_fast_assert(component_idx < rank, "Component index must be less than rank");
return elems[component_idx]; return elems[component_idx];
} }
_CONSTEXPR bool operator==(const ConcreteType& rhs) const _NOEXCEPT _CONSTEXPR bool operator==(const ConcreteType& rhs) const _NOEXCEPT
@ -230,7 +239,7 @@ namespace details
elems[i] /= v; elems[i] /= v;
return to_concrete(); return to_concrete();
} }
value_type elems[rank]; value_type elems[rank] = {};
private: private:
_CONSTEXPR const ConcreteType& to_concrete() const _NOEXCEPT _CONSTEXPR const ConcreteType& to_concrete() const _NOEXCEPT
{ {
@ -266,7 +275,8 @@ class index : private details::coordinate_facade<index<Rank, ValueType>, ValueTy
{ {
using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>; using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>;
friend Base; friend Base;
template <unsigned int OtherRank, typename OtherValueType>
friend class index;
public: public:
using Base::rank; using Base::rank;
using reference = typename Base::reference; using reference = typename Base::reference;
@ -274,9 +284,8 @@ public:
using size_type = typename Base::value_type; using size_type = typename Base::value_type;
using value_type = typename Base::value_type; using value_type = typename Base::value_type;
_CONSTEXPR index() _NOEXCEPT : Base(){} _CONSTEXPR index() _NOEXCEPT : Base(){}
template <bool Enabled = rank == 1, typename = std::enable_if_t<Enabled>> _CONSTEXPR index(const value_type (&values)[rank]) _NOEXCEPT : Base(values) {}
_CONSTEXPR index(value_type e0) _NOEXCEPT : Base(e0){} _CONSTEXPR index(std::initializer_list<value_type> il) : Base(il) {}
_CONSTEXPR index(std::initializer_list<value_type> il) : Base(il){}
_CONSTEXPR index(const index &) = default; _CONSTEXPR index(const index &) = default;
@ -284,6 +293,11 @@ public:
_CONSTEXPR index(const index<Rank, OtherValueType> &other) : Base(other) _CONSTEXPR index(const index<Rank, OtherValueType> &other) : Base(other)
{ {
} }
_CONSTEXPR static index shift_left(const index<rank+1, value_type>& other) _NOEXCEPT
{
value_type (&arr)[rank] = (value_type(&)[rank])(*(other.elems + 1));
return index(arr);
}
using Base::operator[]; using Base::operator[];
using Base::operator==; using Base::operator==;
@ -318,10 +332,13 @@ public:
_CONSTEXPR index(value_type e0) _NOEXCEPT : value(e0) _CONSTEXPR index(value_type e0) _NOEXCEPT : value(e0)
{ {
} }
_CONSTEXPR index(const value_type(&values)[1]) _NOEXCEPT : index(values[0])
{
}
// Preconditions: il.size() == rank // Preconditions: il.size() == rank
_CONSTEXPR index(std::initializer_list<value_type> il) _CONSTEXPR index(std::initializer_list<value_type> il)
{ {
fail_fast_assert(il.size() == rank); fail_fast_assert(il.size() == rank, "Size of the initializer list must match the rank of the array");
value = begin(il)[0]; value = begin(il)[0];
} }
@ -334,18 +351,21 @@ public:
value = static_cast<ValueType>(other.value); value = static_cast<ValueType>(other.value);
} }
_CONSTEXPR static index shift_left(const index<rank + 1, value_type>& other) _NOEXCEPT
{
return other.elems[1];
}
// Preconditions: component_idx < rank // Preconditions: component_idx < rank
_CONSTEXPR reference operator[](size_type component_idx) _NOEXCEPT _CONSTEXPR reference operator[](size_type component_idx) _NOEXCEPT
{ {
fail_fast_assert(component_idx == 0); fail_fast_assert(component_idx == 0, "Component index must be less than rank");
(void)(component_idx); (void)(component_idx);
return value; return value;
} }
// Preconditions: component_idx < rank // Preconditions: component_idx < rank
_CONSTEXPR const_reference operator[](size_type component_idx) const _NOEXCEPT _CONSTEXPR const_reference operator[](size_type component_idx) const _NOEXCEPT
{ {
fail_fast_assert(component_idx == 0); fail_fast_assert(component_idx == 0, "Component index must be less than rank");
(void)(component_idx); (void)(component_idx);
return value; return value;
} }
@ -645,7 +665,7 @@ namespace details
template <typename T, unsigned int Dim = 0> template <typename T, unsigned int Dim = 0>
SizeType linearize(const T & arr) const { SizeType linearize(const T & arr) const {
fail_fast_assert(arr[Dim] < CurrentRange); fail_fast_assert(arr[Dim] < CurrentRange, "Index is out of range");
return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr); return static_cast<SizeType>(this->Base::totalSize()) * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr);
} }
@ -782,7 +802,8 @@ public:
_CONSTEXPR static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin()) _CONSTEXPR static_bounds(std::initializer_list<size_type> il) : m_ranges(il.begin())
{ {
fail_fast_assert(MyRanges::DynamicNum == il.size() && m_ranges.totalSize() <= details::SizeTypeTraits<size_type>::max_value); fail_fast_assert(MyRanges::DynamicNum == il.size(), "Size of the initializer list must match the rank of the array");
fail_fast_assert(m_ranges.totalSize() <= details::SizeTypeTraits<size_type>::max_value, "Size of the range is larger than the max element of the size type");
} }
_CONSTEXPR static_bounds() = default; _CONSTEXPR static_bounds() = default;
@ -808,6 +829,11 @@ public:
return static_cast<size_type>(m_ranges.totalSize()); return static_cast<size_type>(m_ranges.totalSize());
} }
_CONSTEXPR size_type total_size() const _NOEXCEPT
{
return static_cast<size_type>(m_ranges.totalSize());
}
_CONSTEXPR size_type linearize(const index_type & idx) const _CONSTEXPR size_type linearize(const index_type & idx) const
{ {
return m_ranges.linearize(idx); return m_ranges.linearize(idx);
@ -826,6 +852,7 @@ public:
template <unsigned int Dim = 0> template <unsigned int Dim = 0>
_CONSTEXPR size_type extent() const _NOEXCEPT _CONSTEXPR size_type extent() const _NOEXCEPT
{ {
static_assert(Dim < rank, "dimension should be less than rank (dimension count starts from 0)");
return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum(); return details::createTypeListIndexer(m_ranges).template get<Dim>().elementNum();
} }
@ -866,12 +893,9 @@ class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>,
{ {
using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>; using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
friend Base; friend Base;
_CONSTEXPR void makeRegularStriae() _NOEXCEPT template <unsigned int OtherRank, typename OtherSizeType>
{ friend class strided_bounds;
strides[rank - 1] = 1;
for (int i = rank - 2; i >= 0; i--)
strides[i] = strides[i + 1] * Base::elems[i + 1];
}
public: public:
using Base::rank; using Base::rank;
using reference = typename Base::reference; using reference = typename Base::reference;
@ -886,37 +910,40 @@ public:
static const size_t static_size = dynamic_range; static const size_t static_size = dynamic_range;
using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>; using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
using mapping_type = generalized_mapping_tag; using mapping_type = generalized_mapping_tag;
_CONSTEXPR strided_bounds() _NOEXCEPT : Base(), strides() {}
_CONSTEXPR strided_bounds(const strided_bounds &) = default; _CONSTEXPR strided_bounds(const strided_bounds &) = default;
template <typename OtherSizeType> template <typename OtherSizeType>
_CONSTEXPR strided_bounds(const strided_bounds<rank, OtherSizeType> &other) : Base(other) _CONSTEXPR strided_bounds(const strided_bounds<rank, OtherSizeType> &other)
: Base(other), m_strides(other.strides)
{ {
} }
_CONSTEXPR strided_bounds(const index_type &extents, const index_type &stride) _CONSTEXPR strided_bounds(const index_type &extents, const index_type &strides)
: strides(stride) : m_strides(strides)
{ {
for (unsigned int i = 0; i < rank; i++) for (unsigned int i = 0; i < rank; i++)
Base::elems[i] = extents[i]; Base::elems[i] = extents[i];
} }
_CONSTEXPR strided_bounds(std::initializer_list<value_type> il) _CONSTEXPR strided_bounds(const value_type(&values)[rank], index_type strides)
: Base(il) : Base(values), m_strides(std::move(strides))
{ {
#ifndef NDEBUG
for (const auto& v : il)
{
fail_fast_assert(v >= 0);
}
#endif
makeRegularStriae();
} }
index_type strides; _CONSTEXPR index_type strides() const _NOEXCEPT
_CONSTEXPR size_type size() const _NOEXCEPT {
return m_strides;
}
_CONSTEXPR size_type total_size() const _NOEXCEPT
{ {
size_type ret = 0; size_type ret = 0;
for (unsigned int i = 0; i < rank; ++i) for (unsigned int i = 0; i < rank; ++i)
ret += (Base::elems[i] - 1) * strides[i]; ret += (Base::elems[i] - 1) * m_strides[i];
return ret + 1;
}
_CONSTEXPR size_type size() const _NOEXCEPT
{
size_type ret = 1;
for (unsigned int i = 0; i < rank; ++i)
ret *= Base::elems[i];
return ret; return ret;
} }
_CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT _CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
@ -933,32 +960,29 @@ public:
size_type ret = 0; size_type ret = 0;
for (unsigned int i = 0; i < rank; i++) for (unsigned int i = 0; i < rank; i++)
{ {
fail_fast_assert(idx[i] < Base::elems[i]); fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array");
ret += idx[i] * strides[i]; ret += idx[i] * m_strides[i];
} }
return ret; return ret;
} }
_CONSTEXPR size_type stride() const _NOEXCEPT
{
return m_strides[0];
}
template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
_CONSTEXPR sliced_type slice() const _CONSTEXPR sliced_type slice() const
{ {
sliced_type ret; return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) };
for (unsigned int i = 1; i < rank; ++i)
{
ret.elems[i - 1] = Base::elems[i];
ret.strides[i - 1] = strides[i];
}
return ret;
} }
template <unsigned int Dim = 0> template <unsigned int Dim = 0>
_CONSTEXPR size_type extent() const _NOEXCEPT _CONSTEXPR size_type extent() const _NOEXCEPT
{ {
static_assert(Dim < Rank, "dimension should be less than rank (dimension count starts from 0)");
return Base::elems[Dim]; return Base::elems[Dim];
} }
_CONSTEXPR index_type index_bounds() const _NOEXCEPT _CONSTEXPR index_type index_bounds() const _NOEXCEPT
{ {
index_type extents; return index_type(Base::elems);
for (unsigned int i = 0; i < rank; ++i)
extents[i] = (*this)[i];
return extents;
} }
const_iterator begin() const _NOEXCEPT const_iterator begin() const _NOEXCEPT
{ {
@ -968,6 +992,8 @@ public:
{ {
return const_iterator{ *this, index_bounds() }; return const_iterator{ *this, index_bounds() };
} }
private:
index_type m_strides;
}; };
template <typename T> template <typename T>
@ -1296,7 +1322,7 @@ namespace details
template <typename Bounds> template <typename Bounds>
_CONSTEXPR std::enable_if_t<std::is_same<typename Bounds::mapping_type, generalized_mapping_tag>::value, typename Bounds::index_type> make_stride(const Bounds& bnd) _NOEXCEPT _CONSTEXPR std::enable_if_t<std::is_same<typename Bounds::mapping_type, generalized_mapping_tag>::value, typename Bounds::index_type> make_stride(const Bounds& bnd) _NOEXCEPT
{ {
return bnd.strides; return bnd.strides();
} }
// Make a stride vector from bounds, assuming continugous memory. // Make a stride vector from bounds, assuming continugous memory.
@ -1358,6 +1384,7 @@ public:
template <unsigned int Dim = 0> template <unsigned int Dim = 0>
_CONSTEXPR size_type extent() const _NOEXCEPT _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>(); return m_bounds.template extent<Dim>();
} }
_CONSTEXPR size_type size() const _NOEXCEPT _CONSTEXPR size_type size() const _NOEXCEPT
@ -1372,11 +1399,13 @@ public:
{ {
return m_pdata; return m_pdata;
} }
template <bool Enabled = rank != 1, typename Ret = std::enable_if_t<Enabled, sliced_type>> template <bool Enabled = (rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>>
_CONSTEXPR Ret operator[](size_type idx) const _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(); const size_type ridx = idx * m_bounds.stride();
fail_fast_assert(ridx < m_bounds.size());
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()}; return Ret {m_pdata + ridx, m_bounds.slice()};
} }
@ -1422,39 +1451,39 @@ public:
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>> 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_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT _CONSTEXPR bool operator== (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
{ {
return m_bounds.size() == other.m_bounds.size() && return m_bounds.size() == other.m_bounds.size() &&
(m_pdata == other.m_pdata || std::equal(this->begin(), this->end(), other.begin())); (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>> 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_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT _CONSTEXPR bool operator!= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
{ {
return !(*this == other); 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>> 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_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT _CONSTEXPR bool operator< (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
{ {
return std::lexicographical_compare(this->begin(), this->end(), other.begin(), other.end()); 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>> 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_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT _CONSTEXPR bool operator<= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
{ {
return !(other < *this); 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>> 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_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT _CONSTEXPR bool operator> (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
{ {
return (other < *this); 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>> 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_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT _CONSTEXPR bool operator>= (const basic_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
{ {
return !(*this < other); return !(*this < other);
} }
public: public:
template <typename OtherValueType, typename OtherBounds, template <typename OtherValueType, typename OtherBounds,
@ -1622,6 +1651,9 @@ public:
using typename Base::pointer; using typename Base::pointer;
using typename Base::value_type; using typename Base::value_type;
using typename Base::index_type; using typename Base::index_type;
using typename Base::iterator;
using typename Base::const_iterator;
using typename Base::reference;
using Base::rank; using Base::rank;
public: public:
@ -1649,14 +1681,14 @@ public:
template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>, template <typename T, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type (*)[], typename Base::value_type (*)[]>::value
&& std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>> && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
_CONSTEXPR array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size}) _CONSTEXPR array_view(T * const & data, size_type size) : Base(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, size_type, N>, template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, N>,
typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value
&& std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>> && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
_CONSTEXPR array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type()) _CONSTEXPR array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
{ {
} }
@ -1664,8 +1696,8 @@ public:
// 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, size_type, dynamic_range>, template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, size_type, dynamic_range>,
typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value
&& std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >> && std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
_CONSTEXPR array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size }) _CONSTEXPR array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
{ {
fail_fast_assert(size <= N); fail_fast_assert(size <= N);
} }
@ -1693,9 +1725,9 @@ public:
// 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, typename SizeType = typename Cont::size_type, template <typename Cont, typename DataType = typename Cont::value_type, typename SizeType = typename Cont::size_type,
typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::value
&& std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value && std::is_convertible<DataType (*)[], typename Base::value_type (*)[]>::value
&& std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_type>::value && std::is_convertible<static_bounds<SizeType, dynamic_range>, typename Base::bounds_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 array_view (Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size())) _CONSTEXPR array_view (Cont& cont) : Base(static_cast<pointer>(cont.data()), details::newBoundsHelper<typename Base::bounds_type>(cont.size()))
{ {
@ -1824,15 +1856,28 @@ public:
// section // section
_CONSTEXPR strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const _CONSTEXPR strided_array_view<ValueTypeOpt, rank> section(index_type origin, index_type extents) const
{ {
return { &this->operator[](origin), strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())}}; size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
return{ &this->operator[](origin), size, strided_bounds<rank, size_type> {extents, details::make_stride(Base::bounds())} };
} }
using Base::operator==; _CONSTEXPR reference operator[](const index_type& idx) const
using Base::operator!=; {
using Base::operator<; return Base::operator[](idx);
using Base::operator<=; }
using Base::operator>;
using Base::operator>=; template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
_CONSTEXPR array_view<ValueTypeOpt, RestDimensions...> operator[](size_type idx) const
{
auto ret = Base::operator[](idx);
return{ ret.data(), ret.bounds() };
}
using Base::operator==;
using Base::operator!=;
using Base::operator<;
using Base::operator<=;
using Base::operator>;
using Base::operator>=;
}; };
template <typename T, size_t... Dimensions> template <typename T, size_t... Dimensions>
@ -1889,6 +1934,9 @@ template <typename ValueTypeOpt, unsigned int Rank>
class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>> class strided_array_view : public basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>
{ {
using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>; using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>;
template<typename OtherValueOpt, unsigned int OtherRank>
friend class strided_array_view;
public: public:
using Base::rank; using Base::rank;
using typename Base::bounds_type; using typename Base::bounds_type;
@ -1896,18 +1944,103 @@ public:
using typename Base::pointer; using typename Base::pointer;
using typename Base::value_type; using typename Base::value_type;
using typename Base::index_type; using typename Base::index_type;
using typename Base::iterator;
using typename Base::const_iterator;
using typename Base::reference;
strided_array_view (pointer ptr, bounds_type bounds): Base(ptr, std::move(bounds)) // from static array of size N
template<size_type N>
strided_array_view(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
strided_array_view(pointer ptr, size_type size, bounds_type bounds): Base(ptr, std::move(bounds))
{
fail_fast_assert(this->bounds().total_size() <= size, "Bounds cross data boundaries");
}
// from array view
template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>> template <size_t... Dimensions, typename Dummy = std::enable_if<sizeof...(Dimensions) == Rank>>
strided_array_view (array_view<ValueTypeOpt, Dimensions...> av, index_type strides): Base(av.data(), bounds_type{av.bounds().index_bounds(), strides}) strided_array_view(array_view<ValueTypeOpt, Dimensions...> av, bounds_type bounds) : Base(av.data(), std::move(bounds))
{
fail_fast_assert(this->bounds().total_size() <= av.bounds().total_size(), "Bounds cross data boundaries");
}
// convertible
template <typename OtherValueTypeOpt,
typename BaseType = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type>>,
typename OtherBaseType = basic_array_view<typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::value_type, strided_bounds<Rank, typename details::ArrayViewTypeTraits<OtherValueTypeOpt>::size_type>>,
typename Dummy = std::enable_if_t<std::is_convertible<OtherBaseType, BaseType>::value>
>
_CONSTEXPR strided_array_view(const strided_array_view<OtherValueTypeOpt, Rank> &av): Base(static_cast<const typename strided_array_view<OtherValueTypeOpt, Rank>::Base &>(av)) // static_cast is required
{ {
} }
// section
// convert from bytes
template <typename OtherValueType>
strided_array_view<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, rank> as_strided_array_view() 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");
auto d = sizeof(OtherValueType) / sizeof(value_type);
size_type size = this->bounds().total_size() / d;
return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} };
}
strided_array_view section(index_type origin, index_type extents) const strided_array_view section(index_type origin, index_type extents) const
{ {
return { &this->operator[](origin), bounds_type {extents, details::make_stride(Base::bounds())}}; size_type size = this->bounds().total_size() - this->bounds().linearize(origin);
return { &this->operator[](origin), size, bounds_type {extents, details::make_stride(Base::bounds())}};
}
_CONSTEXPR reference operator[](const index_type& idx) const
{
return Base::operator[](idx);
}
template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
_CONSTEXPR strided_array_view<value_type, rank-1> operator[](size_type idx) const
{
auto ret = Base::operator[](idx);
return{ ret.data(), ret.bounds().total_size(), ret.bounds() };
}
private:
static index_type resize_extent(const index_type& extent, size_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");
index_type ret = extent;
ret[rank - 1] /= d;
return ret;
}
template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
static index_type resize_stride(const index_type& strides, size_t d, void *p = 0)
{
fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
return strides;
}
template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
static index_type resize_stride(const index_type& strides, size_t d)
{
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");
for (int i = rank - 2; i >= 0; --i)
{
fail_fast_assert((strides[i] >= strides[i + 1]) && (strides[i] % strides[i + 1] == 0), "Only strided arrays with regular strides can be resized");
}
index_type ret = strides / d;
ret[rank - 1] = 1;
return ret;
} }
}; };
@ -1926,7 +2059,7 @@ private:
const ArrayView * m_validator; const ArrayView * 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()); 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_array_view_iterator (const ArrayView *container, bool isbegin = false) : contiguous_array_view_iterator (const ArrayView *container, bool isbegin = false) :
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) { }
@ -2043,7 +2176,7 @@ private:
template <typename ValueType, typename Bounds> template <typename ValueType, typename Bounds>
friend class basic_array_view; friend class basic_array_view;
const ArrayView * m_container; const ArrayView * m_container;
typename ArrayView::iterator m_itr; typename ArrayView::bounds_type::iterator m_itr;
general_array_view_iterator(const ArrayView *container, bool isbegin = false) : general_array_view_iterator(const ArrayView *container, bool isbegin = false) :
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())
{ {
@ -2148,4 +2281,9 @@ general_array_view_iterator<ArrayView> operator+(typename general_array_view_ite
} // namespace Guide } // namespace Guide
#if _MSC_VER <= 1800
#pragma warning(pop)
#endif // _MSC_VER <= 1800
#pragma pop_macro("_NOEXCEPT") #pragma pop_macro("_NOEXCEPT")

View File

@ -27,12 +27,19 @@ namespace Guide
// //
#if defined(SAFER_CPP_TESTING) #if defined(SAFER_CPP_TESTING)
struct fail_fast : public std::exception {}; struct fail_fast : public std::runtime_error
{
fail_fast() : std::runtime_error("") {}
explicit fail_fast(char const* const message) : std::runtime_error(message) {}
};
inline void fail_fast_assert(bool cond) { if (!cond) throw fail_fast(); } inline void fail_fast_assert(bool cond) { if (!cond) throw fail_fast(); }
inline void fail_fast_assert(bool cond, const char* const message) { if (!cond) throw fail_fast(message); }
#else #else
inline void fail_fast_assert(bool cond) { if (!cond) std::terminate(); } inline void fail_fast_assert(bool cond) { if (!cond) std::terminate(); }
inline void fail_fast_assert(bool cond, const char* const message) { if (!cond) std::terminate(); }
#endif // SAFER_CPP_TESTING #endif // SAFER_CPP_TESTING

File diff suppressed because it is too large Load Diff