xtd 1.0.0
Loading...
Searching...
No Matches
xtd::collections::generic::ienumerable< type_t > Class Template Referenceabstract
Inheritance diagram for xtd::collections::generic::ienumerable< type_t >:
xtd::interface xtd::collections::generic::ienumerable_abstract xtd::collections::generic::extensions::enumerable_iterators< type_t, xtd::collections::generic::ienumerable< type_t > > xtd::collections::generic::extensions::enumerable< ienumerable< type_t >, type_t > xtd::basic_string< char > xtd::basic_string< xtd::char16 > xtd::basic_string< xtd::char32 > xtd::basic_string< xtd::char8 > xtd::basic_string< xtd::wchar > xtd::collections::generic::enumerable_generator< type_t > xtd::collections::generic::icollection< type_t > xtd::collections::generic::iset< type_t > xtd::fixed_array< type_t, len >

Definition

template<typename type_t>
class xtd::collections::generic::ienumerable< type_t >

Exposes the enumerator, which supports a simple iteration over a collection of a specified type.

Definition
template<typename type_t>
friend class ienumerable
Exposes an enumerator, which supports a simple iteration over a non-generic collection.
Definition ienumerable_abstract.hpp:38
#define interface_
This keyword is use to represent an interface.
Definition interface.hpp:58
Header
#include <xtd/collections/generic/ienumerable>
Namespace
xtd::collections::generic
Library
xtd.core
Examples
The following example demonstrates how to implement the xtd::collections::generic::ienumerable <type_t> interface. When you implement xtd::collections::generic::ienumerable <type_t>, you must also implement xtd::collections::generic::ienumerator <type_t>.
#include <xtd/xtd>
class program {
public:
static auto main() -> void {
test_stream_reader_enumerable();
test_reading_file();
}
static void test_stream_reader_enumerable() {
// Check the memory before the iterator is used.
auto strings_found = list<string> {};
// Open a file with the stream_reader_enumerable and check for a string.
try {
for (auto line : stream_reader_enumerable {path::combine(path::get_temp_path(), "temp_file.txt")})
if (line.contains("string to search for")) strings_found.add(line);
console::write_line("Found: {}", strings_found.count());
} catch (const file_not_found_exception&) {
console::write_line("This example requires a file named {}.", path::combine(path::get_temp_path(), "temp_file.txt"));
return;
}
// Check the memory after the iterator and output it to the console.
auto memory_after = memory_information::get_used_process_memory();
console::write_line("Memory Used With Iterator = \t{} kb", (memory_after - memory_before) / 1024);
}
static void test_reading_file() {
usize memory_before = memory_information::get_used_process_memory();
auto file_contents = list<string> {};
try {
auto sr = stream_reader {path::combine(path::get_temp_path(), "temp_file.txt")};
// Add the file contents to a generic list of strings.
while (!sr.end_of_stream())
file_contents.add(sr.read_line());
sr.close();
} catch (const file_not_found_exception&) {
console::write_line("This example requires a file named {}.", path::combine(path::get_temp_path(), "temp_file.txt"));
return;
}
// Check for the string.
auto strings_found = list<string> {};
for (auto line : file_contents)
if (line.contains("string to search for")) strings_found.add(line);
console::write_line("Found: {}", strings_found.count());
// Check the memory after when the iterator is not used, and output it to the console.
auto memory_after = memory_information::get_used_process_memory();
console::write_line("Memory Used Without Iterator = \t{} kb", (memory_after - memory_before) / 1024);
}
// you must also implement xtd::collections::generic::ienumerable <type_t> and xtd::collections::generic::ienumerator <type_t>
class stream_reader_enumerable : public ienumerable<string> {
private:
string file_path_;
public:
stream_reader_enumerable(const string& file_path) : file_path_ {file_path} {}
// Must implement get_enumerator, which returns a new stream_reader_enumerator.
enumerator<string> get_enumerator() const override {return {new_ptr<stream_reader_enumerator>(file_path_)};}
};
// When you implement xtd::collections::generic::ienumerable <type_t>, you must also implement xtd::collections::generic::ienumerator <type_t>, which will walk through the contents of the file one line at a time.
class stream_reader_enumerator : public object, public ienumerator<string> {
private:
stream_reader sr_;
std::optional<string> current_;
public:
stream_reader_enumerator(const string& file_path) : sr_ {file_path} {}
~stream_reader_enumerator() {sr_.close();}
// Implement current, move_next and reset, which are required by ienumerator.
const string& current() const override {
if (!current_.has_value()) throw invalid_operation_exception {};
return current_.value();
}
bool move_next() override {
if (sr_.end_of_stream()) current_.reset();
else current_ = sr_.read_line();
return current_.has_value();
}
void reset() override {
sr_.base_stream()->get().seekg(0, std::ios_base::beg);
current_.reset();
}
};
};
startup_(program::main);
// This code produces the following output :
//
// Found: 2
// Memory Used With Iterator = 0 kb
// ---
// Found: 2
// Memory Used Without Iterator = 64 kb
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
static auto write_line() -> void
Writes the current line terminator to the standard output stream using the specified format informati...
static auto get_used_process_memory() noexcept -> xtd::usize
Gets the used physical memory in bytes.
generic::ienumerator< xtd::any_object > ienumerator
Supports a simple iteration over a non-generic collection.
Definition ienumerator.hpp:38
generic::ienumerable< xtd::any_object > ienumerable
Exposes an enumerator, which supports a simple iteration over a non-generic collection.
Definition ienumerable.hpp:32
#define startup_(...)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:284
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
@ current
Specifies the current position within a stream.
Definition seek_origin.hpp:20
Remarks
xtd::collections::generic::ienumerable <type_t> is the base interface for collections in the xtd::collections::generic namespace such as xtd::collections::generic::list <type_t>, xtd::collections::generic::dictionary <key_t, value_t>, and xtd::collections::generic::stack <type_t> and other generic collections such as xtd::collections::object_model::observable_collection <type_t> and xtd::collections::concurent::concurrent_stack <type_t>. Collections that implement xtd::collections::generic::ienumerable <type_t> can be enumerated by using the for each statement.
For the non-generic version of this interface, see xtd::collections::ienumerable.
xtd::collections::generic::ienumerable <type_t> contains a single method that you must implement when implementing this interface; xtd::collections::generic::ienumerable::get_enumerator, which returns an xtd::collections::generic::enumerator <type_t> object. The returned xtd::collections::generic::enumerator <type_t> provides the ability to iterate through the collection by exposing a xtd::collections::generic::enumerator::current property.

Public Aliases

using value_type
 Represents the xtd::collections::generic::ienumerable value type.
using iterator
 Represents the iterator of xtd::collections::generic::ienumerable value type.
using const_iterator
 Represents the const iterator of xtd::collections::generic::ienumerable value type.

Public Methods

virtual auto get_enumerator () const -> xtd::collections::generic::enumerator< type_t >=0
 Returns an enumerator that iterates through a collection.

Additional Inherited Members

using iterator
 Represents the iterator of enumerable value type.
using const_iterator
 Represents the const iterator of enumerable value type.
using enumerable_type
 Represents the ienumerable enumerable type.
using source_type
 Represents the ienumerable source type.
using ienumerable
 Represents the ienumerable value type.
using list
 Represents the list value type.
virtual auto begin () const -> const_iterator
 Returns an iterator to the first element of the enumerable.
virtual auto cbegin () const -> const_iterator
 Returns an iterator to the first element of the enumerable.
virtual auto cend () const -> const_iterator
 Returns an iterator to the element following the last element of the enumerable.
virtual auto end () const -> const_iterator
 Returns an iterator to the element following the last element of the enumerable.
auto aggregate (const std::function< type_t(const type_t &, const type_t &)> &funcfunc) const -> type_t
 Applies an accumulator function over a sequence.
auto all (const std::function< bool(const type_t &)> &predicatepredicate) const -> bool
 Determines whether all elements of a sequence satisfy a condition.
auto any () const noexcept -> bool
 Determines whether a sequence contains any elements.
auto append (const type_t &element) const noexcept
 Appends a value to the end of the sequence.
auto as_enumerable () const noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
auto average () const noexcept
 Computes the average of a sequence of source_t values.
auto cast () const noexcept
 Casts the elements of an xtd::collections::generic::ienumerable to the specified type.
auto chunk (xtd::usize size) const
 Splits the elements of a sequence into chunks of size at most size.
auto concat (const ienumerable< type_t > &second) const noexcept
 Concatenates two sequences.
auto contains (const type_t &value) const noexcept -> bool
 Determines whether a sequence contains a specified element by using the default equality comparer.
auto count () const noexcept -> xtd::usize
 Returns the number of elements in current sequence.
auto count_by (const std::function< key_t(const type_t &)> &key_selector) const noexcept
 Returns the count of elements in the current sequence grouped by key.
auto default_if_empty () const noexcept
 Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the current sequence is empty.
auto distinct () const noexcept
 Returns distinct elements from a sequence by using the default equality comparer to compare values.
auto first_or_default (const std::function< bool(const type_t &)> &predicatepredicate, const type_t &default_value) const noexcept -> type_t
 Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.
auto order () const
 Sorts the elements of a sequence in ascending order.
auto order_by (const std::function< type_t(const type_t &)> &key_selector) const
 Sorts the elements of a sequence in ascending order according to a key.
auto order_by_descending (const std::function< key_t(const type_t &)> &key_selector) const
 Sorts the elements of a sequence in descending order according to a key.
auto select (auto &&selector) const
 Projects each element of a sequence into a new form.
auto to_array () const noexcept -> xtd::array< type_t >
 Creates a xtd::array <type_t> from an xtd::collections::generic::ienumerable <type_t>.
auto to_list () const noexcept -> xtd::collections::generic::list< type_t >
 Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <type_t>.
auto where (auto &&predicatepredicate) const
 Filters a sequence of values based on a predicate.
static auto to_const_iterator (typename source_collection_t::const_iterator &value, const source_collection_t &source_collection, const target_collection_t &target_collection) noexcept -> typename target_collection_t::const_iterator
 Converts source iterator to target iterator.
static auto to_iterator (typename source_collection_t::const_iterator &value, const source_collection_t &source_collection, const target_collection_t &target_collection) noexcept -> typename target_collection_t::const_iterator
 Converts source iterator to target iterator.

Member Typedef Documentation

◆ value_type

template<typename type_t>
using xtd::collections::generic::ienumerable< type_t >::value_type

Represents the xtd::collections::generic::ienumerable value type.

◆ iterator

template<typename type_t>
using xtd::collections::generic::ienumerable< type_t >::iterator

Represents the iterator of xtd::collections::generic::ienumerable value type.

◆ const_iterator

template<typename type_t>
using xtd::collections::generic::ienumerable< type_t >::const_iterator

Represents the const iterator of xtd::collections::generic::ienumerable value type.

Member Function Documentation

◆ get_enumerator()

template<typename type_t>
virtual auto xtd::collections::generic::ienumerable< type_t >::get_enumerator ( ) const -> xtd::collections::generic::enumerator< type_t >
nodiscardpure virtual

Returns an enumerator that iterates through a collection.

Returns
An xtd::collections::generic::enumerator object that can be used to iterate through the collection.

Implemented in xtd::basic_array< type_t, allocator_t >, xtd::basic_array< byte, allocator_t >, xtd::basic_array< item_t, allocator_t >, xtd::basic_array< type_t, allocator_t >, xtd::basic_array< value_type, allocator_t >, xtd::basic_array< xtd::array< xtd::byte >, allocator_t >, xtd::basic_array< xtd::basic_string< char >, allocator_t >, xtd::basic_array< xtd::byte, allocator_t >, xtd::basic_array< xtd::drawing::imaging::encoder_parameter, allocator_t >, xtd::basic_array< xtd::net::ip_address, allocator_t >, xtd::basic_array< xtd::uint16, allocator_t >, xtd::basic_string< char_t, traits_t, allocator_t >, xtd::basic_string< char >, xtd::basic_string< xtd::char16 >, xtd::basic_string< xtd::char32 >, xtd::basic_string< xtd::char8 >, xtd::basic_string< xtd::wchar >, xtd::collections::bit_array, xtd::collections::concurrent::concurrent_bag< type_t >, xtd::collections::concurrent::iproducer_consumer_collection< type_t >, xtd::collections::generic::dictionary< key_t, value_t, hasher_t, equator_t, allocator_t >, xtd::collections::generic::dictionary< intptr, item >, xtd::collections::generic::dictionary< key_type, mapped_type >, xtd::collections::generic::dictionary< xtd::any_object, xtd::any_object >, xtd::collections::generic::dictionary< xtd::intptr, xtd::ptr< collection_type > >, xtd::collections::generic::dictionary< xtd::string, xtd::any_object >, xtd::collections::generic::dictionary< xtd::string, xtd::string >, xtd::collections::generic::enumerable_generator< type_t >, xtd::collections::generic::hash_set< type_t, hasher_t, equator_t, allocator_t >, xtd::collections::generic::icollection< type_t >, xtd::collections::generic::icollection< bool >, xtd::collections::generic::icollection< byte >, xtd::collections::generic::icollection< int32 >, xtd::collections::generic::icollection< item_t >, xtd::collections::generic::icollection< list_type_t >, xtd::collections::generic::icollection< value_type >, xtd::collections::generic::icollection< xtd::any_object >, xtd::collections::generic::icollection< xtd::array< xtd::byte > >, xtd::collections::generic::icollection< xtd::basic_string< char > >, xtd::collections::generic::icollection< xtd::byte >, xtd::collections::generic::icollection< xtd::collections::generic::key_value_pair< key_t, value_t > >, xtd::collections::generic::icollection< xtd::drawing::color >, xtd::collections::generic::icollection< xtd::drawing::imaging::encoder_parameter >, xtd::collections::generic::icollection< xtd::forms::layout::arranged_element_collection::value_type >, xtd::collections::generic::icollection< xtd::net::ip_address >, xtd::collections::generic::icollection< xtd::sptr< xtd::diagnostics::trace_listener > >, xtd::collections::generic::icollection< xtd::sptr< xtd::forms::control > >, xtd::collections::generic::icollection< xtd::sptr< xtd::forms::menu_item > >, xtd::collections::generic::icollection< xtd::time_zone_info::adjustment_rule >, xtd::collections::generic::icollection< xtd::uint16 >, xtd::collections::generic::idictionary< key_t, value_t >, xtd::collections::generic::idictionary< xtd::any_object, xtd::any_object >, xtd::collections::generic::ilist< type_t >, xtd::collections::generic::ilist< byte >, xtd::collections::generic::ilist< int32 >, xtd::collections::generic::ilist< item_t >, xtd::collections::generic::ilist< list_type_t >, xtd::collections::generic::ilist< value_type >, xtd::collections::generic::ilist< xtd::any_object >, xtd::collections::generic::ilist< xtd::array< xtd::byte > >, xtd::collections::generic::ilist< xtd::basic_string< char > >, xtd::collections::generic::ilist< xtd::byte >, xtd::collections::generic::ilist< xtd::drawing::color >, xtd::collections::generic::ilist< xtd::drawing::imaging::encoder_parameter >, xtd::collections::generic::ilist< xtd::forms::layout::arranged_element_collection::value_type >, xtd::collections::generic::ilist< xtd::net::ip_address >, xtd::collections::generic::ilist< xtd::sptr< xtd::diagnostics::trace_listener > >, xtd::collections::generic::ilist< xtd::sptr< xtd::forms::control > >, xtd::collections::generic::ilist< xtd::sptr< xtd::forms::menu_item > >, xtd::collections::generic::ilist< xtd::time_zone_info::adjustment_rule >, xtd::collections::generic::ilist< xtd::uint16 >, xtd::collections::generic::iset< type_t >, xtd::collections::generic::linked_list< type_t, allocator_t >, xtd::collections::generic::linked_list< type_t >, xtd::collections::generic::linked_list< value_type >, xtd::collections::generic::list< type_t, allocator_t >, xtd::collections::generic::list< byte >, xtd::collections::generic::list< int32 >, xtd::collections::generic::list< item >, xtd::collections::generic::list< key_type >, xtd::collections::generic::list< mapped_type >, xtd::collections::generic::list< thread_pool_asynchronous_io_item >, xtd::collections::generic::list< thread_pool_item >, xtd::collections::generic::list< value_type >, xtd::collections::generic::list< xtd::any_object >, xtd::collections::generic::list< xtd::byte >, xtd::collections::generic::list< xtd::collections::generic::key_value_pair< xtd::string, xtd::drawing::color > >, xtd::collections::generic::list< xtd::collections::generic::key_value_pair< xtd::string, xtd::drawing::font > >, xtd::collections::generic::list< xtd::collections::generic::key_value_pair< xtd::string, xtd::drawing::font_family > >, xtd::collections::generic::list< xtd::collections::generic::key_value_pair< xtd::string, xtd::string > >, xtd::collections::generic::list< xtd::drawing::color >, xtd::collections::generic::list< xtd::drawing::drawing_2d::gradient_stop >, xtd::collections::generic::list< xtd::forms::layout::arranged_element_collection::value_type >, xtd::collections::generic::list< xtd::forms::style_sheets::shadow >, xtd::collections::generic::list< xtd::ref< xtd::forms::form > >, xtd::collections::generic::list< xtd::sptr< xtd::diagnostics::trace_listener > >, xtd::collections::generic::list< xtd::sptr< xtd::forms::control > >, xtd::collections::generic::list< xtd::sptr< xtd::forms::menu_item > >, xtd::collections::generic::list< xtd::string >, xtd::collections::generic::list< xtd::time_zone_info::adjustment_rule >, xtd::collections::generic::list< xtd::tunit::test >, xtd::collections::generic::list< xtd::usize >, xtd::collections::generic::list< xtd_library >, xtd::collections::generic::ordered_dictionary< key_t, value_t, allocator_t >, xtd::collections::generic::ordered_dictionary< xtd::any_object, xtd::any_object >, xtd::collections::generic::queue< type_t, container_t >, xtd::collections::generic::queue< value_type >, xtd::collections::generic::queue< xtd::any_object >, xtd::collections::generic::stack< type_t, container_t >, xtd::collections::generic::stack< xtd::any_object >, xtd::collections::object_model::read_only_collection< type_t >, xtd::collections::object_model::read_only_collection< value_type >, xtd::fixed_array< type_t, len >, xtd::forms::layout::arranged_element_collection< type_t, sorter_t >, xtd::forms::layout::arranged_element_collection< drawing::image >, xtd::forms::layout::arranged_element_collection< item >, xtd::forms::layout::arranged_element_collection< item, item::sorter >, xtd::forms::layout::arranged_element_collection< status_bar_panel_ref >, xtd::forms::layout::arranged_element_collection< tab_page_ref >, xtd::forms::layout::arranged_element_collection< tool_bar_button_ref >, xtd::forms::layout::arranged_element_collection< xtd::forms::control_ref >, xtd::forms::layout::arranged_element_collection< xtd::forms::link_label::link >, xtd::forms::layout::arranged_element_collection< xtd::forms::menu_item_ref >, xtd::forms::layout::arranged_element_collection< xtd::forms::message_notifier_button_ref >, and xtd::forms::layout::arranged_element_collection< xtd::string >.


The documentation for this class was generated from the following file: