Iros
 
Loading...
Searching...
No Matches
repeat_view.h
Go to the documentation of this file.
9#include "di/meta/core.h"
10#include "di/meta/language.h"
11#include "di/meta/operations.h"
12#include "di/types/ssize_t.h"
13#include "di/util/addressof.h"
14#include "di/util/move.h"
15
16namespace di::container {
17template<concepts::Movable T, concepts::Semiregular Bound = UnreachableSentinel>
18requires(concepts::Object<T> && concepts::SameAs<T, meta::RemoveCV<T>> &&
19 (concepts::Integer<Bound> || concepts::SameAs<Bound, UnreachableSentinel>) )
20class RepeatView : public ViewInterface<RepeatView<T, Bound>> {
21private:
22 constexpr static bool is_bounded = !concepts::SameAs<Bound, UnreachableSentinel>;
23
25 using SSizeType = meta::MakeSigned<IndexType>;
26
27 class Iterator : public IteratorBase<Iterator, RandomAccessIteratorTag, T, SSizeType> {
28 public:
29 constexpr Iterator() = default;
30
31 constexpr explicit Iterator(T const* value, IndexType current = IndexType())
32 : m_value(value), m_current(current) {}
33
34 constexpr auto operator*() const -> T const& { return *m_value; }
35
36 constexpr void advance_one() { ++m_current; }
37 constexpr void back_one() { --m_current; }
38 constexpr void advance_n(SSizeType n) { m_current += n; }
39
40 private:
41 constexpr friend auto operator==(Iterator const& a, Iterator const& b) -> bool {
42 return a.m_current == b.m_current;
43 }
44 constexpr friend auto operator<=>(Iterator const& a, Iterator const& b) { return a.m_current <=> b.m_current; }
45
46 constexpr friend auto operator-(Iterator const& a, Iterator const& b) -> SSizeType {
47 return static_cast<SSizeType>(a.m_current) - static_cast<SSizeType>(b.m_current);
48 }
49
50 T const* m_value { nullptr };
51 IndexType m_current {};
52 };
53
54public:
55 constexpr RepeatView()
57 = default;
58
59 constexpr explicit RepeatView(T const& value, Bound bound = Bound())
61 : m_value(value), m_bound(bound) {}
62
63 constexpr explicit RepeatView(T&& value, Bound bound = Bound()) : m_value(util::move(value)), m_bound(bound) {}
64
65 constexpr auto begin() const -> Iterator { return Iterator(util::addressof(m_value)); }
66
67 constexpr auto end() const -> Iterator { return Iterator(util::addressof(m_value), m_bound); }
68 constexpr auto end() const -> UnreachableSentinel
69 requires(!is_bounded)
70 {
72 }
73
74 constexpr auto size() const
75 requires(is_bounded)
76 {
77 return math::to_unsigned(m_bound);
78 }
79
80private:
81 T m_value;
82 Bound m_bound;
83};
84
85template<typename T, typename Bound>
87}
Definition repeat_view.h:20
constexpr RepeatView(T &&value, Bound bound=Bound())
Definition repeat_view.h:63
constexpr auto begin() const -> Iterator
Definition repeat_view.h:65
constexpr auto end() const -> UnreachableSentinel requires(!is_bounded)
Definition repeat_view.h:68
constexpr auto size() const
Definition repeat_view.h:74
constexpr RepeatView(T const &value, Bound bound=Bound())
Definition repeat_view.h:59
constexpr auto end() const -> Iterator
Definition repeat_view.h:67
constexpr RepeatView()=default
Definition view_interface.h:26
Definition operations.h:34
Definition operations.h:27
Definition core.h:114
Definition sequence.h:12
constexpr auto operator<=>(MoveIterator< Iter > const &a, MoveIterator< U > const &b)
Definition move_iterator.h:90
RepeatView(T, Bound) -> RepeatView< T, Bound >
constexpr auto operator-(MoveIterator< Iter > const &a, MoveIterator< U > const &b) -> decltype(a.base() - b.base())
Definition move_iterator.h:95
constexpr auto move
Definition move.h:38
constexpr auto unreachable_sentinel
Definition unreachable_sentinel.h:11
constexpr auto operator==(MoveIterator< Iter > const &a, MoveIterator< U > const &b) -> bool
Definition move_iterator.h:85
constexpr auto to_unsigned
Definition to_unsigned.h:16
detail::ConditionalHelper< value, T, U >::Type Conditional
Definition core.h:88
detail::MakeSignedHelper< RemoveCV< T > >::Type MakeSigned
Definition language.h:316
Definition vocab.h:96
Definition iterator_base.h:14
Definition unreachable_sentinel.h:4