xtd 1.0.0
Loading...
Searching...
No Matches
xtd::hash_code Class Reference
Inheritance diagram for xtd::hash_code:
xtd::object

Definition

Combines the hash code for multiple values into a single hash code.

class hash_code : public xtd::object
hash_code()=default
Create a new instance of the class hash_code.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Namespace
xtd
Library
xtd.core
Examples
The static methods in this class combine the default hash codes of several values.
#include <xtd/xtd>
struct order_order_line : public object, public iequatable<order_order_line> {
public:
order_order_line() = default;
order_order_line(int order_id, int order_line_id) : order_id_ {order_id}, order_line_id_ {order_line_id} {}
int order_id() const noexcept {return order_id_;}
int order_line_id() const noexcept {return order_line_id_;}
bool equals(const object& obj) const noexcept override {return is<order_order_line>(obj) && equals(as<order_order_line>(obj));}
bool equals(const order_order_line& other) const noexcept override {return order_id_ == other.order_id_ && order_line_id_ == other.order_line_id_;}
usize get_hash_code() const noexcept override {return hash_code::combine(order_id_, order_line_id_);}
private:
int order_id_ = 0;
int order_line_id_ = 0;
};
auto main() -> int {
auto set = hash_set<order_order_line> {
order_order_line {1, 1},
order_order_line {1, 1},
order_order_line {1, 2}
};
console::write_line("Item count: {}.", set.count());
}
// This code produces the following output :
//
// Item count: 2.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
virtual auto equals(const object &obj) const noexcept -> bool
Determines whether the specified object is equal to the current object.
object()=default
Create a new instance of the ultimate base class object.
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto is(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
@ other
The operating system is other.
Definition platform_id.hpp:60
auto get_hash_code() const noexcept -> xtd::usize override
Serves as a hash function for a particular type.
Definition read_only_span.hpp:263
Remarks
You can use xtd::hash_code to combine multiple values (for example, fields on a structure or class) into a single hash code. This structure has static and instance methods that operate differently:
* The static methods accept a set of several values to combine.
* Two instance methods operate in a streaming fashion, accepting values one at a time.
Warning
It's best-practice to consider hash codes as an implementation detail, as the implementation may change across xtd versions. Do not store hash codes produced by xtd::hash_code in serialized structures, for example, on-disk. xtd::hash_code uses a statically initialized random seed to enforce this best practice, meaning that the hash codes are only deterministic within the scope of an operating system process.

Public Constructors

 hash_code ()=default
 Create a new instance of the class hash_code.

Public Methods

template<typename type_t>
auto add (const type_t &value) noexcept -> hash_code &
 Adds a single value to the hash code.
auto equals (const xtd::object &other) const noexcept -> bool override
 Determines whether the specified object is equal to the current object.
auto get_hash_code () const noexcept -> xtd::usize override
 Serves as a hash function for a particular type.
auto to_hash_code () const noexcept -> xtd::usize
 Calculates the final hash code after consecutive xtd::hash_code::add invocations.

Public Static Methods

template<typename ... args_t>
static auto combine (args_t... values) noexcept -> xtd::usize
 Combines values into a hash code.

Additional Inherited Members

 object ()=default
 Create a new instance of the ultimate base class object.
virtual auto get_type () const noexcept -> type_object
 Gets the type of the current instance.
template<typename object_t>
auto memberwise_clone () const -> xtd::unique_ptr_object< object_t >
 Creates a shallow copy of the current object.
virtual auto to_string () const -> xtd::string
 Returns a xtd::string that represents the current object.
template<typename object_a_t, typename object_b_t>
static auto equals (const object_a_t &object_a, const object_b_t &object_b) noexcept -> bool
 Determines whether the specified object instances are considered equal.
template<typename object_a_t, typename object_b_t>
static auto reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept -> bool
 Determines whether the specified object instances are the same instance.

Constructor & Destructor Documentation

◆ hash_code()

xtd::hash_code::hash_code ( )
default

Create a new instance of the class hash_code.

Member Function Documentation

◆ add()

template<typename type_t>
auto xtd::hash_code::add ( const type_t & value) -> hash_code &
inlinenoexcept

Adds a single value to the hash code.

Template Parameters
type_tThe type of the value to add to the hash code.
Parameters
valueThe value to add to the hash code.
Returns
The current instance.

◆ equals()

auto xtd::hash_code::equals ( const xtd::object & other) const -> bool
nodiscardoverridevirtualnoexcept

Determines whether the specified object is equal to the current object.

Parameters
objThe object to compare with the current object.
Returns
true if the specified object is equal to the current object. otherwise, false.

Reimplemented from xtd::object.

◆ get_hash_code()

auto xtd::hash_code::get_hash_code ( ) const -> xtd::usize
nodiscardoverridevirtualnoexcept

Serves as a hash function for a particular type.

Returns
A hash code for the current object.

Reimplemented from xtd::object.

◆ to_hash_code()

auto xtd::hash_code::to_hash_code ( ) const -> xtd::usize
nodiscardnoexcept

Calculates the final hash code after consecutive xtd::hash_code::add invocations.

Returns
The calculated hash code.
Remarks
This method must be called at most once per instance of xtd::hash_code.

◆ combine()

template<typename ... args_t>
auto xtd::hash_code::combine ( args_t... values) -> xtd::usize
inlinestaticnodiscardnoexcept

Combines values into a hash code.

Parameters
valuesThe values to combine into the hash code.
Returns
The hash code that represents the values.

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