aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/libcxx/include/__ranges/filter_view.h
blob: 1cef94ca67446cc1ccb76d2bb5c5843889d86eea (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
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
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___RANGES_FILTER_VIEW_H
#define _LIBCPP___RANGES_FILTER_VIEW_H

#include <__algorithm/ranges_find_if.h>
#include <__assert>
#include <__concepts/constructible.h>
#include <__concepts/copyable.h>
#include <__concepts/derived_from.h>
#include <__concepts/equality_comparable.h>
#include <__config>
#include <__functional/bind_back.h>
#include <__functional/invoke.h>
#include <__functional/reference_wrapper.h>
#include <__iterator/concepts.h>
#include <__iterator/iter_move.h>
#include <__iterator/iter_swap.h>
#include <__iterator/iterator_traits.h>
#include <__memory/addressof.h>
#include <__ranges/access.h>
#include <__ranges/all.h>
#include <__ranges/concepts.h>
#include <__ranges/movable_box.h>
#include <__ranges/non_propagating_cache.h>
#include <__ranges/range_adaptor.h>
#include <__ranges/view_interface.h>
#include <__type_traits/conditional.h>
#include <__type_traits/decay.h>
#include <__type_traits/is_nothrow_constructible.h>
#include <__type_traits/is_object.h>
#include <__utility/forward.h>
#include <__utility/in_place.h>
#include <__utility/move.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

#if _LIBCPP_STD_VER >= 20

namespace ranges {
  template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
    requires view<_View> && is_object_v<_Pred>
  class filter_view : public view_interface<filter_view<_View, _Pred>> {
    _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
    _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_;

    // We cache the result of begin() to allow providing an amortized O(1) begin() whenever
    // the underlying range is at least a forward_range.
    static constexpr bool _UseCache = forward_range<_View>;
    using _Cache = _If<_UseCache, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
    _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache();

    class __iterator;
    class __sentinel;

  public:
    _LIBCPP_HIDE_FROM_ABI
    filter_view() requires default_initializable<_View> && default_initializable<_Pred> = default;

    _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 filter_view(_View __base, _Pred __pred)
        : __base_(std::move(__base)), __pred_(in_place, std::move(__pred)) {}

    template<class _Vp = _View>
    _LIBCPP_HIDE_FROM_ABI
    constexpr _View base() const& requires copy_constructible<_Vp> { return __base_; }
    _LIBCPP_HIDE_FROM_ABI
    constexpr _View base() && { return std::move(__base_); }

    _LIBCPP_HIDE_FROM_ABI
    constexpr _Pred const& pred() const { return *__pred_; }

    _LIBCPP_HIDE_FROM_ABI
    constexpr __iterator begin() {
      _LIBCPP_ASSERT_UNCATEGORIZED(
          __pred_.__has_value(),
          "Trying to call begin() on a filter_view that does not have a valid predicate.");
      if constexpr (_UseCache) {
        if (!__cached_begin_.__has_value()) {
          __cached_begin_.__emplace(ranges::find_if(__base_, std::ref(*__pred_)));
        }
        return {*this, *__cached_begin_};
      } else {
        return {*this, ranges::find_if(__base_, std::ref(*__pred_))};
      }
    }

    _LIBCPP_HIDE_FROM_ABI
    constexpr auto end() {
      if constexpr (common_range<_View>)
        return __iterator{*this, ranges::end(__base_)};
      else
        return __sentinel{*this};
    }
  };

  template<class _Range, class _Pred>
  filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;

  template<class _View>
  struct __filter_iterator_category { };

  template<forward_range _View>
  struct __filter_iterator_category<_View> {
    using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category;
    using iterator_category =
      _If<derived_from<_Cat, bidirectional_iterator_tag>, bidirectional_iterator_tag,
      _If<derived_from<_Cat, forward_iterator_tag>,       forward_iterator_tag,
      /* else */                                          _Cat
    >>;
  };

  template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
    requires view<_View> && is_object_v<_Pred>
  class filter_view<_View, _Pred>::__iterator : public __filter_iterator_category<_View> {

  public:
    _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __current_ = iterator_t<_View>();
    _LIBCPP_NO_UNIQUE_ADDRESS filter_view* __parent_ = nullptr;

    using iterator_concept =
      _If<bidirectional_range<_View>, bidirectional_iterator_tag,
      _If<forward_range<_View>,       forward_iterator_tag,
      /* else */                      input_iterator_tag
    >>;
    // using iterator_category = inherited;
    using value_type = range_value_t<_View>;
    using difference_type = range_difference_t<_View>;

    _LIBCPP_HIDE_FROM_ABI
    __iterator() requires default_initializable<iterator_t<_View>> = default;

    _LIBCPP_HIDE_FROM_ABI
    constexpr __iterator(filter_view& __parent, iterator_t<_View> __current)
      : __current_(std::move(__current)), __parent_(std::addressof(__parent))
    { }

    _LIBCPP_HIDE_FROM_ABI
    constexpr iterator_t<_View> const& base() const& noexcept { return __current_; }
    _LIBCPP_HIDE_FROM_ABI
    constexpr iterator_t<_View> base() && { return std::move(__current_); }

    _LIBCPP_HIDE_FROM_ABI
    constexpr range_reference_t<_View> operator*() const { return *__current_; }
    _LIBCPP_HIDE_FROM_ABI
    constexpr iterator_t<_View> operator->() const
      requires __has_arrow<iterator_t<_View>> && copyable<iterator_t<_View>>
    {
      return __current_;
    }

    _LIBCPP_HIDE_FROM_ABI
    constexpr __iterator& operator++() {
      __current_ = ranges::find_if(std::move(++__current_), ranges::end(__parent_->__base_),
                                   std::ref(*__parent_->__pred_));
      return *this;
    }
    _LIBCPP_HIDE_FROM_ABI
    constexpr void operator++(int) { ++*this; }
    _LIBCPP_HIDE_FROM_ABI
    constexpr __iterator operator++(int) requires forward_range<_View> {
      auto __tmp = *this;
      ++*this;
      return __tmp;
    }

    _LIBCPP_HIDE_FROM_ABI
    constexpr __iterator& operator--() requires bidirectional_range<_View> {
      do {
        --__current_;
      } while (!std::invoke(*__parent_->__pred_, *__current_));
      return *this;
    }
    _LIBCPP_HIDE_FROM_ABI
    constexpr __iterator operator--(int) requires bidirectional_range<_View> {
      auto __tmp = *this;
      --*this;
      return __tmp;
    }

    _LIBCPP_HIDE_FROM_ABI
    friend constexpr bool operator==(__iterator const& __x, __iterator const& __y)
      requires equality_comparable<iterator_t<_View>>
    {
      return __x.__current_ == __y.__current_;
    }

    _LIBCPP_HIDE_FROM_ABI
    friend constexpr range_rvalue_reference_t<_View> iter_move(__iterator const& __it)
      noexcept(noexcept(ranges::iter_move(__it.__current_)))
    {
      return ranges::iter_move(__it.__current_);
    }

    _LIBCPP_HIDE_FROM_ABI
    friend constexpr void iter_swap(__iterator const& __x, __iterator const& __y)
      noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_)))
      requires indirectly_swappable<iterator_t<_View>>
    {
      return ranges::iter_swap(__x.__current_, __y.__current_);
    }
  };

  template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
    requires view<_View> && is_object_v<_Pred>
  class filter_view<_View, _Pred>::__sentinel {
  public:
    sentinel_t<_View> __end_ = sentinel_t<_View>();

    _LIBCPP_HIDE_FROM_ABI
    __sentinel() = default;

    _LIBCPP_HIDE_FROM_ABI
    constexpr explicit __sentinel(filter_view& __parent)
      : __end_(ranges::end(__parent.__base_))
    { }

    _LIBCPP_HIDE_FROM_ABI
    constexpr sentinel_t<_View> base() const { return __end_; }

    _LIBCPP_HIDE_FROM_ABI friend constexpr bool
    operator==(__iterator const& __x, __sentinel const& __y) {
      return __x.__current_ == __y.__end_;
    }
  };

namespace views {
namespace __filter {
  struct __fn {
    template<class _Range, class _Pred>
    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
    constexpr auto operator()(_Range&& __range, _Pred&& __pred) const
      noexcept(noexcept(filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
      -> decltype(      filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)))
      { return          filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)); }

    template<class _Pred>
      requires constructible_from<decay_t<_Pred>, _Pred>
    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
    constexpr auto operator()(_Pred&& __pred) const
      noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>)
    { return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred))); }
  };
} // namespace __filter

inline namespace __cpo {
  inline constexpr auto filter = __filter::__fn{};
} // namespace __cpo
} // namespace views

} // namespace ranges

#endif // _LIBCPP_STD_VER >= 20

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___RANGES_FILTER_VIEW_H