aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/iterator/mapped.h
blob: 6a4522e865406d4506ab35f33afb02dba7dbe98e (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
#pragma once

#include <util/generic/iterator_range.h>
#include <util/generic/store_policy.h>

#include <iterator>


namespace NIteratorPrivate {
    template <class TIterator>
    constexpr bool HasRandomAccess() {
        return std::is_same_v<typename std::iterator_traits<TIterator>::iterator_category,
                              std::random_access_iterator_tag>;
    }
}


template <class TIterator, class TMapper>
class TMappedIterator {
protected:
    using TSelf = TMappedIterator<TIterator, TMapper>;
    using TSrcPointerType = typename std::iterator_traits<TIterator>::reference;
    using TValue = typename std::invoke_result_t<TMapper, TSrcPointerType>;
public:
    using difference_type = std::ptrdiff_t;
    using value_type = TValue;
    using reference = TValue&;
    using pointer = std::remove_reference_t<TValue>*;
    using iterator_category = std::conditional_t<NIteratorPrivate::HasRandomAccess<TIterator>(),
        std::random_access_iterator_tag, std::input_iterator_tag>;

    TMappedIterator(TIterator it, TMapper mapper)
        : Iter(it)
        , Mapper(std::move(mapper))
    {
    }

    TSelf& operator++() {
        ++Iter;
        return *this;
    }
    TSelf& operator--() {
        --Iter;
        return *this;
    }
    TValue operator*() {
        return Mapper((*Iter));
    }
    TValue operator*() const {
        return Mapper((*Iter));
    }

    pointer operator->() const {
        return &(Mapper((*Iter)));
    }

    TValue operator[](difference_type n) const {
        return Mapper(*(Iter + n));
    }
    TSelf& operator+=(difference_type n) {
        Iter += n;
        return *this;
    }
    TSelf& operator-=(difference_type n) {
        Iter -= n;
        return *this;
    }
    TSelf operator+(difference_type n) const {
        return TSelf(Iter + n, Mapper);
    }
    TSelf operator-(difference_type n) const {
        return TSelf(Iter - n, Mapper);
    }
    difference_type operator-(const TSelf& other) const {
        return Iter - other.Iter;
    }
    bool operator==(const TSelf& other) const {
        return Iter == other.Iter;
    }
    bool operator!=(const TSelf& other) const {
        return Iter != other.Iter;
    }
    bool operator>(const TSelf& other) const {
        return Iter > other.Iter;
    }
    bool operator<(const TSelf& other) const {
        return Iter < other.Iter;
    }

private:
    TIterator Iter;
    TMapper Mapper;
};


template <class TContainer, class TMapper>
class TInputMappedRange {
protected:
    using TContainerStorage = TAutoEmbedOrPtrPolicy<TContainer>;
    using TMapperStorage = TAutoEmbedOrPtrPolicy<TMapper>;
    using TMapperWrapper = std::reference_wrapper<std::remove_reference_t<TMapper>>;
    using TInternalIterator = decltype(std::begin(std::declval<TContainer&>()));
    using TIterator = TMappedIterator<TInternalIterator, TMapperWrapper>;
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;

    TInputMappedRange(TContainer&& container, TMapper&& mapper)
        : Container(std::forward<TContainer>(container))
        , Mapper(std::forward<TMapper>(mapper))
    {
    }

    TIterator begin() const {
        return {std::begin(*Container.Ptr()), {*Mapper.Ptr()}};
    }

    TIterator end() const {
        return {std::end(*Container.Ptr()), {*Mapper.Ptr()}};
    }

    bool empty() const {
        return std::begin(*Container.Ptr()) == std::end(*Container.Ptr());
    }

protected:
    mutable TContainerStorage Container;
    mutable TMapperStorage Mapper;
};


template <class TContainer, class TMapper>
class TRandomAccessMappedRange : public TInputMappedRange<TContainer, TMapper> {
    using TBase = TInputMappedRange<TContainer, TMapper>;
    using TInternalIterator = typename TBase::TInternalIterator;
    using TIterator = typename TBase::TIterator;
public:
    using iterator = typename TBase::iterator;
    using const_iterator = typename TBase::const_iterator;
    using value_type = typename TBase::value_type;
    using reference = typename TBase::reference;
    using const_reference = typename TBase::const_reference;

    using difference_type = typename std::iterator_traits<iterator>::difference_type;
    using size_type = std::size_t;

    TRandomAccessMappedRange(TContainer&& container, TMapper&& mapper)
        : TBase(std::forward<TContainer>(container), std::forward<TMapper>(mapper))
    {
    }

    using TBase::begin;
    using TBase::end;
    using TBase::empty;

    size_type size() const {
        return std::end(*this->Container.Ptr()) - std::begin(*this->Container.Ptr());
    }

    const_reference operator[](size_t at) const {
        Y_ASSERT(at < this->size());

        return *(this->begin() + at);
    }

    reference operator[](size_t at) {
        Y_ASSERT(at < this->size());

        return *(this->begin() + at);
    }
};

template <class TIterator, class TMapper>
TMappedIterator<TIterator, TMapper> MakeMappedIterator(TIterator iter, TMapper mapper) {
    return {iter, mapper};
}

template <class TIterator, class TMapper>
auto MakeMappedRange(TIterator begin, TIterator end, TMapper mapper) {
    return MakeIteratorRange(MakeMappedIterator(begin, mapper), MakeMappedIterator(end, mapper));
}

template <class TContainer, class TMapper>
auto MakeMappedRange(TContainer&& container, TMapper&& mapper) {
    if constexpr (NIteratorPrivate::HasRandomAccess<decltype(std::begin(container))>()) {
        return TRandomAccessMappedRange<TContainer, TMapper>(std::forward<TContainer>(container), std::forward<TMapper>(mapper));
    } else {
        return TInputMappedRange<TContainer, TMapper>(std::forward<TContainer>(container), std::forward<TMapper>(mapper));
    }
}