VCCC  2024.05
VisualCamp Common C++ library
drop_view.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 2024/01/28.
3 //
4 
5 #ifndef VCCC_RANGES_VIEWS_DROP_VIEW_HPP
6 #define VCCC_RANGES_VIEWS_DROP_VIEW_HPP
7 
8 #include <algorithm>
9 #include <type_traits>
10 #include <utility>
11 
13 #include "vccc/__iterator/next.hpp"
14 #include "vccc/optional.hpp"
16 #include "vccc/__ranges/begin.hpp"
20 #include "vccc/__ranges/range.hpp"
22 #include "vccc/__ranges/size.hpp"
25 #include "vccc/__ranges/view.hpp"
28 
29 namespace vccc {
30 namespace ranges {
31 namespace detail {
32 
33 template<typename V, bool = conjunction<simple_view<V>, sized_range<V>, random_access_range<V> >::value /* true */>
34 class drop_view_cached_begin {
35  public:
36  // Random access range. No cache required
37  constexpr auto begin(const V& base, range_difference_t<V> count) const {
38  return ranges::next(ranges::begin(base), count, ranges::end(base));
39  }
40 };
41 
42 template<typename V>
43 class drop_view_cached_begin<V, false> {
44  public:
45  // Not a random access range. Cache required
46  constexpr auto begin(const V& base, range_difference_t<V> count) {
47  if (!begin_.has_value()) {
48  begin_ = ranges::next(ranges::begin(base), count, ranges::end(base));
49  }
50  return begin_;
51  }
52 
53  private:
54  optional<iterator_t<V>> begin_;
55 };
56 
57 } // namespace detail
58 
61 
62 template<typename V>
63 class drop_view
64  : public ranges::view_interface<drop_view<V>>
65  , public detail::drop_view_cached_begin<V>
66 {
67  using begin_base = detail::drop_view_cached_begin<V>;
68 
69  public:
70  static_assert(view<V>::value, "Constraints not satisfied");
71 
72  drop_view() = default;
73 
74  constexpr explicit drop_view(V base, range_difference_t<V> count)
75  : base_(std::move(base)), count_(count) {}
76 
77 
79  constexpr V base() const& noexcept(std::is_nothrow_copy_constructible<V>::value) {
80  return base_;
81  }
82 
84  return std::move(base_);
85  }
86 
87  template<typename V2 = V, std::enable_if_t<conjunction<
91  >::value == false, int> = 0>
92  constexpr auto begin() {
93  return begin_base::begin(base_, count_);
94  }
95 
96  template<typename V2 = V, std::enable_if_t<conjunction<
99  >::value, int> = 0>
100  constexpr auto begin() const {
101  return begin_base::begin(base_, count_);
102  }
103 
105  constexpr auto end() {
106  return ranges::end(base_);
107  }
108 
110  constexpr auto end() const {
111  return ranges::end(base_);
112  }
113 
115  constexpr auto size() {
116  const auto s = ranges::size(base_);
117  const auto c = static_cast<decltype(s)>(count_);
118  return s < c ? 0 : s - c;
119  }
120 
122  constexpr auto size() const {
123  const auto s = ranges::size(base_);
124  const auto c = static_cast<decltype(s)>(count_);
125  return s < c ? 0 : s - c;
126  }
127 
128  private:
129  V base_;
130  range_difference_t<V> count_ = 0;
131 };
132 
133 template<typename R>
134 constexpr drop_view<views::all_t<R>>
136  return drop_view<views::all_t<R>>(std::forward<R>(r), count);
137 }
138 
139 #if __cplusplus >= 201703L
140 
141 template<typename R>
142 drop_view(R&&, range_difference_t<R>) -> drop_view<views::all_t<R>>;
143 
144 #endif
145 
146 template<typename T>
148 
150 
151 } // namespace ranges
152 } // namespace vccc
153 
154 #endif // VCCC_RANGES_VIEWS_DROP_VIEW_HPP
Definition: drop_view.hpp:66
constexpr V base() const &noexcept(std::is_nothrow_copy_constructible< V >::value)
Definition: drop_view.hpp:79
constexpr auto size() const
Definition: drop_view.hpp:122
constexpr auto size()
Definition: drop_view.hpp:115
constexpr auto end()
Definition: drop_view.hpp:105
constexpr auto end() const
Definition: drop_view.hpp:110
constexpr auto begin() const
Definition: drop_view.hpp:100
constexpr drop_view(V base, range_difference_t< V > count)
Definition: drop_view.hpp:74
constexpr V base() &&noexcept(std::is_nothrow_move_constructible< V >::value)
Definition: drop_view.hpp:83
constexpr auto begin()
Definition: drop_view.hpp:92
helper class template for defining a view, using the curiously recurring template pattern
Definition: view_interface.hpp:78
constexpr VCCC_INLINE_OR_STATIC detail::count_niebloid count
Definition: count.hpp:58
constexpr VCCC_INLINE_OR_STATIC detail::next_niebloid next
Definition: next.hpp:65
constexpr drop_view< views::all_t< R > > make_drop_view(R &&r, range_difference_t< R > count)
Definition: drop_view.hpp:135
constexpr VCCC_INLINE_OR_STATIC detail::begin_niebloid begin
returns an iterator to the beginning of a range
Definition: begin.hpp:116
constexpr VCCC_INLINE_OR_STATIC detail::size_niebloid size
returns the size of a container or array
Definition: size.hpp:145
constexpr VCCC_INLINE_OR_STATIC detail::end_niebloid end
returns a sentinel indicating the end of a range
Definition: end.hpp:120
typename range_difference< R >::type range_difference_t
Used to obtain the difference type of the iterator type of range type R.
Definition: range_difference_t.hpp:41
Definition: matrix.hpp:495
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35
Definition: conjunction.hpp:22
Definition: enable_borrowed_range.hpp:17
specifies a range whose iterator type satisfies random_access_iterator
Definition: random_access_range.hpp:48
Definition: simple_view.hpp:28
specifies that a range knows its size in constant time
Definition: sized_range.hpp:38
specifies that a range is a view, that is, it has constant time copy/move/assignment
Definition: view.hpp:31