aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/iterator/concatenate.h
blob: 64d2cd451a3287b3492c67e68c3c28c6087b007c (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
#pragma once

#include <util/generic/store_policy.h>

#include <iterator>
#include <tuple>


namespace NPrivate {

    template <typename TValue_, typename... TContainers>
    struct TConcatenator {
        template <std::size_t... I>
        struct TConcatenatorWithIndex {
        private:
            using THolders = std::tuple<TAutoEmbedOrPtrPolicy<TContainers>...>;
            using TValue = TValue_;
            using TIteratorState = std::tuple<decltype(std::begin(std::declval<TContainers&>()))...>;
            using TSentinelState = std::tuple<decltype(std::end(std::declval<TContainers&>()))...>;

            struct TIterator;
            struct TSentinelCandidate {
                TSentinelState Iterators_;
                std::size_t Position_;
                THolders* HoldersPtr_;
            };
            using TSentinel = std::conditional_t<std::is_same_v<TIteratorState, TSentinelState>,
                                                 TIterator, TSentinelCandidate>;

            struct TIterator {
            private:
                friend struct TConcatenatorWithIndex<I...>;

                // important, that it is a static function, compiler better optimizes such code
                template <std::size_t index = 0, typename TMaybeConstIteratorState>
                static TValue GetCurrentValue(std::size_t position, TMaybeConstIteratorState& iterators) {
                    if constexpr (index >= sizeof...(TContainers)) {
                        // never happened when use of iterator is correct
                        return *std::get<0>(iterators);
                    } else {
                        if (position == index) {
                            return *std::get<index>(iterators);
                        } else {
                            return GetCurrentValue<index + 1>(position, iterators);
                        }
                    }
                }

                template <bool needIncrement, std::size_t index = 0>
                void MaybeIncrementIteratorAndSkipExhaustedContainers() {
                    if constexpr (index >= sizeof...(TContainers)) {
                        return;
                    } else {
                        if (Position_ == index) {
                            if constexpr (needIncrement) {
                                ++std::get<index>(Iterators_);
                            }
                            if (!(std::get<index>(Iterators_) != std::end(*std::get<index>(*HoldersPtr_).Ptr()))) {
                                ++Position_;
                                MaybeIncrementIteratorAndSkipExhaustedContainers<false, index + 1>();
                            }
                        } else {
                            MaybeIncrementIteratorAndSkipExhaustedContainers<needIncrement, index + 1>();
                        }
                    }
                }
            public:
                using difference_type = std::ptrdiff_t;
                using value_type = TValue;
                using pointer = std::remove_reference_t<TValue>*;
                using reference = std::remove_reference_t<TValue>&;
                using iterator_category = std::input_iterator_tag;

                TValue operator*() {
                    return GetCurrentValue(Position_, Iterators_);
                }
                TValue operator*() const {
                    return GetCurrentValue(Position_, Iterators_);
                }
                TIterator& operator++() {
                    MaybeIncrementIteratorAndSkipExhaustedContainers<true>();
                    return *this;
                }
                bool operator!=(const TSentinel& other) const {
                    // give compiler an opportunity to optimize sentinel case (-70% of time)
                    if (other.Position_ == sizeof...(TContainers)) {
                        return Position_ < sizeof...(TContainers);
                    } else {
                        return (Position_ != other.Position_ ||
                                ((std::get<I>(Iterators_) != std::get<I>(other.Iterators_)) || ...));
                    }
                }
                bool operator==(const TSentinel& other) const {
                    return !(*this != other);
                }

                TIteratorState Iterators_;
                std::size_t Position_;
                THolders* HoldersPtr_;
            };
        public:
            using iterator = TIterator;
            using const_iterator = TIterator;
            using value_type = typename TIterator::value_type;
            using reference = typename TIterator::reference;
            using const_reference = typename TIterator::reference;

            TIterator begin() const {
                TIterator iterator{TIteratorState{std::begin(*std::get<I>(Holders_).Ptr())...}, 0, &Holders_};
                iterator.template MaybeIncrementIteratorAndSkipExhaustedContainers<false>();
                return iterator;
            }

            TSentinel end() const {
                return {TSentinelState{std::end(*std::get<I>(Holders_).Ptr())...}, sizeof...(TContainers), &Holders_};
            }

            mutable THolders Holders_;
        };

        template <std::size_t... I>
        static auto Concatenate(TContainers&&... containers, std::index_sequence<I...>) {
            return TConcatenatorWithIndex<I...>{{std::forward<TContainers>(containers)...}};
        }
    };

}


//! Usage: for (auto x : Concatenate(a, b)) {...}
template <typename TFirstContainer, typename... TContainers>
auto Concatenate(TFirstContainer&& container, TContainers&&... containers) {
    return NPrivate::TConcatenator<decltype(*std::begin(container)), TFirstContainer, TContainers...>::Concatenate(
        std::forward<TFirstContainer>(container), std::forward<TContainers>(containers)...,
        std::make_index_sequence<sizeof...(TContainers) + 1>{});
}