[ox/std] Add iterator to Vector

This commit is contained in:
Gary Talent 2021-05-08 20:58:20 -05:00
parent 9a31e898d0
commit 15d8e9de5b
3 changed files with 161 additions and 0 deletions

36
deps/ox/src/ox/std/iterator.hpp vendored Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#ifndef OX_USE_STDLIB
namespace std {
struct input_iterator_tag {
};
struct output_iterator_tag {
};
struct forward_iterator_tag: public input_iterator_tag {
};
struct bidirectional_iterator_tag: public forward_iterator_tag {
};
struct random_access_iterator_tag: public bidirectional_iterator_tag {
};
struct contiguous_iterator_tag: public random_access_iterator_tag {
};
template<typename Category, typename T>
struct iterator {
using iterator_category = Category;
using value_type = T;
using pointer = T*;
using reference = T&;
};
}
#else
#include <iterator>
#endif

View File

@ -18,6 +18,7 @@
#include "hardware.hpp"
#include "hashmap.hpp"
#include "heapmgr.hpp"
#include "iterator.hpp"
#include "math.hpp"
#include "memops.hpp"
#include "memory.hpp"

View File

@ -9,6 +9,8 @@
#pragma once
#include "bit.hpp"
#include "iterator.hpp"
#include "math.hpp"
#include "new.hpp"
#include "types.hpp"
#include "utility.hpp"
@ -22,6 +24,96 @@ class Vector {
using value_type = T;
using size_type = std::size_t;
template<typename RefType, bool reverse = false>
struct iterator: public std::iterator<std::bidirectional_iterator_tag, T> {
private:
T *m_t = nullptr;
size_type m_offset = 0;
size_type m_max = 0;
public:
constexpr iterator(T *t, size_type offset, size_type max) {
m_t = t;
m_offset = offset;
m_max = max;
}
constexpr iterator operator+(size_type s) const noexcept {
if constexpr(reverse) {
return iterator(m_t, max<size_type>(m_offset - s, 0), m_max);
} else {
return iterator(m_t, min<size_type>(m_offset + s, m_max), m_max);
}
}
constexpr iterator operator-(size_type s) const noexcept {
if constexpr(reverse) {
return iterator(m_t, min<size_type>(m_offset + s, m_max), m_max);
} else {
return iterator(m_t, max<size_type>(m_offset - s, 0), m_max);
}
}
constexpr iterator &operator+=(size_type s) noexcept {
if constexpr(reverse) {
m_offset = max<size_type>(m_offset - s, 0);
} else {
m_offset = min(m_offset + s, m_max);
}
return *this;
}
constexpr iterator &operator-=(size_type s) noexcept {
if constexpr(reverse) {
m_offset = min(m_offset + s, m_max);
} else {
m_offset = max<size_type>(m_offset - s, 0);
}
return *this;
}
constexpr iterator &operator++() noexcept {
return operator+=(1);
}
constexpr iterator &operator--() noexcept {
return operator-=(1);
}
constexpr RefType operator*() const noexcept {
return m_t[m_offset];
}
constexpr RefType operator[](size_type s) const noexcept {
return m_t[s];
}
constexpr bool operator<(const iterator &other) const noexcept {
return m_offset < other.m_offset;
}
constexpr bool operator>(const iterator &other) const noexcept {
return m_offset > other.m_offset;
}
constexpr bool operator<=(const iterator &other) const noexcept {
return m_offset <= other.m_offset;
}
constexpr bool operator>=(const iterator &other) const noexcept {
return m_offset >= other.m_offset;
}
constexpr bool operator==(const iterator &other) const noexcept {
return m_t == other.m_t && m_offset == other.m_offset && m_max == other.m_max;
}
constexpr bool operator!=(const iterator &other) const noexcept {
return m_t != other.m_t || m_offset != other.m_offset || m_max != other.m_max;
}
};
private:
std::size_t m_size = 0;
std::size_t m_cap = 0;
@ -38,6 +130,38 @@ class Vector {
~Vector();
constexpr iterator<T&> begin() const noexcept {
return iterator<T&>(m_items, 0, m_size);
}
constexpr iterator<T&> end() const noexcept {
return iterator<T&>(m_items, m_size, m_size);
}
constexpr iterator<const T&> cbegin() const noexcept {
return iterator<const T&>(m_items, 0, m_size);
}
constexpr iterator<const T&> cend() const noexcept {
return iterator<const T&>(m_items, m_size, m_size);
}
constexpr iterator<T&, true> rbegin() const noexcept {
return iterator<T&, true>(m_items, 0, m_size);
}
constexpr iterator<T&, true> rend() const noexcept {
return iterator<T&, true>(m_items, m_size, m_size);
}
constexpr iterator<const T&, true> crbegin() const noexcept {
return iterator<const T&, true>(m_items, 0, m_size);
}
constexpr iterator<const T&, true> crend() const noexcept {
return iterator<const T&, true>(m_items, m_size, m_size);
}
bool operator==(const Vector &other) const;
constexpr Vector &operator=(const Vector &other);