Shows how to use xtd::collections::generic::icollection interface.
#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});
display(boxes);
boxes.remove(program::box {6, 10, 4});
display(boxes);
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 height = 0;
int width = 0;
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;}
};
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;}
}
};
static void display(const box_collection& boxes) {
}
};
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:61
static constexpr type_t max_value
Represents the largest possible value of type_t. This field is constant.
Definition box_integer.hpp:67
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
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Combines the hash code for multiple values into a single hash code.
Definition hash_code.hpp:26
static xtd::size combine(args_t... values) noexcept
Combines values into a hash code.
Definition hash_code.hpp:70
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
virtual bool equals(const object &obj) const noexcept
Determines whether the specified object is equal to the current object.
#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:167
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
bool is(std::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:365
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ clear
The CLEAR key.
Definition console_key.hpp:26
@ h
The H key.
Definition console_key.hpp:102
@ l
The L key.
Definition console_key.hpp:110
@ add
The Add key.
Definition console_key.hpp:170
@ o
The O key.
Definition console_key.hpp:116
@ w
The W key.
Definition console_key.hpp:132
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
virtual bool remove(const type_t &item)=0
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
void copy_to(span< type_t, length > &destination) const
Copies the contents of this xtd::read_only_span <type_t> into a destination xtd:span <type_t>.
Definition read_only_span.hpp:264
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
Represents a boxed object.
Definition box.hpp:57
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
Definition box.hpp:144
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38