Stroika Library 3.0d24
 
Loading...
Searching...
No Matches
RandomAccessIterator.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4namespace Stroika::Foundation::Traversal {
5
6 /*
7 ********************************************************************************
8 ****************** RandomAccessIterator<T, ITERATOR_TRAITS> ********************
9 ********************************************************************************
10 */
11 template <typename T, typename ITERATOR_TRAITS>
12 inline RandomAccessIterator<T, ITERATOR_TRAITS>::RandomAccessIterator (const unique_ptr<IRep>& rep) noexcept
13 : inherited{rep}
14 {
15 }
16 template <typename T, typename ITERATOR_TRAITS>
17 inline RandomAccessIterator<T, ITERATOR_TRAITS>::RandomAccessIterator (unique_ptr<IRep>&& rep) noexcept
18 : inherited{move (rep)}
19 {
20 }
21 template <typename T, typename ITERATOR_TRAITS>
22 constexpr RandomAccessIterator<T, ITERATOR_TRAITS>::RandomAccessIterator (const default_sentinel_t&) noexcept
23 : inherited{default_sentinel}
24 {
25 }
26 template <typename T, typename ITERATOR_TRAITS>
27 constexpr RandomAccessIterator<T, ITERATOR_TRAITS>::RandomAccessIterator (nullptr_t) noexcept
28 : inherited{nullptr}
29 {
30 }
31 template <typename T, typename ITERATOR_TRAITS>
32 constexpr RandomAccessIterator<T, ITERATOR_TRAITS>::RandomAccessIterator () noexcept
33 : inherited{}
34 {
35 }
36 template <typename T, typename ITERATOR_TRAITS>
38 {
39 GetRep ().Advance (i);
40 this->Refresh ();
41 }
42 template <typename T, typename ITERATOR_TRAITS>
44 {
45 if (this->AtEnd ()) {
46 if (rhs.AtEnd ()) {
47 return 0;
48 }
49 else {
50 return -rhs.Difference (*this);
51 }
52 }
53 else {
54 if (rhs.AtEnd ()) {
55 return ConstGetRep ().Difference (nullptr);
56 }
57 else {
58 return ConstGetRep ().Difference (&rhs.ConstGetRep ());
59 }
60 }
61 }
62 template <typename T, typename ITERATOR_TRAITS>
64 {
65 inherited::operator++ ();
66 return *this;
67 }
68 template <typename T, typename ITERATOR_TRAITS>
70 {
71 RandomAccessIterator result{*this};
72 inherited::operator++ ();
73 return result;
74 }
75 template <typename T, typename ITERATOR_TRAITS>
77 {
78 inherited::operator-- ();
79 return *this;
80 }
81 template <typename T, typename ITERATOR_TRAITS>
83 {
84 auto result = *this;
85 inherited::operator-- ();
86 return result;
87 }
88 template <typename T, typename ITERATOR_TRAITS>
90 {
91 RandomAccessIterator result{*this};
92 result.Advance (i);
93 return result;
94 }
95 template <typename T, typename ITERATOR_TRAITS>
97 {
98 RandomAccessIterator result{*this};
99 result.Advance (-i);
100 return result;
101 }
102 template <typename T, typename ITERATOR_TRAITS>
104 {
105 Advance (i);
106 return *this;
107 }
108 template <typename T, typename ITERATOR_TRAITS>
110 {
111 Advance (-i);
112 return *this;
113 }
114 template <typename T, typename ITERATOR_TRAITS>
115 inline const T& RandomAccessIterator<T, ITERATOR_TRAITS>::operator[] (difference_type i) const
116 {
117 return *ConstGetRep ().PeekAtElement (i);
118 }
119 template <typename T, typename ITERATOR_TRAITS>
121 {
122 // logically, the ordering comes from the difference between the iterators if its negative - this is less, and if its positive, this is greater
123 return Difference (rhs) <=> 0;
124 }
125 template <typename T, typename ITERATOR_TRAITS>
127 {
128 return Debug::UncheckedDynamicCast<IRep&> (inherited::GetRep ());
129 }
130 template <typename T, typename ITERATOR_TRAITS>
132 {
133 return Debug::UncheckedDynamicCast<const IRep&> (inherited::ConstGetRep ());
134 }
135
136 template <typename T, typename ITERATOR_TRAITS>
137 inline RandomAccessIterator<T, ITERATOR_TRAITS> operator+ (typename RandomAccessIterator<T, ITERATOR_TRAITS>::difference_type i,
139 {
140 return it + i;
141 }
142 template <typename T, typename ITERATOR_TRAITS>
143 inline typename RandomAccessIterator<T, ITERATOR_TRAITS>::difference_type operator- (const RandomAccessIterator<T, ITERATOR_TRAITS>& lhs,
144 const RandomAccessIterator<T, ITERATOR_TRAITS>& rhs)
145 {
146 return lhs.Difference (rhs);
147 }
148
149}
A BidirectionalIterator is an Iterator that can be moved both forward and backward.
The interface for the internal representation of a RandomAccessIterator.
nonvirtual const T & operator[](difference_type i) const
Access the element at the specified index (relative to the current position).
nonvirtual RandomAccessIterator & operator-=(difference_type i)
Advance (backward) this iterator by the specified number of positions (result MUST be in range)
nonvirtual IRep & GetRep()
Get a reference to the IRep owned by the iterator. This is an implementation detail,...
nonvirtual const IRep & ConstGetRep() const
Get a reference to the IRep owned by the iterator. This is an implementation detail,...
nonvirtual difference_type Difference(const RandomAccessIterator &rhs) const
Calculate the difference between this iterator and another.
nonvirtual void Advance(difference_type i)
Advance the iterator by the specified number of positions (which may be negative)....
friend RandomAccessIterator< T2, ITERATOR_TRAITS2 > operator+(typename RandomAccessIterator< T2, ITERATOR_TRAITS2 >::difference_type i, const RandomAccessIterator< T2, ITERATOR_TRAITS2 > &it)
addition of iterator and int is commutative.
nonvirtual RandomAccessIterator & operator++()
same as Iterator::operator++ () - advances iterator - but returns the subclass iterator type.
friend RandomAccessIterator< T2, ITERATOR_TRAITS2 >::difference_type operator-(const RandomAccessIterator< T2, ITERATOR_TRAITS2 > &lhs, const RandomAccessIterator< T2, ITERATOR_TRAITS2 > &rhs)
Difference of two iterators is difference_type (number of elements between them)
nonvirtual RandomAccessIterator & operator+=(difference_type i)
Advance () this iterator by the specified number of positions (which may be negative,...
nonvirtual strong_ordering operator<=>(const RandomAccessIterator &rhs) const
compare two iterators by their position in underlying container.