Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
Iterators.hpp
1#pragma once
2
3#include <type_traits>
4#include "../Assert.hpp"
5
6namespace Grindstone::Containers {
7 template<typename T>
8 struct ConstArrayIterator {
9 using iterator_category = std::forward_iterator_tag;
10 using difference_type = std::ptrdiff_t;
11 using value_type = T;
12 using pointer = T*;
13 using reference = T&;
14
15 ConstArrayIterator(pointer ptr) : ptr(ptr) {}
16
17 const reference operator*() const { return *ptr; }
18 const pointer operator->() const { return ptr; }
19 ConstArrayIterator& operator++() { ptr++; return *this; }
20 ConstArrayIterator operator++(int) { ConstArrayIterator tmp = *this; ++(*this); return tmp; }
21 friend bool operator== (const ConstArrayIterator& a, const ConstArrayIterator& b) { return a.ptr == b.ptr; };
22 friend bool operator!= (const ConstArrayIterator& a, const ConstArrayIterator& b) { return a.ptr != b.ptr; };
23
24 private:
25 pointer ptr;
26 };
27
28 template<typename T>
29 struct ArrayIterator {
30 using iterator_category = std::forward_iterator_tag;
31 using difference_type = std::ptrdiff_t;
32 using value_type = T;
33 using pointer = T*;
34 using reference = T&;
35
36 ArrayIterator(pointer ptr) : ptr(ptr) {}
37
38 reference operator*() const { return *ptr; }
39 pointer operator->() { return ptr; }
40 ArrayIterator& operator++() { ptr++; return *this; }
41 ArrayIterator operator++(int) { ArrayIterator tmp = *this; ++(*this); return tmp; }
42 ArrayIterator& operator--() { ptr--; return *this; }
43 ArrayIterator operator--(int) { ArrayIterator tmp = *this; --(*this); return tmp; }
44 ArrayIterator operator+(int offset) { ptr += offset; return *this; }
45 ArrayIterator operator-(int offset) { ptr -= offset; return *this; }
46 friend bool operator== (const ArrayIterator& a, const ArrayIterator& b) { return a.ptr == b.ptr; };
47 friend bool operator!= (const ArrayIterator& a, const ArrayIterator& b) { return a.ptr != b.ptr; };
48 friend bool operator<(const ArrayIterator& l, const ArrayIterator& r) { return &(*l) < &(*r); }
49 friend bool operator>(const ArrayIterator& l, const ArrayIterator& r) { return r < l; }
50 friend bool operator<=(const ArrayIterator& l, const ArrayIterator& r) { return !(r < l); }
51 friend bool operator>=(const ArrayIterator& l, const ArrayIterator& r) { return !(l < r); }
52
53 protected:
54 pointer ptr;
55 };
56
57 template<typename T>
58 struct ConstReverseArrayIterator {
59 using iterator_category = std::forward_iterator_tag;
60 using difference_type = std::ptrdiff_t;
61 using value_type = T;
62 using pointer = T*;
63 using reference = T&;
64
65 ConstReverseArrayIterator(pointer ptr) : ptr(ptr) {}
66
67 const reference operator*() const { return *ptr; }
68 const pointer operator->() const { return ptr; }
69 ConstReverseArrayIterator& operator++() { ptr--; return *this; }
70 ConstReverseArrayIterator operator++(int) { ConstReverseArrayIterator tmp = *this; --(*this); return tmp; }
71 friend bool operator== (const ConstReverseArrayIterator& a, const ConstReverseArrayIterator& b) { return a.ptr == b.ptr; };
72 friend bool operator!= (const ConstReverseArrayIterator& a, const ConstReverseArrayIterator& b) { return a.ptr != b.ptr; };
73
74 private:
75 pointer ptr;
76 };
77
78 template<typename T>
79 struct ReverseArrayIterator {
80 using iterator_category = std::forward_iterator_tag;
81 using difference_type = std::ptrdiff_t;
82 using value_type = T;
83 using pointer = T*;
84 using reference = T&;
85
86 ReverseArrayIterator(pointer ptr) : ptr(ptr) {}
87
88 reference operator*() const { return *ptr; }
89 pointer operator->() { return ptr; }
90 ReverseArrayIterator& operator++() { ptr--; return *this; }
91 ReverseArrayIterator operator++(int) { ReverseArrayIterator tmp = *this; --(*this); return tmp; }
92 friend bool operator== (const ReverseArrayIterator& a, const ReverseArrayIterator& b) { return a.ptr == b.ptr; };
93 friend bool operator!= (const ReverseArrayIterator& a, const ReverseArrayIterator& b) { return a.ptr != b.ptr; };
94
95 private:
96 pointer ptr;
97 };
98}