BAN: Implement basic Span

This is wrapper over contiguous block of memory e.g. Vector
This commit is contained in:
Bananymous 2023-03-20 13:28:01 +02:00
parent ef381d0600
commit 70f2908056
3 changed files with 120 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <BAN/Iterators.h> #include <BAN/Iterators.h>
#include <BAN/Span.h>
#include <stddef.h> #include <stddef.h>
@ -34,6 +35,9 @@ namespace BAN
const T& front() const; const T& front() const;
T& front(); T& front();
Span<T> span() { return Span(m_data, size()); }
const Span<T> span() const { return Span(m_data, size()); }
constexpr size_type size() const; constexpr size_type size() const;
private: private:

112
BAN/include/BAN/Span.h Normal file
View File

@ -0,0 +1,112 @@
#pragma once
#include <BAN/Assert.h>
#include <BAN/Iterators.h>
#include <stddef.h>
namespace BAN
{
template<typename T>
class Span
{
public:
using value_type = T;
using size_type = size_t;
using iterator = IteratorSimple<T, Span>;
using const_iterator = ConstIteratorSimple<T, Span>;
public:
Span() = default;
Span(T*, size_type);
iterator begin() { return iterator(m_data); }
iterator end() { return iterator(m_data + m_size); }
const_iterator begin() const { return const_iterator(m_data); }
const_iterator end() const { return const_iterator(m_data + m_size); }
T& operator[](size_type);
const T& operator[](size_type) const;
T* data();
const T* data() const;
bool empty() const;
size_type size() const;
void clear();
Span slice(size_type, size_type = ~size_type(0));
private:
T* m_data = nullptr;
size_type m_size = 0;
};
template<typename T>
Span<T>::Span(T* data, size_type size)
: m_data(data)
, m_size(size)
{
}
template<typename T>
T& Span<T>::operator[](size_type index)
{
ASSERT(m_data);
ASSERT(index < m_size);
return m_data[index];
}
template<typename T>
const T& Span<T>::operator[](size_type index) const
{
ASSERT(m_data);
ASSERT(index < m_size);
return m_data[index];
}
template<typename T>
T* Span<T>::data()
{
return m_data;
}
template<typename T>
const T* Span<T>::data() const
{
return m_data;
}
template<typename T>
bool Span<T>::empty() const
{
return m_size == 0;
}
template<typename T>
typename Span<T>::size_type Span<T>::size() const
{
return m_size;
}
template<typename T>
void Span<T>::clear()
{
m_data = nullptr;
m_size = 0;
}
template<typename T>
Span<T> Span<T>::slice(size_type start, size_type length)
{
ASSERT(m_data);
ASSERT(start <= m_size);
if (length == ~size_type(0))
length = m_size - start;
ASSERT(start + length <= m_size);
return Span(m_data + start, m_size - start - length);
}
}

View File

@ -5,6 +5,7 @@
#include <BAN/Math.h> #include <BAN/Math.h>
#include <BAN/Memory.h> #include <BAN/Memory.h>
#include <BAN/Move.h> #include <BAN/Move.h>
#include <BAN/Span.h>
namespace BAN namespace BAN
{ {
@ -52,6 +53,9 @@ namespace BAN
bool contains(const T&) const; bool contains(const T&) const;
Span<T> span() { return Span(m_data, m_size); }
const Span<T> span() const { return Span(m_data, m_size); }
const T& operator[](size_type) const; const T& operator[](size_type) const;
T& operator[](size_type); T& operator[](size_type);