msl 1.3.0
Loading...
Searching...
No Matches
Iterator.h
Go to the documentation of this file.
1// Copyright 2025-2026 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
17template<typename T>
19{
20public:
22 using Container = T;
23
26 : _counter(0), _container()
27 {
29 }
30
31 ConstIterator(ConstIterator<T> const &) = default;
32 ConstIterator(ConstIterator<T> &&) = default;
33 ConstIterator<T> & operator=(ConstIterator<T> const &) = default;
34 ConstIterator<T> & operator=(ConstIterator<T> &&) = default;
35 virtual ~ConstIterator() = default;
36
38 T const & container() const
39 {
40 return this->_container;
41 }
42
44 void setContainer(T const & container)
45 {
46 this->_counter.setEnd(container.size());
47 this->_counter.reset();
48
49 this->_container = container;
50 }
51
53 std::size_t index() const
54 {
55 return this->_counter.index();
56 }
57
59 typename T::value_type const & item() const
60 {
61 return this->_container[this->_counter.index()];
62 }
63
65 std::size_t end() const
66 {
67 return this->_counter.end();
68 }
69
72 {
73 this->_counter.reset();
74 return *this;
75 }
76
82 {
83 ++this->_counter;
84 return *this;
85 }
86
88 bool first() const
89 {
90 return this->_counter.first();
91 }
92
94 bool last() const
95 {
96 return this->_counter.last();
97 }
98
100 bool done() const
101 {
102 return this->_counter.done();
103 }
104
105protected:
108
111};
112
118template<typename T>
119class Iterator: public ConstIterator<T>
120{
121public:
123 Iterator(T const & container={})
125 {
126 // Nothing else
127 }
128
129 Iterator(Iterator<T> const &) = default;
130 Iterator(Iterator<T> &&) = default;
131 Iterator<T> & operator=(Iterator<T> const &) = default;
132 Iterator<T> & operator=(Iterator<T> &&) = default;
133
135 typename T::value_type & item()
136 {
137 return this->_container[this->_counter.index()];
138 }
139};
140
141}
142
143#endif // _3608ae00_0c4a_4bb2_a439_7094206b302f
Read-only iterator to a container.
Definition Iterator.h:19
std::size_t end() const
Return the end value.
Definition Iterator.h:65
std::vector< sSLICE_POS > const & container() const
Definition Iterator.h:38
bool first() const
Test if the index is 0.
Definition Iterator.h:88
T _container
Container being iterated.
Definition Iterator.h:110
bool last() const
Test if the index is equal to end-1.
Definition Iterator.h:94
ConstIterator & reset()
Reset the iterator to the start of the container.
Definition Iterator.h:71
T::value_type const & item() const
Return the current item.
Definition Iterator.h:59
void setContainer(T const &container)
Set the container, reset the counter.
Definition Iterator.h:44
ConstIterator & operator++()
Move to the next element.
Definition Iterator.h:81
T Container
Type of the container iterated on.
Definition Iterator.h:22
Counter _counter
Current position in the container.
Definition Iterator.h:107
ConstIterator(T const &container={})
Create an iterator at the beginning of a container.
Definition Iterator.h:25
bool done() const
Test if the index is equal to end.
Definition Iterator.h:100
std::size_t index() const
Return the current index.
Definition Iterator.h:53
Counter from 0 (included) to end (excluded).
Definition Counter.h:14
Iterator(T const &container={})
Create an iterator at the beginning of a container.
Definition Iterator.h:123
T::value_type & item()
Return the current item.
Definition Iterator.h:135
Top-level namespace of the msl library.
Definition acceleration.h:17