Skip to content

Commit

Permalink
[FOLD] Make RangeSet an alias
Browse files Browse the repository at this point in the history
  • Loading branch information
bachase committed May 18, 2017
1 parent c0be836 commit 978f6b5
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 395 deletions.
4 changes: 0 additions & 4 deletions Builds/VisualStudio2015/RippleD.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1431,10 +1431,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\basics\impl\RangeSet.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\basics\impl\ResolverAsio.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
Expand Down
3 changes: 0 additions & 3 deletions Builds/VisualStudio2015/RippleD.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1959,9 +1959,6 @@
<ClCompile Include="..\..\src\ripple\basics\impl\mulDiv.cpp">
<Filter>ripple\basics\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\basics\impl\RangeSet.cpp">
<Filter>ripple\basics\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\basics\impl\ResolverAsio.cpp">
<Filter>ripple\basics\impl</Filter>
</ClCompile>
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/app/ledger/LedgerMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class LedgerMaster
std::unique_ptr<LedgerReplay> replayData;

std::recursive_mutex mCompleteLock;
RangeSet mCompleteLedgers;
RangeSet<std::uint32_t> mCompleteLedgers;

std::unique_ptr <detail::LedgerCleaner> mLedgerCleaner;

Expand Down
242 changes: 115 additions & 127 deletions src/ripple/app/ledger/impl/LedgerMaster.cpp

Large diffs are not rendered by default.

117 changes: 93 additions & 24 deletions src/ripple/basics/RangeSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,113 @@
#ifndef RIPPLE_BASICS_RANGESET_H_INCLUDED
#define RIPPLE_BASICS_RANGESET_H_INCLUDED

#include <string>
#include <boost/optional.hpp>
#include <boost/icl/closed_interval.hpp>
#include <boost/icl/interval_set.hpp>
#include <cstdint>

namespace ripple
{
/** A sparse set of integers. */
class RangeSet

/** A closed interval over the domain T.
For an instance ClosedInterval c, this represents the closed interval
(c.first(), c.last()). A single element interval has c.first() == c.last().
This is simply a type-alias for boost interval container library interval
set, so users should consult that documentation for available supporting
member and free functions.
*/
template <class T>
using ClosedInterval = boost::icl::closed_interval<T>;

/** Create a closed range interval
Helper function to create a closed range interval without having to qualify
the template argument.
*/
template <class T>
ClosedInterval<T>
range(T low, T high)
{
public:
static const std::uint32_t absent = static_cast<std::uint32_t>(-1);
return ClosedInterval<T>(low, high);
}

RangeSet() = default;
/** A set of closed intervals over the domain T.
bool hasValue(std::uint32_t) const;
Represents a set of values of the domain T using the minimum number
of disjoint ClosedInterval<T>. This is useful to represent ranges of
T where a few instances are missing, e.g. the set 1-5,8-9,11-14.
// First number in the set
std::uint32_t getFirst() const;
This is simply a type-alias for boost interval container library interval
set, so users should consult that documentation for available supporting
member and free functions.
*/
template <class T>
using RangeSet = boost::icl::interval_set<T, std::less, ClosedInterval<T>>;

// Largest number not in the set that is less than the given number
std::uint32_t prevMissing(std::uint32_t) const;

// Add an item to the set
void setValue(std::uint32_t);
/** Convert a ClosedInterval to a styled string
// Add the closed interval to the set
void setRange(std::uint32_t, std::uint32_t);
The styled string is
"c.first()-c.last()" if c.first() != c.last()
"c.first()" if c.first() == c.last()
// Remove the item from the set
void clearValue(std::uint32_t);
@param ci The closed interval to convert
@return The style string
*/
template <class T>
std::string to_string(ClosedInterval<T> const & ci)
{
if (ci.first() == ci.last())
return std::to_string(ci.first());
return std::to_string(ci.first()) + "-" + std::to_string(ci.last());
}

/** Convert the given RangeSet to a styled string.
The styled string represention is the set of disjoint intervals joined by
commas. The string "empty" is returned if the set is empty.
@param rs The rangeset to convert
@return The styled string
*/
template <class T>
std::string to_string(RangeSet<T> const & rs)
{
using ripple::to_string;

std::string toString() const;
if (rs.empty())
return "empty";
std::string res = "";
for (auto const & interval : rs)
{
if (!res.empty())
res += ",";
res += to_string(interval);
}
return res;
}

private:
using Interval = boost::icl::closed_interval<std::uint32_t>;
using Map = boost::icl::interval_set<std::uint32_t, std::less, Interval>;
Map mRanges;
};
/** Find the largest value not in the set that is less than a given value.
} // namespace ripple
@param rs The set of interest
@param t The value that must be larger than the result
@param minVal (Default is 0) The smallest allowed value
@return The largest v such that minV <= v < t and !contains(rs, v) or
boost::none if no such v exists.
*/
template <class T>
boost::optional<T>
prevMissing(RangeSet<T> const & rs, T t, T minVal = 0)
{
if (rs.empty() || t == minVal)
return boost::none;
RangeSet<T> tgt{ ClosedInterval<T>{minVal, t - 1} };
tgt -= rs;
if (tgt.empty())
return boost::none;
return boost::icl::last(tgt);
}
} // namespace ripple
#endif
98 changes: 0 additions & 98 deletions src/ripple/basics/impl/RangeSet.cpp

This file was deleted.

1 change: 0 additions & 1 deletion src/ripple/unity/basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <ripple/basics/impl/Log.cpp>
#include <ripple/basics/impl/make_SSLContext.cpp>
#include <ripple/basics/impl/mulDiv.cpp>
#include <ripple/basics/impl/RangeSet.cpp>
#include <ripple/basics/impl/ResolverAsio.cpp>
#include <ripple/basics/impl/strHex.cpp>
#include <ripple/basics/impl/StringUtilities.cpp>
Expand Down
Loading

0 comments on commit 978f6b5

Please sign in to comment.