template<class type_t>
class xtd::collections::generic::icollection< type_t >
Defines methods to manipulate generic collections.
- Definition
template<class type_t>
Internal collection operators definition.
Definition collection_operators.hpp:31
Defines methods to manipulate generic collections.
Definition icollection.hpp:45
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:36
- Header
#include <xtd/collections/generic/icollection>
- Namespace
- xtd::collections::generic
- Library
- xtd.core
- Examples
- The following example implements the xtd::collections::generic::icollection <type_t> interface to create a collection of custom
box
objects named box_collection
. Each box
has height
, length
, and width
properties, which are used to define equality. Equality can be defined as all dimensions being the same or the volume being the same. The box
class implements the xtd::iequatable <type_t> interface to define the default equality as the dimensions being the same.
The box_collection
class implements the xtd::collections::generic::icollection::contains method to use the default equality to determine whether a box
is in the collection. This method is used by the xtd::collections::generic::icollection::add method so that each box
added to the collection has a unique set of dimensions. The box_collection
class also provides an overload of the xtd::collections::generic::icollection::contains method that takes a specified xtd::collections::generic::iequality_comparer <type_t> interface, such as b
ox_same_dimensionsand
box_same_volume` classes in the example.
This example also implements an xtd::collections::generic::ienumerator <type_t> interface for the box_collection
class so that the collection can be enumerated.
#include <xtd/xtd>
class program {
public:
static auto main() -> void {
auto boxes = box_collection {};
boxes.add(program::box {10, 4, 6});
boxes.add(program::box {4, 6, 10});
boxes.add(program::box {6, 10, 4});
boxes.add(program::box {12, 8, 10});
boxes.add(program::box {10, 4, 6});
console::write_line("Removing 6x10x4");
boxes.remove(program::box {6, 10, 4});
auto box_check = program::box {8, 12, 10};
console::write_line("Contains {}x{}x{} by dimensions: {}", box_check.height, box_check.length, box_check.width, boxes.contains(box_check));
console::write_line("Contains {}x{}x{} by volume: {}", box_check.height, box_check.length, box_check.width, boxes.contains(box_check, box_same_volume {}));
}
int length = 0;
bool equals(const object& o) const noexcept override {return is<program::box>(o) && equals(as<program::box>(o));}
bool equals(const program::box& o) const noexcept override {return box_same_dimensions {}.equals(*this, o);}
};
class box_collection :
public icollection<program::box> {
public:
box_collection(const std::initializer_list<program::box>& boxes) : boxes_(boxes) {}
size count() const noexcept
override {
return boxes_.count();}
bool is_read_only() const noexcept override {return false;}
bool is_synchronized() const noexcept override {return false;}
const object& sync_root() const noexcept override {return sync_root_;}
void add(
const program::box& item)
override {
if (!contains(item))
boxes_.add(item);
else
console::write_line("A box with {}x{}x{} dimensions was already added to the collection.", item.height, item.length, item.width);
}
void clear()
override {boxes_.clear();}
bool contains(const program::box& item) const noexcept override {return boxes_.contains(item);}
return false;
}
bool remove(const program::box& item) override {return boxes_.remove(item);}
private:
object sync_root_;
};
class box_enumerator :
public ienumerator<program::box> {
public:
const program::box&
current()
const override {
return items_[index_];}
bool move_next() override {return ++index_ < items_.size();}
protected:
};
public:
bool equals(const program::box& b1, const program::box& b2) const noexcept override {return b1.height == b2.height && b1.length == b2.length && b1.width == b2.width;}
size get_hash_code(
const program::box&
box)
const noexcept override {
return hash_code::combine(
box.height,
box.length,
box.width);}
};
public:
bool equals(const program::box& b1, const program::box& b2) const noexcept override {return b1.height * b1.length * b1.width == b2.height * b2.length * b2.width;}
size get_hash_code(
const program::box&
box)
const noexcept override {
}
};
static void display(
const box_collection& boxes) {
console::write_line("\nheight length width hash code");
console::write_line();
}
};
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:59
Represents a boxed integer object.
Definition box_integer.hpp:52
Provides a base class for implementations of the xtd::collections::generic::icomparer <type_t> generi...
Definition comparer.hpp:33
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Defines methods to support the comparison of objects for equality.
Definition iequality_comparer.hpp:34
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
Represents the standard input, output, and error streams for console applications.
Definition console.hpp:36
Combines the hash code for multiple values into a single hash code.
Definition hash_code.hpp:26
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
#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:175
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
@ current
Specifies the current position within a stream.
@ display
Specifies 1/75 inch as the unit of measure.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Represents a boxed object.
Definition box.hpp:56
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38
- Examples
- generic_icollection.cpp.
|
using | value_type = type_t |
| Represents the xtd::collections::generic::ienumerable value type.
|
|
using | iterator = typename xtd::collections::generic::extensions::enumerable_iterators< type_t, xtd::collections::generic::ienumerable< type_t > >::iterator |
| Represents the iterator of xtd::collections::generic::ienumerable value type.
|
|
using | const_iterator = typename xtd::collections::generic::extensions::enumerable_iterators< type_t, xtd::collections::generic::ienumerable< type_t > >::const_iterator |
| Represents the const iterator of xtd::collections::generic::ienumerable value type.
|
|
using | iterator = enumerable_iterator |
| Represents the iterator of enumarable value type.
|
|
using | const_iterator = const enumerable_iterator |
| Represents the const iterator of enumarable value type.
|
|
using | enumerable_type = ienumerable< type_t > |
| Represents the ienumerable enumerable type.
|
|
using | source_type = type_t |
| Represents the ienumerable source type.
|
|
using | ienumerable = typename xtd::linq::enumerable::ienumerable< type_t > |
| Represents the ienumerable value type.
|
|
using | list = typename xtd::linq::enumerable::list< type_t > |
| Represents the list value type.
|
|
virtual xtd::collections::generic::enumerator< type_t > | get_enumerator () const =0 |
| Returns an enumerator that iterates through a collection.
|
|
virtual const_iterator | begin () const |
| Returns an iterator to the first element of the enumarable.
|
|
virtual iterator | begin () |
| Returns an iterator to the first element of the enumarable.
|
|
virtual const_iterator | cbegin () const |
| Returns an iterator to the first element of the enumarable.
|
|
virtual const_iterator | cend () const |
| Returns an iterator to the element following the last element of the enumarable.
|
|
virtual const_iterator | end () const |
| Returns an iterator to the element following the last element of the enumarable.
|
|
virtual iterator | end () |
| Returns an iterator to the element following the last element of the enumarable.
|
|
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.
|
|
const ienumerable< type_t > & | append (const type_t &element) const noexcept |
| Appends a value to the end of the sequence.
|
|
const ienumerable< type_t > & | 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.
|
|
const ienumerable< result_t > & | cast () const noexcept |
| Casts the elements of an xtd::collections::generic::ienumerable to the specified type.
|
|
const ienumerable< xtd::array< type_t > > & | chunk (size_t size) const |
| Splits the elements of a sequence into chunks of size at most size.
|
|
const ienumerable< type_t > & | 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.
|
|
const ienumerable< key_value_pair< key_t, xtd::size > > & | 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.
|
|
const ienumerable< key_value_pair< key_t, xtd::size > > & | 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.
|
|
const ienumerable< type_t > & | 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.
|
|
const ienumerable< type_t > & | 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.
|
|
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.
|
|
const ienumerable< result_t > & | select (const std::function< result_t(const type_t &)> &selector) const |
| Projects each element of a sequence into a new form.
|
|
const ienumerable< type_t > & | select (const std::function< type_t(const type_t &)> &selector) const |
| Projects each element of a sequence into a new form.
|
|
const ienumerable< result_t > & | 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.
|
|
const ienumerable< type_t > & | 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>.
|
|
const ienumerable< type_t > & | where (const std::function< bool(const type_t &)> &predicate) const |
| Filters a sequence of values based on a predicate.
|
|
const ienumerable< type_t > & | 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.
|
|
virtual icollection< type_t > & | operator<< (const type_t &item) |
| The shift left operator adds an item to the xtd::collections::generic::icollection <type_t>.
|
|
virtual icollection< type_t > & | operator>> (const type_t &item) |
| The shift right operator removes tthe first occurrence of a specific object from the xtd::collections::generic::icollection <type_t>.
|
|
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.
|
|