You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.7 KiB
79 lines
1.7 KiB
// Copyright 2022 Simon Boyé
|
|
#pragma once
|
|
|
|
|
|
#include <core/types.h>
|
|
|
|
|
|
template<typename T>
|
|
class ArrayView {
|
|
public:
|
|
ArrayView() = default;
|
|
ArrayView(const ArrayView&) = default;
|
|
ArrayView(ArrayView&&) = default;
|
|
~ArrayView() = default;
|
|
|
|
template<typename U>
|
|
ArrayView(Index size, U* data, Index stride=sizeof(T), Index byte_offset=0)
|
|
: m_data(reinterpret_cast<Byte*>(data) + byte_offset)
|
|
, m_size(size)
|
|
, m_stride(stride)
|
|
{
|
|
assert(m_data != nullptr || m_size == 0);
|
|
}
|
|
|
|
explicit ArrayView(std::vector<T>& vector)
|
|
: m_data(vector.data())
|
|
, m_data(vector.size())
|
|
{}
|
|
|
|
ArrayView& operator=(const ArrayView&) = default;
|
|
ArrayView& operator=(ArrayView&&) = default;
|
|
|
|
explicit operator bool() const {
|
|
return m_size != 0;
|
|
}
|
|
|
|
Index size() const {
|
|
return m_size;
|
|
}
|
|
|
|
Index stride() const {
|
|
return m_stride;
|
|
}
|
|
|
|
const T* data() const {
|
|
return m_data;
|
|
}
|
|
|
|
T* data() {
|
|
return m_data;
|
|
}
|
|
|
|
const T& operator[](Index index) const {
|
|
assert(index < m_size);
|
|
return *reinterpret_cast<T*>(m_data + index * m_stride);
|
|
}
|
|
|
|
T& operator[](Index index) {
|
|
return const_cast<T&>(const_cast<const ArrayView&>(*this)[index]);
|
|
}
|
|
|
|
ArrayView slice(Index start, Index count, Index step=1) {
|
|
assert(step > 0);
|
|
assert(start + count * step <= m_size);
|
|
return ArrayView(
|
|
count,
|
|
m_data + start * m_stride,
|
|
m_stride * step
|
|
);
|
|
}
|
|
|
|
private:
|
|
Byte* m_data = nullptr;
|
|
Index m_size = 0;
|
|
Index m_stride = sizeof(T);
|
|
};
|
|
|
|
using Vector3AV = ArrayView<Vector3>;
|
|
using TriangleAV = ArrayView<Triangle>;
|
|
|