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
25#include <ostream>
26
27namespace ripple {
28
29// A SHAMapHash is the hash of a node in a SHAMap, and also the
30// type of the hash of the entire SHAMap.
31
33{
35
36public:
37 SHAMapHash() = default;
38 explicit SHAMapHash(uint256 const& hash) : hash_(hash)
39 {
40 }
41
42 uint256 const&
43 as_uint256() const
44 {
45 return hash_;
46 }
47 uint256&
49 {
50 return hash_;
51 }
52 bool
53 isZero() const
54 {
55 return hash_.isZero();
56 }
57 bool
58 isNonZero() const
59 {
60 return hash_.isNonZero();
61 }
62 int
63 signum() const
64 {
65 return hash_.signum();
66 }
67 void
69 {
70 hash_.zero();
71 }
72
73 friend bool
74 operator==(SHAMapHash const& x, SHAMapHash const& y)
75 {
76 return x.hash_ == y.hash_;
77 }
78
79 friend bool
80 operator<(SHAMapHash const& x, SHAMapHash const& y)
81 {
82 return x.hash_ < y.hash_;
83 }
84
87 {
88 return os << x.hash_;
89 }
90
91 friend std::string
93 {
94 return to_string(x.hash_);
95 }
96
97 template <class H>
98 friend void
99 hash_append(H& h, SHAMapHash const& x)
100 {
101 hash_append(h, x.hash_);
102 }
103};
104
105inline bool
107{
108 return !(x == y);
109}
110
111template <>
112inline std::size_t
114{
115 return *reinterpret_cast<std::size_t const*>(key.as_uint256().data());
116}
117
118} // namespace ripple
119
120#endif // RIPPLE_BASICS_SHAMAP_HASH_H_INCLUDED
SHAMapHash()=default
friend std::string to_string(SHAMapHash const &x)
Definition: SHAMapHash.h:92
friend bool operator==(SHAMapHash const &x, SHAMapHash const &y)
Definition: SHAMapHash.h:74
friend bool operator<(SHAMapHash const &x, SHAMapHash const &y)
Definition: SHAMapHash.h:80
SHAMapHash(uint256 const &hash)
Definition: SHAMapHash.h:38
bool isNonZero() const
Definition: SHAMapHash.h:58
friend void hash_append(H &h, SHAMapHash const &x)
Definition: SHAMapHash.h:99
uint256 const & as_uint256() const
Definition: SHAMapHash.h:43
friend std::ostream & operator<<(std::ostream &os, SHAMapHash const &x)
Definition: SHAMapHash.h:86
bool isZero() const
Definition: SHAMapHash.h:53
uint256 & as_uint256()
Definition: SHAMapHash.h:48
int signum() const
Definition: SHAMapHash.h:63
pointer data()
Definition: base_uint.h:125
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:644