1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___RANGES_FILTER_VIEW_H
11#define _LIBCPP___RANGES_FILTER_VIEW_H
12
13#include <__algorithm/ranges_find_if.h>
14#include <__concepts/constructible.h>
15#include <__concepts/copyable.h>
16#include <__concepts/derived_from.h>
17#include <__concepts/equality_comparable.h>
18#include <__config>
19#include <__debug>
20#include <__functional/bind_back.h>
21#include <__functional/invoke.h>
22#include <__functional/reference_wrapper.h>
23#include <__iterator/concepts.h>
24#include <__iterator/iter_move.h>
25#include <__iterator/iter_swap.h>
26#include <__iterator/iterator_traits.h>
27#include <__memory/addressof.h>
28#include <__ranges/access.h>
29#include <__ranges/all.h>
30#include <__ranges/concepts.h>
31#include <__ranges/copyable_box.h>
32#include <__ranges/non_propagating_cache.h>
33#include <__ranges/range_adaptor.h>
34#include <__ranges/view_interface.h>
35#include <__utility/forward.h>
36#include <__utility/in_place.h>
37#include <__utility/move.h>
38#include <type_traits>
39
40#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
41#  pragma GCC system_header
42#endif
43
44_LIBCPP_BEGIN_NAMESPACE_STD
45
46#if _LIBCPP_STD_VER > 17
47
48namespace ranges {
49  template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
50    requires view<_View> && is_object_v<_Pred>
51  class filter_view : public view_interface<filter_view<_View, _Pred>> {
52    _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
53    _LIBCPP_NO_UNIQUE_ADDRESS __copyable_box<_Pred> __pred_;
54
55    // We cache the result of begin() to allow providing an amortized O(1) begin() whenever
56    // the underlying range is at least a forward_range.
57    static constexpr bool _UseCache = forward_range<_View>;
58    using _Cache = _If<_UseCache, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
59    _LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache();
60
61    class __iterator;
62    class __sentinel;
63
64  public:
65    _LIBCPP_HIDE_FROM_ABI
66    filter_view() requires default_initializable<_View> && default_initializable<_Pred> = default;
67
68    _LIBCPP_HIDE_FROM_ABI
69    constexpr filter_view(_View __base, _Pred __pred)
70      : __base_(std::move(__base)), __pred_(in_place, std::move(__pred))
71    { }
72
73    template<class _Vp = _View>
74    _LIBCPP_HIDE_FROM_ABI
75    constexpr _View base() const& requires copy_constructible<_Vp> { return __base_; }
76    _LIBCPP_HIDE_FROM_ABI
77    constexpr _View base() && { return std::move(__base_); }
78
79    _LIBCPP_HIDE_FROM_ABI
80    constexpr _Pred const& pred() const { return *__pred_; }
81
82    _LIBCPP_HIDE_FROM_ABI
83    constexpr __iterator begin() {
84      _LIBCPP_ASSERT(__pred_.__has_value(), "Trying to call begin() on a filter_view that does not have a valid predicate.");
85      if constexpr (_UseCache) {
86        if (!__cached_begin_.__has_value()) {
87          __cached_begin_.__emplace(ranges::find_if(__base_, std::ref(*__pred_)));
88        }
89        return {*this, *__cached_begin_};
90      } else {
91        return {*this, ranges::find_if(__base_, std::ref(*__pred_))};
92      }
93    }
94
95    _LIBCPP_HIDE_FROM_ABI
96    constexpr auto end() {
97      if constexpr (common_range<_View>)
98        return __iterator{*this, ranges::end(__base_)};
99      else
100        return __sentinel{*this};
101    }
102  };
103
104  template<class _Range, class _Pred>
105  filter_view(_Range&&, _Pred) -> filter_view<views::all_t<_Range>, _Pred>;
106
107  template<class _View>
108  struct __filter_iterator_category { };
109
110  template<forward_range _View>
111  struct __filter_iterator_category<_View> {
112    using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category;
113    using iterator_category =
114      _If<derived_from<_Cat, bidirectional_iterator_tag>, bidirectional_iterator_tag,
115      _If<derived_from<_Cat, forward_iterator_tag>,       forward_iterator_tag,
116      /* else */                                          _Cat
117    >>;
118  };
119
120  template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
121    requires view<_View> && is_object_v<_Pred>
122  class filter_view<_View, _Pred>::__iterator : public __filter_iterator_category<_View> {
123
124  public:
125    _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __current_ = iterator_t<_View>();
126    _LIBCPP_NO_UNIQUE_ADDRESS filter_view* __parent_ = nullptr;
127
128    using iterator_concept =
129      _If<bidirectional_range<_View>, bidirectional_iterator_tag,
130      _If<forward_range<_View>,       forward_iterator_tag,
131      /* else */                      input_iterator_tag
132    >>;
133    // using iterator_category = inherited;
134    using value_type = range_value_t<_View>;
135    using difference_type = range_difference_t<_View>;
136
137    _LIBCPP_HIDE_FROM_ABI
138    __iterator() requires default_initializable<iterator_t<_View>> = default;
139
140    _LIBCPP_HIDE_FROM_ABI
141    constexpr __iterator(filter_view& __parent, iterator_t<_View> __current)
142      : __current_(std::move(__current)), __parent_(std::addressof(__parent))
143    { }
144
145    _LIBCPP_HIDE_FROM_ABI
146    constexpr iterator_t<_View> const& base() const& noexcept { return __current_; }
147    _LIBCPP_HIDE_FROM_ABI
148    constexpr iterator_t<_View> base() && { return std::move(__current_); }
149
150    _LIBCPP_HIDE_FROM_ABI
151    constexpr range_reference_t<_View> operator*() const { return *__current_; }
152    _LIBCPP_HIDE_FROM_ABI
153    constexpr iterator_t<_View> operator->() const
154      requires __has_arrow<iterator_t<_View>> && copyable<iterator_t<_View>>
155    {
156      return __current_;
157    }
158
159    _LIBCPP_HIDE_FROM_ABI
160    constexpr __iterator& operator++() {
161      __current_ = ranges::find_if(std::move(++__current_), ranges::end(__parent_->__base_),
162                                   std::ref(*__parent_->__pred_));
163      return *this;
164    }
165    _LIBCPP_HIDE_FROM_ABI
166    constexpr void operator++(int) { ++*this; }
167    _LIBCPP_HIDE_FROM_ABI
168    constexpr __iterator operator++(int) requires forward_range<_View> {
169      auto __tmp = *this;
170      ++*this;
171      return __tmp;
172    }
173
174    _LIBCPP_HIDE_FROM_ABI
175    constexpr __iterator& operator--() requires bidirectional_range<_View> {
176      do {
177        --__current_;
178      } while (!std::invoke(*__parent_->__pred_, *__current_));
179      return *this;
180    }
181    _LIBCPP_HIDE_FROM_ABI
182    constexpr __iterator operator--(int) requires bidirectional_range<_View> {
183      auto tmp = *this;
184      --*this;
185      return tmp;
186    }
187
188    _LIBCPP_HIDE_FROM_ABI
189    friend constexpr bool operator==(__iterator const& __x, __iterator const& __y)
190      requires equality_comparable<iterator_t<_View>>
191    {
192      return __x.__current_ == __y.__current_;
193    }
194
195    _LIBCPP_HIDE_FROM_ABI
196    friend constexpr range_rvalue_reference_t<_View> iter_move(__iterator const& __it)
197      noexcept(noexcept(ranges::iter_move(__it.__current_)))
198    {
199      return ranges::iter_move(__it.__current_);
200    }
201
202    _LIBCPP_HIDE_FROM_ABI
203    friend constexpr void iter_swap(__iterator const& __x, __iterator const& __y)
204      noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_)))
205      requires indirectly_swappable<iterator_t<_View>>
206    {
207      return ranges::iter_swap(__x.__current_, __y.__current_);
208    }
209  };
210
211  template<input_range _View, indirect_unary_predicate<iterator_t<_View>> _Pred>
212    requires view<_View> && is_object_v<_Pred>
213  class filter_view<_View, _Pred>::__sentinel {
214  public:
215    sentinel_t<_View> __end_ = sentinel_t<_View>();
216
217    _LIBCPP_HIDE_FROM_ABI
218    __sentinel() = default;
219
220    _LIBCPP_HIDE_FROM_ABI
221    constexpr explicit __sentinel(filter_view& __parent)
222      : __end_(ranges::end(__parent.__base_))
223    { }
224
225    _LIBCPP_HIDE_FROM_ABI
226    constexpr sentinel_t<_View> base() const { return __end_; }
227
228    _LIBCPP_HIDE_FROM_ABI friend constexpr bool
229    operator==(__iterator const& __x, __sentinel const& __y) {
230      return __x.__current_ == __y.__end_;
231    }
232  };
233
234namespace views {
235namespace __filter {
236  struct __fn {
237    template<class _Range, class _Pred>
238    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
239    constexpr auto operator()(_Range&& __range, _Pred&& __pred) const
240      noexcept(noexcept(filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
241      -> decltype(      filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)))
242      { return          filter_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)); }
243
244    template<class _Pred>
245      requires constructible_from<decay_t<_Pred>, _Pred>
246    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
247    constexpr auto operator()(_Pred&& __pred) const
248      noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>)
249    { return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred))); }
250  };
251} // namespace __filter
252
253inline namespace __cpo {
254  inline constexpr auto filter = __filter::__fn{};
255} // namespace __cpo
256} // namespace views
257
258} // namespace ranges
259
260#endif // _LIBCPP_STD_VER > 17
261
262_LIBCPP_END_NAMESPACE_STD
263
264#endif // _LIBCPP___RANGES_FILTER_VIEW_H
265