xtd 0.2.0
Loading...
Searching...
No Matches
icollection.hpp File Reference

Definition

Contains xtd::collections::generic::icollection <type_t> interface.

Go to the source code of this file.

Namespaces

namespace  xtd
 The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
namespace  xtd::collections
 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  xtd::collections::generic
 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.

Public Aliases

Defines methods to manipulate generic collections.

Definition
template<class type_t>
class icollection : public xtd::collections::generic::ienumerable<type_t>, public xtd::collections::generic::extensions::collection_common<type_t, icollection<type_t>>;
Internal collection common definition.
Definition collection_common.hpp:32
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
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 box_same_dimensions and 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});
// 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 :
xtd::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, xtd::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:
list<program::box> boxes_;
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_.count();}
void reset() override {index_ = size_object::max_value;}
protected:
const list<program::box>& items_;
size index_ = size_object::max_value;
};
// 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());
//}
}
};
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
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
virtual bool equals(const object &obj) const noexcept
Determines whether the specified object is equal to the current object.
generic::icollection< xtd::any_object > icollection
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition icollection.hpp:32
xtd::collections::generic::comparer< xtd::any_object > comparer
Exposes a method that compares two objects.
Definition comparer.hpp:25
generic::ienumerator< xtd::any_object > ienumerator
Supports a simple iteration over a non-generic collection.
Definition ienumerator.hpp:38
generic::iequality_comparer< xtd::any_object > iequality_comparer
Defines methods to support the comparison of objects for equality.
Definition iequality_comparer.hpp:29
#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:283
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
@ h
The H key.
Definition console_key.hpp:102
@ l
The L key.
Definition console_key.hpp:110
@ w
The W key.
Definition console_key.hpp:132
@ display
Specifies 1/75 inch as the unit of measure.
Definition graphics_unit.hpp:21
@ height
Specifies that the height of the control is defined.
Definition bounds_specified.hpp:34
@ width
Specifies that the width of the control is defined.
Definition bounds_specified.hpp:32
virtual auto remove(const type_t &item) -> bool=0
Removes the first occurrence of a specific object from the xtd::collections::generic::icollection <ty...
virtual auto sync_root() const noexcept -> const xtd::object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
virtual auto contains(const type_t &item) const noexcept -> bool=0
Determines whether the xtd::collections::generic::icollection <type_t> contains a specific value.
virtual auto copy_to(xtd::array< type_t > &array, xtd::size array_index) const -> void=0
Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array,...
virtual auto add(const type_t &item) -> void=0
Adds an item to the xtd::collections::generic::icollection <type_t>.
virtual auto is_synchronized() const noexcept -> bool=0
Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is sync...
virtual auto clear() -> void=0
Removes all items from the xtd::collections::generic::icollection <type_t>.
virtual auto is_read_only() const noexcept -> bool=0
Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.
virtual auto count() const noexcept -> xtd::size=0
Gets the number of elements contained in the xtd::collections::generic::icollection <type_t>.
bool equals(const object &obj) const noexcept override
Determines whether the specified object is equal to the current object.
Definition read_only_span.hpp:272
constexpr size_type length() const noexcept
Returns the length of the current read_only_span.
Definition read_only_span.hpp:229
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
Definition read_only_span.hpp:296
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. */ template<class type_t> class icollection : public xtd::collections::generic::ienumerable<type_t>, public xtd::collections::generic::extensions::collection_common<type_t, icollection<type_t >> { public: /

/**

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

using xtd::collections::generic::value_type

Public Properties

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

Public Methods

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