aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/adaptor.h
blob: 80249a41e1e2d864b89cf07be05667daa5083074 (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
#pragma once

#include "store_policy.h"
#include "typetraits.h"

namespace NPrivate {
    template <class Range>
    class TReverseRangeStorage {
    public:
        TReverseRangeStorage(Range&& range)
            : Base_(std::forward<Range>(range))
        {
        }

        decltype(auto) Base() const {
            return *Base_.Ptr();
        }

        decltype(auto) Base() {
            return *Base_.Ptr();
        }

    private:
        TAutoEmbedOrPtrPolicy<Range> Base_;
    };

    template <class Range>
    constexpr bool HasReverseIterators(i32, decltype(std::declval<Range>().rbegin())*) {
        return true;
    }

    template <class Range>
    constexpr bool HasReverseIterators(char, std::nullptr_t*) {
        return false;
    }

    template <class Range, bool hasReverseIterators = HasReverseIterators<Range>((i32)0, nullptr)>
    class TReverseRangeBase: public TReverseRangeStorage<Range> {
        using TBase = TReverseRangeStorage<Range>;

    public:
        using TBase::Base;
        using TBase::TBase;

        auto begin() const {
            return Base().rbegin();
        }

        auto end() const {
            return Base().rend();
        }

        auto begin() {
            return Base().rbegin();
        }

        auto end() {
            return Base().rend();
        }
    };

    template <class Range>
    class TReverseRangeBase<Range, false>: public TReverseRangeStorage<Range> {
        using TBase = TReverseRangeStorage<Range>;

    public:
        using TBase::Base;
        using TBase::TBase;

        auto begin() const {
            using std::end;
            return std::make_reverse_iterator(end(Base()));
        }

        auto end() const {
            using std::begin;
            return std::make_reverse_iterator(begin(Base()));
        }

        auto begin() {
            using std::end;
            return std::make_reverse_iterator(end(Base()));
        }

        auto end() {
            using std::begin;
            return std::make_reverse_iterator(begin(Base()));
        }
    };

    template <class Range>
    class TReverseRange: public TReverseRangeBase<Range> {
        using TBase = TReverseRangeBase<Range>;

    public:
        using TBase::Base;
        using TBase::TBase;

        TReverseRange(TReverseRange&&) = default;
        TReverseRange(const TReverseRange&) = default;

        auto rbegin() const {
            using std::begin;
            return begin(Base());
        }

        auto rend() const {
            using std::end;
            return end(Base());
        }

        auto rbegin() {
            using std::begin;
            return begin(Base());
        }

        auto rend() {
            using std::end;
            return end(Base());
        }
    };
} // namespace NPrivate

/**
 * Provides a reverse view into the provided container.
 *
 * Example usage:
 * @code
 * for(auto&& value: Reversed(container)) {
 *     // use value here.
 * }
 * @endcode
 *
 * @param cont                          Container to provide a view into. Must be an lvalue.
 * @returns                             A reverse view into the provided container.
 */
template <class Range>
constexpr ::NPrivate::TReverseRange<Range> Reversed(Range&& range) {
    return ::NPrivate::TReverseRange<Range>(std::forward<Range>(range));
}