xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
basics
RangeSet.h
1
#pragma once
2
3
#include <xrpl/beast/core/LexicalCast.h>
4
5
#include <boost/algorithm/string.hpp>
6
#include <boost/icl/closed_interval.hpp>
7
#include <boost/icl/interval_set.hpp>
8
9
#include <
functional
>
10
#include <
optional
>
11
#include <
string
>
12
#include <
vector
>
13
14
namespace
xrpl
{
15
26
template
<
class
T>
27
using
ClosedInterval
= boost::icl::closed_interval<T>;
28
35
template
<
class
T>
36
ClosedInterval<T>
37
range
(T low, T high)
38
{
39
return
ClosedInterval<T>
(low, high);
40
}
41
53
template
<
class
T>
54
using
RangeSet
= boost::icl::interval_set<T, std::less, ClosedInterval<T>>;
55
66
template
<
class
T>
67
std::string
68
to_string
(
ClosedInterval<T>
const
& ci)
69
{
70
if
(ci.first() == ci.last())
71
return
std::to_string
(ci.first());
72
return
std::to_string
(ci.first()) +
"-"
+
std::to_string
(ci.last());
73
}
74
84
template
<
class
T>
85
std::string
86
to_string
(
RangeSet<T>
const
& rs)
87
{
88
if
(rs.empty())
89
return
"empty"
;
90
91
std::string
s;
92
for
(
auto
const
& interval : rs)
93
s +=
xrpl::to_string
(interval) +
","
;
94
s.
pop_back
();
95
96
return
s;
97
}
98
109
template
<
class
T>
110
[[nodiscard]]
bool
111
fromString
(
RangeSet<T>
& rs,
std::string
const
& s)
112
{
113
std::vector<std::string>
intervals;
114
std::vector<std::string>
tokens;
115
bool
result{
true
};
116
117
rs.
clear
();
118
boost::split(tokens, s, boost::algorithm::is_any_of(
","
));
119
for
(
auto
const
& t : tokens)
120
{
121
boost::split(intervals, t, boost::algorithm::is_any_of(
"-"
));
122
switch
(intervals.
size
())
123
{
124
case
1: {
125
T front;
126
if
(!
beast::lexicalCastChecked
(front, intervals.
front
()))
127
{
128
result =
false
;
129
}
130
else
131
{
132
rs.insert(front);
133
}
134
break
;
135
}
136
case
2: {
137
T front;
138
if
(!
beast::lexicalCastChecked
(front, intervals.
front
()))
139
{
140
result =
false
;
141
}
142
else
143
{
144
T back;
145
if
(!
beast::lexicalCastChecked
(back, intervals.
back
()))
146
{
147
result =
false
;
148
}
149
else
150
{
151
rs.insert(
range
(front, back));
152
}
153
}
154
break
;
155
}
156
default
:
157
result =
false
;
158
}
159
160
if
(!result)
161
break
;
162
intervals.
clear
();
163
}
164
165
if
(!result)
166
rs.clear();
167
return
result;
168
}
169
179
template
<
class
T>
180
std::optional<T>
181
prevMissing
(
RangeSet<T>
const
& rs, T t, T minVal = 0)
182
{
183
if
(rs.empty() || t == minVal)
184
return
std::nullopt;
185
RangeSet<T>
tgt{
ClosedInterval<T>
{minVal, t - 1}};
186
tgt -= rs;
187
if
(tgt.empty())
188
return
std::nullopt;
189
return
boost::icl::last(tgt);
190
}
191
192
}
// namespace xrpl
std::vector::back
T back(T... args)
std::string
std::vector::clear
T clear(T... args)
std::vector::front
T front(T... args)
functional
beast::lexicalCastChecked
constexpr bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Definition
LexicalCast.h:181
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::prevMissing
std::optional< T > prevMissing(RangeSet< T > const &rs, T t, T minVal=0)
Find the largest value not in the set that is less than a given value.
Definition
RangeSet.h:181
xrpl::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition
RangeSet.h:37
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::fromString
bool fromString(RangeSet< T > &rs, std::string const &s)
Convert the given styled string to a RangeSet.
Definition
RangeSet.h:111
xrpl::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition
RangeSet.h:27
xrpl::RangeSet
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition
RangeSet.h:54
optional
std::string::pop_back
T pop_back(T... args)
std::vector::size
T size(T... args)
string
std::to_string
T to_string(T... args)
vector
Generated by
1.17.0