xrpld
Loading...
Searching...
No Matches
Book.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/protocol/Asset.h>
6
7#include <boost/utility/base_from_member.hpp>
8
9namespace xrpl {
10
15class Book final : public CountedObject<Book>
16{
17public:
21
22 Book() = default;
23
25 : in(in), out(out), domain(domain)
26 {
27 }
28};
29
30bool
31isConsistent(Book const& book);
32
34to_string(Book const& book);
35
37operator<<(std::ostream& os, Book const& x);
38
39template <class Hasher>
40void
41hash_append(Hasher& h, Book const& b)
42{
44 hash_append(h, b.in, b.out);
45 if (b.domain)
46 hash_append(h, *(b.domain));
47}
48
49Book
50reversed(Book const& book);
51
54[[nodiscard]] constexpr bool
55operator==(Book const& lhs, Book const& rhs)
56{
57 return (lhs.in == rhs.in) && (lhs.out == rhs.out) && (lhs.domain == rhs.domain);
58}
59
60
63[[nodiscard]] constexpr std::weak_ordering
64operator<=>(Book const& lhs, Book const& rhs)
65{
66 if (auto const c{lhs.in <=> rhs.in}; c != 0)
67 return c;
68 if (auto const c{lhs.out <=> rhs.out}; c != 0)
69 return c;
70
71 // Manually compare optionals
72 if (lhs.domain && rhs.domain)
73 return *lhs.domain <=> *rhs.domain; // Compare values if both exist
74 if (!lhs.domain && rhs.domain)
75 return std::weak_ordering::less; // Empty is considered less
76 if (lhs.domain && !rhs.domain)
77 return std::weak_ordering::greater; // Non-empty is greater
78
79 return std::weak_ordering::equivalent; // Both are empty
80}
81
82
83} // namespace xrpl
84
85//------------------------------------------------------------------------------
86
87namespace std {
88
89template <>
90struct hash<xrpl::Issue> : private boost::base_from_member<std::hash<xrpl::Currency>, 0>,
91 private boost::base_from_member<std::hash<xrpl::AccountID>, 1>
92{
93private:
94 using currency_hash_type = boost::base_from_member<std::hash<xrpl::Currency>, 0>;
95 using issuer_hash_type = boost::base_from_member<std::hash<xrpl::AccountID>, 1>;
96
97public:
98 hash() = default;
99
102
104 operator()(argument_type const& value) const
105 {
106 value_type result(currency_hash_type::member(value.currency));
107 if (!isXRP(value.currency))
108 boost::hash_combine(result, issuer_hash_type::member(value.account));
109 return result;
110 }
111};
112
113template <>
114struct hash<xrpl::MPTIssue> : private boost::base_from_member<std::hash<xrpl::MPTID>, 0>
115{
116private:
117 using id_hash_type = boost::base_from_member<std::hash<xrpl::MPTID>, 0>;
118
119public:
120 explicit hash() = default;
121
124
126 operator()(argument_type const& value) const
127 {
128 value_type const result(id_hash_type::member(value.getMptID()));
129 return result;
130 }
131};
132
133template <>
134struct hash<xrpl::Asset>
135{
136private:
139
142
145
146public:
147 explicit hash() = default;
148
150 operator()(argument_type const& asset) const
151 {
152 return asset.visit(
153 [&](xrpl::Issue const& issue) {
154 value_type const result(mIssueHasher_(issue));
155 return result;
156 },
157 [&](xrpl::MPTIssue const& issue) {
158 value_type const result(mMptissueHasher_(issue));
159 return result;
160 });
161 }
162};
163
164//------------------------------------------------------------------------------
165
166template <>
167struct hash<xrpl::Book>
168{
169private:
172
175
176public:
177 hash() = default;
178
181
183 operator()(argument_type const& value) const
184 {
185 value_type result(issueHasher_(value.in));
186 boost::hash_combine(result, issueHasher_(value.out));
187
188 if (value.domain)
189 boost::hash_combine(result, uint256Hasher_(*value.domain));
190
191 return result;
192 }
193};
194
195} // namespace std
196
197//------------------------------------------------------------------------------
198
199namespace boost {
200
201template <>
202struct hash<xrpl::Issue> : std::hash<xrpl::Issue>
203{
204 hash() = default;
205
207 // VFALCO NOTE broken in vs2012
208 // using Base::Base; // inherit ctors
209};
210
211template <>
212struct hash<xrpl::MPTIssue> : std::hash<xrpl::MPTIssue>
213{
214 explicit hash() = default;
215
217};
218
219template <>
220struct hash<xrpl::Asset> : std::hash<xrpl::Asset>
221{
222 explicit hash() = default;
223
225};
226
227template <>
228struct hash<xrpl::Book> : std::hash<xrpl::Book>
229{
230 hash() = default;
231
233 // VFALCO NOTE broken in vs2012
234 // using Base::Base; // inherit ctors
235};
236
237} // namespace boost
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition Asset.h:107
HardenedHash<> hasher
Definition base_uint.h:151
Specifies an order book.
Definition Book.h:16
Asset in
Definition Book.h:18
Asset out
Definition Book.h:19
Book(Asset const &in, Asset const &out, std::optional< uint256 > const &domain)
Definition Book.h:24
std::optional< uint256 > domain
Definition Book.h:20
Book()=default
A currency issued by an account.
Definition Issue.h:13
std::enable_if_t< IsContiguouslyHashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:588
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
Definition base_uint.h:648
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
constexpr std::strong_ordering operator<=>(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:567
Book reversed(Book const &book)
Definition Book.cpp:30
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:175
bool isConsistent(Asset const &asset)
Definition Asset.h:312
std::hash< xrpl::Asset > Base
Definition Book.h:224
std::hash< xrpl::Book > Base
Definition Book.h:232
std::hash< xrpl::Issue > Base
Definition Book.h:206
std::hash< xrpl::MPTIssue > Base
Definition Book.h:216
value_type operator()(argument_type const &asset) const
Definition Book.h:150
std::hash< xrpl::Issue > issue_hasher
Definition Book.h:140
mptissue_hasher mMptissueHasher_
Definition Book.h:144
xrpl::Asset argument_type
Definition Book.h:138
std::hash< xrpl::MPTIssue > mptissue_hasher
Definition Book.h:141
std::size_t value_type
Definition Book.h:137
issue_hasher mIssueHasher_
Definition Book.h:143
std::hash< xrpl::Asset > asset_hasher
Definition Book.h:170
xrpl::uint256::hasher uint256_hasher
Definition Book.h:171
std::size_t value_type
Definition Book.h:179
xrpl::Book argument_type
Definition Book.h:180
value_type operator()(argument_type const &value) const
Definition Book.h:183
uint256_hasher uint256Hasher_
Definition Book.h:174
asset_hasher issueHasher_
Definition Book.h:173
xrpl::Issue argument_type
Definition Book.h:101
value_type operator()(argument_type const &value) const
Definition Book.h:104
boost::base_from_member< std::hash< xrpl::Currency >, 0 > currency_hash_type
Definition Book.h:94
std::size_t value_type
Definition Book.h:100
boost::base_from_member< std::hash< xrpl::AccountID >, 1 > issuer_hash_type
Definition Book.h:95
xrpl::MPTIssue argument_type
Definition Book.h:123
value_type operator()(argument_type const &value) const
Definition Book.h:126
boost::base_from_member< std::hash< xrpl::MPTID >, 0 > id_hash_type
Definition Book.h:117
std::size_t value_type
Definition Book.h:122