xtd 0.2.0
Loading...
Searching...
No Matches
collections

Definition

Contains collections definitions.

Modules

 generic
 Contains generic collections definitions.
 
 object model
 Contains object model collections definitions.
 
 specialized
 Contains specialized collections definitions.
 

Classes

class  xtd::forms::layout::arranged_element_collection< type_t, sorter_t >
 Represents a collection of objects. More...
 

Typedefs

using xtd::collections::any_pair = xtd::collections::generic::key_value_pair< std::any, std::any >
 Implements a std::pair with the key and the value strongly typed to be std::any.
 
using xtd::collections::array_list = generic::list< xtd::any_object >
 Represents a collection of std::any.
 
using xtd::collections::enumerator = generic::enumerator< xtd::any_object >
 Supports a simple iteration over a non-generic collection.
 
using xtd::collections::hashtable = generic::dictionary< xtd::any_object, xtd::any_object >
 Represents a collection of key/value pairs that are organized based on the hash code of the key.
 
using xtd::collections::vector_list = array_list
 Represents a collection of std::any.
 

Typedef Documentation

◆ any_pair

#include <xtd.core/include/xtd/collections/any_pair.hpp>

Implements a std::pair with the key and the value strongly typed to be std::any.

xtd::collections::generic::key_value_pair< std::any, std::any > any_pair
Implements a std::pair with the key and the value strongly typed to be std::any.
Definition any_pair.hpp:26
std::pair< key_t, value_t > key_value_pair
Defines a key/value pair that can be set or retrieved.
Definition key_value_pair.hpp:29
Header
#include <xtd/collections/any_pair>
Namespace
xtd::collections
Library
xtd.core

◆ array_list

#include <xtd.core/include/xtd/collections/array_list.hpp>

Represents a collection of std::any.

Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:71
Header
#include <xtd/collections/array_list>
Namespace
xtd::collections
Library
xtd.core
Examples
The following example shows hows to use xtd::collections::array_list.
#include <xtd/collections/array_list>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections;
template<typename collection_t>
void print_values(const collection_t& my_list) {
for (auto obj : my_list)
console::write(" {0}", obj);
}
auto main() -> int {
// Creates and initializes a new vector_list.
auto my_vl = array_list {};
my_vl.emplace_back("Hello");
my_vl.emplace_back("World");
my_vl.emplace_back("!");
// Displays the properties and values of the vector_list.
console::write_line(" size: {0}", my_vl.size());
console::write_line(" Capacity: {0}", my_vl.capacity());
console::write(" Values:");
print_values(my_vl);
}
// This code produces the following output :
//
// my_vl
// size: 3
// Capacity: 4
// Values: Hello World !
reference emplace_back(args_t &&... args)
Appends a new element to the end of the container. The element is constructed through std::allocator_...
Definition list.hpp:530
Represents the standard input, output, and error streams for console applications.
Definition console.hpp:36
static void write(arg_t &&value)
Writes the text representation of the specified value to the standard output stream.
Definition console.hpp:462
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Remarks
TTo add a class, structure or any other type unknown to xtd, you need to implement the xtd::iequatable and xtd::icompoabale interfaces or overrride the == and < operetors.

◆ enumerator

#include <xtd.core/include/xtd/collections/enumerator.hpp>

Supports a simple iteration over a non-generic collection.

Supports a simple iteration over a generic collection.
Definition enumerator.hpp:31
Header
#include <xtd/collections/ienumerator>
Namespace
xtd::collections
Library
xtd.core
Remarks
The xtd::collections::enumerator class is used to encapsulate an xtd::collections::ienumerator.

◆ hashtable

#include <xtd.core/include/xtd/collections/hashtable.hpp>

Represents a collection of key/value pairs that are organized based on the hash code of the key.

generic::dictionary< xtd::any_object, xtd::any_object > hashtable
Represents a collection of key/value pairs that are organized based on the hash code of the key.
Definition hashtable.hpp:47
std::unordered_map< key_t, value_t, helpers::hasher< key_t >, helpers::equator< key_t >, helpers::allocator< std::pair< const key_t, value_t > > > dictionary
Represents a collection of keys and values.
Definition dictionary.hpp:36
Header
#include <xtd/collections/hashtable>
Namespace
xtd::collections
Library
xtd.core
Remarks
Each element is a key/value pair stored in a xtd::collections::dictionary_entry object. A key cannot be null, but a value can be.
The objects used as keys by a xtd::collections::hashtable are required to override the xtd::object::get_hash_code method (or the xtd::ihash_code_provider interface) and the xtd::object::equals method (or the xtd::icomparer interface). The implementation of both methods and interfaces must handle case sensitivity the same way; otherwise, the xtd::collections::hashtable might behave incorrectly. For example, when creating a xtd::collections::hashtable, you must use the xtd::case_insensitive_hash_code_provider class (or any case-insensitive xtd::ihash_code_provider implementation) with the xtd::case_insensitive_comparer class (or any case-insensitive xtd::icComparer implementation).
Furthermore, these methods must produce the same results when called with the same parameters while the key exists in the xtd::collections::hashtable. An alternative is to use a xtd::collections::hashtable constructor with an xtd::iequality_comparer parameter. If key equality were simply reference equality, the inherited implementation of xtd::object::get_hash_code and xtd::object::equals would suffice.
Key objects must be immutable as long as they are used as keys in the xtd::collections::hashtable.
When an element is added to the xtd::collections::hashtable, the element is placed into a bucket based on the hash code of the key. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element.
The load factor of a xtd::collections::hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default load factor of 1.0 generally provides the best balance between speed and size. A different load factor can also be specified when the xtd::collections::hashtable is created.
As elements are added to a xtd::collections::hashtable, the actual load factor of the xtd::collections::hashtable increases. When the actual load factor reaches the specified load factor, the number of buckets in the xtd::collections::hashtable is automatically increased to the smallest prime number that is larger than twice the current number of xtd::collections::hashtable buckets.
Each key object in the xtd::collections::hashtable must provide its own hash function, which can be accessed by calling GetHash. However, any object implementing IHashCodeProvider can be passed to a xtd::collections::hashtable constructor, and that hash function is used for all objects in the table
The capacity of a xtd::collections::hashtable is the number of elements the xtd::collections::hashtable can hold. As elements are added to a xtd::collections::hashtable, the capacity is automatically increased as required through reallocation.
The foreach statement returns an object of the type of the elements in the collection. Since each element of the xtd::collections::hashtable is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is xtd::collections::dictionary_entry. For example:
for (xtd::collections::dictionary_entry de : my_hashtable) {
// ...
}
The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.
Because serializing and deserializing an enumerator for a xtd::collections::hashtable can cause the elements to become reordered, it is not possible to continue enumeration without calling the Reset method.
Note
Because keys can be inherited and their behavior changed, their absolute uniqueness cannot be guaranteed by comparisons using the Equals method.
Exemples
The following example shows how to create, initialize and perform various functions to a xtd::collections::hashtable and how to print out its keys and values.
#include <xtd/collections/hashtable>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections;
auto main() -> int {
// Create a new hash table.
//
hashtable open_with;
// Add some elements to the hash table. There are no
// duplicate keys, but some of the values are duplicates.
open_with["txt"] = "notepad.exe";
open_with["bmp"] = "paint.exe";
open_with["dib"] = "paint.exe";
open_with["rtf"] = "wordpad.exe";
// The Item property is the default property, so you
// can omit its name when accessing elements.
console::write_line("For key = \"rtf\", value = {0}.", open_with["rtf"]);
// The default Item property can be used to change the value
// associated with a key.
open_with["rtf"] = "winword.exe";
console::write_line("For key = \"rtf\", value = {0}.", open_with["rtf"]);
// If a key does not exist, setting the default Item property
// for that key adds a new key/value pair.
open_with["doc"] = "winword.exe";
// contains can be used to test keys before inserting
// them.
if (!open_with.contains("ht")) {
open_with["ht"] = "hypertrm.exe";
console::write_line("Value added for key = \"ht\": {0}", open_with["ht"]);
}
// When you use foreach to enumerate hash table elements,
// the elements are retrieved as xtd::collections::generic::key_value_pair objects.
for (auto de : open_with)
console::write_line("Key = {0}, Value = {1}", de.first, de.second);
// Use the Remove method to remove a key/value pair.
console::write_line("\nerase(\"doc\")");
open_with.erase("doc");
if (!open_with.contains("doc"))
console::write_line("Key \"doc\" is not found.");
}
// This code produces the following output :
//
// For key = "rtf", value = wordpad.exe.
// For key = "rtf", value = winword.exe.
// Value added for key = "ht": hypertrm.exe
//
// Key = ht, Value = hypertrm.exe
// Key = doc, Value = winword.exe
// Key = bmp, Value = paint.exe
// Key = rtf, Value = winword.exe
// Key = dib, Value = paint.exe
// Key = txt, Value = notepad.exe
//
// erase("doc")
// Key "doc" is not found.

◆ vector_list

#include <xtd.core/include/xtd/collections/vector_list.hpp>

Represents a collection of std::any.

Header
#include <xtd/collections/vector_list>
Namespace
xtd::collections
Library
xtd.core
Examples
The following example shows hows to use xtd::collections::array_list.
#include <xtd/collections/array_list>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections;
template<typename collection_t>
void print_values(const collection_t& my_list) {
for (auto obj : my_list)
console::write(" {0}", obj);
}
auto main() -> int {
// Creates and initializes a new vector_list.
auto my_vl = array_list {};
my_vl.emplace_back("Hello");
my_vl.emplace_back("World");
my_vl.emplace_back("!");
// Displays the properties and values of the vector_list.
console::write_line(" size: {0}", my_vl.size());
console::write_line(" Capacity: {0}", my_vl.capacity());
console::write(" Values:");
print_values(my_vl);
}
// This code produces the following output :
//
// my_vl
// size: 3
// Capacity: 4
// Values: Hello World !
Deprecated:
Replaced by xtd::collections::array_list - Will be removed in version 0.4.0.