template<class 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<class 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() {
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.push_back(line);
} 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;
}
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() {
size 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")};
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;
}
auto strings_found = list<string> {};
for (auto line : file_contents)
if (line.contains("string to search for")) strings_found.push_back(line);
console::write_line("Found: {}", strings_found.size());
auto memory_after = memory_information::get_used_process_memory();
console::write_line("Memory Used Without Iterator = \t{} kb", (memory_after - memory_before) / 1024);
}
class stream_reader_enumerable :
public ienumerable<string> {
private:
string file_path_;
public:
stream_reader_enumerable(const string& file_path) : file_path_ {file_path} {}
enumerator<string> get_enumerator() const override {return {new_ptr<stream_reader_enumerator>(file_path_)};}
};
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();}
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();
}
};
};
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
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_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
@ current
Specifies the current position within a stream.
Definition seek_origin.hpp:20
|
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 const_iterator | begin () const |
| Returns an iterator to the first element of the enumerable.
|
|
virtual iterator | begin () |
| Returns an iterator to the first element of the enumerable.
|
|
virtual const_iterator | cbegin () const |
| Returns an iterator to the first element of the enumerable.
|
|
virtual const_iterator | cend () const |
| Returns an iterator to the element following the last element of the enumerable.
|
|
virtual const_iterator | end () const |
| Returns an iterator to the element following the last element of the enumerable.
|
|
virtual iterator | end () |
| Returns an iterator to the element following the last element of the enumerable.
|
|
type_t | aggregate (const std::function< type_t(const type_t &, const type_t &)> &func) const |
| Applies an accumulator function over a sequence.
|
|
type_t | aggregate (const type_t &seed, const std::function< type_t(const type_t &, const type_t &)> &func) const |
| Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
|
|
accumulate_t | aggregate (const accumulate_t &seed, const std::function< accumulate_t(const type_t &, const accumulate_t &)> &func) const |
| Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
|
|
type_t | aggregate (const type_t &seed, const std::function< type_t(const type_t &, const type_t &)> &func, const std::function< type_t(const type_t &)> &result_selector) const |
| Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
|
|
result_t | aggregate (const accumulate_t &seed, const std::function< accumulate_t(const type_t &, const accumulate_t &)> &func, const std::function< result_t(const accumulate_t &)> &result_selector) const |
| Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
|
|
bool | all (const std::function< bool(const type_t &)> &predicate) const |
| Determines whether all elements of a sequence satisfy a condition.
|
|
bool | any () const noexcept |
| Determines whether a sequence contains any elements.
|
|
bool | any (const std::function< bool(const type_t &)> &predicate) const |
| Determines whether any element of a sequence satisfies a condition.
|
|
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 (size_t 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.
|
|
bool | contains (const type_t &value) const noexcept |
| Determines whether a sequence contains a specified element by using the default equality comparer.
|
|
bool | contains (const type_t &value, const xtd::collections::generic::iequality_comparer< type_t > &comparer) const noexcept |
| Determines whether a sequence contains a specified element by using a specified equality comparer.
|
|
size_t | count () const noexcept |
| Returns the number of elements in current sequence.
|
|
size_t | count (const std::function< bool(const type_t &)> &predicate) const noexcept |
| Returns a number that represents how many elements in the specified sequence satisfy a condition.
|
|
xtd::size | count (const type_t &value) const noexcept |
| Returns the number of elements with the specified value.
|
|
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 | count_by (const std::function< key_t(const type_t &)> &key_selector, const iequality_comparer< key_t > &key_comparer) 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 | default_if_empty (const type_t &default_value) const noexcept |
| Returns the elements of the specified sequence or the specified 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 | distinct (const xtd::collections::generic::iequality_comparer< type_t > &comparer) const noexcept |
| Returns distinct elements from a sequence by using a specified xtd::collections::generic::iequality_comparer <type_t> to compare values.
|
|
type_t | first_or_default (const std::function< bool(const type_t &)> &predicate, const type_t &default_value) const noexcept |
| Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.
|
|
type_t | first_or_default (const std::function< bool(const type_t &)> &predicate) const noexcept |
| Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
|
|
type_t | first_or_default (const type_t default_value) const noexcept |
| Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
|
|
type_t | first_or_default () const noexcept |
| Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
|
|
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 (const std::function< key_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 | order_by_descending (const std::function< type_t(const type_t &)> &key_selector) const |
| Sorts the elements of a sequence in descending order according to a key.
|
|
auto | select (const std::function< result_t(const type_t &)> &selector) const |
| Projects each element of a sequence into a new form.
|
|
auto | select (const std::function< type_t(const type_t &)> &selector) const |
| Projects each element of a sequence into a new form.
|
|
auto | select (const std::function< result_t(const type_t &, size_t index)> &selector) const |
| Projects each element of a sequence into a new form by incorporating the element's index.
|
|
auto | select (const std::function< type_t(const type_t &, size_t index)> &selector) const |
| Projects each element of a sequence into a new form by incorporating the element's index.
|
|
const list< type_t > & | to_list () const noexcept |
| Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <type_t>.
|
|
auto | where (const std::function< bool(const type_t &)> &predicate) const |
| Filters a sequence of values based on a predicate.
|
|
auto | where (const std::function< bool(const type_t &, size_t)> &predicate) const |
| Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.
|
|
static target_collection_t::const_iterator | to_const_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.
|
|
static target_collection_t::const_iterator | to_const_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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|