libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
span.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstddef>
11#include <span>
13
15
16// Forward declarations
17template <typename T> class List;
18template <typename T, size_t N> class Array;
19
30template <typename T>
31class Span {
32 public:
34 using Iterator = typename std::span<T>::iterator;
35
37 using ConstIterator = typename std::span<const T>::iterator;
38
40 using RevIterator = typename std::span<T>::reverse_iterator;
41
43 using ConstRevIterator = typename std::span<const T>::reverse_iterator;
44
46 Span() = default;
47
53 Span(T *ptr, size_t count) : d(ptr, count) {}
54
60
66 template <size_t N>
67 Span(T (&arr)[N]) : d(arr, N) {}
68
74 template <size_t N>
76
78 Span(const Span &other) = default;
79
81 Span &operator=(const Span &other) = default;
82
83 // -- Iterators --
84
86 Iterator begin() noexcept { return d.begin(); }
87
89 ConstIterator begin() const noexcept { return std::span<const T>(d.data(), d.size()).begin(); }
90
92 Iterator end() noexcept { return d.end(); }
93
95 ConstIterator end() const noexcept { return std::span<const T>(d.data(), d.size()).end(); }
96
99
102
104 RevIterator rbegin() noexcept { return d.rbegin(); }
105
107 RevIterator revBegin() noexcept { return d.rbegin(); }
108
110 RevIterator rend() noexcept { return d.rend(); }
111
113 RevIterator revEnd() noexcept { return d.rend(); }
114
116 ConstRevIterator crbegin() const noexcept { return std::span<const T>(d.data(), d.size()).rbegin(); }
117
120
122 ConstRevIterator crend() const noexcept { return std::span<const T>(d.data(), d.size()).rend(); }
123
126
127 // -- Access --
128
134 T &operator[](size_t index) { return d[index]; }
135
137 const T &operator[](size_t index) const { return d[index]; }
138
140 T &front() { return d.front(); }
141
143 const T &front() const { return d.front(); }
144
146 T &back() { return d.back(); }
147
149 const T &back() const { return d.back(); }
150
152 T *data() noexcept { return d.data(); }
153
155 const T *data() const noexcept { return d.data(); }
156
157 // -- Capacity --
158
160 bool isEmpty() const noexcept { return d.empty(); }
161
163 size_t size() const noexcept { return d.size(); }
164
166 size_t sizeBytes() const noexcept { return d.size_bytes(); }
167
168 // -- Sub-views --
169
176 Span subspan(size_t offset, size_t count) {
177 return Span(d.data() + offset, count);
178 }
179
185 Span first(size_t count) {
186 return Span(d.data(), count);
187 }
188
194 Span last(size_t count) {
195 return Span(d.data() + d.size() - count, count);
196 }
197
198 // -- Convenience --
199
205 template <typename Func>
206 void forEach(Func &&func) const {
207 for(size_t i = 0; i < d.size(); ++i) func(d[i]);
208 return;
209 }
210
211 private:
212 std::span<T> d;
213};
214
Fixed-size array container wrapping std::array.
Definition array.h:35
Dynamic array container wrapping std::vector.
Definition list.h:40
RevIterator rbegin() noexcept
Returns a mutable reverse iterator to the last element.
Definition list.h:178
Iterator begin() noexcept
Returns a mutable iterator to the first element.
Definition list.h:158
Iterator end() noexcept
Returns a mutable iterator to one past the last element.
Definition list.h:198
RevIterator rend() noexcept
Returns a mutable reverse iterator to one before the first element.
Definition list.h:218
Non-owning view over contiguous storage, wrapping std::span.
Definition span.h:31
typename std::span< T >::iterator Iterator
Mutable forward iterator.
Definition span.h:34
RevIterator rend() noexcept
Returns a mutable reverse iterator to one before the first element.
Definition span.h:110
Span(const Span &other)=default
Copy constructor.
Span first(size_t count)
Returns a span of the first count elements.
Definition span.h:185
T & front()
Returns a reference to the first element.
Definition span.h:140
RevIterator revBegin() noexcept
Returns a mutable reverse iterator to the last element.
Definition span.h:107
const T * data() const noexcept
Returns a pointer to the underlying contiguous storage.
Definition span.h:155
RevIterator rbegin() noexcept
Returns a mutable reverse iterator to the last element.
Definition span.h:104
ConstIterator end() const noexcept
Returns a const iterator to one past the last element.
Definition span.h:95
T * data() noexcept
Returns a pointer to the underlying contiguous storage.
Definition span.h:152
const T & operator[](size_t index) const
Returns a reference to the element at index without bounds checking.
Definition span.h:137
Span(Array< T, N > &arr)
Constructs a span from an Array.
Definition span.h:75
typename std::span< const T >::iterator ConstIterator
Const forward iterator.
Definition span.h:37
typename std::span< T >::reverse_iterator RevIterator
Mutable reverse iterator.
Definition span.h:40
void forEach(Func &&func) const
Calls func for every element.
Definition span.h:206
ConstRevIterator crend() const noexcept
Returns a const reverse iterator to one before the first element.
Definition span.h:122
ConstRevIterator constRevBegin() const noexcept
Returns a const reverse iterator to the last element.
Definition span.h:119
Iterator end() noexcept
Returns a mutable iterator to one past the last element.
Definition span.h:92
Span()=default
Default constructor. Creates an empty span.
Span(T(&arr)[N])
Constructs a span from a C array.
Definition span.h:67
ConstIterator constBegin() const noexcept
Returns a const iterator to the first element.
Definition span.h:98
size_t size() const noexcept
Returns the number of elements.
Definition span.h:163
typename std::span< const T >::reverse_iterator ConstRevIterator
Const reverse iterator.
Definition span.h:43
T & back()
Returns a reference to the last element.
Definition span.h:146
const T & front() const
Returns a reference to the first element.
Definition span.h:143
bool isEmpty() const noexcept
Returns true if the span has no elements.
Definition span.h:160
ConstIterator constEnd() const noexcept
Returns a const iterator to one past the last element.
Definition span.h:101
Span subspan(size_t offset, size_t count)
Returns a sub-span starting at offset with count elements.
Definition span.h:176
Iterator begin() noexcept
Returns a mutable iterator to the first element.
Definition span.h:86
ConstRevIterator crbegin() const noexcept
Returns a const reverse iterator to the last element.
Definition span.h:116
Span(List< T > &list)
Constructs a span from a List.
Definition span.h:59
Span last(size_t count)
Returns a span of the last count elements.
Definition span.h:194
ConstIterator begin() const noexcept
Returns a const iterator to the first element.
Definition span.h:89
size_t sizeBytes() const noexcept
Returns the size in bytes.
Definition span.h:166
RevIterator revEnd() noexcept
Returns a mutable reverse iterator to one before the first element.
Definition span.h:113
Span & operator=(const Span &other)=default
Copy assignment operator.
const T & back() const
Returns a reference to the last element.
Definition span.h:149
Span(T *ptr, size_t count)
Constructs a span from a pointer and size.
Definition span.h:53
ConstRevIterator constRevEnd() const noexcept
Returns a const reverse iterator to one before the first element.
Definition span.h:125
T & operator[](size_t index)
Returns a reference to the element at index without bounds checking.
Definition span.h:134
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19