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 #include <vector>
32 
33 namespace ripple {
34 
44 template <class T>
45 using ClosedInterval = boost::icl::closed_interval<T>;
46 
52 template <class T>
54 range(T low, T high)
55 {
56  return ClosedInterval<T>(low, high);
57 }
58 
69 template <class T>
70 using RangeSet = boost::icl::interval_set<T, std::less, ClosedInterval<T>>;
71 
81 template <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 
98 template <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 
122 template <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 
181 template <class T>
183 prevMissing(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
std::string
STL class.
vector
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:124
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:54
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:183
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:164
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:70
ripple::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:45
string