aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/array_ref.h
blob: 1ac60ac7d3c5884851379a9062502047849c1cf4 (plain) (blame)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#pragma once

#include <util/generic/yexception.h>

#include <algorithm>
#include <initializer_list>
#include <iterator>

/**
 * `TArrayRef` works pretty much like `std::span` with dynamic extent, presenting
 * an array-like interface into a contiguous sequence of objects.
 *
 * It can be used at interface boundaries instead of `TVector` or
 * pointer-size pairs, and is actually a preferred way to pass contiguous data
 * into functions.
 *
 * Note that `TArrayRef` can be auto-constructed from any contiguous container
 * (with `size` and `data` members), and thus you don't have to change client code
 * when switching over from passing `TVector` to `TArrayRef`.
 *
 * Note that `TArrayRef` has the same const-semantics as raw pointers:
 * - `TArrayRef<T>` is a non-const reference to non-const data (like `T*`);
 * - `TArrayRef<const T>` is a non-const reference to const data (like `const T*`);
 * - `const TArrayRef<T>` is a const reference to non-const data (like `T* const`);
 * - `const TArrayRef<const T>` is a const reference to const data (like `const T* const`).
 */
template <class T>
class TArrayRef {
public:
    using iterator = T*;
    using const_iterator = const T*;
    using reference = T&;
    using const_reference = const T&;
    using value_type = T;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    constexpr inline TArrayRef() noexcept
        : T_(nullptr)
        , S_(0)
    {
    }

    constexpr inline TArrayRef(T* data, size_t len) noexcept
        : T_(data)
        , S_(len)
    {
    }

    constexpr inline TArrayRef(T* begin, T* end) noexcept
        : T_(begin)
        , S_(end - begin)
    {
    }

    constexpr inline TArrayRef(std::initializer_list<T> list) noexcept
        : T_(list.begin())
        , S_(list.size())
    {
    }

    template <class Container>
    constexpr inline TArrayRef(Container&& container, decltype(std::declval<T*&>() = container.data(), nullptr) = nullptr) noexcept
        : T_(container.data())
        , S_(container.size())
    {
    }

    template <size_t N>
    constexpr inline TArrayRef(T (&array)[N]) noexcept
        : T_(array)
        , S_(N)
    {
    }

    template <class TT, typename = std::enable_if_t<std::is_same<std::remove_const_t<T>, std::remove_const_t<TT>>::value>>
    bool operator==(const TArrayRef<TT>& other) const noexcept {
        return (S_ == other.size()) && std::equal(begin(), end(), other.begin());
    }

    constexpr inline T* data() const noexcept {
        return T_;
    }

    constexpr inline size_t size() const noexcept {
        return S_;
    }

    constexpr size_t size_bytes() const noexcept {
        return (size() * sizeof(T));
    }

    constexpr inline bool empty() const noexcept {
        return (S_ == 0);
    }

    constexpr inline iterator begin() const noexcept {
        return T_;
    }

    constexpr inline iterator end() const noexcept {
        return (T_ + S_);
    }

    constexpr inline const_iterator cbegin() const noexcept {
        return T_;
    }

    constexpr inline const_iterator cend() const noexcept {
        return (T_ + S_);
    }

    constexpr inline reverse_iterator rbegin() const noexcept {
        return reverse_iterator(T_ + S_);
    }

    constexpr inline reverse_iterator rend() const noexcept {
        return reverse_iterator(T_);
    }

    constexpr inline const_reverse_iterator crbegin() const noexcept {
        return const_reverse_iterator(T_ + S_);
    }

    constexpr inline const_reverse_iterator crend() const noexcept {
        return const_reverse_iterator(T_);
    }

    constexpr inline reference front() const noexcept {
        return *T_;
    }

    inline reference back() const noexcept {
        Y_ASSERT(S_ > 0);

        return *(end() - 1);
    }

    inline reference operator[](size_t n) const noexcept {
        Y_ASSERT(n < S_);

        return *(T_ + n);
    }

    inline reference at(size_t n) const {
        if (n >= S_) {
            throw std::out_of_range("array ref range error");
        }

        return (*this)[n];
    }

    constexpr inline explicit operator bool() const noexcept {
        return (S_ > 0);
    }

    /**
     * Obtains a ref that is a view over the first `count` elements of this TArrayRef.
     *
     * The behavior is undefined if count > size().
     */
    TArrayRef first(size_t count) const {
        Y_ASSERT(count <= size());
        return TArrayRef(data(), count);
    }

    /**
     * Obtains a ref that is a view over the last `count` elements of this TArrayRef.
     *
     * The behavior is undefined if count > size().
     */
    TArrayRef last(size_t count) const {
        Y_ASSERT(count <= size());
        return TArrayRef(end() - count, end());
    }

    /**
     * Obtains a ref that is a view over the `count` elements of this TArrayRef starting at `offset`.
     *
     * The behavior is undefined in either offset or count is out of range.
     */
    TArrayRef subspan(size_t offset) const {
        Y_ASSERT(offset <= size());
        return TArrayRef(data() + offset, size() - offset);
    }

    TArrayRef subspan(size_t offset, size_t count) const {
        Y_ASSERT(offset + count <= size());
        return TArrayRef(data() + offset, count);
    }

    TArrayRef Slice(size_t offset) const {
        return subspan(offset);
    }

    TArrayRef Slice(size_t offset, size_t size) const {
        return subspan(offset, size);
    }

    /* FIXME:
     * This method is placed here for backward compatibility only and should be removed.
     * Keep in mind that it's behavior is different from Slice():
     *      SubRegion() never throws. It returns empty TArrayRef in case of invalid input.
     *
     * DEPRECATED. DO NOT USE.
     */
    TArrayRef SubRegion(size_t offset, size_t size) const {
        if (size == 0 || offset >= S_) {
            return TArrayRef();
        }

        if (size > S_ - offset) {
            size = S_ - offset;
        }

        return TArrayRef(T_ + offset, size);
    }

    constexpr inline yssize_t ysize() const noexcept {
        return static_cast<yssize_t>(this->size());
    }

private:
    T* T_;
    size_t S_;
};

/**
 * Obtains a view to the object representation of the elements of the TArrayRef arrayRef.
 *
 * Named as its std counterparts, std::as_bytes.
 */
template <typename T>
TArrayRef<const char> as_bytes(TArrayRef<T> arrayRef) noexcept {
    return TArrayRef<const char>(
        reinterpret_cast<const char*>(arrayRef.data()),
        arrayRef.size_bytes());
}

/**
 * Obtains a view to the writable object representation of the elements of the TArrayRef arrayRef.
 *
 * Named as its std counterparts, std::as_writable_bytes.
 */
template <typename T>
TArrayRef<char> as_writable_bytes(TArrayRef<T> arrayRef) noexcept {
    return TArrayRef<char>(
        reinterpret_cast<char*>(arrayRef.data()),
        arrayRef.size_bytes());
}

template <class Range>
constexpr TArrayRef<const typename Range::value_type> MakeArrayRef(const Range& range) {
    return TArrayRef<const typename Range::value_type>(range);
}

template <class Range>
constexpr TArrayRef<typename Range::value_type> MakeArrayRef(Range& range) {
    return TArrayRef<typename Range::value_type>(range);
}

template <class Range>
constexpr TArrayRef<const typename Range::value_type> MakeConstArrayRef(const Range& range) {
    return TArrayRef<const typename Range::value_type>(range);
}

template <class Range>
constexpr TArrayRef<const typename Range::value_type> MakeConstArrayRef(Range& range) {
    return TArrayRef<const typename Range::value_type>(range);
}

template <class T>
constexpr TArrayRef<T> MakeArrayRef(T* data, size_t size) {
    return TArrayRef<T>(data, size);
}

template <class T>
constexpr TArrayRef<T> MakeArrayRef(T* begin, T* end) {
    return TArrayRef<T>(begin, end);
}