Fixed issues in strided_array_views, added tests

This commit is contained in:
Anna Gringauze 2015-08-30 23:30:15 -07:00
parent a2a6a1fc52
commit 3b87190336
3 changed files with 1089 additions and 131 deletions

View File

@ -81,10 +81,14 @@ namespace details
using value_type = ValueType;
static const unsigned int rank = Rank;
_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.");
for (unsigned int i = 0; i < rank; ++i)
elems[i] = {};
elems[i] = values[i];
}
_CONSTEXPR coordinate_facade(value_type e0) _NOEXCEPT
{
@ -96,7 +100,7 @@ namespace details
_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.");
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)
{
elems[i] = begin(il)[i];
@ -119,13 +123,13 @@ namespace details
// Preconditions: component_idx < rank
_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];
}
// Preconditions: component_idx < rank
_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];
}
_CONSTEXPR bool operator==(const ConcreteType& rhs) const _NOEXCEPT
@ -230,7 +234,7 @@ namespace details
elems[i] /= v;
return to_concrete();
}
value_type elems[rank];
value_type elems[rank] = {};
private:
_CONSTEXPR const ConcreteType& to_concrete() const _NOEXCEPT
{
@ -266,7 +270,8 @@ class index : private details::coordinate_facade<index<Rank, ValueType>, ValueTy
{
using Base = details::coordinate_facade<index<Rank, ValueType>, ValueType, Rank>;
friend Base;
template <unsigned int OtherRank, typename OtherValueType>
friend class index;
public:
using Base::rank;
using reference = typename Base::reference;
@ -274,9 +279,8 @@ public:
using size_type = typename Base::value_type;
using value_type = typename Base::value_type;
_CONSTEXPR index() _NOEXCEPT : Base(){}
template <bool Enabled = rank == 1, typename = std::enable_if_t<Enabled>>
_CONSTEXPR index(value_type e0) _NOEXCEPT : Base(e0){}
_CONSTEXPR index(std::initializer_list<value_type> il) : Base(il){}
_CONSTEXPR index(const value_type (&values)[rank]) _NOEXCEPT : Base(values) {}
_CONSTEXPR index(std::initializer_list<value_type> il) : Base(il) {}
_CONSTEXPR index(const index &) = default;
@ -284,6 +288,10 @@ public:
_CONSTEXPR index(const index<Rank, OtherValueType> &other) : Base(other)
{
}
_CONSTEXPR static index shift_left(const index<rank+1, value_type>& other) _NOEXCEPT
{
return (value_type(&)[rank])other.elems[1];
}
using Base::operator[];
using Base::operator==;
@ -318,10 +326,13 @@ public:
_CONSTEXPR index(value_type e0) _NOEXCEPT : value(e0)
{
}
_CONSTEXPR index(const value_type(&values)[1]) _NOEXCEPT : index(values[0])
{
}
// Preconditions: il.size() == rank
_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];
}
@ -334,18 +345,21 @@ public:
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
_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);
return value;
}
// Preconditions: component_idx < rank
_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);
return value;
}
@ -645,7 +659,7 @@ namespace details
template <typename T, unsigned int Dim = 0>
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);
}
@ -782,7 +796,8 @@ public:
_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;
@ -808,6 +823,11 @@ public:
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
{
return m_ranges.linearize(idx);
@ -826,6 +846,7 @@ public:
template <unsigned int Dim = 0>
_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();
}
@ -866,12 +887,9 @@ class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>,
{
using Base = details::coordinate_facade<strided_bounds<Rank>, SizeType, Rank>;
friend Base;
_CONSTEXPR void makeRegularStriae() _NOEXCEPT
{
strides[rank - 1] = 1;
for (int i = rank - 2; i >= 0; i--)
strides[i] = strides[i + 1] * Base::elems[i + 1];
}
template <unsigned int OtherRank, typename OtherSizeType>
friend class strided_bounds;
public:
using Base::rank;
using reference = typename Base::reference;
@ -886,37 +904,40 @@ public:
static const size_t static_size = dynamic_range;
using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
using mapping_type = generalized_mapping_tag;
_CONSTEXPR strided_bounds() _NOEXCEPT : Base(), strides() {}
_CONSTEXPR strided_bounds(const strided_bounds &) = default;
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)
: strides(stride)
_CONSTEXPR strided_bounds(const index_type &extents, const index_type &strides)
: m_strides(strides)
{
for (unsigned int i = 0; i < rank; i++)
Base::elems[i] = extents[i];
}
_CONSTEXPR strided_bounds(std::initializer_list<value_type> il)
: Base(il)
_CONSTEXPR strided_bounds(const value_type(&values)[rank], index_type strides)
: 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 size_type size() const _NOEXCEPT
_CONSTEXPR index_type strides() const _NOEXCEPT
{
return m_strides;
}
_CONSTEXPR size_type total_size() const _NOEXCEPT
{
size_type ret = 0;
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;
}
_CONSTEXPR bool contains(const index_type& idx) const _NOEXCEPT
@ -933,32 +954,29 @@ public:
size_type ret = 0;
for (unsigned int i = 0; i < rank; i++)
{
fail_fast_assert(idx[i] < Base::elems[i]);
ret += idx[i] * strides[i];
fail_fast_assert(idx[i] < Base::elems[i], "index is out of bounds of the array");
ret += idx[i] * m_strides[i];
}
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
{
sliced_type ret;
for (unsigned int i = 1; i < rank; ++i)
{
ret.elems[i - 1] = Base::elems[i];
ret.strides[i - 1] = strides[i];
}
return ret;
return{ (value_type(&)[rank - 1])Base::elems[1], sliced_type::index_type::shift_left(m_strides) };
}
template <unsigned int Dim = 0>
_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];
}
_CONSTEXPR index_type index_bounds() const _NOEXCEPT
{
index_type extents;
for (unsigned int i = 0; i < rank; ++i)
extents[i] = (*this)[i];
return extents;
return index_type(Base::elems);
}
const_iterator begin() const _NOEXCEPT
{
@ -968,6 +986,8 @@ public:
{
return const_iterator{ *this, index_bounds() };
}
private:
index_type m_strides;
};
template <typename T>
@ -1296,7 +1316,7 @@ namespace details
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
{
return bnd.strides;
return bnd.strides();
}
// Make a stride vector from bounds, assuming continugous memory.
@ -1358,6 +1378,7 @@ public:
template <unsigned int 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
@ -1372,11 +1393,13 @@ public:
{
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
{
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.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()};
}
@ -1422,39 +1445,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>>
_CONSTEXPR bool operator== (const basic_array_view<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()));
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_array_view<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_array_view<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_array_view<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_array_view<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_array_view<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_array_view<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_array_view<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_array_view<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_array_view<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_array_view<OtherValueType, OtherBoundsType> & other) const _NOEXCEPT
{
return !(*this < other);
}
public:
template <typename OtherValueType, typename OtherBounds,
@ -1614,7 +1637,7 @@ class array_view : public basic_array_view<typename details::ArrayViewTypeTraits
template <typename ValueTypeOpt2, size_t FirstDimension2, size_t... RestDimensions2>
friend class array_view;
using Base = basic_array_view<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type,
static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions...>>;
static_bounds<typename details::ArrayViewTypeTraits<ValueTypeOpt>::size_type, FirstDimension, RestDimensions... >>;
public:
using typename Base::bounds_type;
@ -1622,6 +1645,8 @@ public:
using typename Base::pointer;
using typename Base::value_type;
using typename Base::index_type;
using typename Base::iterator;
using typename Base::const_iterator;
using Base::rank;
public:
@ -1647,25 +1672,25 @@ public:
// 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, size_type, dynamic_range>,
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>>
_CONSTEXPR array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{size})
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 >>
_CONSTEXPR array_view(T * const & data, size_type size) : Base(data, typename Helper::bounds_type{ size })
{
}
// from n-dimensions static array
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
&& std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value>>
_CONSTEXPR array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
&& std::is_convertible<typename Helper::bounds_type, typename Base::bounds_type>::value >>
_CONSTEXPR array_view(T(&arr)[N]) : Base(arr, typename Helper::bounds_type())
{
}
// from n-dimensions static array with size
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
&& 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 })
&& 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 })
{
fail_fast_assert(size <= N);
}
@ -1693,11 +1718,11 @@ public:
// 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,
typename Dummy = std::enable_if_t<!details::is_array_view<Cont>::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_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::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_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()))
{
}
@ -1720,7 +1745,7 @@ public:
using BoundsType = typename array_view<ValueTypeOpt, (Dimensions2::value)...>::bounds_type;
auto tobounds = details::static_as_array_view_helper<BoundsType>(dims..., details::Sep{});
details::verifyBoundsReshape(this->bounds(), tobounds);
return {this->data(), tobounds};
return{ this->data(), tobounds };
}
// to bytes array
@ -1729,7 +1754,7 @@ public:
array_view<array_view_options<const byte, size_type>, static_cast<size_t>(details::StaticSizeHelper<size_type, Base::bounds_type::static_size, sizeof(value_type)>::value)>
{
static_assert(Enabled, "The value_type of array_view must be standarded layout");
return { reinterpret_cast<const byte*>(this->data()), this->bytes() };
return{ reinterpret_cast<const byte*>(this->data()), this->bytes() };
}
template <bool Enabled = std::is_standard_layout<std::decay_t<typename details::ArrayViewTypeTraits<ValueTypeOpt>::value_type>>::value>
@ -1737,10 +1762,9 @@ public:
array_view<array_view_options<byte, size_type>, static_cast<size_t>(details::StaticSizeHelper<size_type, Base::bounds_type::static_size, sizeof(value_type)>::value)>
{
static_assert(Enabled, "The value_type of array_view must be standarded layout");
return { reinterpret_cast<byte*>(this->data()), this->bytes() };
return{ reinterpret_cast<byte*>(this->data()), this->bytes() };
}
// from bytes array
template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
_CONSTEXPR auto as_array_view() const _NOEXCEPT -> array_view<const U, (Base::bounds_type::dynamic_rank == 0 ? Base::bounds_type::static_size / sizeof(U) : static_cast<size_type>(dynamic_range))>
@ -1748,7 +1772,7 @@ public:
static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
"Target type must be standard layout and its size must match the byte array size");
fail_fast_assert((this->bytes() % sizeof(U)) == 0);
return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
return{ reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) };
}
template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
@ -1757,7 +1781,7 @@ public:
static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0),
"Target type must be standard layout and its size must match the byte array size");
fail_fast_assert((this->bytes() % sizeof(U)) == 0);
return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
return{ reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) };
}
// section on linear space
@ -1766,13 +1790,13 @@ public:
{
static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size()); // ensures we only check condition when needed
return { this->data(), Count };
return{ this->data(), Count };
}
_CONSTEXPR array_view<ValueTypeOpt, dynamic_range> first(size_type count) const _NOEXCEPT
{
fail_fast_assert(count <= this->size());
return { this->data(), count };
return{ this->data(), count };
}
template<size_t Count>
@ -1780,13 +1804,13 @@ public:
{
static_assert(bounds_type::static_size == dynamic_range || Count <= bounds_type::static_size, "Index is out of bound");
fail_fast_assert(bounds_type::static_size != dynamic_range || Count <= this->size());
return { this->data() + this->size() - Count, Count };
return{ this->data() + this->size() - Count, Count };
}
_CONSTEXPR array_view<ValueTypeOpt, dynamic_range> last(size_type count) const _NOEXCEPT
{
fail_fast_assert(count <= this->size());
return { this->data() + this->size() - count, count };
return{ this->data() + this->size() - count, count };
}
template<size_t Offset, size_t Count>
@ -1794,13 +1818,13 @@ public:
{
static_assert(bounds_type::static_size == dynamic_range || ((Offset == 0 || Offset < bounds_type::static_size) && Offset + Count <= bounds_type::static_size), "Index is out of bound");
fail_fast_assert(bounds_type::static_size != dynamic_range || ((Offset == 0 || Offset < this->size()) && Offset + Count <= this->size()));
return { this->data() + Offset, Count };
return{ this->data() + Offset, Count };
}
_CONSTEXPR array_view<ValueTypeOpt, dynamic_range> sub(size_type offset, size_type count) const _NOEXCEPT
{
fail_fast_assert((offset == 0 || offset < this->size()) && offset + count <= this->size());
return { this->data() + offset, count };
return{ this->data() + offset, count };
}
// size
@ -1824,15 +1848,26 @@ public:
// section
_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 = bounds().total_size() - bounds().linearize(origin);
return{ &this->operator[](origin), size, strided_bounds<rank, size_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 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>=;
using Base::operator==;
using Base::operator!=;
using Base::operator<;
using Base::operator<=;
using Base::operator>;
using Base::operator>=;
};
template <typename T, size_t... Dimensions>
@ -1889,6 +1924,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>>
{
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:
using Base::rank;
using typename Base::bounds_type;
@ -1896,18 +1934,102 @@ public:
using typename Base::pointer;
using typename Base::value_type;
using typename Base::index_type;
using typename Base::iterator;
using typename Base::const_iterator;
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>>
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, typename Dummy = std::enable_if_t<std::is_same<value_type, const byte>::value>>
strided_array_view<OtherValueType, 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 = bounds().total_size() / d;
return{ (OtherValueType*)data(), size, bounds_type{ resize_extent(bounds().index_bounds(), d), resize_stride(bounds().strides(), d)} };
}
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 = bounds().total_size() - 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;
}
};
@ -2043,7 +2165,7 @@ private:
template <typename ValueType, typename Bounds>
friend class basic_array_view;
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) :
m_container(container), m_itr(isbegin ? m_container->bounds().begin() : m_container->bounds().end())
{

View File

@ -27,12 +27,22 @@ namespace Guide
//
#if defined(SAFER_CPP_TESTING)
struct fail_fast : public std::exception {};
struct fail_fast : public std::exception
{
fail_fast() = default;
explicit fail_fast(char const* const message) :
std::exception(message)
{}
};
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
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

View File

@ -265,6 +265,835 @@ SUITE(array_view_tests)
auto subsub = sub.section({1, 0, 0}, Guide::index<3>{1, 1, 1});
}
TEST(array_view_section)
{
std::vector<int> data(5 * 10);
std::iota(begin(data), end(data), 0);
const array_view<int, 5, 10> av = as_array_view(data).as_array_view(dim<5>(), dim<10>());
strided_array_view<int, 2> av_section_1 = av.section({ 1, 2 }, { 3, 4 });
CHECK((av_section_1[{0, 0}] == 12));
CHECK((av_section_1[{0, 1}] == 13));
CHECK((av_section_1[{1, 0}] == 22));
CHECK((av_section_1[{2, 3}] == 35));
strided_array_view<int, 2> av_section_2 = av_section_1.section({ 1, 2 }, { 2,2 });
CHECK((av_section_2[{0, 0}] == 24));
CHECK((av_section_2[{0, 1}] == 25));
CHECK((av_section_2[{1, 0}] == 34));
}
TEST(strided_array_view_constructors)
{
// Check stride constructor
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const int carr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
strided_array_view<int, 1> sav1{ arr, {{9}, {1}} }; // T -> T
CHECK(sav1.bounds().index_bounds() == index<1>{ 9 });
CHECK(sav1.bounds().stride() == 1);
CHECK(sav1[0] == 1 && sav1[8] == 9);
strided_array_view<const int, 1> sav2{ carr, {{ 4 }, { 2 }} }; // const T -> const T
CHECK(sav2.bounds().index_bounds() == index<1>{ 4 });
CHECK(sav2.bounds().strides() == index<1>{2});
CHECK(sav2[0] == 1 && sav2[3] == 7);
strided_array_view<int, 2> sav3{ arr, {{ 2, 2 },{ 6, 2 }} }; // T -> const T
CHECK((sav3.bounds().index_bounds() == index<2>{ 2, 2 }));
CHECK((sav3.bounds().strides() == index<2>{ 6, 2 }));
CHECK((sav3[{0, 0}] == 1 && sav3[{0, 1}] == 3 && sav3[{1, 0}] == 7));
}
// Check array_view constructor
{
int arr[] = { 1, 2 };
// From non-cv-qualified source
{
const array_view<int> src{ arr };
strided_array_view<int, 1> sav{ src, {2, 1} };
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav.bounds().strides() == index<1>{ 1 });
CHECK(sav[1] == 2);
strided_array_view<const int, 1> sav_c{ {src}, {2, 1} };
CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav_c.bounds().strides() == index<1>{ 1 });
CHECK(sav_c[1] == 2);
strided_array_view<volatile int, 1> sav_v{ {src}, {2, 1} };
CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav_v.bounds().strides() == index<1>{ 1 });
CHECK(sav_v[1] == 2);
strided_array_view<const volatile int, 1> sav_cv{ {src}, {2, 1} };
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
CHECK(sav_cv[1] == 2);
}
// From const-qualified source
{
const array_view<const int> src{ arr };
strided_array_view<const int, 1> sav_c{ src, {2, 1} };
CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav_c.bounds().strides() == index<1>{ 1 });
CHECK(sav_c[1] == 2);
strided_array_view<const volatile int, 1> sav_cv{ {src}, {2, 1} };
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
CHECK(sav_cv[1] == 2);
}
// From volatile-qualified source
{
const array_view<volatile int> src{ arr };
strided_array_view<volatile int, 1> sav_v{ src, {2, 1} };
CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav_v.bounds().strides() == index<1>{ 1 });
CHECK(sav_v[1] == 2);
strided_array_view<const volatile int, 1> sav_cv{ {src}, {2, 1} };
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
CHECK(sav_cv[1] == 2);
}
// From cv-qualified source
{
const array_view<const volatile int> src{ arr };
strided_array_view<const volatile int, 1> sav_cv{ src, {2, 1} };
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
CHECK(sav_cv[1] == 2);
}
}
// Check const-casting constructor
{
int arr[2] = { 4, 5 };
const array_view<int, 2> av(arr, 2);
array_view<const int, 2> av2{ av };
CHECK(av2[1] == 5);
static_assert(std::is_convertible<const array_view<int, 2>, array_view<const int, 2>>::value, "ctor is not implicit!");
const strided_array_view<int, 1> src{ arr, {2, 1} };
strided_array_view<const int, 1> sav{ src };
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav.bounds().stride() == 1);
CHECK(sav[1] == 5);
static_assert(std::is_convertible<const strided_array_view<int, 1>, strided_array_view<const int, 1>>::value, "ctor is not implicit!");
}
// Check copy constructor
{
int arr1[2] = { 3, 4 };
const strided_array_view<int, 1> src1{ arr1, {2, 1} };
strided_array_view<int, 1> sav1{ src1 };
CHECK(sav1.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav1.bounds().stride() == 1);
CHECK(sav1[0] == 3);
int arr2[6] = { 1, 2, 3, 4, 5, 6 };
const strided_array_view<const int, 2> src2{ arr2, {{ 3, 2 }, { 2, 1 }} };
strided_array_view<const int, 2> sav2{ src2 };
CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 }));
CHECK((sav2.bounds().strides() == index<2>{ 2, 1 }));
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
}
// Check const-casting assignment operator
{
int arr1[2] = { 1, 2 };
int arr2[6] = { 3, 4, 5, 6, 7, 8 };
const strided_array_view<int, 1> src{ arr1, {{2}, {1}} };
strided_array_view<const int, 1> sav{ arr2, {{3}, {2}} };
strided_array_view<const int, 1>& sav_ref = (sav = src);
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav.bounds().strides() == index<1>{ 1 });
CHECK(sav[0] == 1);
CHECK(&sav_ref == &sav);
}
// Check copy assignment operator
{
int arr1[2] = { 3, 4 };
int arr1b[1] = { 0 };
const strided_array_view<int, 1> src1{ arr1, {2, 1} };
strided_array_view<int, 1> sav1{ arr1b, {1, 1} };
strided_array_view<int, 1>& sav1_ref = (sav1 = src1);
CHECK(sav1.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav1.bounds().strides() == index<1>{ 1 });
CHECK(sav1[0] == 3);
CHECK(&sav1_ref == &sav1);
const int arr2[6] = { 1, 2, 3, 4, 5, 6 };
const int arr2b[1] = { 0 };
const strided_array_view<const int, 2> src2{ arr2, {{ 3, 2 },{ 2, 1 }} };
strided_array_view<const int, 2> sav2{ arr2b, {{ 1, 1 },{ 1, 1 }} };
strided_array_view<const int, 2>& sav2_ref = (sav2 = src2);
CHECK((sav2.bounds().index_bounds() == index<2>{ 3, 2 }));
CHECK((sav2.bounds().strides() == index<2>{ 2, 1 }));
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
CHECK(&sav2_ref == &sav2);
}
}
TEST(strided_array_view_slice)
{
std::vector<int> data(5 * 10);
std::iota(begin(data), end(data), 0);
const array_view<int, 5, 10> src = as_array_view(data).as_array_view(dim<5>(), dim<10>());
const strided_array_view<int, 2> sav{ src, {{5, 10}, {10, 1}} };
#ifdef CONFIRM_COMPILATION_ERRORS
const strided_array_view<const int, 2> csav{ {src},{ { 5, 10 },{ 10, 1 } } };
#endif
const strided_array_view<const int, 2> csav{ array_view<const int, 5, 10>{ src }, { { 5, 10 },{ 10, 1 } } };
strided_array_view<int, 1> sav_sl = sav[2];
CHECK(sav_sl[0] == 20);
CHECK(sav_sl[9] == 29);
strided_array_view<const int, 1> csav_sl = sav[3];
CHECK(csav_sl[0] == 30);
CHECK(csav_sl[9] == 39);
CHECK(sav[4][0] == 40);
CHECK(sav[4][9] == 49);
}
TEST(strided_array_view_column_major)
{
// strided_array_view may be used to accomodate more peculiar
// use cases, such as column-major multidimensional array
// (aka. "FORTRAN" layout).
int cm_array[3 * 5] = {
1, 4, 7, 10, 13,
2, 5, 8, 11, 14,
3, 6, 9, 12, 15
};
strided_array_view<int, 2> cm_sav{ cm_array, {{ 5, 3 },{ 1, 5 }} };
// Accessing elements
CHECK((cm_sav[{0, 0}] == 1));
CHECK((cm_sav[{0, 1}] == 2));
CHECK((cm_sav[{1, 0}] == 4));
CHECK((cm_sav[{4, 2}] == 15));
// Slice
strided_array_view<int, 1> cm_sl = cm_sav[3];
CHECK(cm_sl[0] == 10);
CHECK(cm_sl[1] == 11);
CHECK(cm_sl[2] == 12);
// Section
strided_array_view<int, 2> cm_sec = cm_sav.section( { 2, 1 }, { 3, 2 });
CHECK((cm_sec.bounds().index_bounds() == index<2>{3, 2}));
CHECK((cm_sec[{0, 0}] == 8));
CHECK((cm_sec[{0, 1}] == 9));
CHECK((cm_sec[{1, 0}] == 11));
CHECK((cm_sec[{2, 1}] == 15));
}
TEST(strided_array_view_bounds)
{
int arr[] = { 0, 1, 2, 3 };
array_view<int> av(arr);
{
// incorrect sections
CHECK_THROW(av.section(0, 0)[0], fail_fast);
CHECK_THROW(av.section(1, 0)[0], fail_fast);
CHECK_THROW(av.section(1, 1)[1], fail_fast);
CHECK_THROW(av.section(2, 5), fail_fast);
CHECK_THROW(av.section(5, 2), fail_fast);
CHECK_THROW(av.section(5, 0), fail_fast);
CHECK_THROW(av.section(0, 5), fail_fast);
CHECK_THROW(av.section(5, 5), fail_fast);
}
{
// zero stride
strided_array_view<int, 1> sav{ av, {{4}, {}} };
CHECK(sav[0] == 0);
CHECK(sav[3] == 0);
CHECK_THROW(sav[4], fail_fast);
}
{
// zero extent
strided_array_view<int, 1> sav{ av,{ {},{1} } };
CHECK_THROW(sav[0], fail_fast);
}
{
// zero extent and stride
strided_array_view<int, 1> sav{ av,{ {},{} } };
CHECK_THROW(sav[0], fail_fast);
}
{
// strided array ctor with matching strided bounds
strided_array_view<int, 1> sav{ arr,{ 4, 1 } };
CHECK(sav.bounds().index_bounds() == index<1>{ 4 });
CHECK(sav[3] == 3);
CHECK_THROW(sav[4], fail_fast);
}
{
// strided array ctor with smaller strided bounds
strided_array_view<int, 1> sav{ arr,{ 2, 1 } };
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav[1] == 1);
CHECK_THROW(sav[2], fail_fast);
}
{
// strided array ctor with fitting irregular bounds
strided_array_view<int, 1> sav{ arr,{ 2, 3 } };
CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
CHECK(sav[0] == 0);
CHECK(sav[1] == 3);
CHECK_THROW(sav[2], fail_fast);
}
{
// bounds cross data boundaries - from static arrays
CHECK_THROW((strided_array_view<int, 1> { arr, { 3, 2 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { arr, { 3, 3 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { arr, { 4, 5 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { arr, { 5, 1 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { arr, { 5, 5 } }), fail_fast);
}
{
// bounds cross data boundaries - from array view
CHECK_THROW((strided_array_view<int, 1> { av, { 3, 2 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av, { 3, 3 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av, { 4, 5 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av, { 5, 1 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av, { 5, 5 } }), fail_fast);
}
{
// bounds cross data boundaries - from dynamic arrays
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 3, 2 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 3, 3 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 4, 5 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 5, 1 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av.data(), 4, { 5, 5 } }), fail_fast);
CHECK_THROW((strided_array_view<int, 1> { av.data(), 2, { 2, 2 } }), fail_fast);
}
#ifdef CONFIRM_COMPILATION_ERRORS
{
strided_array_view<int, 1> sav0{ av.data(), { 3, 2 } };
strided_array_view<int, 1> sav1{ arr, { 1 } };
strided_array_view<int, 1> sav2{ arr, { 1,1,1 } };
strided_array_view<int, 1> sav3{ av, { 1 } };
strided_array_view<int, 1> sav4{ av, { 1,1,1 } };
strided_array_view<int, 2> sav5{ av.as_array_view(dim<2>(), dim<2>()), { 1 } };
strided_array_view<int, 2> sav6{ av.as_array_view(dim<2>(), dim<2>()), { 1,1,1 } };
strided_array_view<int, 2> sav7{ av.as_array_view(dim<2>(), dim<2>()), { { 1,1 },{ 1,1 },{ 1,1 } } };
}
#endif
{
// stride initializer list size should match the rank of the array
CHECK_THROW((index<1>{ 0,1 }), fail_fast);
CHECK_THROW((strided_array_view<int, 1>{ arr, {1, {1,1}} }), fail_fast);
CHECK_THROW((strided_array_view<int, 1>{ arr, {{1,1 }, {1,1}} }), fail_fast);
CHECK_THROW((strided_array_view<int, 1>{ av, {1, {1,1}} }), fail_fast);
CHECK_THROW((strided_array_view<int, 1>{ av, {{1,1 }, {1,1}} }), fail_fast);
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1}, {1}} }), fail_fast);
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1}, {1,1,1}} }), fail_fast);
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1,1,1}, {1}} }), fail_fast);
}
}
TEST(strided_array_view_type_conversion)
{
int arr[] = { 0, 1, 2, 3 };
array_view<int> av(arr);
{
strided_array_view<int, 1> sav{ av.data(), av.size(), { av.size() / 2, 2 } };
#ifdef CONFIRM_COMPILATION_ERRORS
strided_array_view<long, 1> lsav1 = sav.as_strided_array_view<long, 1>();
#endif
}
{
strided_array_view<int, 1> sav{ av, { av.size() / 2, 2 } };
#ifdef CONFIRM_COMPILATION_ERRORS
strided_array_view<long, 1> lsav1 = sav.as_strided_array_view<long, 1>();
#endif
}
array_view<const byte, dynamic_range> bytes = av.as_bytes();
// retype strided array with regular strides - from raw data
{
strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } };
strided_array_view<const byte, 2> sav2{ bytes.data(), bytes.size(), bounds };
strided_array_view<const int, 2> sav3 = sav2.as_strided_array_view<const int>();
CHECK(sav3[0][0] == 0);
CHECK(sav3[1][0] == 2);
CHECK_THROW(sav3[1][1], fail_fast);
CHECK_THROW(sav3[0][1], fail_fast);
}
// retype strided array with regular strides - from array_view
{
strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } };
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
strided_array_view<int, 2> sav3 = sav2.as_strided_array_view<int>();
CHECK(sav3[0][0] == 0);
CHECK(sav3[1][0] == 2);
CHECK_THROW(sav3[1][1], fail_fast);
CHECK_THROW(sav3[0][1], fail_fast);
}
// retype strided array with not enough elements - last dimension of the array is too small
{
strided_bounds<2> bounds{ { 4,2 },{ 4, 1 } };
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
}
// retype strided array with not enough elements - strides are too small
{
strided_bounds<2> bounds{ { 4,2 },{ 2, 1 } };
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
}
// retype strided array with not enough elements - last dimension does not divide by the new typesize
{
strided_bounds<2> bounds{ { 2,6 },{ 4, 1 } };
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
}
// retype strided array with not enough elements - strides does not divide by the new typesize
{
strided_bounds<2> bounds{ { 2, 1 },{ 6, 1 } };
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
}
// retype strided array with irregular strides - from raw data
{
strided_bounds<1> bounds{ bytes.size() / 2, 2 };
strided_array_view<const byte, 1> sav2{ bytes.data(), bytes.size(), bounds };
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
}
// retype strided array with irregular strides - from array_view
{
strided_bounds<1> bounds{ bytes.size() / 2, 2 };
strided_array_view<const byte, 1> sav2{ bytes, bounds };
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
}
}
TEST(empty_arrays)
{
#ifdef CONFIRM_COMPILATION_ERRORS
{
array_view<int, 1> empty;
strided_array_view<int, 2> empty2;
strided_array_view<int, 1> empty3{ nullptr,{ 0, 1 } };
}
#endif
array_view<int, 0> empty_av(nullptr);
CHECK(empty_av.bounds().index_bounds() == index<1>{ 0 });
CHECK_THROW(empty_av[0], fail_fast);
strided_array_view<int, 1> empty_sav{ empty_av, { 0, 1 } };
CHECK(empty_sav.bounds().index_bounds() == index<1>{ 0 });
CHECK_THROW(empty_sav[0], fail_fast);
strided_array_view<int, 1> empty_sav2{ nullptr, 0, { 0, 1 } };
CHECK(empty_sav.bounds().index_bounds() == index<1>{ 0 });
CHECK_THROW(empty_sav[0], fail_fast);
}
TEST(index_constructor)
{
auto arr = new int[8];
for (int i = 0; i < 4; ++i)
{
arr[2 * i] = 4 + i;
arr[2 * i + 1] = i;
}
array_view<int, dynamic_range> av(arr, 8);
unsigned int a[1] = { 0 };
index<1> i = index<1>(a);
CHECK(av[i] == 4);
auto av2 = av.as_array_view(dim<4>(), dim<>(2));
unsigned int a2[2] = { 0, 1 };
index<2> i2 = index<2>(a2);
CHECK(av2[i2] == 0);
CHECK(av2[0][i] == 4);
delete[] arr;
}
TEST(index_operations)
{
unsigned int a[3] = { 0, 1, 2 };
unsigned int b[3] = { 3, 4, 5 };
index<3> i = a;
index<3> j = b;
CHECK(i[0] == 0);
CHECK(i[1] == 1);
CHECK(i[2] == 2);
{
index<3> k = i + j;
CHECK(i[0] == 0);
CHECK(i[1] == 1);
CHECK(i[2] == 2);
CHECK(k[0] == 3);
CHECK(k[1] == 5);
CHECK(k[2] == 7);
}
{
index<3> k = i * 3;
CHECK(i[0] == 0);
CHECK(i[1] == 1);
CHECK(i[2] == 2);
CHECK(k[0] == 0);
CHECK(k[1] == 3);
CHECK(k[2] == 6);
}
{
index<2> k = index<2>::shift_left(i);
CHECK(i[0] == 0);
CHECK(i[1] == 1);
CHECK(i[2] == 2);
CHECK(k[0] == 1);
CHECK(k[1] == 2);
}
}
void iterate_second_column(array_view<int, dynamic_range, dynamic_range> av)
{
auto length = av.size() / 2;
// view to the second column
auto section = av.section({ 0,1 }, { length,1 });
CHECK(section.size() == length);
for (unsigned int i = 0; i < section.size(); ++i)
{
CHECK(section[i][0] == av[i][1]);
}
for (unsigned int i = 0; i < section.size(); ++i)
{
CHECK(section[index<2>({ i,0 })] == av[i][1]);
}
CHECK(section.bounds().index_bounds()[0] == length);
CHECK(section.bounds().index_bounds()[1] == 1);
for (unsigned int i = 0; i < section.bounds().index_bounds()[0]; ++i)
{
for (unsigned int j = 0; j < section.bounds().index_bounds()[1]; ++j)
{
CHECK(section[index<2>({ i,j })] == av[i][1]);
}
}
unsigned int idx = 0;
for (auto num : section)
{
CHECK(num == av[idx][1]);
idx++;
}
}
TEST(array_view_section_iteration)
{
int arr[4][2] = { { 4,0 },{ 5,1 },{ 6,2 },{ 7,3 } };
// static bounds
{
array_view<int, 4, 2> av = arr;
iterate_second_column(av);
}
// first bound is dynamic
{
array_view<int, dynamic_range, 2> av = arr;
iterate_second_column(av);
}
// second bound is dynamic
{
array_view<int, 4, dynamic_range> av = arr;
iterate_second_column(av);
}
// both bounds are dynamic
{
array_view<int, dynamic_range, dynamic_range> av(arr, 4);
iterate_second_column(av);
}
}
TEST(dynamic_array_view_section_iteration)
{
unsigned int height = 4, width = 2;
unsigned int size = height * width;
auto arr = new int[size];
for (int unsigned i = 0; i < size; ++i)
{
arr[i] = i;
}
auto av = as_array_view(arr, size);
// first bound is dynamic
{
array_view<int, dynamic_range, 2> av2 = av.as_array_view(dim<>(height), dim<>(width));
iterate_second_column(av2);
}
// second bound is dynamic
{
array_view<int, 4, dynamic_range> av2 = av.as_array_view(dim<>(height), dim<>(width));
iterate_second_column(av2);
}
// both bounds are dynamic
{
array_view<int, dynamic_range, dynamic_range> av2 = av.as_array_view(dim<>(height), dim<>(width));
iterate_second_column(av2);
}
delete[] arr;
}
void iterate_every_other_element(array_view<int, dynamic_range> av)
{
// pick every other element
auto length = av.size() / 2;
auto bounds = strided_bounds<1>({ length }, { 2 });
strided_array_view<int, 1> strided(&av.data()[1], av.size() - 1, bounds);
CHECK(strided.size() == length);
CHECK(strided.bounds().index_bounds()[0] == length);
for (unsigned int i = 0; i < strided.size(); ++i)
{
CHECK(strided[i] == av[2 * i + 1]);
}
int idx = 0;
for (auto num : strided)
{
CHECK(num == av[2 * idx + 1]);
idx++;
}
}
TEST(strided_array_view_section_iteration)
{
int arr[8] = {4,0,5,1,6,2,7,3};
// static bounds
{
array_view<int, 8> av(arr, 8);
iterate_every_other_element(av);
}
// dynamic bounds
{
array_view<int, dynamic_range> av(arr, 8);
iterate_every_other_element(av);
}
}
TEST(dynamic_strided_array_view_section_iteration)
{
auto arr = new int[8];
for (int i = 0; i < 4; ++i)
{
arr[2 * i] = 4 + i;
arr[2 * i + 1] = i;
}
auto av = as_array_view(arr, 8);
iterate_every_other_element(av);
delete[] arr;
}
void iterate_second_slice(array_view<int, dynamic_range, dynamic_range, dynamic_range> av)
{
int expected[6] = { 2,3,10,11,18,19 };
auto section = av.section({ 0,1,0 }, { 3,1,2 });
for (unsigned int i = 0; i < section.extent<0>(); ++i)
{
for (unsigned int j = 0; j < section.extent<1>(); ++j)
for (unsigned int k = 0; k < section.extent<2>(); ++k)
CHECK(section[index<3>({ i,j,k })] == expected[2 * i + 2 * j + k]);
}
for (unsigned int i = 0; i < section.extent<0>(); ++i)
{
for (unsigned int j = 0; j < section.extent<1>(); ++j)
for (unsigned int k = 0; k < section.extent<2>(); ++k)
CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]);
}
int i = 0;
for (auto num : section)
{
CHECK(num == expected[i]);
i++;
}
}
TEST(strided_array_view_section_iteration_3d)
{
int arr[3][4][2];
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 4; ++j)
for (unsigned int k = 0; k < 2; ++k)
arr[i][j][k] = 8 * i + 2 * j + k;
}
{
array_view<int, 3, 4, 2> av = arr;
iterate_second_slice(av);
}
}
TEST(dynamic_strided_array_view_section_iteration_3d)
{
unsigned int height = 12, width = 2;
unsigned int size = height * width;
auto arr = new int[size];
for (int unsigned i = 0; i < size; ++i)
{
arr[i] = i;
}
{
auto av = as_array_view(arr, 24).as_array_view(dim<3>(),dim<4>(),dim<2>());
iterate_second_slice(av);
}
{
auto av = as_array_view(arr, 24).as_array_view(dim<>(3), dim<4>(), dim<2>());
iterate_second_slice(av);
}
{
auto av = as_array_view(arr, 24).as_array_view(dim<3>(), dim<>(4), dim<2>());
iterate_second_slice(av);
}
{
auto av = as_array_view(arr, 24).as_array_view(dim<3>(), dim<4>(), dim<>(2));
iterate_second_slice(av);
}
delete[] arr;
}
TEST(strided_array_view_conversion)
{
// get an array_view of 'c' values from the list of X's
struct X { int a; int b; int c; };
X arr[4] = { { 0,1,2 },{ 3,4,5 },{ 6,7,8 },{ 9,10,11 } };
auto s = sizeof(int) / sizeof(byte);
auto d2 = 3 * s;
auto d1 = sizeof(int) * 12 / d2;
// convert to 4x12 array of bytes
auto av = as_array_view(arr, 4).as_bytes().as_array_view(dim<>(d1), dim<>(d2));
CHECK(av.bounds().index_bounds()[0] == 4);
CHECK(av.bounds().index_bounds()[1] == 12);
// get the last 4 columns
auto section = av.section({ 0, 2 * s }, { 4, s }); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2], arr[0].c[3] } , { arr[1].c[0], ... } , ... }
// convert to array 4x1 array of integers
auto cs = section.as_strided_array_view<int>(); // { { arr[0].c }, {arr[1].c } , ... }
CHECK(cs.bounds().index_bounds()[0] == 4);
CHECK(cs.bounds().index_bounds()[1] == 1);
// transpose to 1x4 array
strided_bounds<2> reverse_bounds{
{ cs.bounds().index_bounds()[1] , cs.bounds().index_bounds()[0] },
{ cs.bounds().strides()[1], cs.bounds().strides()[0] }
};
strided_array_view<int, 2> transposed{ cs.data(), cs.bounds().total_size(), reverse_bounds };
// slice to get a one-dimensional array of c's
strided_array_view<int, 1> result = transposed[0];
CHECK(result.bounds().index_bounds()[0] == 4);
CHECK_THROW(result.bounds().index_bounds()[1], fail_fast);
int i = 0;
for (auto& num : result)
{
CHECK(num == arr[i].c);
i++;
}
}
TEST(constructors)
{
@ -303,8 +1132,6 @@ SUITE(array_view_tests)
DerivedClass *p = nullptr;
array_view<BaseClass> av11(p, 0);
#endif
}
TEST(copyandassignment)
@ -627,7 +1454,6 @@ SUITE(array_view_tests)
}
TEST(ArrayViewComparison)
{
{