xtd 0.2.0
Loading...
Searching...
No Matches
hasher.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "../../../object.hpp"
8#include <type_traits>
9
11namespace xtd {
13 namespace collections {
15 namespace generic {
17 namespace helpers {
39 template<class key_t>
40 struct hasher {
42
45 using argument_type = key_t;
49
51
54 hasher() noexcept = default;
57 explicit hasher(const xtd::collections::generic::iequality_comparer<key_t>& comparer) noexcept : comparer {const_cast<xtd::collections::generic::iequality_comparer<key_t>*>(&comparer)} {}
59
61 hasher(const hasher&) noexcept = default;
62 hasher(hasher&&) noexcept = default;
63 hasher& operator=(const hasher&) noexcept = default;
64 hasher& operator=(hasher&&) noexcept = default;
66
68
74 auto operator()(const argument_type& key) const noexcept -> result_type {
75 if (comparer) return comparer->get_hash_code(key);
76 if constexpr(std::is_polymorphic_v<argument_type> && std::is_base_of_v<xtd::ihashable, argument_type>) return static_cast<const xtd::ihashable&>(key).get_hash_code();
77 else if constexpr(std::is_polymorphic_v<argument_type> && std::is_base_of_v<xtd::object, argument_type>) return static_cast<const xtd::object&>(key).get_hash_code();
78 else if constexpr(std::is_invocable_v<std::hash<argument_type>, const argument_type&>) return std::hash<argument_type> {}(key);
79 else return std::hash<std::intptr_t> {}(reinterpret_cast<std::intptr_t>(&key));
80 }
81
82
83 private:
85 };
86 }
87 }
88 }
89}
Defines methods to support the comparison of objects for equality.
Definition iequality_comparer.hpp:34
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition ihashable.hpp:21
virtual xtd::size get_hash_code() const noexcept=0
Serves as a hash function for a particular type.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
virtual xtd::size get_hash_code() const noexcept
Serves as a hash function for a particular type.
Contains xtd::collections::generic::iequality_comparer <type_t> interface.
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
Contains xtd::ihashable interface.
The xtd::collections::generic::helpers namespace contains helpers for generic collections,...
Definition allocator.hpp:14
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::object class.
Implements a function object for compare data.
Definition comparer.hpp:32
key_t argument_type
Represents the argument type.
Definition hasher.hpp:45
xtd::size result_type
Represents the result type.
Definition hasher.hpp:47
hasher() noexcept=default
Initializes a new instance of the hasher.