ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SequenceRange.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File SequenceRange.h --------------------------*/
3/*--------------------------------------------------------------------------*/
32/*--------------------------------------------------------------------------*/
33/*----------------------------- DEFINITIONS --------------------------------*/
34/*--------------------------------------------------------------------------*/
35
36#ifndef __ADS_SequenceRange
37 #define __ADS_SequenceRange
38/* self-identification: #endif constant */
39
40/*--------------------------------------------------------------------------*/
41/*------------------------------ INCLUDES ----------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#include "ADS/ADT.h"
45#include "ADS/Contract.h"
46
47#include <cstddef>
48#include <iterator>
49#include <ranges>
50
51/*--------------------------------------------------------------------------*/
52/*--------------------------- NAMESPACE ------------------------------------*/
53/*--------------------------------------------------------------------------*/
54
55namespace ADS
56{
57
58/*--------------------------------------------------------------------------*/
59/*------------------------- CLASS SequenceRange ----------------------------*/
60/*--------------------------------------------------------------------------*/
61
63template< Sequence S >
64class SequenceRange : public std::ranges::view_interface< SequenceRange< S > >
65{
66
67/*--------------------------------------------------------------------------*/
68 public:
72 {
73 public:
75 using iterator_category = std::forward_iterator_tag;
76 using value_type = typename S::Item;
77 using difference_type = std::ptrdiff_t;
78 using reference = const value_type &;
79 using pointer = const value_type *;
80
82 iterator( void ) = default;
83
85 iterator( const S * s , typename S::Pos p ) : f_s( s ) , f_p( p ) { }
86
88 static iterator end( const S * s ) {
89 iterator e;
90 e.f_s = s;
91 e.f_end = true;
92 return( e );
93 }
94
96 [[nodiscard]] reference operator*( void ) const {
97 return( f_s->read( f_p ) );
98 }
99
101 [[nodiscard]] pointer operator->( void ) const { return( &**this ); }
102
105 f_p = f_s->next( f_p );
106 return( *this );
107 }
108
111 iterator t = *this;
112 ++*this;
113 return( t );
114 }
115
117 [[nodiscard]] bool operator==( std::default_sentinel_t ) const {
118 return( atEnd() );
119 }
120
122 [[nodiscard]] bool operator==( const iterator & o ) const {
123 const bool a = atEnd() , b = o.atEnd();
124 return( ( a == b ) && ( a || ( f_p == o.f_p ) ) );
125 }
126
128 [[nodiscard]] typename S::Pos position( void ) const { return( f_p ); }
129
130 private:
131 [[nodiscard]] bool atEnd( void ) const {
132 return( f_end || ( f_s == nullptr ) || f_s->finished( f_p ) );
133 }
134
135 const S * f_s = nullptr;
136 typename S::Pos f_p{};
137 bool f_end = false;
138 };
139
141 SequenceRange( void ) = default;
142
144 explicit SequenceRange( const S & s ) : f_s( &s ) { }
145
147 [[nodiscard]] iterator begin( void ) const {
148 return( iterator( f_s , f_s->head() ) );
149 }
150
154 [[nodiscard]] iterator end( void ) const { return( iterator::end( f_s ) ); }
155
156/*--------------------------------------------------------------------------*/
157 private:
158 const S * f_s = nullptr;
159
160 }; // end( class SequenceRange )
161
163template< Sequence S >
165
166/*--------------------------------------------------------------------------*/
167
168} // end( namespace ADS )
169
170// the iterators point into the sequence, not into the view; out of the
171// documentation, which cannot tie a specialization in std to its template
173template< ADS::Sequence S >
174inline constexpr bool
175 std::ranges::enable_borrowed_range< ADS::SequenceRange< S > > = true;
177
178/*--------------------------------------------------------------------------*/
179/*-------------------------- IMPLEMENTATION --------------------------------*/
180/*--------------------------------------------------------------------------*/
181
182#include "ADS/sequence/impl/SequenceRange.tpp"
183
184/*--------------------------------------------------------------------------*/
185
186#endif /* SequenceRange.h included */
187
188/*--------------------------------------------------------------------------*/
189/*------------------------ End File SequenceRange.h ------------------------*/
190/*--------------------------------------------------------------------------*/
The abstract data types of the book, as concepts of C++20: SEQUENCE, SET and DICTIONARY (chapter 3),...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
a position of the sequence, read by *; the end is an iterator marked as such, equal to every iterator...
Definition SequenceRange.h:72
iterator operator++(int)
to the next position, returning the iterator before the step
Definition SequenceRange.h:110
iterator & operator++(void)
to the next position, next( p )
Definition SequenceRange.h:104
reference operator*(void) const
the item in the position, read( p )
Definition SequenceRange.h:96
std::ptrdiff_t difference_type
the type of the distances
Definition SequenceRange.h:77
S::Pos position(void) const
the position of the book
Definition SequenceRange.h:128
const value_type & reference
a constant reference
Definition SequenceRange.h:78
bool operator==(std::default_sentinel_t) const
at the end, i.e., on a finished position
Definition SequenceRange.h:117
const value_type * pointer
a constant pointer
Definition SequenceRange.h:79
bool operator==(const iterator &o) const
equal if both are at the end, or on the same position
Definition SequenceRange.h:122
typename S::Item value_type
the type of the items
Definition SequenceRange.h:76
pointer operator->(void) const
the address of the item in the position
Definition SequenceRange.h:101
iterator(void)=default
an iterator on no sequence, at the end
static iterator end(const S *s)
the end of the range of
Definition SequenceRange.h:88
std::forward_iterator_tag iterator_category
the category of the iterator, forward
Definition SequenceRange.h:75
iterator(const S *s, typename S::Pos p)
on the position of the sequence
Definition SequenceRange.h:85
the items of a SEQUENCE as a range of C++
Definition SequenceRange.h:65
iterator end(void) const
the end, an iterator, so that the range is common and the algorithms of C++17 and of Boost take it,...
Definition SequenceRange.h:154
SequenceRange(void)=default
the range of no sequence
iterator begin(void) const
the iterator on the first item, head()
Definition SequenceRange.h:147
SequenceRange(const S &s)
the range of the items of
Definition SequenceRange.h:144
SequenceRange< S > range(const S &s)
the range of the items of
Definition ADT.h:96