xtd 0.2.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
Combines the hash code for multiple values into a single hash code.
Definition hash_code.hpp:26
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:42
Namespace
xtd
Library
xtd.core
Examples
The static methods in this class combine the default hash codes of several values.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::collections::generic;
struct order_order_line : public object, public iequatable<order_order_line> {
public:
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_;}
size 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 {
order_order_line {1, 1},
order_order_line {1, 1},
order_order_line {1, 2}
};
console::write_line("Item count: {}.", set.size());
}
// This code produces the following output :
//
// Item count: 2.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
std::unordered_set< type_t, helpers::hasher< type_t >, helpers::equator< type_t >, helpers::allocator< type_t > > hash_set
Represents a set of values.
Definition hash_set.hpp:33
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
@ other
The operating system is other.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
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.
Examples
generic_icollection.cpp, and generic_ilist.cpp.

Public Constructors

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

Public Methods

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

Public Static Methods

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

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::string to_string () const noexcept
 Returns a xtd::string that represents the current object.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<typename object_a_t , typename object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 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 >
hash_code & xtd::hash_code::add ( const type_t  value)
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()

bool xtd::hash_code::equals ( const xtd::object obj) const
overridevirtualnoexcept

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.
Examples
The following code example compares the current instance with another object.
#include <xtd/console>
using namespace xtd;
auto main() -> int {
auto object1 = new_ptr<object>();
auto object2 = new_ptr<object>();
auto object3 = object2;
console::write_line(object1->equals(*object3));
console::write_line(*object1 == *object3);
object3 = object1;
console::write_line(object1->equals(*object3));
console::write_line(*object1 == *object3);
}
// This code produces the following output :
//
// false
// false
// true
// true

Reimplemented from xtd::object.

◆ get_hash_code()

xtd::size xtd::hash_code::get_hash_code ( ) const
overridevirtualnoexcept

Serves as a hash function for a particular type.

Returns
size_t A hash code for the current object.

Reimplemented from xtd::object.

◆ to_hash_code()

xtd::size xtd::hash_code::to_hash_code ( ) const
noexcept

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>
static xtd::size xtd::hash_code::combine ( args_t...  values)
inlinestaticnoexcept

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: