Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
Span.hpp
1#pragma once
2
3#include <type_traits>
4
5#include <Common/Assert.hpp>
6#include <Common/IntTypes.hpp>
7
8#include "Iterators.hpp"
9
10namespace Grindstone::Containers {
11 template<typename T>
12 class Span {
13 public:
14 using Iterator = ArrayIterator<T>;
15 using ConstIterator = ConstArrayIterator<T>;
16 using ReverseIterator = ReverseArrayIterator<T>;
17 using ConstReverseIterator = ConstArrayIterator<T>;
18
19 Span() = default;
20
21 Span(T* ptr, size_t size) :
22 contents(ptr),
23 size(size) {}
24
25 Span(const Span& other) : size(other.size), contents(other.contents) {}
26
27 Span(Span&& other) noexcept : size(other.size), contents(other.contents) {
28 other.size = 0;
29 other.contents = nullptr;
30 }
31
32 Span& operator=(const Span& other) {
33 size = other.size;
34 contents = other.contents;
35 }
36
37 Span& operator=(Span&& other) {
38 size = other.size;
39 contents = other.contents;
40
41 other.size = 0;
42 other.contents = nullptr;
43 }
44
45 ~Span() {
46 contents = nullptr;
47 size = 0;
48 }
49
50 [[nodiscard]] const T& GetBegin() const {
51 return contents[0];
52 }
53
54 [[nodiscard]] T& GetBegin() {
55 return contents[0];
56 }
57
58 [[nodiscard]] const T& GetEnd() const {
59 return contents[size];
60 }
61
62 [[nodiscard]] T& GetEnd() {
63 return contents[size];
64 }
65
66 bool TryGet(T& outValue, size_t index) {
67 if (index < size) {
68 contents[index];
69 return true;
70 }
71
72 return false;
73 }
74
75 size_t GetSize() const {
76 return size;
77 }
78
79 Span<T> GetSubspan(size_t firstIndex, size_t count) {
80 GS_ASSERT_ENGINE_WITH_MESSAGE(count == 0 || firstIndex + count <= size, "Array index is invalid.");
81 return Span<T>{ &contents[firstIndex], count };
82 }
83
84 T& operator[](size_t index) {
85 GS_ASSERT_ENGINE_WITH_MESSAGE(index < size, "Array index is invalid.");
86 return contents[index];
87 }
88
89 const T& operator[](size_t index) const {
90 GS_ASSERT_ENGINE_WITH_MESSAGE(index < size, "Array index is invalid.");
91 return contents[index];
92 }
93
94 [[nodiscard]] constexpr Iterator begin() noexcept {
95 return Iterator(contents);
96 }
97
98 [[nodiscard]] constexpr ConstIterator begin() const noexcept {
99 return ConstIterator(contents);
100 }
101
102 [[nodiscard]] constexpr Iterator end() noexcept {
103 return Iterator(&contents[size]);
104 }
105
106 [[nodiscard]] constexpr ConstIterator end() const noexcept {
107 return ConstIterator(&contents[size - 1]);
108 }
109
110 [[nodiscard]] constexpr ReverseIterator rbegin() noexcept {
111 return ReverseIterator(&contents[size - 1]);
112 }
113
114 [[nodiscard]] constexpr ConstReverseIterator rbegin() const noexcept {
115 return ConstReverseIterator(&contents[size - 1]);
116 }
117
118 [[nodiscard]] constexpr ReverseIterator rend() noexcept {
119 return ReverseIterator(contents - 1);
120 }
121
122 [[nodiscard]] constexpr ConstReverseIterator rend() const noexcept {
123 return ConstReverseIterator(contents - 1);
124 }
125
126 [[nodiscard]] constexpr ConstIterator cbegin() const noexcept {
127 return ConstIterator(contents);
128 }
129
130 [[nodiscard]] constexpr ConstIterator cend() const noexcept {
131 return ConstIterator(&contents[size]);
132 }
133
134 [[nodiscard]] constexpr ConstReverseIterator crbegin() const noexcept {
135 return ConstReverseIterator(&contents[size - 1]);
136 }
137
138 [[nodiscard]] constexpr ConstReverseIterator crend() const noexcept {
139 return ConstReverseIterator(contents - 1);
140 }
141 protected:
142 size_t size = 0;
143 T* contents = nullptr;
144 };
145
146 using BufferSpan = Span<Grindstone::Byte>;
147
148 template<typename T>
149 class ReverseRange {
150 public:
151 explicit ReverseRange(T& iterable) : iterable{ iterable } {}
152 auto begin() const { return std::rbegin(iterable); }
153 auto end() const { return std::rend(iterable); }
154 private:
155 T& iterable;
156 };
157
158 template<typename T>
159 class ReverseRangeTemp {
160 public:
161 explicit ReverseRangeTemp(T&& iterable) : iterable{ std::move(iterable) } {}
162 auto begin() const { return std::rbegin(iterable); }
163 auto end() const { return std::rend(iterable); }
164 private:
165 T iterable;
166 };
167}
Definition Span.hpp:12
Definition Iterators.hpp:29