17 #ifndef OPENKALMAN_COMPATIBILITY_VIEWS_REPEAT_HPP 18 #define OPENKALMAN_COMPATIBILITY_VIEWS_REPEAT_HPP 20 #ifndef __cpp_lib_ranges_repeat 22 #include <type_traits> 23 #ifdef __cpp_lib_ranges 30 #include "basics/compatibility/ranges/range-access.hpp" 39 #ifdef __cpp_lib_ranges 40 template<std::move_constructible W, std::semiregular Bound = unreachable_sentinel_t> requires
41 std::is_object_v<W> and std::same_as<W, std::remove_cv_t<W>> and
42 (OpenKalman::internal::is_signed_integer_like<Bound> or
43 (OpenKalman::internal::is_integer_like<Bound> and weakly_incrementable<Bound> or
44 std::same_as<Bound, std::unreachable_sentinel_t>))
45 struct repeat_view : std::ranges::view_interface<repeat_view<W, Bound>>
47 template<typename W, typename Bound = unreachable_sentinel_t>
55 #ifdef __cpp_lib_ranges 56 using index_type = std::conditional_t<std::is_same_v<Bound, std::unreachable_sentinel_t>, std::ptrdiff_t, Bound>;
58 using index_type = std::conditional_t<std::is_same_v<Bound, unreachable_sentinel_t>, std::ptrdiff_t, Bound>;
62 using iota_diff_t = std::conditional_t<
63 not std::is_integral_v<I> or (
sizeof(iter_difference_t<I>) >
sizeof(I)), iter_difference_t<I>, std::ptrdiff_t>;
67 using iterator_concept = std::random_access_iterator_tag;
68 using iterator_category = std::random_access_iterator_tag;
70 using difference_type = std::conditional_t<OpenKalman::internal::is_signed_integer_like<index_type>,
71 index_type, iota_diff_t<index_type>>;
77 constexpr
explicit iterator(
const W*
value, index_type b = index_type{}) : value_ {value}, current_ {b} {}
79 constexpr
const W& operator*()
const noexcept {
return *value_; }
81 constexpr
const W& operator[](difference_type n)
const noexcept {
return *(*
this + n); }
83 constexpr
iterator& operator++() { ++current_;
return *
this; }
85 constexpr
auto operator++(
int) {
auto tmp = *
this; ++*
this;
return tmp; }
87 constexpr
iterator& operator--() { --current_;
return *
this; }
89 constexpr
auto operator--(
int) {
auto tmp = *
this; --*
this;
return tmp; }
91 constexpr
iterator& operator+=(difference_type n) { current_ += n;
return *
this; }
93 constexpr
iterator& operator-=(difference_type n) { current_ -= n;
return *
this; }
95 friend constexpr
bool operator==(
const iterator& x,
const iterator& y) {
return x.current_ == y.current_; }
97 #ifdef __cpp_impl_three_way_comparison 98 friend constexpr
auto operator<=>(
const iterator& x,
const iterator& y) {
return x.current_ <=> y.current_; }
100 friend constexpr
bool operator!=(
const iterator& x,
const iterator& y) {
return x.current_ != y.current_; }
101 friend constexpr
bool operator<(
const iterator& x,
const iterator& y) {
return x.current_ < y.current_; }
102 friend constexpr
bool operator>(
const iterator& x,
const iterator& y) {
return x.current_ > y.current_; }
103 friend constexpr
bool operator<=(
const iterator& x,
const iterator& y) {
return x.current_ <= y.current_; }
104 friend constexpr
bool operator>=(
const iterator& x,
const iterator& y) {
return x.current_ >= y.current_; }
107 friend constexpr
auto operator+(
iterator i, difference_type n) { i += n;
return i; }
109 friend constexpr
auto operator+(difference_type n,
iterator i) { i += n;
return i; }
111 friend constexpr
auto operator-(
iterator i, difference_type n) { i -= n;
return i; }
115 return static_cast<difference_type
>(x.current_) - static_cast<difference_type>(y.current_);
126 #ifdef __cpp_lib_concepts 127 repeat_view() requires std::default_initializable<W> =
default;
129 template<
bool Enable = true, std::enable_if_t<Enable and std::is_default_constructible_v<W>,
int> = 0>
135 repeat_view(
const W&
value, Bound bound = {}) : value_ {value}, bound_ {bound} {}
139 repeat_view(W&& value, Bound bound = {}) : value_ {std::move(value)}, bound_ {bound} {}
142 #ifdef __cpp_lib_concepts 143 template <
typename...WArgs,
typename...BoundArgs> requires
144 std::constructible_from<W, WArgs...> and std::constructible_from<Bound, BoundArgs...>
146 template<
typename...WArgs,
typename...BoundArgs, std::enable_if_t<
147 constructible_from<W, WArgs...> and constructible_from<Bound, BoundArgs...>,
int> = 0>
150 repeat_view(std::piecewise_construct_t, std::tuple<WArgs...> value_args, std::tuple<BoundArgs...> bound_args = std::tuple<>{})
151 : value_ {std::make_from_tuple<W>(std::move(value_args))}, bound_ {std::make_from_tuple<Bound>(std::move(bound_args))} {}
155 begin()
const {
return iterator {std::addressof(*value_)}; }
158 template<
bool Enable = true, std::enable_if_t<Enable and not std::is_same_v<Bound, unreachable_sentinel_t>,
int> = 0>
160 end()
const {
return iterator {std::addressof(*value_), bound_}; }
164 end()
const {
return {}; }
167 #ifdef __cpp_lib_concepts 169 size()
const requires (not std::same_as<Bound, std::unreachable_sentinel_t>)
171 template<
bool Enable = true, std::enable_if_t<Enable and not std::is_same_v<Bound, unreachable_sentinel_t>,
int> = 0>
172 constexpr
auto size()
const 175 if constexpr (std::is_integral_v<Bound>)
return static_cast<std::make_unsigned_t<Bound>
>(bound_);
176 else return static_cast<std::size_t
>(bound_);
188 template<
typename W,
typename Bound = unreachable_sentinel_t>
200 #ifdef __cpp_lib_concepts 201 template<std::move_constructible W, std::semiregular Bound = unreachable_sentinel_t> requires
202 std::is_object_v<W> and std::same_as<W, std::remove_cv_t<W>> and
203 (OpenKalman::internal::is_signed_integer_like<Bound> or
204 (OpenKalman::internal::is_integer_like<Bound> and weakly_incrementable<Bound> or
205 std::same_as<Bound, std::unreachable_sentinel_t>))
207 template<
typename W,
typename Bound = unreachable_sentinel_t>
210 operator() [[nodiscard]] (W&&
value, Bound&& bound = {})
const
Equivalent to std::ranges::repeat_view.
Definition: repeat.hpp:48
Definition: view_interface.hpp:32
constexpr detail::repeat_adaptor repeat
a std::ranges::range_adaptor_closure for a set of repeatenated collection objects.
Definition: repeat.hpp:150
constexpr bool value
T is numerical value or is reducible to a numerical value.
Definition: value.hpp:31
Definition: iterator.hpp:423
Definition: repeat.hpp:198
Definition: range-access.hpp:25
Definitions relating to the availability of c++ language features.
Definition: repeat.hpp:51
Definitions implementing features of the c++ ranges library for compatibility.