BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
repeat.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
16#include <algorithm>
17#include <ranges>
18
21
22namespace bio::ranges::detail
23{
24
25// ---------------------------------------------------------------------------------------------------------------------
26// repeat_view class
27// ---------------------------------------------------------------------------------------------------------------------
28
43template <typename value_t>
44class repeat_view : public std::ranges::view_interface<repeat_view<value_t>>
45{
46private:
48 using base_t = std::ranges::view_interface<repeat_view<value_t>>;
49
51 using sentinel_type = std::default_sentinel_t;
52
54 using single_value_t = value_t; //decltype(std::views::single(std::declval<value_t>()));
55
61 using value_type = std::remove_reference_t<value_t>;
63 using reference = value_type &;
65 using const_reference = value_type const &;
67 using difference_type = ptrdiff_t;
69
71 template <typename parent_type>
72 class basic_iterator;
73
78 using iterator = basic_iterator<repeat_view>;
80 using const_iterator = basic_iterator<repeat_view const>;
82
84 template <typename, template <typename...> typename>
85 friend class detail::random_access_iterator_base;
86
87public:
91 repeat_view() = default;
92 repeat_view(repeat_view const &) = default;
93 repeat_view & operator=(repeat_view const &) = default;
94 repeat_view(repeat_view &&) noexcept = default;
95 repeat_view & operator=(repeat_view &&) noexcept = default;
96 ~repeat_view() = default;
97
99 constexpr explicit repeat_view(value_t const & value)
100 requires std::copy_constructible<value_t>
101 : single_value{value}
102
103 {}
104
106 constexpr explicit repeat_view(value_t && value) : single_value{std::move(value)} {}
108
127 constexpr iterator begin() noexcept { return iterator{*this}; }
128
130 constexpr const_iterator begin() const noexcept { return const_iterator{*this}; }
131
147 constexpr sentinel_type end() noexcept { return {}; }
148
150 constexpr sentinel_type end() const noexcept { return {}; }
152
172 constexpr const_reference operator[](difference_type const BIOCPP_DOXYGEN_ONLY(n)) const noexcept
173 {
174 return single_value;
175 }
176
178 constexpr reference operator[](difference_type const BIOCPP_DOXYGEN_ONLY(n)) noexcept { return single_value; }
180
181private:
183 single_value_t single_value;
184};
185
187template <typename value_t>
188template <typename parent_type>
189class repeat_view<value_t>::basic_iterator : public detail::random_access_iterator_base<parent_type, basic_iterator>
190{
192 using base_t = detail::random_access_iterator_base<parent_type, basic_iterator>;
193
195 using typename base_t::position_type;
196
197public:
199 using typename base_t::difference_type;
201 using typename base_t::value_type;
203 using typename base_t::reference;
205 using typename base_t::pointer;
207 using typename base_t::iterator_category;
208
212 basic_iterator() = default;
213 basic_iterator(basic_iterator const &) = default;
214 basic_iterator & operator=(basic_iterator const &) = default;
215 basic_iterator(basic_iterator &&) noexcept = default;
216 basic_iterator & operator=(basic_iterator &&) noexcept = default;
217 ~basic_iterator() = default;
218
222 explicit constexpr basic_iterator(parent_type & host) noexcept : base_t{host} {}
223
227 template <typename parent_type2>
228 requires(std::is_const_v<parent_type> && (!std::is_const_v<parent_type2>) &&
230 constexpr basic_iterator(basic_iterator<parent_type2> const & rhs) noexcept : base_t{rhs}
231 {}
233
238 friend constexpr bool operator==(basic_iterator const &, std::default_sentinel_t const &) noexcept { return false; }
240};
241
242// ---------------------------------------------------------------------------------------------------------------------
243// repeat (factory)
244// ---------------------------------------------------------------------------------------------------------------------
245
247struct repeat_fn
248{
250 template <typename value_t>
251 constexpr auto operator()(value_t && value) const
252 {
253 static_assert(std::constructible_from<std::remove_cvref_t<value_t>, value_t>,
254 "The value passed to views::repeat must be (1) an lvalue and its type copy constructible; or "
255 "(2) an rvalue and its type move constructible.");
256
257 return detail::repeat_view{std::forward<value_t>(value)};
258 }
259};
260
261} // namespace bio::ranges::detail
262
263namespace bio::ranges::views
264{
265
266// ---------------------------------------------------------------------------------------------------------------------
267// views::repeat (factory instance)
268// ---------------------------------------------------------------------------------------------------------------------
269
314inline constexpr detail::repeat_fn repeat{};
316
317} // namespace bio::ranges::views
T begin(T... args)
T end(T... args)
constexpr detail::repeat_fn repeat
A view factory that repeats a given value infinitely.
Definition: repeat.hpp:314
T is_same_v
T move(T... args)
The BioC++ namespace for views.
Provides the bio::ranges::detail::random_access_iterator class.
Provides various transformation traits used by the range module.