xtd 0.2.0
Loading...
Searching...
No Matches
enumerable_iterators.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "enumerator.hpp"
6#include "../../ptrdiff.hpp"
7#include "../../size.hpp"
8#include <limits>
9
11namespace xtd {
13 namespace collections {
15 namespace generic {
43 template<typename type_t, typename enumerable_t>
45 public:
47
50 class iterator {
51 public:
53
56 using iterator_category = std::forward_iterator_tag;
58 using value_type = type_t;
64 using const_pointer = const value_type*;
70
74 static iterator begin(const enumerable_t* enumerable) {
75 auto begin = iterator {};
76 begin.enumerable_ = enumerable;
77 begin.enumerator_ = begin.enumerable_->get_enumerator();
78 begin.pos_ = 0;
79 begin.reset();
80 return begin;
81 }
82
86 static iterator end(const enumerable_t* enumerable) {
87 auto end = iterator {};
88 end.enumerable_ = enumerable;
89 end.enumerator_ = end.enumerable_->get_enumerator();
90 end.pos_ = std::numeric_limits<xtd::size>::max();
91 end.reset();
92 return end;
93 }
94
96
99 iterator() = default;
101
103 iterator(const iterator& value) noexcept : enumerable_(value.enumerable_), enumerator_(value.enumerable_->get_enumerator()), pos_ {value.pos_} {reset();}
104 iterator(iterator&& value) = default;
105 iterator& operator =(const iterator& value) noexcept {
106 enumerable_ = value.enumerable_;
107 enumerator_ = value.enumerable_->get_enumerator();
108 pos_ = value.pos_;
109 reset();
110 return *this;
111 }
113
115
119 const_reference operator *() const {return enumerator_.current();}
122 reference operator *() {return const_cast<reference>(enumerator_.current());}
129
132 iterator& operator ++() noexcept {
133 if (pos_ != std::numeric_limits<xtd::size>::max()) pos_ = enumerator_.move_next() ? pos_ + 1 : std::numeric_limits<xtd::size>::max();
134 return *this;
135 }
138 iterator operator ++(int) noexcept {
139 auto current = *this;
140 operator ++();
141 return current;
142 }
143
147 template<typename value_t>
148 iterator operator +(value_t value) const noexcept {return iterator {*this, value};}
149
153 difference_type operator -(iterator value) const noexcept {
154 if (pos_ == std::numeric_limits<xtd::size>::max()) return std::numeric_limits<xtd::size>::max();
155 return static_cast<difference_type>(pos_ - value.pos_);
156 }
157
160 friend bool operator ==(const iterator& a, const iterator& b) noexcept {return a.pos_ == b.pos_;}
163 friend bool operator !=(const iterator& a, const iterator& b) noexcept {return !operator==(a, b);}
165
166 private:
167 template<typename value_t>
168 iterator(const iterator& base, value_t value) noexcept : enumerable_(base.enumerable_), enumerator_(base.enumerable_->get_enumerator()), pos_ {base.pos_ + value} {reset();}
169
170 void reset() {
171 enumerator_.reset();
172 if (pos_ == std::numeric_limits<xtd::size>::max()) return;
173 for (auto index = xtd::size {}; index <= pos_; ++index)
174 if (enumerator_.move_next() == false) {
175 pos_ = std::numeric_limits<xtd::size>::max();
176 break;
177 }
178 }
179
180 const enumerable_t* enumerable_ = nullptr;
181 enumerator<type_t> enumerator_;
182 xtd::size pos_ = 0;
183 };
185
187
192
194
198 virtual const_iterator begin() const {return const_iterator::begin(static_cast<const enumerable_t*>(this));}
201 virtual iterator begin() {return iterator::begin(static_cast<enumerable_t*>(this));}
202
205 virtual const_iterator cbegin() const {return const_iterator::begin(static_cast<const enumerable_t*>(this));}
206
209 virtual const_iterator cend() const {return const_iterator::end(static_cast<const enumerable_t*>(this));}
210
213 virtual const_iterator end() const {return const_iterator::end(static_cast<const enumerable_t*>(this));}
216 virtual iterator end() {return iterator::end(static_cast<enumerable_t*>(this));}
218 };
219 }
220 }
221}
Represents the iterator of enumarable value type.
Definition enumerable_iterators.hpp:50
const_reference operator*() const
Returns reference to the current element, or a proxy holding it.
Definition enumerable_iterators.hpp:119
friend bool operator!=(const iterator &a, const iterator &b) noexcept
Definition enumerable_iterators.hpp:163
value_type & reference
Represents the reference of the value type.
Definition enumerable_iterators.hpp:66
iterator operator+(value_t value) const noexcept
Add operator with specified value.
Definition enumerable_iterators.hpp:148
type_t value_type
Represents the value type.
Definition enumerable_iterators.hpp:58
std::forward_iterator_tag iterator_category
Represents the iterator category type.
Definition enumerable_iterators.hpp:56
const_pointer operator->() const
Returns pointer to the current element, or a proxy holding it.
Definition enumerable_iterators.hpp:125
iterator & operator++() noexcept
Pre increments the underlying iterator.
Definition enumerable_iterators.hpp:132
const value_type * const_pointer
Represents the const pointer of the value type.
Definition enumerable_iterators.hpp:64
static iterator end(const enumerable_t *enumerable)
Create end xtd::collections::generic::iterator with specified enumerator.
Definition enumerable_iterators.hpp:86
iterator()=default
Initializes a new instance of the xtd::collections::generic::iterator class.
const value_type & const_reference
Represents the const reference of the value type.
Definition enumerable_iterators.hpp:68
value_type * pointer
Represents the pointer of the value type.
Definition enumerable_iterators.hpp:62
static iterator begin(const enumerable_t *enumerable)
Create begin xtd::collections::generic::iterator with specified enumerator.
Definition enumerable_iterators.hpp:74
friend bool operator==(const iterator &a, const iterator &b) noexcept
Definition enumerable_iterators.hpp:160
xtd::ptrdiff difference_type
Represents the value type.
Definition enumerable_iterators.hpp:60
difference_type operator-(iterator value) const noexcept
Subtract The specified iterator from the current iterator.
Definition enumerable_iterators.hpp:153
Internal enumarable iterators definition.
Definition enumerable_iterators.hpp:44
virtual const_iterator cend() const
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:209
virtual iterator begin()
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:201
virtual iterator end()
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:216
virtual const_iterator cbegin() const
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:205
virtual const_iterator end() const
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:213
virtual const_iterator begin() const
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:198
Contains xtd::collections::generic::enumerator <type_t> interface.
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
@ a
The A key.
@ b
The B key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10