mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 15:01:49 +00:00
add util::span
This commit is contained in:
parent
53b89cdb8e
commit
4b19c29a29
1 changed files with 42 additions and 0 deletions
42
src/util/span.hpp
Normal file
42
src/util/span.hpp
Normal 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;
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue