5#include "../enumerator.hpp"
6#include "../../../icomparable.hpp"
7#include "../../../iequatable.hpp"
8#include "../../../ptrdiff.hpp"
9#include "../../../size.hpp"
15 namespace collections {
19 namespace extensions {
47 template<
class type_t,
class enumerable_t,
class iterator_tag_t = std::b
idirectional_iterator_tag>
52 using value_type = type_t;
53 using iterator_category = iterator_tag_t;
54 using iterator_concept = iterator_tag_t;
56 using pointer = value_type*;
57 using const_pointer =
const value_type*;
58 using reference = value_type&;
59 using const_reference =
const value_type&;
61 static constexpr xtd::size npos() {
return std::numeric_limits<xtd::size>::max();}
63 enumerable_iterator() =
default;
65 enumerable_iterator(enumerable_iterator&& value)
noexcept =
default;
66 enumerable_iterator(
const enumerable_iterator& value) noexcept : enumerable_(value.enumerable_), enumerator_(value.enumerable_->get_enumerator()), pos_ {value.pos_} {reset();}
67 enumerable_iterator& operator =(enumerable_iterator&& value)
noexcept =
default;
68 enumerable_iterator& operator =(
const enumerable_iterator& value)
const noexcept {
69 enumerable_ = value.enumerable_;
70 enumerator_ = value.enumerable_->get_enumerator();
73 return const_cast<enumerable_iterator&
>(*this);
75 enumerable_iterator& operator =(
const enumerable_iterator& value)
noexcept {
76 enumerable_ = value.enumerable_;
77 enumerator_ = value.enumerable_->get_enumerator();
80 return const_cast<enumerable_iterator&
>(*this);
83 int32 compare_to(
const enumerable_iterator& rhs)
const noexcept override {
return pos_ < rhs.pos_ ? -1 : pos_ > rhs.pos_ ? 1 : 0;}
84 bool equals(
const enumerable_iterator& rhs)
const noexcept override {
return pos_ == rhs.pos_;}
86 const_reference operator *()
const {
return enumerator_.current();}
87 reference operator *() {
return const_cast<reference
>(enumerator_.current());}
88 const_pointer operator ->()
const {
return &operator*();}
89 pointer operator ->() {
return &operator*();}
91 enumerable_iterator& operator ++()
const noexcept {
92 if (pos_ != std::numeric_limits<xtd::size>::max()) pos_ = enumerator_.move_next() ? pos_ + 1 : std::numeric_limits<xtd::size>::max();
93 return const_cast<enumerable_iterator&
>(*this);
95 enumerable_iterator operator ++(
int)
const noexcept {
101 enumerable_iterator& operator --()
const noexcept {
102 if (pos_ != 0) pos_ = enumerator_.move_next() ? pos_ - 1 : std::numeric_limits<xtd::size>::max();
103 return const_cast<enumerable_iterator&
>(*this);
105 enumerable_iterator operator --(
int)
const noexcept {
106 auto current = *
this;
111 template<
class value_t>
112 enumerable_iterator operator +(value_t value)
const noexcept {
return enumerable_iterator {enumerable_, pos_ + value};}
113 template<
class value_t>
114 enumerable_iterator& operator +=(value_t value)
noexcept {
115 *
this = *
this + value;
118 difference_type operator -(enumerable_iterator value)
const noexcept {
119 if (pos_ == std::numeric_limits<xtd::size>::max())
return std::numeric_limits<xtd::size>::max();
120 return static_cast<difference_type
>(pos_ - value.pos_);
126 if (pos_ == std::numeric_limits<xtd::size>::max())
return;
127 for (
auto index =
xtd::size {}; index <= pos_; ++index)
128 if (enumerator_.move_next() ==
false) {
129 pos_ = std::numeric_limits<xtd::size>::max();
134 mutable const enumerable_t* enumerable_ =
nullptr;
187 template<
class source_collection_t,
class target_collection_t>
188 static typename target_collection_t::const_iterator
to_iterator(
typename source_collection_t::const_iterator& value,
const source_collection_t& source_collection,
const target_collection_t& target_collection)
noexcept {
189 if (value == source_collection.cbegin())
return target_collection.cbegin();
190 if (value == source_collection.cend())
return target_collection.cend();
192 auto result = target_collection.cbegin();
193 for (
auto index =
ptrdiff {}; index < std::distance(source_collection.cbegin(), value); ++index, ++result);
205 template<
class source_collection_t,
class target_collection_t>
206 static typename target_collection_t::iterator
to_iterator(
typename source_collection_t::iterator& value,
const source_collection_t& source_collection,
const target_collection_t& target_collection)
noexcept {
207 if (value == source_collection.begin())
return target_collection.begin();
208 if (value == source_collection.end())
return target_collection.end();
210 auto result = target_collection.begin();
211 for (
auto index =
ptrdiff {}; index < std::distance(source_collection.begin(), value); ++index, ++result);
223 template<
class source_collection_t,
class target_collection_t>
224 static typename target_collection_t::const_iterator
to_iterator(
typename source_collection_t::const_iterator& value, source_collection_t& source_collection, target_collection_t& target_collection)
noexcept {
225 if (value == source_collection.cbegin())
return target_collection.cbegin();
226 if (value == source_collection.cend())
return target_collection.cend();
228 auto result = target_collection.cbegin();
229 for (
auto index =
ptrdiff {}; index < std::distance(source_collection.cbegin(), value); ++index, ++result);
241 template<
class source_collection_t,
class target_collection_t>
242 static typename target_collection_t::iterator
to_iterator(
typename source_collection_t::iterator& value, source_collection_t& source_collection, target_collection_t& target_collection)
noexcept {
243 if (value == source_collection.begin())
return target_collection.begin();
244 if (value == source_collection.end())
return target_collection.end();
246 auto result = target_collection.begin();
247 for (
auto index =
ptrdiff {}; index < std::distance(source_collection.begin(), value); ++index, ++result);
Internal enumarable iterators definition.
Definition enumerable_iterators.hpp:48
virtual iterator end()
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:173
static target_collection_t::iterator to_iterator(typename source_collection_t::iterator &value, source_collection_t &source_collection, target_collection_t &target_collection) noexcept
Converts source iterator to target iterator.
Definition enumerable_iterators.hpp:242
static target_collection_t::const_iterator to_iterator(typename source_collection_t::const_iterator &value, const source_collection_t &source_collection, const target_collection_t &target_collection) noexcept
Converts source iterator to target iterator.
Definition enumerable_iterators.hpp:188
virtual const_iterator end() const
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:170
const enumerable_iterator const_iterator
Represents the const iterator of enumarable value type.
Definition enumerable_iterators.hpp:147
virtual const_iterator begin() const
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:155
enumerable_iterator iterator
Represents the iterator of enumarable value type.
Definition enumerable_iterators.hpp:145
virtual const_iterator cbegin() const
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:162
static target_collection_t::const_iterator to_iterator(typename source_collection_t::const_iterator &value, source_collection_t &source_collection, target_collection_t &target_collection) noexcept
Converts source iterator to target iterator.
Definition enumerable_iterators.hpp:224
virtual const_iterator cend() const
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:166
virtual iterator begin()
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:158
static target_collection_t::iterator to_iterator(typename source_collection_t::iterator &value, const source_collection_t &source_collection, const target_collection_t &target_collection) noexcept
Converts source iterator to target iterator.
Definition enumerable_iterators.hpp:206
Provides a set of static methods for querying objects that implement ienumerable <type_t>.
Definition enumerable.hpp:32
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.hpp:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
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
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38