Skip to content
Snippets Groups Projects
Commit 7745b0e9 authored by patavirt's avatar patavirt
Browse files

array: add StoredArray

parent a88f5a04
No related branches found
No related tags found
No related merge requests found
......@@ -106,6 +106,21 @@ namespace detail
using type = typename TailNth<I-1, typename Shape::Tail>::type;
};
template <typename Shape>
inline size_t size_from_shape(const std::array<size_t, Shape::ndim>& shape)
{
size_t total = 1;
for (size_t i = 0; i < Shape::ndim; ++i) {
if (Shape::dim(i) == Dynamic)
total *= shape[i];
else
total *= Shape::dim(i);
}
return total;
}
/**
* \class fixed_base
*
......@@ -227,7 +242,7 @@ namespace detail
template <typename Scalar, typename Shape, size_t Alignment=0, bool BoundsCheck=true>
class Array : public detail::Base<Shape, BoundsCheck>
{
private:
protected:
typedef detail::Base<Shape, BoundsCheck> Base;
Scalar *data_;
......@@ -252,27 +267,28 @@ private:
}
};
public:
Array(Scalar *data, size_t size, const std::array<size_t, Shape::ndim> shape)
: Base(shape), data_(data)
void data_check(size_t size)
{
static_assert(!Shape::fixed, "array shape must not be compile-time fixed");
Base::data_bounds_check(size);
if constexpr(BoundsCheck) {
if (reinterpret_cast<intptr_t>(data) % alignment() != 0)
if (reinterpret_cast<intptr_t>(data_) % alignment() != 0)
throw std::runtime_error("data is not aligned");
}
};
public:
Array(Scalar *data, size_t size, const std::array<size_t, Shape::ndim> shape)
: Base(shape), data_(data)
{
static_assert(!Shape::fixed, "array shape must not be compile-time fixed");
data_check(size);
}
Array(Scalar *data, size_t size)
: Base(), data_(data)
{
static_assert(Shape::fixed, "array shape must be compile-time fixed");
Base::data_bounds_check(size);
if constexpr(BoundsCheck) {
if (reinterpret_cast<intptr_t>(data) % alignment() != 0)
throw std::runtime_error("data is not aligned");
}
data_check(size);
}
template <typename... Idx>
......@@ -357,7 +373,7 @@ public:
}
typedef Eigen::Matrix<Scalar, eigen_shape<0>(), eigen_shape<1>(), Eigen::RowMajor> EigenMatrix;
typedef Eigen::Map<EigenMatrix, eigen_alignment()> EigenMap;
typedef Eigen::Map<EigenMatrix> EigenMap;
EigenMap matrix() const
{
......@@ -368,6 +384,38 @@ public:
operator EigenMap() const { return matrix(); }
};
/**
* \class StoredArray
*
* \brief Array that is also the storage for the data.
*/
template <typename Scalar, typename Shape, size_t Alignment=0, bool BoundsCheck=true>
class StoredArray : public Array<Scalar, Shape, Alignment, BoundsCheck>
{
private:
typedef Array<Scalar, Shape, Alignment, BoundsCheck> BaseType;
typedef std::vector<Scalar> Storage;
Storage storage_;
public:
StoredArray(const std::array<size_t, Shape::ndim> shape)
: BaseType(nullptr, detail::size_from_shape<Shape>(shape), shape),
storage_(detail::size_from_shape<Shape>(shape))
{
BaseType::data_ = storage_.data();
BaseType::data_check(storage_.size());
}
StoredArray()
: BaseType(nullptr, Shape::size),
storage_(Shape::size)
{
BaseType::data_ = storage_.data();
BaseType::data_check(storage_.size());
}
};
} // namespace array
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment