add util::span

This commit is contained in:
MihailRis 2025-10-13 23:34:56 +03:00
parent 53b89cdb8e
commit 4b19c29a29

42
src/util/span.hpp Normal file
View File

@ -0,0 +1,42 @@
#pragma once
#include <stdexcept>
namespace util {
template <typename T>
class span {
public:
constexpr span(const T* ptr, size_t length)
: ptr(ptr), length(length) {}
const T& operator[](size_t index) const {
return ptr[index];
}
const T& at(size_t index) const {
if (index >= length) {
throw std::out_of_range();
}
return ptr[index];
}
auto begin() const {
return ptr;
}
auto end() const {
return ptr + length;
}
const T* data() const {
return ptr;
}
size_t size() const {
return length;
}
private:
const T* ptr;
size_t length;
};
}