rippled
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 <ripple/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 
32 namespace ripple {
33 
43 template <class T>
44 using ClosedInterval = boost::icl::closed_interval<T>;
45 
51 template <class T>
53 range(T low, T high)
54 {
55  return ClosedInterval<T>(low, high);
56 }
57 
68 template <class T>
69 using RangeSet = boost::icl::interval_set<T, std::less, ClosedInterval<T>>;
70 
80 template <class T>
83 {
84  if (ci.first() == ci.last())
85  return std::to_string(ci.first());
86  return std::to_string(ci.first()) + "-" + std::to_string(ci.last());
87 }
88 
97 template <class T>
100 {
101  if (rs.empty())
102  return "empty";
103 
104  std::string s;
105  for (auto const& interval : rs)
106  s += ripple::to_string(interval) + ",";
107  s.pop_back();
108 
109  return s;
110 }
111 
121 template <class T>
122 [[nodiscard]] bool
124 {
125  std::vector<std::string> intervals;
127  bool result{true};
128 
129  rs.clear();
130  boost::split(tokens, s, boost::algorithm::is_any_of(","));
131  for (auto const& t : tokens)
132  {
133  boost::split(intervals, t, boost::algorithm::is_any_of("-"));
134  switch (intervals.size())
135  {
136  case 1: {
137  T front;
138  if (!beast::lexicalCastChecked(front, intervals.front()))
139  result = false;
140  else
141  rs.insert(front);
142  break;
143  }
144  case 2: {
145  T front;
146  if (!beast::lexicalCastChecked(front, intervals.front()))
147  result = false;
148  else
149  {
150  T back;
151  if (!beast::lexicalCastChecked(back, intervals.back()))
152  result = false;
153  else
154  rs.insert(range(front, back));
155  }
156  break;
157  }
158  default:
159  result = false;
160  }
161 
162  if (!result)
163  break;
164  intervals.clear();
165  }
166 
167  if (!result)
168  rs.clear();
169  return result;
170 }
171 
180 template <class T>
182 prevMissing(RangeSet<T> const& rs, T t, T minVal = 0)
183 {
184  if (rs.empty() || t == minVal)
185  return std::nullopt;
186  RangeSet<T> tgt{ClosedInterval<T>{minVal, t - 1}};
187  tgt -= rs;
188  if (tgt.empty())
189  return std::nullopt;
190  return boost::icl::last(tgt);
191 }
192 
193 } // namespace ripple
194 
195 #endif
std::string
STL class.
std::vector< std::string >
std::vector::size
T size(T... args)
ripple::from_string
bool from_string(RangeSet< T > &rs, std::string const &s)
Convert the given styled string to a RangeSet.
Definition: RangeSet.h:123
std::vector::back
T back(T... args)
std::vector::front
T front(T... args)
std::vector::clear
T clear(T... args)
std::to_string
T to_string(T... args)
std::string::pop_back
T pop_back(T... args)
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:53
ripple::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:182
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
beast::lexicalCastChecked
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Definition: LexicalCast.h:266
optional
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::RangeSet
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition: RangeSet.h:69
ripple::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:44
string