diff --git a/deps/ox/src/ox/std/iterator.hpp b/deps/ox/src/ox/std/iterator.hpp new file mode 100644 index 00000000..9e7f9e97 --- /dev/null +++ b/deps/ox/src/ox/std/iterator.hpp @@ -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 +struct iterator { + using iterator_category = Category; + using value_type = T; + using pointer = T*; + using reference = T&; +}; + +} +#else +#include +#endif diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 3016bc48..073fffc7 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -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" diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index b396878f..37732886 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -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 + struct iterator: public std::iterator { + 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(m_offset - s, 0), m_max); + } else { + return iterator(m_t, min(m_offset + s, m_max), m_max); + } + } + + constexpr iterator operator-(size_type s) const noexcept { + if constexpr(reverse) { + return iterator(m_t, min(m_offset + s, m_max), m_max); + } else { + return iterator(m_t, max(m_offset - s, 0), m_max); + } + } + + constexpr iterator &operator+=(size_type s) noexcept { + if constexpr(reverse) { + m_offset = max(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(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 begin() const noexcept { + return iterator(m_items, 0, m_size); + } + + constexpr iterator end() const noexcept { + return iterator(m_items, m_size, m_size); + } + + constexpr iterator cbegin() const noexcept { + return iterator(m_items, 0, m_size); + } + + constexpr iterator cend() const noexcept { + return iterator(m_items, m_size, m_size); + } + + constexpr iterator rbegin() const noexcept { + return iterator(m_items, 0, m_size); + } + + constexpr iterator rend() const noexcept { + return iterator(m_items, m_size, m_size); + } + + constexpr iterator crbegin() const noexcept { + return iterator(m_items, 0, m_size); + } + + constexpr iterator crend() const noexcept { + return iterator(m_items, m_size, m_size); + } + bool operator==(const Vector &other) const; constexpr Vector &operator=(const Vector &other);