rexlib 0.1.0
Device-agnostic array computing library for CryoEM and CryoET image processing, with a pluggable backend architecture
Loading...
Searching...
No Matches
span.hpp
1// SPDX-License-Identifier: GPL-3.0-only
2// Implementation based on: https://github.com/tcbrindle/span
3
4#pragma once
5
6#include "memory/byte.hpp"
8#include "platform/constexpr.hpp"
9
10#include <type_traits>
11#include <limits>
12#include <vector>
13#include <array>
14
15namespace rexlib
16{
17
22REXLIB_CONSTEXPR auto dynamic_extent = std::numeric_limits<std::size_t>::max();
23
24namespace detail
25{
26
33template <typename T, std::size_t S>
35{
36 T* data;
37 static REXLIB_CONST_CONSTEXPR std::size_t size = S;
38
39 span_storage() = delete;
40 REXLIB_CONSTEXPR span_storage(T* ptr, std::size_t count) noexcept;
41 span_storage(const span_storage &other) = default;
42 span_storage(span_storage &&other) = default;
43 ~span_storage() = default;
44
45 span_storage& operator=(const span_storage &other) = default;
46 span_storage& operator=(span_storage &&other) = default;
47};
48
54template <typename T>
56{
57 T* data;
58 std::size_t size;
59
60 REXLIB_CONSTEXPR span_storage() noexcept;
61 REXLIB_CONSTEXPR span_storage(T* ptr, std::size_t count) noexcept;
62 span_storage(const span_storage &other) = default;
63 span_storage(span_storage &&other) = default;
64 ~span_storage() = default;
65
66 span_storage& operator=(const span_storage &other) = default;
67 span_storage& operator=(span_storage &&other) = default;
68};
69
70template <typename T>
71struct span_storage<T, 0>
72{
73 static REXLIB_CONST_CONSTEXPR T* data = nullptr;
74 static REXLIB_CONST_CONSTEXPR std::size_t size = 0;
75
76 span_storage() = default;
77 REXLIB_CONSTEXPR span_storage(T* ptr, std::size_t count) noexcept;
78 span_storage(const span_storage &other) = default;
79 span_storage(span_storage &&other) = default;
80 ~span_storage() = default;
81
82 span_storage& operator=(const span_storage &other) = default;
83 span_storage& operator=(span_storage &&other) = default;
84};
85
86} // namespace detail
87
88
97template <typename T, std::size_t Extent = dynamic_extent>
98class span
99{
101
102public:
103 using element_type = T;
104 using value_type = typename std::remove_cv<element_type>::type;
105 using size_type = std::size_t;
106 using difference_type = std::ptrdiff_t;
107 using pointer = element_type*;
108 using const_pointer = const element_type*;
109 using reference = element_type&;
110 using const_reference = const element_type&;
111 using iterator = pointer;
112 using reverse_iterator = std::reverse_iterator<iterator>;
113
114 static constexpr size_type extent = Extent;
115
122 REXLIB_CONSTEXPR span(pointer ptr, size_type count);
123
130 REXLIB_CONSTEXPR span(pointer first, pointer last);
131
140 template <std::size_t Extent2>
141 REXLIB_CONSTEXPR span(const span<value_type, Extent2> &other);
142
151 template <std::size_t Extent2>
153
154 span() = default;
155 span(const span &other) = default;
156 span(span &&other) = default;
157 ~span() = default;
158
159 span& operator=(const span &other) = default;
160 span& operator=(span &&other) = default;
161
167 REXLIB_CONSTEXPR size_type size() const noexcept;
168
174 REXLIB_CONSTEXPR size_type size_bytes() const noexcept;
175
182 REXLIB_NODISCARD REXLIB_CONSTEXPR bool empty() const noexcept;
183
192 REXLIB_CONSTEXPR reference operator[](size_type idx) const noexcept;
193
201 REXLIB_CONSTEXPR reference front() const noexcept;
202
210 REXLIB_CONSTEXPR reference back() const noexcept;
211
217 REXLIB_CONSTEXPR pointer data() const noexcept;
218
224 REXLIB_CONSTEXPR iterator begin() const noexcept;
225
231 REXLIB_CONSTEXPR iterator end() const noexcept;
232
238 REXLIB_CONSTEXPR reverse_iterator rbegin() const noexcept;
239
245 REXLIB_CONSTEXPR reverse_iterator rend() const noexcept;
246
247private:
248 storage_type m_storage;
249};
250
252REXLIB_CONSTEXPR
254
256REXLIB_CONSTEXPR
258
259template <typename T, std::size_t N>
260REXLIB_CONSTEXPR span<byte, N*sizeof(T)>
262
263template <typename T, std::size_t N>
264REXLIB_CONSTEXPR span<const byte, N*sizeof(T)>
266
268REXLIB_CONSTEXPR
269span<T> make_span(T* ptr, std::size_t count) noexcept;
270
272REXLIB_CONSTEXPR
273span<const T> make_span(const T* ptr, std::size_t count) noexcept;
274
275template <typename T, std::size_t N>
276REXLIB_CONSTEXPR span<T, N> make_span(std::array<T, N> &array) noexcept;
277
278template <typename T, std::size_t N>
279REXLIB_CONSTEXPR span<const T, N>
280make_span(const std::array<T, N> &array) noexcept;
281
283REXLIB_CONSTEXPR span<T> make_span(std::vector<T> &array) noexcept;
284
286REXLIB_CONSTEXPR span<const T>
287make_span(const std::vector<T> &array) noexcept;
288
289} // namespace rexlib
290
291#include "span.inl"
Macro definitions for supported attributes.
#define REXLIB_NODISCARD
Indicates that the return value should be used.
Definition attributes.hpp:76
Container for multidimensional numeric data.
Definition array.hpp:31
Represents a non-owning area of contiguous memory.
Definition span.hpp:99
REXLIB_CONSTEXPR iterator end() const noexcept
Get an iterator to the past-the-end item.
REXLIB_CONSTEXPR span(pointer first, pointer last)
Construct from a pointer pair.
REXLIB_CONSTEXPR reverse_iterator rend() const noexcept
Get a reverse iterator to the before-first item.
REXLIB_CONSTEXPR reference back() const noexcept
Obtain a reference to the last object.
REXLIB_CONSTEXPR span(const span< value_type, Extent2 > &other)
Construct span from another span.
REXLIB_CONSTEXPR pointer data() const noexcept
Get a pointer to the referenced data.
REXLIB_CONSTEXPR size_type size() const noexcept
Get the number of referenced objects.
REXLIB_CONSTEXPR size_type size_bytes() const noexcept
Get the size in bytes of the referenced objects.
REXLIB_CONSTEXPR reverse_iterator rbegin() const noexcept
Get a reverse iterator to the last item.
REXLIB_CONSTEXPR iterator begin() const noexcept
Get an iterator to the first item.
REXLIB_CONSTEXPR span(pointer ptr, size_type count)
Construct from a pointer and size.
REXLIB_CONSTEXPR span(const span< const value_type, Extent2 > &other)
Construct span from another span.
REXLIB_CONSTEXPR reference front() const noexcept
Obtain a reference to the first object.
REXLIB_NODISCARD REXLIB_CONSTEXPR bool empty() const noexcept
Check if no objects are references.
Arrays, the operations on them and the devices that run those operations.
Definition cpu_device.hpp:10
REXLIB_CONSTEXPR auto dynamic_extent
Constant representing dynamic size of an array.
Definition span.hpp:22
byte * as_bytes(T *ptr) noexcept
View an object as the bytes it is stored in.
Storage for the span object.
Definition span.hpp:35