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
#pragma once


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


#include "snippet/aliases.hpp"
#include "internal/types.hpp"

#include "adaptor/vector.hpp"


namespace uni {

template<class T, class container = vector<std::pair<T,internal::size_t>>>
struct run_length : container {
    run_length() noexcept(NO_EXCEPT) = default;

    template<std::input_iterator I, std::sentinel_for<I> S>
    run_length(I first, S last) noexcept(NO_EXCEPT) {
        this->clear();
        typename container::value_type::second_type cnt = 0;
        for(I itr=first, prev=itr; itr!=last; ++itr) {
            if(*prev != *itr) this->emplace_back(*prev, cnt), cnt = 0;
            ++cnt;
            prev = itr;
        }
        this->emplace_back(*std::ranges::prev(last), cnt);
    }

    template<std::ranges::input_range R>
    explicit run_length(R&& range) : run_length(ALL(range)) {};
};

template<std::input_iterator I, std::sentinel_for<I> S>
run_length(I, S) -> run_length<std::iter_value_t<I>>;

template<std::ranges::input_range R>
explicit run_length(R&& range) -> run_length<std::ranges::range_value_t<R>>;


} // namespace uni