BAN: Add iterators to all containers with contiguous memory
This commit is contained in:
parent
ec22b86e00
commit
8edabaea55
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <BAN/Errors.h>
|
#include <BAN/Errors.h>
|
||||||
|
#include <BAN/Iterators.h>
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
@ -13,11 +14,18 @@ namespace BAN
|
||||||
public:
|
public:
|
||||||
using size_type = decltype(S);
|
using size_type = decltype(S);
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
|
using iterator = IteratorSimple<T, Array>;
|
||||||
|
using const_iterator = ConstIteratorSimple<T, Array>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Array();
|
Array();
|
||||||
Array(const T&);
|
Array(const T&);
|
||||||
|
|
||||||
|
iterator begin() { return iterator(m_data); }
|
||||||
|
iterator end() { return iterator(m_data + size()); }
|
||||||
|
const_iterator begin() const { return const_iterator(m_data); }
|
||||||
|
const_iterator end() const { return const_iterator(m_data + size()); }
|
||||||
|
|
||||||
const T& operator[](size_type) const;
|
const T& operator[](size_type) const;
|
||||||
T& operator[](size_type);
|
T& operator[](size_type);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <BAN/Errors.h>
|
#include <BAN/Errors.h>
|
||||||
|
#include <BAN/Iterators.h>
|
||||||
#include <BAN/Math.h>
|
#include <BAN/Math.h>
|
||||||
#include <BAN/Memory.h>
|
#include <BAN/Memory.h>
|
||||||
#include <BAN/Move.h>
|
#include <BAN/Move.h>
|
||||||
|
@ -14,6 +15,8 @@ namespace BAN
|
||||||
public:
|
public:
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
|
using iterator = IteratorSimple<T, Queue>;
|
||||||
|
using const_iterator = ConstIteratorSimple<T, Queue>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Queue() = default;
|
Queue() = default;
|
||||||
|
@ -32,6 +35,11 @@ namespace BAN
|
||||||
ErrorOr<void> reserve(size_type);
|
ErrorOr<void> reserve(size_type);
|
||||||
ErrorOr<void> shrink_to_fit();
|
ErrorOr<void> shrink_to_fit();
|
||||||
|
|
||||||
|
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); }
|
||||||
|
|
||||||
void pop();
|
void pop();
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <BAN/ForwardList.h>
|
#include <BAN/ForwardList.h>
|
||||||
#include <BAN/Formatter.h>
|
#include <BAN/Formatter.h>
|
||||||
#include <BAN/Hash.h>
|
#include <BAN/Hash.h>
|
||||||
|
#include <BAN/Iterators.h>
|
||||||
|
|
||||||
namespace BAN
|
namespace BAN
|
||||||
{
|
{
|
||||||
|
@ -11,6 +12,8 @@ namespace BAN
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
|
using iterator = IteratorSimple<char, String>;
|
||||||
|
using const_iterator = ConstIteratorSimple<char, String>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
String();
|
String();
|
||||||
|
@ -38,6 +41,11 @@ namespace BAN
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
|
const_iterator begin() const { return const_iterator(m_data); }
|
||||||
|
iterator begin() { return iterator(m_data); }
|
||||||
|
const_iterator end() const { return const_iterator(m_data + m_size); }
|
||||||
|
iterator end() { return iterator(m_data + m_size); }
|
||||||
|
|
||||||
char operator[](size_type) const;
|
char operator[](size_type) const;
|
||||||
char& operator[](size_type);
|
char& operator[](size_type);
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <BAN/ForwardList.h>
|
#include <BAN/ForwardList.h>
|
||||||
#include <BAN/Formatter.h>
|
#include <BAN/Formatter.h>
|
||||||
|
#include <BAN/Iterators.h>
|
||||||
|
|
||||||
namespace BAN
|
namespace BAN
|
||||||
{
|
{
|
||||||
|
@ -10,12 +11,16 @@ namespace BAN
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
|
using const_iterator = ConstIteratorSimple<char, StringView>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StringView();
|
StringView();
|
||||||
StringView(const String&);
|
StringView(const String&);
|
||||||
StringView(const char*, size_type = -1);
|
StringView(const char*, size_type = -1);
|
||||||
|
|
||||||
|
const_iterator begin() const { return const_iterator(m_data); }
|
||||||
|
const_iterator end() const { return const_iterator(m_data + m_size); }
|
||||||
|
|
||||||
char operator[](size_type) const;
|
char operator[](size_type) const;
|
||||||
|
|
||||||
bool operator==(const String&) const;
|
bool operator==(const String&) const;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <BAN/Errors.h>
|
#include <BAN/Errors.h>
|
||||||
|
#include <BAN/Iterators.h>
|
||||||
#include <BAN/Math.h>
|
#include <BAN/Math.h>
|
||||||
#include <BAN/Memory.h>
|
#include <BAN/Memory.h>
|
||||||
#include <BAN/Move.h>
|
#include <BAN/Move.h>
|
||||||
|
@ -8,9 +9,6 @@
|
||||||
namespace BAN
|
namespace BAN
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T, bool CONST>
|
|
||||||
class VectorIterator;
|
|
||||||
|
|
||||||
// T must be move assignable, move constructable (and copy constructable for some functions)
|
// T must be move assignable, move constructable (and copy constructable for some functions)
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Vector
|
class Vector
|
||||||
|
@ -18,8 +16,8 @@ namespace BAN
|
||||||
public:
|
public:
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
using iterator = VectorIterator<T, false>;
|
using iterator = IteratorSimple<T, Vector>;
|
||||||
using const_iterator = VectorIterator<T, true>;
|
using const_iterator = ConstIteratorSimple<T, Vector>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Vector() = default;
|
Vector() = default;
|
||||||
|
@ -40,10 +38,10 @@ namespace BAN
|
||||||
ErrorOr<void> insert(size_type, T&&);
|
ErrorOr<void> insert(size_type, T&&);
|
||||||
ErrorOr<void> insert(size_type, const T&);
|
ErrorOr<void> insert(size_type, const T&);
|
||||||
|
|
||||||
iterator begin() { return iterator (m_data); }
|
iterator begin() { return iterator(m_data); }
|
||||||
const_iterator begin() const { return const_iterator(m_data); }
|
iterator end() { return iterator(m_data + m_size); }
|
||||||
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); }
|
const_iterator end() const { return const_iterator(m_data + m_size); }
|
||||||
|
|
||||||
void pop_back();
|
void pop_back();
|
||||||
void remove(size_type);
|
void remove(size_type);
|
||||||
|
@ -79,51 +77,6 @@ namespace BAN
|
||||||
size_type m_size = 0;
|
size_type m_size = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, bool CONST>
|
|
||||||
class VectorIterator
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
using value_type = T;
|
|
||||||
using data_type = maybe_const_t<CONST, T>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
VectorIterator() = default;
|
|
||||||
template<bool C>
|
|
||||||
VectorIterator(const VectorIterator<T, C>& other, enable_if_t<C == CONST || !C>)
|
|
||||||
: m_data(other.m_data)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
VectorIterator<T, CONST>& operator++() { m_data++; return *this; }
|
|
||||||
VectorIterator<T, CONST>& operator--() { m_data--; return *this; }
|
|
||||||
VectorIterator<T, CONST> operator++(int) { auto temp = *this; ++(*this); return temp; }
|
|
||||||
VectorIterator<T, CONST> operator--(int) { auto temp = *this; --(*this); return temp; }
|
|
||||||
|
|
||||||
template<bool ENABLE = !CONST>
|
|
||||||
enable_if_t<ENABLE, T&> operator*() { ASSERT(m_data); return *m_data; }
|
|
||||||
const T& operator*() const { ASSERT(m_data); return *m_data; }
|
|
||||||
|
|
||||||
template<bool ENABLE = !CONST>
|
|
||||||
enable_if_t<ENABLE, T*> operator->() { ASSERT(m_data); return m_data; }
|
|
||||||
const T* operator->() const { ASSERT(m_data); return m_data; }
|
|
||||||
|
|
||||||
bool operator==(const VectorIterator<T, CONST>& other) const { return m_data == other.m_data; }
|
|
||||||
bool operator!=(const VectorIterator<T, CONST>& other) const { return !(*this == other); }
|
|
||||||
|
|
||||||
operator bool() const { return m_data; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
VectorIterator(data_type* data) : m_data(data) { }
|
|
||||||
|
|
||||||
private:
|
|
||||||
data_type* m_data = nullptr;
|
|
||||||
|
|
||||||
friend class Vector<T>;
|
|
||||||
friend class VectorIterator<T, !CONST>;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Vector<T>::Vector(Vector<T>&& other)
|
Vector<T>::Vector(Vector<T>&& other)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue