rippled
Loading...
Searching...
No Matches
SHAMapHash.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2021 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_BASICS_SHAMAP_HASH_H_INCLUDED
21#define RIPPLE_BASICS_SHAMAP_HASH_H_INCLUDED
22
23#include <xrpl/basics/base_uint.h>
24#include <xrpl/basics/partitioned_unordered_map.h>
25
26#include <ostream>
27
28namespace ripple {
29
30// A SHAMapHash is the hash of a node in a SHAMap, and also the
31// type of the hash of the entire SHAMap.
32
34{
36
37public:
38 SHAMapHash() = default;
39 explicit SHAMapHash(uint256 const& hash) : hash_(hash)
40 {
41 }
42
43 uint256 const&
44 as_uint256() const
45 {
46 return hash_;
47 }
48 uint256&
50 {
51 return hash_;
52 }
53 bool
54 isZero() const
55 {
56 return hash_.isZero();
57 }
58 bool
59 isNonZero() const
60 {
61 return hash_.isNonZero();
62 }
63 int
64 signum() const
65 {
66 return hash_.signum();
67 }
68 void
70 {
71 hash_.zero();
72 }
73
74 friend bool
75 operator==(SHAMapHash const& x, SHAMapHash const& y)
76 {
77 return x.hash_ == y.hash_;
78 }
79
80 friend bool
81 operator<(SHAMapHash const& x, SHAMapHash const& y)
82 {
83 return x.hash_ < y.hash_;
84 }
85
88 {
89 return os << x.hash_;
90 }
91
92 friend std::string
94 {
95 return to_string(x.hash_);
96 }
97
98 template <class H>
99 friend void
100 hash_append(H& h, SHAMapHash const& x)
101 {
102 hash_append(h, x.hash_);
103 }
104};
105
106inline bool
108{
109 return !(x == y);
110}
111
112template <>
113inline std::size_t
115{
116 return *reinterpret_cast<std::size_t const*>(key.as_uint256().data());
117}
118
119} // namespace ripple
120
121#endif // RIPPLE_BASICS_SHAMAP_HASH_H_INCLUDED
SHAMapHash()=default
friend std::string to_string(SHAMapHash const &x)
Definition SHAMapHash.h:93
friend bool operator==(SHAMapHash const &x, SHAMapHash const &y)
Definition SHAMapHash.h:75
friend bool operator<(SHAMapHash const &x, SHAMapHash const &y)
Definition SHAMapHash.h:81
SHAMapHash(uint256 const &hash)
Definition SHAMapHash.h:39
bool isNonZero() const
Definition SHAMapHash.h:59
friend void hash_append(H &h, SHAMapHash const &x)
Definition SHAMapHash.h:100
uint256 const & as_uint256() const
Definition SHAMapHash.h:44
friend std::ostream & operator<<(std::ostream &os, SHAMapHash const &x)
Definition SHAMapHash.h:87
bool isZero() const
Definition SHAMapHash.h:54
uint256 & as_uint256()
Definition SHAMapHash.h:49
int signum() const
Definition SHAMapHash.h:64
bool isZero() const
Definition base_uint.h:540
constexpr int signum() const
Definition base_uint.h:334
bool isNonZero() const
Definition base_uint.h:545
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:232
std::size_t extract(uint256 const &key)
Definition base_uint.h:654