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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#pragma once


#include <concepts><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ranges><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


#include "internal/ranges.hpp"

#include "numeric/arithmetic.hpp"


namespace uni {


namespace internal {


template<class View>
concept slide_caches_nothing = std::ranges::random_access_range<View> && std::ranges::sized_range<View>;

template<class View>
concept slide_caches_last = !slide_caches_nothing<View> && std::ranges::bidirectional_range<View> && std::ranges::common_range<View>;


template<class View>
concept slide_caches_first = !slide_caches_nothing<View> && !slide_caches_last<View>;

} // namespace internal

template<std::ranges::forward_range View>
    requires std::ranges::view<View>
struct slide_view : std::ranges::view_interface<slide_view<View>> {
  private:
    View _base;
    std::ranges::range_difference_t<View> _n;

    [[no_unique_address]] internal::maybe_present_t<internal::slide_caches_first<View>, internal::cached_position<View>, 0> _cache_begin;
    [[no_unique_address]] internal::maybe_present_t<internal::slide_caches_last<View>, internal::cached_position<View>, 1> _cache_end;

    template<bool> struct iterator;
    struct sentinel;

  public:
    constexpr explicit slide_view(View base, std::ranges::range_difference_t<View> n)
      : _base(std::move(base)), _n(n)
    {
        assert(n > 0);
    }

    constexpr auto begin()
        requires(!(internal::simple_view<View> && internal::slide_caches_nothing<const View>))
    {
        if constexpr(internal::slide_caches_first<View>) {
            std::ranges::iterator_t<View> itr;

            if(this->_cache_begin.has_value()) {
                itr = this->_cache_begin.get(this->_base);
            }
            else {
                itr = std::ranges::next(std::ranges::begin(this->_base), this->_n - 1, std::ranges::end(this->_base));
                this->_cache_begin.set(this->_base, itr);
            }

            return iterator<false>(std::ranges::begin(this->_base), std::move(itr), this->_n);
        }
        else {
            return iterator<false>(std::ranges::begin(this->_base), this->_n);
        }
    }

    constexpr auto begin() const
        requires internal::slide_caches_nothing<const View>
    {
        return iterator<true>(std::ranges::begin(this->_base), this->_n);
    }

    constexpr auto end()
        requires (!(internal::simple_view<View> && internal::slide_caches_nothing<const View>))
    {
        if constexpr(internal::slide_caches_nothing<View>) {
            return iterator<false>(std::ranges::begin(this->_base) + std::ranges::range_difference_t<View>(this->size()), this->_n);
        }
        else if constexpr(internal::slide_caches_last<View>) {
            std::ranges::iterator_t<View> itr;

            if(this->_cache_end.has_value()) {
                itr = this->_cache_end.get(this->_base);
            }
            else {
                itr = std::ranges::prev(std::ranges::end(this->_base), this->_n - 1, std::ranges::begin(this->_base));
                this->_cache_end.set(this->_base, itr);
            }

            return iterator<false>(std::move(itr), this->_n);
        }
        else if constexpr(std::ranges::common_range<View>) {
            return iterator<false>(std::ranges::end(this->_base), std::ranges::end(this->_base), this->_n);
        }
        else {
            return sentinel(std::ranges::end(this->_base));
        }
    }

    constexpr auto end() const
        requires internal::slide_caches_nothing<const View>
    {
        return this->begin() + std::ranges::range_difference_t<const View>(this->size());
    }

    constexpr auto size()
        requires std::ranges::sized_range<View>
    {
        auto size = std::ranges::distance(this->_base) - this->_n + 1;
        if(size < 0) size = 0;

        return to_unsigned(size);
    }

    constexpr auto size() const
        requires std::ranges::sized_range<const View>
    {
        auto size = std::ranges::distance(this->_base) - this->_n + 1;
        if(size < 0) size = 0;

        return to_unsigned(size);
    }
};

template<class Range>
slide_view(Range &&, std::ranges::range_difference_t<Range>) -> slide_view<std::views::all_t<Range>>;


template<std::ranges::forward_range View>
    requires std::ranges::view<View>
template<bool CONST>
struct slide_view<View>::iterator {
  private:
    using Base = internal::maybe_const_t<CONST, View>;

    static constexpr bool LAST_PRESENT = internal::slide_caches_first<Base>;

    std::ranges::iterator_t<Base> _current = std::ranges::iterator_t<Base>();

    [[no_unique_address]] internal::maybe_present_t<LAST_PRESENT, std::ranges::iterator_t<Base>> _last = decltype(_last)();
    std::ranges::range_difference_t<Base> _n = 0;

    constexpr iterator(std::ranges::iterator_t<Base> current, std::ranges::range_difference_t<Base> n)
        requires (!LAST_PRESENT)
      : _current(current), _n(n)
    {}

    constexpr iterator(std::ranges::iterator_t<Base> current, std::ranges::iterator_t<Base> last, std::ranges::range_difference_t<Base> n)
        requires (LAST_PRESENT)
        : _current(current), _last(last), _n(n)
    {}


    friend slide_view;
    friend slide_view::sentinel;

  public:
    using iterator_category = std::input_iterator_tag;
    using iterator_concept = internal::most_primitive_iterator_concept<false, Base>;

    using value_type = decltype(std::views::counted(_current, _n));

    using difference_type = std::ranges::range_difference_t<Base>;

    iterator() = default;

    constexpr iterator(iterator<!CONST> itr)
        requires CONST && std::convertible_to<std::ranges::iterator_t<View>, std::ranges::iterator_t<Base>>
      : _current(std::move(itr._current)), _n(itr._n)
    {}

    constexpr auto operator*() const {
        return std::views::counted(_current, _n);
    }

    constexpr iterator &operator++() {
        ++this->_current;
        if constexpr(LAST_PRESENT) ++this->_last;
        return *this;
    }

    constexpr iterator operator++(int) {
        const auto temp = *this;
        return ++*this, temp;
    }

    constexpr iterator &operator--()
        requires std::ranges::bidirectional_range<Base>
    {
        --this->_current;
        if constexpr(LAST_PRESENT) --this->_last;
        return *this;
    }

    constexpr iterator operator--(int)
        requires std::ranges::bidirectional_range<Base>
    {
        auto temp = *this;
        return --*this, temp;
    }

    constexpr iterator &operator+=(const difference_type rhs)
        requires std::ranges::random_access_range<Base>
    {
        this->_current += rhs;
        if constexpr(LAST_PRESENT) this->_last += rhs;
        return *this;
    }

    constexpr iterator &operator-=(const difference_type rhs)
        requires std::ranges::random_access_range<Base>
    {
        this->_current -= rhs;
        if constexpr(LAST_PRESENT) this->_last -= rhs;
        return *this;
    }

    constexpr auto operator[](const difference_type count) const
        requires std::ranges::random_access_range<Base>
    {
        return std::views::counted(this->_current + count, this->_n);
    }

    friend constexpr bool operator==(const iterator &lhs, const iterator &rhs) {
        if constexpr(LAST_PRESENT) return lhs._last == rhs._last;
        else return lhs._current == rhs._current;
    }

    friend constexpr auto operator<=>(const iterator &lhs, const iterator &rhs)
        requires std::ranges::random_access_range<Base> && std::three_way_comparable<std::ranges::iterator_t<Base>>
    {
        return lhs._current <=> rhs._current;
    }

    friend constexpr iterator operator+(iterator itr, const difference_type count)
        requires std::ranges::random_access_range<Base>
    {
        auto res = itr;
        return res += count, res;
    }

    friend constexpr iterator operator+(const difference_type count, const iterator &itr)
        requires std::ranges::random_access_range<Base>
    {
        auto res = itr;
        return res += count, res;
    }

    friend constexpr iterator operator-(const iterator &itr, difference_type count)
        requires std::ranges::random_access_range<Base>
    {
        auto res = itr;
        return res -= count, res;
    }

    friend constexpr difference_type operator-(const iterator &lhs, const iterator &rhs)
        requires std::sized_sentinel_for<std::ranges::iterator_t<Base>, std::ranges::iterator_t<Base>>
    {
        if constexpr(LAST_PRESENT) return lhs._last - rhs._last;
        else return lhs._current - rhs._current;
    }
};


template<std::ranges::forward_range View>
    requires std::ranges::view<View>
struct slide_view<View>::sentinel {
  private:
    std::ranges::sentinel_t<View> _end = std::ranges::sentinel_t<View>();

    constexpr explicit sentinel(std::ranges::sentinel_t<View> end) : _end(end) {}

    friend slide_view;

  public:
    sentinel() = default;

    friend constexpr bool operator==(const iterator<false> &lhs, const sentinel &rhs) {
        return lhs._last == rhs._end;
    }

    friend constexpr std::ranges::range_difference_t<View>
    operator-(const iterator<false> &lhs, const sentinel &rhs)
        requires std::sized_sentinel_for<std::ranges::sentinel_t<View>, std::ranges::iterator_t<View>>
    {
        return lhs._last - rhs._end;
    }

    friend constexpr std::ranges::range_difference_t<View>
    operator-(const sentinel &rhs, const iterator<false> &lhs)
        requires std::sized_sentinel_for<std::ranges::sentinel_t<View>, std::ranges::iterator_t<View>>
    {
        return rhs._end - lhs._last;
    }
};


namespace views {


namespace internal {


template<class Range, class Diff>
concept can_slide_view = requires { slide_view(std::declval<Range>(), std::declval<Diff>()); };


} // namespace internal



struct Slide : adaptor::range_adaptor<Slide> {
    template<std::ranges::viewable_range Range, class Diff = std::ranges::range_difference_t<Range>>
        requires internal::can_slide_view<Range, Diff>

    constexpr auto operator() [[nodiscard]] (Range &&res, std::type_identity_t<Diff> n) const {
        return slide_view(std::forward<Range>(res), n);
    }

    using adaptor::range_adaptor<Slide>::operator();

    static constexpr int arity = 2;
    static constexpr bool has_simple_extra_args = true;
};


inline constexpr Slide slide;


} // namespace views

} // namespace uni


namespace std::ranges {


template<class View>
inline constexpr bool enable_borrowed_range<uni::slide_view<View>> = enable_borrowed_range<View>;


}