Select Git revision
MeshRandomizer.hpp
ShrinkVectorView.hpp 1.38 KiB
#ifndef SHRINK_VECTOR_VIEW_HPP
#define SHRINK_VECTOR_VIEW_HPP
#include <utils/PugsAssert.hpp>
#include <utils/PugsMacros.hpp>
#include <cstddef>
#include <iostream>
#include <utils/NaNHelper.hpp>
template <typename VectorType>
class ShrinkVectorView
{
public:
using index_type = typename VectorType::index_type;
using data_type = typename VectorType::data_type;
private:
VectorType& m_vector;
const size_t m_dimension;
public:
friend std::ostream&
operator<<(std::ostream& os, const ShrinkVectorView& x)
{
if (x.size() > 0) {
os << 0 << ':' << NaNHelper(x[0]);
}
for (size_t i = 1; i < x.size(); ++i) {
os << ' ' << i << ':' << NaNHelper(x[i]);
}
return os;
}
PUGS_INLINE size_t
dimension() const noexcept
{
return m_dimension;
}
PUGS_INLINE
data_type&
operator[](index_type i) const noexcept(NO_ASSERT)
{
Assert(i < m_dimension, "cannot access element: invalid indices");
return m_vector[i];
}
ShrinkVectorView(VectorType& vector, size_t dimension) noexcept(NO_ASSERT) : m_vector{vector}, m_dimension{dimension}
{
Assert(m_dimension <= vector.dimension(), "shrink number of rows must be smaller than original vector's");
}
ShrinkVectorView(const ShrinkVectorView&) = delete;
ShrinkVectorView(ShrinkVectorView&&) = delete;
~ShrinkVectorView() noexcept = default;
};
#endif // SHRINK_VECTOR_VIEW_HPP