-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add xtd::collections::generic::enumerator and xtd::collections::gener…
…ic::ienumerable classes
- Loading branch information
1 parent
6d1a7ad
commit ec6bb9e
Showing
8 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/xtd.core/include/xtd/collections/generic/enumerable_iterators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#pragma once | ||
#include "enumerable_iterators.h" |
133 changes: 133 additions & 0 deletions
133
src/xtd.core/include/xtd/collections/generic/enumerable_iterators.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/// @file | ||
/// @brief Contains xtd::collections::generic::enumerable_iterators <type_t, enumerator_t> class. | ||
/// @copyright Copyright (c) 2024 Gammasoft. All rights reserved. | ||
#pragma once | ||
#include "enumerator.h" | ||
#include "../../size.h" | ||
#include <limits> | ||
|
||
/// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more. | ||
namespace xtd { | ||
/// @brief The xtd::collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries. | ||
namespace collections { | ||
/// @brief The xtd::collections::generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections. | ||
namespace generic { | ||
/// @brief Internal enumarable iterators definition. | ||
/// @par Definition | ||
/// ```cpp | ||
/// template<typename type_t, typename enumerator_t> | ||
/// class enumerable_iterators | ||
/// ``` | ||
/// @par Header | ||
/// ```cpp | ||
/// #include <xtd/collections/enumerable_iterators | ||
/// ``` | ||
/// @par Namespace | ||
/// xtd::collections::generic | ||
/// @par Library | ||
/// xtd.core | ||
/// @ingroup xtd_core | ||
/// @warning Internal use only for xtd::collections::generic::ienumerable interfece. | ||
template<typename type_t, typename enumerator_t> | ||
class enumerable_iterators { | ||
public: | ||
/// @name Public Aliases | ||
|
||
/// @{ | ||
/// @brief Represents the iterator of enumarable value type. | ||
class iterator { | ||
public: | ||
/// @name Public Aliases | ||
|
||
/// @{ | ||
/// @brief Represents the iterator category type. | ||
using iterator_category = std::forward_iterator_tag; | ||
/// @brief Represents the value type. | ||
using value_type = type_t; | ||
/// @brief Represents the pointer of the value type. | ||
using pointer_type = value_type*; | ||
/// @brief Represents the reference of the value type. | ||
using reference_type = value_type&; | ||
/// @} | ||
|
||
/// @name Public Constructors | ||
|
||
/// @{ | ||
/// @brief Initializes a new instance of the xtd::collections::generic::iterator class with specified enumerator. | ||
/// @param enumerator The enumerator to iterate with. | ||
iterator(enumerator<type_t> enumerator) : iterator(enumerator, false) {} | ||
/// @brief Initializes a new instance of the xtd::collections::generic::iterator class with specified enumerator and end. | ||
/// @param enumerator The enumerator to iterate with. | ||
/// @parem end true to specify the end iterotator; otherwise false. | ||
iterator(enumerator<type_t> enumerator, bool end) : enumerator_(enumerator) {enumerator_.reset(); pos_ = enumerator_.move_next() == false || end == true ? std::numeric_limits<xtd::size>::max() : 0;} | ||
/// @} | ||
|
||
/// @name Public Operators | ||
|
||
/// @{ | ||
/// @brief Returns reference to the current element, or a proxy holding it. | ||
/// @return The reference to the current element. | ||
reference_type operator *() const {return const_cast<reference_type>(enumerator_.current());} | ||
/// @brief Returns pointer to the current element, or a proxy holding it. | ||
/// @return The pointer to the current element. | ||
pointer_type operator ->() {return &operator*();} | ||
|
||
/// @brief Pre increments the underlying iterator. | ||
/// @return The underlying iterator. | ||
iterator& operator++() {if (pos_ != std::numeric_limits<xtd::size>::max()) pos_ = enumerator_.move_next() ? pos_ + 1 : std::numeric_limits<xtd::size>::max(); return *this;} | ||
/// @brief Post increments the underlying iterator. | ||
/// @return The underlying iterator. | ||
iterator operator++(int) {iterator tmp = *this; ++(*this); return tmp;} | ||
|
||
/// @biref The equality operator of specified underlyig itertators. | ||
/// @return true if underlying iterators are equels; otherwise false. | ||
friend bool operator==(const iterator& a, const iterator& b) { return a.pos_ == b.pos_; } | ||
/// @biref The inequality operator of specified underlyig itertators. | ||
/// @return true if underlying iterators are not equels; otherwise false. | ||
friend bool operator!=(const iterator& a, const iterator& b) { return !operator==(a, b); } | ||
/// @} | ||
|
||
private: | ||
enumerator<type_t> enumerator_; | ||
xtd::size pos_ = std::numeric_limits<xtd::size>::max(); | ||
}; | ||
/// @} | ||
|
||
/// @name Public Aliases | ||
|
||
/// @{ | ||
/// @brief Represents the const iterator of enumarable value type. | ||
using const_iterator = const iterator; | ||
/// @} | ||
|
||
/// @name Public Methods | ||
|
||
/// @{ | ||
/// @brief Returns an iterator to the first element of the enumarable. | ||
/// @return Iterator to the first element. | ||
const_iterator cbegin() const {return iterator(enumerator_->get_enumerator());} | ||
/// @brief Returns an iterator to the element following the last element of the enumarable. | ||
/// @return Iterator to the element following the last element. | ||
const_iterator cend() const {return iterator(enumerator_->get_enumerator(), true);} | ||
/// @brief Returns an iterator to the first element of the enumarable. | ||
/// @return Iterator to the first element. | ||
const_iterator begin() const {return iterator(enumerator_->get_enumerator());} | ||
/// @brief Returns an iterator to the first element of the enumarable. | ||
/// @return Iterator to the first element. | ||
iterator begin() {return iterator(enumerator_->get_enumerator());} | ||
/// @brief Returns an iterator to the element following the last element of the enumarable. | ||
/// @return Iterator to the element following the last element. | ||
const_iterator end() const {return iterator(enumerator_->get_enumerator(), true);} | ||
/// @brief Returns an iterator to the element following the last element of the enumarable. | ||
/// @return Iterator to the element following the last element. | ||
iterator end() {return iterator(enumerator_->get_enumerator(), true);} | ||
/// @} | ||
|
||
private: | ||
friend enumerator_t; | ||
enumerable_iterators(enumerator_t* enumerator) : enumerator_(enumerator) {} | ||
enumerator_t* enumerator_; | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#pragma once | ||
#include "enumerator.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/// @file | ||
/// @brief Contains xtd::collections::generic::enumerator <type_t> interface. | ||
/// @copyright Copyright (c) 2024 Gammasoft. All rights reserved. | ||
#pragma once | ||
#include "ienumerator.h" | ||
#include "../../ptr.h" | ||
|
||
/// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more. | ||
namespace xtd { | ||
/// @brief The xtd::collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries. | ||
namespace collections { | ||
/// @brief The xtd::collections::generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections. | ||
namespace generic { | ||
/// @brief Supports a simple iteration over a generic collection. | ||
/// @par Definition | ||
/// ```cpp | ||
/// template<typename type_t> | ||
/// class enumerator interface_ | ||
/// ``` | ||
/// @par Header | ||
/// ```cpp | ||
/// #include <xtd/collections/enumerator | ||
/// ``` | ||
/// @par Namespace | ||
/// xtd::collections::generic | ||
/// @par Library | ||
/// xtd.core | ||
/// @ingroup xtd_core generic_collections | ||
template<typename type_t> | ||
class enumerator : public ienumerator<type_t> { | ||
public: | ||
/// @name Public Constructors | ||
|
||
/// @{ | ||
enumerator() = default; | ||
enumerator(ptr<ienumerator<type_t>>& enumerator) : enumerator_ {enumerator} {} // Can't be explicit by design. | ||
enumerator(const ptr<ienumerator<type_t>>& enumerator) : enumerator_ {enumerator} {} // Can't be explicit by design. | ||
enumerator(ptr<ienumerator<type_t>>&& enumerator) : enumerator_ {enumerator} {} // Can't be explicit by design. | ||
/// @} | ||
|
||
/// @{ | ||
/// @name Public Properties | ||
|
||
/// @{ | ||
/// @brief Gets the element in the collection at the current position of the enumerator. | ||
/// @return The element in the collection at the current position of the enumerator. | ||
const type_t& current() const override {return enumerator_->current();} | ||
/// @} | ||
|
||
/// @name Public Methods | ||
|
||
/// @{ | ||
/// @brief Advances the enumerator to the next element of the collection. | ||
/// @return true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. | ||
/// @exception xtd::invalid_operation_exception The collection was modified after the enumerator was created. | ||
bool move_next() override {return enumerator_->move_next();} | ||
|
||
/// @brief Sets the enumerator to its initial position, which is before the first element in the collection. | ||
/// @exception xtd::invalid_operation_exception The collection was modified after the enumerator was created. | ||
/// @exception xtd::not_supported_exception The enumerator does not support being reset. | ||
void reset() override {enumerator_->reset();} | ||
/// @} | ||
|
||
private: | ||
ptr<ienumerator<type_t>> enumerator_; | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#pragma once | ||
#include "ienumerable.h" |
45 changes: 45 additions & 0 deletions
45
src/xtd.core/include/xtd/collections/generic/ienumerable.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/// @file | ||
/// @brief Contains xtd::collections::generic::ienumarable <type_t> interface. | ||
/// @copyright Copyright (c) 2024 Gammasoft. All rights reserved. | ||
#pragma once | ||
#include "enumerable_iterators.h" | ||
|
||
/// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more. | ||
namespace xtd { | ||
/// @brief The xtd::collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries. | ||
namespace collections { | ||
/// @brief The xtd::collections::generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections. | ||
namespace generic { | ||
/// @brief Supports a simple iteration over a generic collection. | ||
/// @par Definition | ||
/// ```cpp | ||
/// template<typename type_t> | ||
/// class ienumarable interface_ | ||
/// ``` | ||
/// @par Header | ||
/// ```cpp | ||
/// #include <xtd/collections/ienumarable | ||
/// ``` | ||
/// @par Namespace | ||
/// xtd::collections::generic | ||
/// @par Library | ||
/// xtd.core | ||
/// @ingroup xtd_core interfaces | ||
template <typename type_t> | ||
class ienumerable : public interface, public enumerable_iterators<type_t, ienumerable<type_t>> { | ||
public: | ||
/// @name Public Methods | ||
|
||
/// @{ | ||
/// @brief Returns an enumerator that iterates through a collection. | ||
/// @return An xtd::collections::generic::enumerator object that can be used to iterate through the collection. | ||
virtual enumerator<type_t> get_enumerator() const = 0; | ||
/// @} | ||
|
||
/// @cond | ||
ienumerable() : enumerable_iterators<type_t, ienumerable<type_t>>(this) {} | ||
/// @endcond | ||
}; | ||
} | ||
} | ||
} |