xtd 0.2.0
Loading...
Searching...
No Matches
xtd::collections::generic::icollection< type_t > Class Template Referenceabstract
Inheritance diagram for xtd::collections::generic::icollection< type_t >:
xtd::collections::generic::ienumerable< type_t > xtd::interface xtd::collections::generic::enumerable_iterators< type_t, ienumerable< type_t > > xtd::collections::generic::ilist< list_type_t > xtd::collections::generic::ilist< value_type > xtd::collections::generic::ilist< type_t > xtd::basic_array< type_t, allocator_t > xtd::basic_array< value_type, allocator_t > xtd::basic_array< type_t, allocator_t > xtd::collections::generic::list< type_t, allocator_t > xtd::collections::object_model::read_only_collection< type_t > xtd::array< type_t, rank_, allocator_t > xtd::array< type_t, 1, allocator_t > xtd::array< type_t, 2, allocator_t > xtd::array< type_t, 3, allocator_t > xtd::array< value_type >

Definition

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

Defines methods to manipulate generic collections.

Definition
template<typename type_t>
Defines methods to manipulate generic collections.
Definition icollection.h:44
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.h:35
Header
#include <xtd/collections/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 box_same_dimensionsandbox_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>
using namespace xtd;
using namespace xtd::collections::generic;
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});
// Same dimensions. Cannot be added:
boxes.add(program::box {10, 4, 6});
// Test the Remove method.
display(boxes);
console::write_line("Removing 6x10x4");
boxes.remove(program::box {6, 10, 4});
display(boxes);
// Test the Contains method.
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));
// Test the Contains method overload with a specified equality comparer.
console::write_line("Contains {}x{}x{} by volume: {}", box_check.height, box_check.length, box_check.width, boxes.contains(box_check, box_same_volume {}));
}
struct box : public object, public iequatable<program::box> {
// Public Constructors :
box() = default;
box(int h, int l, int w) : height {h}, length{l}, width {w} {}
// Public Properties :
int height = 0;
int length = 0;
int width = 0;
// Public Methods :
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:
// Public Constructors :
box_collection(const std::initializer_list<program::box>& boxes) : boxes_(boxes) {}
// Public Properties :
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_;}
// Public Methods :
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);}
bool contains(const program::box& item, const iequality_comparer<program::box>& comparer) const noexcept {
for (auto box : boxes_)
if (comparer.equals(box, item)) return true;
return false;
}
void copy_to(array<program::box>& array, size array_index) const override {boxes_.copy_to(array, array_index);}
enumerator<program::box> get_enumerator() const override {return {new_ptr<box_enumerator>(boxes_)};}
bool remove(const program::box& item) override {return boxes_.remove(item);}
private:
object sync_root_;
};
class box_enumerator : public ienumerator<program::box> {
public:
// Public Constructors :
explicit box_enumerator(const list<program::box>& items) : items_(items) {}
// Public Properties :
const program::box& current() const override {return items_[index_];}
// Public Methods :
bool move_next() override {return ++index_ < items_.size();}
void reset() override {index_ = box_integer<size>::max_value;}
protected:
const list<program::box>& items_;
};
// Defines two boxes as equal if they have the same dimensions.
class box_same_dimensions : public iequality_comparer<program::box> {
public:
// Public Methods :
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);}
};
// Defines two boxes as equal if they have the same volume.
class box_same_volume : public iequality_comparer<program::box> {
public:
// Public Methods :
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 {
auto hash_code = hash_code::combine(box.height, box.length, box.width);
console::write_line("HC: {}", hash_code);
return hash_code;
}
};
static void display(const box_collection& boxes) {
console::write_line("\nheight length width hash code");
for (auto box : boxes)
console::write_line("{,-6} {,-6} {,-6} {}", box.height, box.length, box.width, box.get_hash_code());
// Results by manipulating the enumerator directly:
//auto enumerator = boxes.get_enumerator();
//console::write_line("\nheight length width hash code");
//while (enumerator.move_next()) {
// auto b = enumerator.current();
// console::write_line("{,-6} {,-6} {,-6} {}", b.height, b.length, b.width, b.get_hash_code());
//}
console::write_line();
}
};
startup_(program::main);
// This code can produce the following output :
//
// A box with 10x4x6 dimensions was already added to the collection.
//
// height length width hash code
// 10 4 6 215240349697
// 4 6 10 215240349697
// 6 10 4 215240349697
// 12 8 10 215240349697
//
// Removing 6x10x4
//
// height length width hash code
// 10 4 6 215240349697
// 4 6 10 215240349697
// 12 8 10 215240349697
//
// Contains 8x12x10 by dimensions: false
// Contains 8x12x10 by volume: true
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.h:58
Represents a boxed integer object.
Definition box_integer.h:52
Represents a boxed object.
Definition box.h:53
Provides a base class for implementations of the xtd::collections::generic::icomparer <type_t> generi...
Definition comparer.h:32
Supports a simple iteration over a generic collection.
Definition enumerator.h:31
Supports a simple iteration over a generic collection.
Definition ienumerator.h:58
Defines methods to support the comparison of objects for equality.
Definition iequality_comparer.h:34
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
Represents the standard input, output, and error streams for console applications.
Definition console.h:36
Combines the hash code for multiple values into a single hash code.
Definition hash_code.h:26
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.h:22
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
size_t size
Represents a size of any object in bytes.
Definition size.h:23
@ clear
The CLEAR key.
@ h
The H key.
@ l
The L key.
@ add
The Add key.
@ w
The W key.
@ display
Specifies 1/75 inch as the unit of measure.
@ height
Specifies that the height of the control is defined.
@ width
Specifies that the width of the control is defined.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
The xtd::collections::generic::icollection <type_t> interface is the base interface for classes in the xtd::collections::generic namespace.
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.
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.
Examples
generic_icollection.cpp.

Public Aliases

using iterator = typename ienumerable< type_t >::iterator
 Represents the iterator of xtd::collections::generic::ienumerable value type.
 
using const_iterator = typename ienumerable< type_t >::const_iterator
 Represents the const iterator of xtd::collections::generic::ienumerable value type.
 

Public Properties

virtual xtd::size count () const noexcept=0
 Gets the number of elements contained in the xtd::collections::generic::icollection <type_t>.
 
virtual bool is_read_only () const noexcept=0
 Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.
 
virtual bool is_synchronized () const noexcept=0
 Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is synchronized (thread safe).
 
virtual const xtd::objectsync_root () const noexcept=0
 Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.
 

Public Methods

virtual void add (const type_t &item)=0
 Adds an item to the xtd::collections::generic::icollection <type_t>.
 
virtual void clear ()=0
 Removes all items from the xtd::collections::generic::icollection <type_t>.
 
virtual bool contains (const type_t &item) const noexcept=0
 Determines whether the xtd::collections::generic::icollection <type_t> contains a specific value.
 
virtual void copy_to (xtd::array< type_t > &array, xtd::size array_index) const =0
 Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array, starting at a particular xtd::array index.
 
virtual bool remove (const type_t &item)=0
 Removes the first occurrence of a specific object from the xtd::collections::generic::icollection <type_t>.
 

Additional Inherited Members

- Public Types inherited from xtd::collections::generic::ienumerable< type_t >
using iterator = typename enumerable_iterators< type_t, ienumerable< type_t > >::iterator
 Represents the iterator of xtd::collections::generic::ienumerable value type.
 
using const_iterator = typename enumerable_iterators< type_t, ienumerable< type_t > >::const_iterator
 Represents the const iterator of xtd::collections::generic::ienumerable value type.
 
- Public Types inherited from xtd::collections::generic::enumerable_iterators< type_t, ienumerable< type_t > >
using const_iterator = const iterator
 Represents the const iterator of enumarable value type.
 
- Public Member Functions inherited from xtd::collections::generic::ienumerable< type_t >
virtual enumerator< type_t > get_enumerator () const =0
 Returns an enumerator that iterates through a collection.
 
- Public Member Functions inherited from xtd::collections::generic::enumerable_iterators< type_t, ienumerable< type_t > >
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.
 

Member Typedef Documentation

◆ iterator

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

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

◆ const_iterator

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

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

Member Function Documentation

◆ count()

◆ is_read_only()

template<typename type_t >
virtual bool xtd::collections::generic::icollection< type_t >::is_read_only ( ) const
pure virtualnoexcept

Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.

Returns
true if the xtd::collections::generic::icollection <type_t> is read-only; otherwise, false.
Remarks
A collection that is read-only does not allow the addition or removal of elements after the collection is created. Note that read-only in this context does not indicate whether individual elements of the collection can be modified, since the xtd::collections::generic::icollection <type_t> interface only supports addition and removal operations. For example, the IsReadOnly property of an array that is cast or converted to an xtd::collections::generic::icollection <type_t> object returns true, even though individual array elements can be modified.

Implemented in xtd::basic_array< type_t, allocator_t >, xtd::basic_array< type_t, allocator_t >, xtd::basic_array< value_type, allocator_t >, and xtd::collections::generic::list< type_t, allocator_t >.

◆ is_synchronized()

template<typename type_t >
virtual bool xtd::collections::generic::icollection< type_t >::is_synchronized ( ) const
pure virtualnoexcept

Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is synchronized (thread safe).

Returns
true if access to the xtd::collections::generic::icollection <type_t> is synchronized (thread safe); otherwise, false.
Remarks
xtd::collections::generic::icollection::sync_root returns an object, which can be used to synchronize access to the xtd::collections::generic::icollection <type_t>.
Most collection classes in the xtd::collections namespace also implement a synchronized method, which provides a synchronized wrapper around the underlying collection.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the xtd::collections::generic::icollection::sync_root property during the entire enumeration.
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
for (auto item : my_collection) {
// Insert your code here.
}
}
virtual const xtd::object & sync_root() const noexcept=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.h:85

Implemented in xtd::basic_array< type_t, allocator_t >, xtd::basic_array< type_t, allocator_t >, xtd::basic_array< value_type, allocator_t >, and xtd::collections::generic::list< type_t, allocator_t >.

◆ sync_root()

template<typename type_t >
virtual const xtd::object & xtd::collections::generic::icollection< type_t >::sync_root ( ) const
pure virtualnoexcept

Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.

Returns
An object that can be used to synchronize access to the the xtd::collections::generic::icollection <type_t>.
Remarks
For collections whose underlying store is not publicly available, the expected implementation is to return the current instance. Note that the pointer to the current instance might not be sufficient for collections that wrap other collections; those should return the underlying collection's sync_root property.
Most collection classes in the xts::.collections namespace also implement a synchronized method, which provides a synchronized wrapper around the underlying collection. However, derived classes can provide their own synchronized version of the collection using the xtd::collections::generic::icollection::sync_root property. The synchronizing code must perform operations on the xtd::collections::generic::icollection::sync_root property of the collection, not directly on the collection. This ensures proper operation of collections that are derived from other objects. Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the collection instance.
In the absence of a synchronized method on a collection, the expected usage for the xtd::collections::generic::icollection::sync_root looks as follows:
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
// Some operation on the collection, which is now thread safe.
}
@encode
@remarks Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
@remarks The following code example shows how to lock the collection using the xtd::collections::generic::icollection::sync_root property during the entire enumeration.
@code
icollection& my_collection = some_collection;
lock_(my_collection.sync_root()) {
for (auto item : my_collection) {
// Insert your code here.
}
}
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.h:28
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock_guard.h:32
bool is(std::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.h:365
@ other
The operating system is other.
@ a
The A key.

Implemented in xtd::basic_array< type_t, allocator_t >, xtd::basic_array< type_t, allocator_t >, xtd::basic_array< value_type, allocator_t >, xtd::collections::generic::list< type_t, allocator_t >, and xtd::collections::object_model::read_only_collection< type_t >.

◆ add()

template<typename type_t >
virtual void xtd::collections::generic::icollection< type_t >::add ( const type_t &  item)
pure virtual

Adds an item to the xtd::collections::generic::icollection <type_t>.

Parameters
itemThe object to add to the xtd::collections::generic::icollection <type_t>.
Exceptions
xtd::not_supported_exceptionThe xtd::collections::generic::icollection <type_t> is read-only.

Implemented in xtd::collections::generic::list< type_t, allocator_t >.

◆ clear()

template<typename type_t >
virtual void xtd::collections::generic::icollection< type_t >::clear ( )
pure virtual

Removes all items from the xtd::collections::generic::icollection <type_t>.

Exceptions
xtd::not_supported_exceptionThe xtd::collections::generic::icollection <type_t> is read-only.
Remarks
xtd::collections::generic::icollection::count must be set to 0, and references to other objects from elements of the collection must be released.

Implemented in xtd::collections::generic::list< type_t, allocator_t >.

◆ contains()

template<typename type_t >
virtual bool xtd::collections::generic::icollection< type_t >::contains ( const type_t &  item) const
pure virtualnoexcept

Determines whether the xtd::collections::generic::icollection <type_t> contains a specific value.

Parameters
itemThe object to locate in the xtd::collections::generic::icollection <type_t>.
Returns
true if item is found in the xtd::collections::generic::icollection <type_t>; otherwise, false.
Remarks
Implementations can vary in how they determine equality of objects; for example, xtd::collections::generic::list <type_t> uses xtd::collections::generic::compoarer <type_t>::default_comparer, whereas xtd::collections::generic::dictionary <key_t, value_t> allows the user to specify the xtd::collections::generic::icompoarer <type_t> implementation to use for comparing keys.

Implemented in xtd::collections::object_model::read_only_collection< type_t >, xtd::basic_array< type_t, allocator_t >, xtd::basic_array< type_t, allocator_t >, and xtd::collections::generic::list< type_t, allocator_t >.

◆ copy_to()

template<typename type_t >
virtual void xtd::collections::generic::icollection< type_t >::copy_to ( xtd::array< type_t > &  array,
xtd::size  array_index 
) const
pure virtual

Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array, starting at a particular xtd::array index.

Parameters
arrayThe one-dimensional xtd::array that is the destination of the elements copied from xtd::collections::generic::icollection <type_t>. The xtd::array must have zero-based indexing.
array_indexThe zero-based index in array at which copying begins.
Exceptions
xtd::argument_exceptionThe number of elements in the source xtd::collections::generic::icollection <type_t> is greater than the available space from `array_index` to the end of the destination `array`.

Implemented in xtd::collections::generic::list< type_t, allocator_t >, xtd::array< type_t, 1, allocator_t >, xtd::basic_array< type_t, allocator_t >, xtd::basic_array< type_t, allocator_t >, and xtd::collections::object_model::read_only_collection< type_t >.

◆ remove()

template<typename type_t >
virtual bool xtd::collections::generic::icollection< type_t >::remove ( const type_t &  item)
pure virtual

Removes the first occurrence of a specific object from the xtd::collections::generic::icollection <type_t>.

Parameters
itemThe object to remove from the xtd::collections::generic::icollection <type_t>.
Returns
true if item was successfully removed from the xtd::collections::generic::icollection <type_t>; otherwise, false. This method also returns false if item is not found in the original xtd::collections::generic::icollection <type_t>.
Exceptions
xtd::not_supported_exceptionThe xtd::collections::generic::icollection <type_t> is read-only.
Remarks
Implementations can vary in how they determine equality of objects; for example, xtd::collections::generic::list <type_t> uses xtd::collections::generic::compoarer <type_t>::default_comparer, whereas, xtd::collections::generic::dictionary <key_t, value_t> allows the user to specify the xtd::collections::generic::icompoarer <type_t> implementation to use for comparing keys.
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hash table.

Implemented in xtd::collections::generic::list< type_t, allocator_t >.


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