Skip to content

Commit

Permalink
Add xtd::collections::generic::enumerator and xtd::collections::gener…
Browse files Browse the repository at this point in the history
…ic::ienumerable classes
  • Loading branch information
gammasoft71 committed Jul 29, 2024
1 parent 6d1a7ad commit ec6bb9e
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/xtd.core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,16 @@ add_sources(
include/xtd/collections/vector_list
include/xtd/collections/generic/dictionary.h
include/xtd/collections/generic/dictionary
include/xtd/collections/generic/enumerable_iterators.h
include/xtd/collections/generic/enumerable_iterators
include/xtd/collections/generic/enumerator.h
include/xtd/collections/generic/enumerator
include/xtd/collections/generic/hash_set.h
include/xtd/collections/generic/hash_set
include/xtd/collections/generic/icollection.h
include/xtd/collections/generic/icollection
include/xtd/collections/generic/ienumerable.h
include/xtd/collections/generic/ienumerable
include/xtd/collections/generic/ienumerator.h
include/xtd/collections/generic/ienumerator
include/xtd/collections/generic/key_value_pair.h
Expand Down
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 src/xtd.core/include/xtd/collections/generic/enumerable_iterators.h
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_;
};
}
}
}
2 changes: 2 additions & 0 deletions src/xtd.core/include/xtd/collections/generic/enumerator
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "enumerator.h"
69 changes: 69 additions & 0 deletions src/xtd.core/include/xtd/collections/generic/enumerator.h
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_;
};
}
}
}
4 changes: 2 additions & 2 deletions src/xtd.core/include/xtd/collections/generic/icollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// @brief Contains xtd::collections::generic::icollection <type_t> interface.
/// @copyright Copyright (c) 2024 Gammasoft. All rights reserved.
#pragma once
#include "../../interface.h"
#include "ienumerable.h"
#include "../../array.h"
#include "../../object.h"
#include "../../size.h"
Expand Down Expand Up @@ -32,7 +32,7 @@ namespace xtd {
/// @remarks The xtd::collections::generic::icollection <type_t> interface extends xtd::collections::generic::ienumerable <type_t>; xtd::collections::generic::idictionary <key_t, value_t> and xtd::collections::generic::ilist <type_t> are more specialized interfaces that extend xtd::collections::generic::icollection <type_t>. A xtd::collections::generic::idictionary <key_t, value_t> implementation is a collection of key/value pairs, like the xtd::collections::generic::dictoinary <key_t, value_t> class. A xtd::collections::generic::ilist <type_t> implementation is a collection of values, and its members can be accessed by index, like the xtd::collections::generic::list <type_t> class.
/// @remarks If neither the xtd::collections::generic::idictionary <key_t, value_t> interface nor the xtd::collections::generic::ilist <type_t> interface meet the requirements of the required collection, derive the new collection class from the xtd::collections::generic::icollection <type_t> interface instead for more flexibility.
template<typename type_t>
class icollection interface_ {
class icollection : public ienumerable<type_t> {
public:
/// @name Public Properties

Expand Down
2 changes: 2 additions & 0 deletions src/xtd.core/include/xtd/collections/generic/ienumerable
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 src/xtd.core/include/xtd/collections/generic/ienumerable.h
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
};
}
}
}

0 comments on commit ec6bb9e

Please sign in to comment.