aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/ranges.h
blob: 0f53fb1f8288b1cd22476bec0f87eca1f8889bfd (plain)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include <ranges>

#include <QMetaEnum>

namespace Utils {

namespace ranges {

template<typename ENUMTYPE>
struct MetaEnum
{
    struct Iterator
    {
        using iterator_category = std::forward_iterator_tag;
        using value_type = int;
        using difference_type = std::ptrdiff_t;
        using pointer = int *;
        using reference = int &;

        Iterator() = default;

        Iterator(const QMetaEnum *e, int idx)
            : m_enum(e)
            , m_index(idx)
        {}

        int operator*() const { return m_enum->value(m_index); }
        Iterator &operator++()
        {
            ++m_index;
            return *this;
        }
        Iterator operator++(int) // post-incrementable, returns prev value
        {
            Iterator temp = *this;
            ++*this;
            return temp;
        }

        bool operator!=(const Iterator &other) const
        {
            return m_index != other.m_index && m_enum == other.m_enum;
        }
        bool operator==(const Iterator &other) const
        {
            return m_index == other.m_index && m_enum == other.m_enum;
        }

        const QMetaEnum *m_enum{nullptr};
        int m_index{-1};
    };

    using value_type = int;

    MetaEnum()
        : m_enum(QMetaEnum::fromType<ENUMTYPE>())
    {}
    Iterator begin() const { return Iterator(&m_enum, 0); }
    Iterator end() const { return Iterator(&m_enum, m_enum.keyCount()); }
    size_t size() const { return m_enum.keyCount(); }

    QMetaEnum m_enum;
};

} // namespace ranges

} // namespace Utils

#if defined(__cpp_lib_ranges_cache_latest) && __cpp_lib_ranges >= 202411L
#  include <ranges>

namespace views {
using std::views::cache_latest_view;
}

} // namespace Utils
#else

namespace Utils::ranges {

namespace Internal {
template<typename Type>
constexpr Type &as_lvalue(Type &&type)
{
    return static_cast<Type &>(type);
}

template<class Type>
    requires(std::is_object_v<Type>)
class non_propagating_cache : public std::optional<Type>
{
public:
    using std::optional<Type>::operator=;
    non_propagating_cache() = default;

    constexpr non_propagating_cache(std::nullopt_t) noexcept
        : std::optional<Type>{}
    {}

    constexpr non_propagating_cache(const non_propagating_cache &) noexcept
        : std::optional<Type>{}
    {}

    constexpr non_propagating_cache(non_propagating_cache &&other) noexcept
        : std::optional<Type>{}
    {
        other.reset();
    }

    constexpr non_propagating_cache &operator=(const non_propagating_cache &other) noexcept
    {
        if (std::addressof(other) != this)
            this->reset();
        return *this;
    }

    constexpr non_propagating_cache &operator=(non_propagating_cache &&other) noexcept
    {
        this->reset();
        other.reset();
        return *this;
    }

    template<class Iterator>
    constexpr Type &emplace_deref(const Iterator &i)
    {
        return this->emplace(*i);
    }
};
} // namespace Internal

template<std::ranges::input_range Range>
    requires std::ranges::view<Range>
class cache_latest_view : public std::ranges::view_interface<cache_latest_view<Range>>
{
    using cache_t = std::conditional_t<std::is_reference_v<std::ranges::range_reference_t<Range>>,
                                       std::add_pointer_t<std::ranges::range_reference_t<Range>>,
                                       std::ranges::range_reference_t<Range>>;

    Range m_base = Range{};

    Internal::non_propagating_cache<cache_t> m_cache;

public:
    class Sentinel;

    class Iterator
    {
        cache_latest_view *m_view = nullptr;

        constexpr explicit Iterator(cache_latest_view &view)
            : m_view(std::addressof(view))
            , m_current(std::ranges::begin(view.m_base))
        {}

        friend cache_latest_view;
        friend Sentinel;

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

        std::ranges::iterator_t<Range> m_current; // needs to be public because otherwise old compiler complain

        Iterator(Iterator &&) = default;

        Iterator &operator=(Iterator &&) = default;

        constexpr std::ranges::iterator_t<Range> base() && { return std::move(m_current); }

        constexpr const std::ranges::iterator_t<Range> &base() const & noexcept
        {
            return m_current;
        }

        constexpr Iterator &operator++()
        {
            m_view->m_cache.reset();
            ++m_current;
            return *this;
        }

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

        constexpr std::ranges::range_reference_t<Range> &operator*() const
        {
            if constexpr (std::is_reference_v<std::ranges::range_reference_t<Range>>) {
                if (!m_view->m_cache)
                    m_view->m_cache = std::addressof(Internal::as_lvalue(*m_current));
                return **m_view->m_cache;
            } else {
                if (!m_view->m_cache)
                    m_view->m_cache.emplace_deref(m_current);
                return *m_view->m_cache;
            }
        }

        friend constexpr std::ranges::range_rvalue_reference_t<Range> iter_move(
            const Iterator &iterator) noexcept(noexcept(std::ranges::iter_move(iterator.m_current)))
        {
            return std::ranges::iter_move(iterator.m_current);
        }

        friend constexpr void iter_swap(const Iterator &first, const Iterator &second) noexcept(
            noexcept(std::ranges::iter_swap(first.m_current, second.m_current)))
            requires std::indirectly_swappable<std::ranges::iterator_t<Range>>
        {
            std::ranges::iter_swap(first.m_current, second.m_current);
        }
    };

    class Sentinel
    {
        std::ranges::sentinel_t<Range> m_end = std::ranges::sentinel_t<Range>();

        constexpr explicit Sentinel(cache_latest_view &view)
            : m_end(std::ranges::end(view.m_base))
        {}

        friend cache_latest_view;

    public:
        Sentinel() = default;

        constexpr std::ranges::sentinel_t<Range> base() const { return m_end; }

        friend constexpr bool operator==(const Iterator &first, const Sentinel &second)
        {
            return first.m_current == second.m_end;
        }

        friend constexpr std::ranges::range_difference_t<Range> operator-(const Iterator &first,
                                                                          const Sentinel &second)
            requires std::sized_sentinel_for<std::ranges::sentinel_t<Range>, std::ranges::iterator_t<Range>>
        {
            return first.m_current - second.m_end;
        }

        friend constexpr std::ranges::range_difference_t<Range> operator-(const Sentinel &first,
                                                                          const Iterator &second)
            requires std::sized_sentinel_for<std::ranges::sentinel_t<Range>, std::ranges::iterator_t<Range>>
        {
            return first.m_end - second.m_current;
        }
    };

    cache_latest_view()
        requires std::default_initializable<Range>
    = default;

    constexpr explicit cache_latest_view(Range base)
        : m_base(std::move(base))
    {}

    constexpr Range base() const &
        requires std::copy_constructible<Range>
    {
        return m_base;
    }

    constexpr Range base() && { return std::move(m_base); }

    constexpr auto begin() { return Iterator(*this); }

    constexpr auto end() { return Sentinel(*this); }

    constexpr auto size()
        requires std::ranges::sized_range<Range>
    {
        return std::ranges::size(m_base);
    }

    constexpr auto size() const
        requires std::ranges::sized_range<const Range>
    {
        return std::ranges::size(m_base);
    }
};

template<typename Range>
cache_latest_view(Range &&) -> cache_latest_view<std::views::all_t<Range>>;

template<typename Type>
concept can_cache_latest = requires { cache_latest_view(std::declval<Type>()); };

template<class Type>
    requires std::is_class_v<Type> && std::same_as<Type, std::remove_cv_t<Type>>
struct range_adaptor_closure
{};

namespace Internal {
template<class Function>
struct pipeable : Function,
                  range_adaptor_closure<pipeable<Function>>
{
    constexpr explicit pipeable(Function &&function)
        : Function(std::move(function))
    {}
};

template<class Function, class... Functions>
constexpr auto compose(Function &&arg, Functions &&...args)
{
    return [function = std::forward<Function>(arg),
            ... functions = std::forward<Functions>(args)]<class... Arguments>(
               Arguments &&...arguments) mutable
        requires std::invocable<Function, Arguments...>
    {
        if constexpr (sizeof...(Functions)) {
            return compose(std::forward<Functions>(functions)...)(
                std::invoke(std::forward<Function>(function), std::forward<Arguments>(arguments)...));
        } else {
            return std::invoke(std::forward<Function>(function),
                               std::forward<Arguments>(arguments)...);
        }
    };
}
} // namespace Internal
template<class Type>
Type derived_from_range_adaptor_closure(range_adaptor_closure<Type> *);

template<class Type>
concept RangeAdaptorClosure = !std::ranges::range<std::remove_cvref_t<Type>> && requires {
    {
        derived_from_range_adaptor_closure(static_cast<std::remove_cvref_t<Type> *>(nullptr))
    } -> std::same_as<std::remove_cvref_t<Type>>;
};

template<std::ranges::range Range, RangeAdaptorClosure Closure>
    requires std::invocable<Closure, Range>
[[nodiscard]] constexpr decltype(auto) operator|(Range &&range, Closure &&closure) noexcept(
    std::is_nothrow_invocable_v<Closure, Range>)
{
    return std::invoke(std::forward<Closure>(closure), std::forward<Range>(range));
}

template<RangeAdaptorClosure FirstClosure, RangeAdaptorClosure SecondClosure>
    requires std::constructible_from<std::decay_t<FirstClosure>, FirstClosure>
             && std::constructible_from<std::decay_t<SecondClosure>, SecondClosure>
[[nodiscard]] constexpr auto operator|(FirstClosure &&first, SecondClosure &&second) noexcept(
    std::is_nothrow_constructible_v<std::decay_t<FirstClosure>, FirstClosure>
    && std::is_nothrow_constructible_v<std::decay_t<SecondClosure>, SecondClosure>)
{
    return Internal::pipeable(
        Internal::compose(std::forward<FirstClosure>(second), std::forward<SecondClosure>(first)));
}

namespace views {

struct CacheLatestFunctor : range_adaptor_closure<CacheLatestFunctor>
{
#  if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 10 && __GNUC_MINOR__ < 5
    template<std::ranges::viewable_range Range>
    constexpr auto operator() [[nodiscard]] (Range &&range) const
    {
        return std::forward<Range>(range);
    }
#  else
    template<std::ranges::viewable_range Range>
        requires can_cache_latest<Range>
    constexpr auto operator() [[nodiscard]] (Range &&range) const
    {
        return cache_latest_view(std::forward<Range>(range));
    }
#  endif
};

inline constexpr CacheLatestFunctor cache_latest;
} // namespace views
} // namespace Utils::ranges

namespace Utils::views {
using namespace Utils::ranges::views;
} // namespace Utils::views

#endif