msl 1.3.0
Loading...
Searching...
No Matches
Iterator.h
Go to the documentation of this file.
1// Copyright 2025 Julien Lamy, ICube, Université de Strasbourg-CNRS.
2// Part of msl, distributed under the terms of the MIT license.
3
4#ifndef _3608ae00_0c4a_4bb2_a439_7094206b302f
5#define _3608ae00_0c4a_4bb2_a439_7094206b302f
6
7#include "Counter.h"
8
9namespace msl
10{
11
13template<typename T>
15{
16public:
19 : _counter(0), _container()
20 {
22 }
23
24 ConstIterator(ConstIterator<T> const &) = default;
28 virtual ~ConstIterator() = default;
29
31 T const & container() const
32 {
33 return this->_container;
34 }
35
37 void setContainer(T const & container)
38 {
39 this->_counter.setEnd(container.size());
40 this->_counter.reset();
41
42 this->_container = container;
43 }
44
46 std::size_t index() const
47 {
48 return this->_counter.index();
49 }
50
52 typename T::value_type const & item() const
53 {
54 return this->_container[this->_counter.index()];
55 }
56
58 std::size_t end() const
59 {
60 return this->_counter.end();
61 }
62
65 {
66 this->_counter.reset();
67 return *this;
68 }
69
75 {
76 ++this->_counter;
77 return *this;
78 }
79
81 bool first() const
82 {
83 return this->_counter.first();
84 }
85
87 bool last() const
88 {
89 return this->_counter.last();
90 }
91
93 bool done() const
94 {
95 return this->_counter.done();
96 }
97
98protected:
99 Counter _counter;
100 T _container;
101};
102
104template<typename T>
105class Iterator: public ConstIterator<T>
106{
107public:
109 Iterator(T const & container={})
111 {
112 // Nothing else
113 }
114
115 Iterator(Iterator<T> const &) = default;
116 Iterator(Iterator<T> &&) = default;
117 Iterator<T> & operator=(Iterator<T> const &) = default;
119
121 typename T::value_type & item()
122 {
123 return this->_container[this->_counter.index()];
124 }
125};
126
127}
128
129#endif // _3608ae00_0c4a_4bb2_a439_7094206b302f
Read-only iterator to a container.
Definition Iterator.h:15
std::size_t end() const
Return the end value.
Definition Iterator.h:58
T const & container() const
Return the container.
Definition Iterator.h:31
bool first() const
Test if the index is 0.
Definition Iterator.h:81
ConstIterator< T > & operator=(ConstIterator< T > &&)=default
bool last() const
Test if the index is equal to end-1.
Definition Iterator.h:87
ConstIterator & reset()
Reset the iterator to the start of the container.
Definition Iterator.h:64
T::value_type const & item() const
Return the current item.
Definition Iterator.h:52
void setContainer(T const &container)
Set the container, reset the counter.
Definition Iterator.h:37
ConstIterator & operator++()
Move to the next element.
Definition Iterator.h:74
ConstIterator(ConstIterator< T > &&)=default
ConstIterator(ConstIterator< T > const &)=default
virtual ~ConstIterator()=default
ConstIterator< T > & operator=(ConstIterator< T > const &)=default
ConstIterator(T const &container={})
Create an iterator at the beginning of a container.
Definition Iterator.h:18
bool done() const
Test if the index is equal to end.
Definition Iterator.h:93
std::size_t index() const
Return the current index.
Definition Iterator.h:46
Counter from 0 (included) to end (excluded).
Definition Counter.h:14
bool last() const
Test if the index is equal to end-1.
bool done() const
Test if the index is equal to end.
Counter & reset()
Set the index to 0.
Counter & setEnd(std::size_t end)
Set the end value.
std::size_t end() const
Return the end value.
bool first() const
Test if the index is 0.
std::size_t index() const
Return the current index.
Read/write iterator to a container.
Definition Iterator.h:106
Iterator< T > & operator=(Iterator< T > &&)=default
Iterator(Iterator< T > const &)=default
Iterator< T > & operator=(Iterator< T > const &)=default
Iterator(T const &container={})
Create an iterator at the beginning of a container.
Definition Iterator.h:109
T::value_type & item()
Return the current item.
Definition Iterator.h:121
Iterator(Iterator< T > &&)=default
Top-level namespace of the msl library.
Definition acceleration.h:17