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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#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 {


template<std::ranges::view View>
    requires std::ranges::input_range<View>
struct chunk_view : std::ranges::view_interface<chunk_view<View>> {
  private:
    View _base;
    std::ranges::range_difference_t<View> _n;
    std::ranges::range_difference_t<View> _remainder = 0;
    std::optional<std::ranges::iterator_t<View>> _current;

    struct outer_iterator;
    struct inner_iterator;

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

    constexpr View base() const &
        requires std::copy_constructible<View>
    {
        return this->_base;
    }

    constexpr View base() && { return std::move(this->_base); }

    constexpr outer_iterator begin() {
        this->_current = std::ranges::begin(this->_base);
        this->_remainder = this->_n;
        return outer_iterator(*this);
    }

    constexpr std::default_sentinel_t end() const noexcept {
        return std::default_sentinel;
    }

    constexpr auto size()
        requires std::ranges::sized_range<View>
    {
        return to_unsigned(div_ceil(std::ranges::distance(this->_base), this->_n));
    }

    constexpr auto size() const
        requires std::ranges::sized_range<const View>
    {
        return to_unsigned(div_ceil(std::ranges::distance(this->_base), this->_n));
    }
};

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


template<std::ranges::view View>
    requires std::ranges::input_range<View>
struct chunk_view<View>::outer_iterator {
  private:
    chunk_view *_parent;

    constexpr explicit outer_iterator(chunk_view &parent) noexcept
      : _parent(std::addressof(parent))
    {}

    friend chunk_view;

  public:
    using iterator_concept = std::input_iterator_tag;
    using difference_type = std::ranges::range_difference_t<View>;

    struct value_type;

    outer_iterator(outer_iterator &&) = default;
    outer_iterator &operator=(outer_iterator &&) = default;

    constexpr value_type operator*() const {
        assert(*this != std::default_sentinel);
        return value_type(*this->_parent);
    }

    constexpr outer_iterator &operator++() {
        assert(*this != std::default_sentinel);
        std::ranges::advance(*this->_parent->_current, this->_parent->_remainder, std::ranges::end(this->_parent->_base));
        this->_parent->_remainder = this->_parent->_n;
        return *this;
    }

    constexpr void operator++(int) { ++*this; }

    friend constexpr bool operator==(const outer_iterator &lhs, std::default_sentinel_t) {
        return *lhs._parent->_current == std::ranges::end(lhs._parent->_base) && lhs._parent->_remainder != 0;
    }

    friend constexpr difference_type operator-(std::default_sentinel_t, const outer_iterator &rhs)
        requires std::sized_sentinel_for<std::ranges::sentinel_t<View>, std::ranges::iterator_t<View>>
    {
        const auto dist = std::ranges::end(rhs._parent->_base) - *rhs._parent->_current;

        if(dist < rhs._parent->_remainder) return dist == 0 ? 0 : 1;
        return 1 + div_ceil(dist - rhs._parent->_remainder, rhs._parent->_n);
    }

    friend constexpr difference_type operator-(const outer_iterator &lhs, std::default_sentinel_t rhs)
        requires std::sized_sentinel_for<std::ranges::sentinel_t<View>, std::ranges::iterator_t<View>>
    {
        return -(rhs - lhs);
    }
};

template<std::ranges::view View>
    requires std::ranges::input_range<View>
struct chunk_view<View>::outer_iterator::value_type : std::ranges::view_interface<value_type> {
  private:
    chunk_view *_parent;

    constexpr explicit value_type(chunk_view &parent) noexcept
      : _parent(std::addressof(parent))
    {}

    friend outer_iterator;

  public:
    constexpr inner_iterator begin() const noexcept {
        return inner_iterator(*this->_parent);
    }

    constexpr std::default_sentinel_t end() const noexcept {
        return std::default_sentinel;
    }

    constexpr auto size() const
        requires std::sized_sentinel_for<std::ranges::sentinel_t<View>, std::ranges::iterator_t<View>>
    {
        return to_unsigned(std::ranges::min(this->_parent->_remainder, std::ranges::end(this->_parent->_base) - *this->_parent->_current));
    }
};

template<std::ranges::view View>
    requires std::ranges::input_range<View>
struct chunk_view<View>::inner_iterator {
  private:
    chunk_view *_parent;

    constexpr explicit inner_iterator(chunk_view &parent) noexcept
      : _parent(std::addressof(parent))
    {}

    friend outer_iterator::value_type;

  public:
    using iterator_concept = std::input_iterator_tag;
    using difference_type = std::ranges::range_difference_t<View>;
    using value_type = std::ranges::range_value_t<View>;

    inner_iterator(inner_iterator &&) = default;
    inner_iterator &operator=(inner_iterator &&) = default;

    constexpr const std::ranges::iterator_t<View> &base() const & {
        return *this->_parent->_current;
    }

    constexpr std::ranges::range_reference_t<View> operator*() const {
        assert(*this != std::default_sentinel);
        return **this->_parent->_current;
    }

    constexpr inner_iterator &operator++() {
        assert(*this != std::default_sentinel);
        ++*this->_parent->_current;

        if(*this->_parent->_current == std::ranges::end(this->_parent->_base)) {
            this->_parent->_remainder = 0;
        }
        else {
            --this->_parent->_remainder;
        }

        return *this;
    }

    constexpr void operator++(int) { ++*this; }

    friend constexpr bool operator==(const inner_iterator &lhs, std::default_sentinel_t) noexcept {
        return lhs._parent->_remainder == 0;
    }

    friend constexpr difference_type operator-(std::default_sentinel_t, const inner_iterator &rhs)
        requires std::sized_sentinel_for<std::ranges::sentinel_t<View>, std::ranges::iterator_t<View>>
    {
        return std::ranges::min(rhs._parent->_remainder, std::ranges::end(rhs._parent->_base) - *rhs._parent->_current);
    }

    friend constexpr difference_type operator-(const inner_iterator &lhs, std::default_sentinel_t rhs)
        requires std::sized_sentinel_for<std::ranges::sentinel_t<View>, std::ranges::iterator_t<View>>
    {
        return -(rhs - lhs);
    }
};

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

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

    constexpr View base() const &
        requires std::copy_constructible<View>
    {
        return this->_base;
    }

    constexpr View base() && { return std::move(this->_base); }

    constexpr auto begin()
        requires(!internal::simple_view<View>)
    {
        return iterator<false>(this, std::ranges::begin(this->_base));
    }

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

    constexpr auto end()
        requires(!internal::simple_view<View>)
    {
        if constexpr(std::ranges::common_range<View> && std::ranges::sized_range<View>) {
            const auto missing = (this->_n - std::ranges::distance(this->_base) % this->_n) % this->_n;
            return iterator<false>(this, std::ranges::end(this->_base), missing);
        }
        else if constexpr(std::ranges::common_range<View> && !std::ranges::bidirectional_range<View>) {
            return iterator<false>(this, std::ranges::end(this->_base));
        }
        else {
            return std::default_sentinel;
        }
    }

    constexpr auto end() const
        requires std::ranges::forward_range<const View>
    {
        if constexpr(std::ranges::common_range<const View> && std::ranges::sized_range<const View>) {
            auto missing = (this->_n - std::ranges::distance(this->_base) % this->_n) % this->_n;
            return iterator<true>(this, std::ranges::end(this->_base), missing);
        } else if constexpr(std::ranges::common_range<const View> && !std::ranges::bidirectional_range<const View>) {
            return iterator<true>(this, std::ranges::end(this->_base));
        }
        else {
            return std::default_sentinel;
        }
    }

    constexpr auto size()
        requires std::ranges::sized_range<View>
    {
        return to_unsigned(div_ceil(std::ranges::distance(this->_base), this->_n));
    }

    constexpr auto size() const
        requires std::ranges::sized_range<const View>
    {
        return to_unsigned(div_ceil(std::ranges::distance(this->_base), this->_n));
    }
};


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

    std::ranges::iterator_t<Base> _current = std::ranges::iterator_t<Base>();
    std::ranges::sentinel_t<Base> _end = std::ranges::sentinel_t<Base>();
    std::ranges::range_difference_t<Base> _n = 0;
    std::ranges::range_difference_t<Base> _missing = 0;

    constexpr iterator(Parent *parent, std::ranges::iterator_t<Base> current, const std::ranges::range_difference_t<Base> missing = 0)
      : _current(current), _end(std::ranges::end(parent->_base)), _n(parent->_n), _missing(missing)
    {}

    friend chunk_view;

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

    using value_type = decltype(std::views::take(std::ranges::subrange(_current, _end), _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>> &&
            std::convertible_to<std::ranges::sentinel_t<View>, std::ranges::sentinel_t<Base>>
      : _current(std::move(itr._current)), _end(std::move(itr._end)), _n(itr._n), _missing(itr._missing)
    {}

    constexpr std::ranges::iterator_t<Base> base() const { return this->_current; }

    constexpr value_type operator*() const {
        assert(this->_current != this->_end);
        return std::views::take(std::ranges::subrange(this->_current, this->_end), this->_n);
    }

    constexpr iterator &operator++() {
        assert(this->_current != this->_end);
        this->_missing = std::ranges::advance(this->_current, this->_n, this->_end);
        return *this;
    }

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

    constexpr iterator &operator--()
        requires std::ranges::bidirectional_range<Base>
    {
        std::ranges::advance(this->_current, this->_missing - this->_n);
        this->_missing = 0;
        return *this;
    }

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

    constexpr iterator &operator+=(difference_type lhs)
        requires std::ranges::random_access_range<Base>
    {
        if(lhs > 0) {
            assert(std::ranges::distance(this->_current, this->_end) > this->_n * (lhs - 1));
            this->_missing = std::ranges::advance(this->_current, this->_n * lhs, this->_end);
        }
        else if(lhs < 0) {
            std::ranges::advance(this->_current, this->_n * lhs + this->_missing);
            this->_missing = 0;
        }

        return *this;
    }

    constexpr iterator &operator-=(difference_type lhs)
        requires std::ranges::random_access_range<Base>
    {
        return *this += -lhs;
    }

    constexpr value_type operator[](difference_type n) const
        requires std::ranges::random_access_range<Base>
    {
        return *(*this + n);
    }

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

    friend constexpr bool operator==(const iterator &lhs, std::default_sentinel_t) {
        return lhs._current == lhs._end;
    }

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

    friend constexpr bool operator>(const iterator &lhs, const iterator &rhs)
        requires std::ranges::random_access_range<Base>
    {
        return rhs < lhs;
    }

    friend constexpr bool operator<=(const iterator &lhs, const iterator &rhs)
        requires std::ranges::random_access_range<Base>
    {
        return !(rhs < lhs);
    }

    friend constexpr bool operator>=(const iterator &lhs, const iterator &rhs)
        requires std::ranges::random_access_range<Base>
    {
        return !(lhs < rhs);
    }

    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+(const 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, const 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>>
    {
        return (lhs._current - rhs._current + lhs._missing - rhs._missing) / lhs._n;
    }

    friend constexpr difference_type operator-(std::default_sentinel_t rhs, const iterator &lhs)
        requires std::sized_sentinel_for<std::ranges::sentinel_t<Base>, std::ranges::iterator_t<Base>>
    {
        return div_ceil(lhs._end - lhs._current, lhs._n);
    }

    friend constexpr difference_type operator-(const iterator &lhs, std::default_sentinel_t rhs)
        requires std::sized_sentinel_for<std::ranges::sentinel_t<Base>, std::ranges::iterator_t<Base>>
    {
        return -(rhs - lhs);
    }
};


namespace views {
namespace internal {


template<class Range, typename Diff>
concept can_chunk_view = requires { chunk_view(std::declval<Range>(), std::declval<Diff>()); };


} // namespace internal


struct Chunk : adaptor::range_adaptor<Chunk> {
    template<std::ranges::viewable_range Range, class Diff = std::ranges::range_difference_t<Range>>
        requires internal::can_chunk_view<Range, Diff>
    constexpr auto operator() [[nodiscard]] (Range &&res, std::type_identity_t<Diff> n) const {
        return chunk_view(std::forward<Range>(res), n);
    }

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

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


inline constexpr Chunk chunk;


} // namespace views

} // namespace uni


namespace std::ranges {


template<class View>
inline constexpr bool enable_borrowed_range<uni::chunk_view<View>> = forward_range<View> && enable_borrowed_range<View>;


} // namespace std::ranges