rippled
Loading...
Searching...
No Matches
RangeSet.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 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_RANGESET_H_INCLUDED
21#define RIPPLE_BASICS_RANGESET_H_INCLUDED
22
23#include <xrpl/beast/core/LexicalCast.h>
24
25#include <boost/algorithm/string.hpp>
26#include <boost/icl/closed_interval.hpp>
27#include <boost/icl/interval_set.hpp>
28
29#include <optional>
30#include <string>
31#include <vector>
32
33namespace ripple {
34
44template <class T>
45using ClosedInterval = boost::icl::closed_interval<T>;
46
52template <class T>
54range(T low, T high)
55{
56 return ClosedInterval<T>(low, high);
57}
58
69template <class T>
70using RangeSet = boost::icl::interval_set<T, std::less, ClosedInterval<T>>;
71
81template <class T>
84{
85 if (ci.first() == ci.last())
86 return std::to_string(ci.first());
87 return std::to_string(ci.first()) + "-" + std::to_string(ci.last());
88}
89
98template <class T>
101{
102 if (rs.empty())
103 return "empty";
104
105 std::string s;
106 for (auto const& interval : rs)
107 s += ripple::to_string(interval) + ",";
108 s.pop_back();
109
110 return s;
111}
112
122template <class T>
123[[nodiscard]] bool
125{
126 std::vector<std::string> intervals;
128 bool result{true};
129
130 rs.clear();
131 boost::split(tokens, s, boost::algorithm::is_any_of(","));
132 for (auto const& t : tokens)
133 {
134 boost::split(intervals, t, boost::algorithm::is_any_of("-"));
135 switch (intervals.size())
136 {
137 case 1: {
138 T front;
139 if (!beast::lexicalCastChecked(front, intervals.front()))
140 result = false;
141 else
142 rs.insert(front);
143 break;
144 }
145 case 2: {
146 T front;
147 if (!beast::lexicalCastChecked(front, intervals.front()))
148 result = false;
149 else
150 {
151 T back;
152 if (!beast::lexicalCastChecked(back, intervals.back()))
153 result = false;
154 else
155 rs.insert(range(front, back));
156 }
157 break;
158 }
159 default:
160 result = false;
161 }
162
163 if (!result)
164 break;
165 intervals.clear();
166 }
167
168 if (!result)
169 rs.clear();
170 return result;
171}
172
181template <class T>
183prevMissing(RangeSet<T> const& rs, T t, T minVal = 0)
184{
185 if (rs.empty() || t == minVal)
186 return std::nullopt;
187 RangeSet<T> tgt{ClosedInterval<T>{minVal, t - 1}};
188 tgt -= rs;
189 if (tgt.empty())
190 return std::nullopt;
191 return boost::icl::last(tgt);
192}
193
194} // namespace ripple
195
196#endif
T back(T... args)
T clear(T... args)
T front(T... args)
T is_same_v
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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:183
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:45
bool from_string(RangeSet< T > &rs, std::string const &s)
Convert the given styled string to a RangeSet.
Definition RangeSet.h:124
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition RangeSet.h:70
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:54
T pop_back(T... args)
T size(T... args)
T to_string(T... args)