1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
 * @file span.hpp
 * @author Robin Passama
 * @brief include file for span class
 * @date 2022-06-10
 *
 * @ingroup containers
 *
 */
#pragma once

#include <array>
#include <cstddef>
#include <iterator>
#include <limits>
#include <type_traits>

#include <cassert>

namespace pid {

inline constexpr std::size_t dynamic_extent =
    std::numeric_limits<std::size_t>::max();

template <std::size_t Extent>
class SpanSize {
public:
    constexpr SpanSize() noexcept = default;
    explicit constexpr SpanSize([[maybe_unused]] size_t size) noexcept {
        assert(size == Extent);
    };

    [[nodiscard]] constexpr std::size_t size() const {
        return Extent;
    }
};

template <>
class SpanSize<dynamic_extent> {
public:
    constexpr SpanSize() noexcept = default;

    explicit constexpr SpanSize(size_t size) noexcept : size_{size} {
    }

    [[nodiscard]] constexpr std::size_t size() const {
        return size_;
    }

private:
    std::size_t size_{0};
};

template <typename T, std::size_t Extent = dynamic_extent>
class Span : public SpanSize<Extent> {
public:
    static constexpr std::size_t extent = Extent;

    using element_type = T;
    using value_type = std::remove_cv_t<T>;
    using size_type = std::size_t;
    using difference_type = std::ptrdiff_t;
    using pointer = T*;
    using const_pointer = const T*;
    using reference = T&;
    using const_reference = const T&;
    using iterator = pointer;
    using reverse_iterator = std::reverse_iterator<iterator>;

    template <std::size_t N = Extent,
              std::enable_if_t<N == 0 or N == dynamic_extent, int> = 0>
    constexpr Span() noexcept : SpanSize<Extent>{} {
    }

    constexpr Span(T* first, size_type count)
        : SpanSize<Extent>{count}, data_{first} {
    }

    constexpr Span(T* first, T* last)<--- Parameter 'last' can be declared with const
        : SpanSize<Extent>{static_cast<size_type>(last - first)}, data_{first} {
    }

    template <std::size_t N>
    // NOLINTNEXTLINE(modernize-avoid-c-arrays)
    constexpr Span(T (&arr)[N]) noexcept : SpanSize<Extent>{N}, data_{arr} {
    }

    template <typename U, std::size_t N>
    constexpr Span(std::array<U, N>& arr) noexcept
        : SpanSize<Extent>{N}, data_{arr.data()} {
    }

    template <typename U, std::size_t N>
    constexpr Span(const std::array<U, N>& arr) noexcept
        : SpanSize<Extent>{N}, data_{arr.data()} {
    }

    template <
        typename U, std::size_t N, std::size_t S = Extent,
        std::enable_if_t<
            S == dynamic_extent or N == dynamic_extent or N == Extent, int> = 0>
    constexpr Span(const Span<U, N>& span) noexcept
        : SpanSize<Extent>{span.size()}, data_{span.data()} {
    }

    constexpr Span(const Span&) noexcept = default;
    constexpr Span& operator=(const Span&) noexcept = default;

    using SpanSize<Extent>::size;

    [[nodiscard]] constexpr iterator begin() const {
        return data_;
    }

    [[nodiscard]] constexpr iterator end() const {
        return data_ + size();
    }

    [[nodiscard]] constexpr reverse_iterator rbegin() const {
        return std::reverse_iterator{begin()};
    }

    [[nodiscard]] constexpr reverse_iterator rend() const {
        return std::reverse_iterator{end()};
    }

    [[nodiscard]] constexpr reference front() const {
        return *begin();
    }

    [[nodiscard]] constexpr reference back() const {
        return *(end() - 1);
    }

    [[nodiscard]] constexpr reference operator[](size_type idx) const {
        return data_[idx];
    }

    [[nodiscard]] constexpr pointer data() const noexcept {
        return data_;
    }

    [[nodiscard]] constexpr size_type size_bytes() const noexcept {
        return size() * sizeof(T);
    }

    [[nodiscard]] constexpr bool empty() const noexcept {
        return size() == 0;
    }

    template <std::size_t Count>
    [[nodiscard]] constexpr Span<T, Count> first() const {
        static_assert(Count <= Extent);
        return {data_, Count};
    }

    [[nodiscard]] constexpr Span<T, dynamic_extent>
    first(size_type count) const {
        return {data_, count};
    }

    template <std::size_t Count>
    [[nodiscard]] constexpr Span<T, Count> last() const {
        static_assert(Count <= Extent);
        return {data_ + size() - Count, Count};
    }

    [[nodiscard]] constexpr Span<T, dynamic_extent>
    last(size_type count) const {
        return {data_ + size() - count, count};
    }

    template <std::size_t Offset, std::size_t Count = dynamic_extent>
    [[nodiscard]] constexpr Span<T, Count != dynamic_extent    ? Count
                                    : Extent != dynamic_extent ? Extent - Offset
                                                               : dynamic_extent>
    subspan() const {
        if constexpr (Count == dynamic_extent) {
            return {data_ + Offset, size() - Offset};
        } else {
            return {data_ + Offset, Count};
        }
    }

    [[nodiscard]] constexpr Span<T, dynamic_extent>
    subspan(size_type offset, size_type count = dynamic_extent) const {
        if (count == dynamic_extent) {
            return {data_ + offset, size() - offset};
        } else {
            return {data_ + offset, count};
        }
    }

private:
    T* data_{nullptr};
};

// deduction guides

template <typename T, std::size_t Extent>
Span(T (&)[Extent]) -> Span<T, Extent>; // NOLINT(modernize-avoid-c-arrays)

template <typename T, std::size_t Extent>
Span(std::array<T, Extent>&) -> Span<T, Extent>;

template <typename T, std::size_t Extent>
Span(const std::array<T, Extent>&) -> Span<const T, Extent>;

template <typename T>
Span(T*, T*) -> Span<T>;

template <typename T>
Span(T*, std::size_t) -> Span<T>;

// iterator access free functions

template <typename T, std::size_t Extent>
auto begin(pid::Span<T, Extent>& span) {
    return span.begin();
}

template <typename T, std::size_t Extent>
auto begin(const pid::Span<T, Extent>& span) {
    return span.begin();
}

template <typename T, std::size_t Extent>
auto end(pid::Span<T, Extent>& span) {
    return span.end();
}

template <typename T, std::size_t Extent>
auto end(const pid::Span<T, Extent>& span) {
    return span.end();
}

template <typename T, std::size_t Extent>
auto rbegin(pid::Span<T, Extent>& span) {
    return span.rbegin();
}

template <typename T, std::size_t Extent>
auto rbegin(const pid::Span<T, Extent>& span) {
    return span.rbegin();
}

template <typename T, std::size_t Extent>
auto rend(pid::Span<T, Extent>& span) {
    return span.rend();
}

template <typename T, std::size_t Extent>
auto rend(const pid::Span<T, Extent>& span) {
    return span.rend();
}

template <typename T, std::size_t Extent = dynamic_extent>
using span = Span<T, Extent>;

} // namespace pid