Move RangeSet to ripple_basics module

This commit is contained in:
Vinnie Falco
2013-05-25 14:59:48 -07:00
parent d762abfc85
commit 54d246cb5e
8 changed files with 35 additions and 42 deletions

View File

@@ -7,7 +7,6 @@
#include "LedgerAcquire.h"
#include "Transaction.h"
#include "TransactionEngine.h"
#include "RangeSet.h"
#include "CanonicalTXSet.h"
// Tracks the current ledger and any ledgers in the process of closing

View File

@@ -1,133 +0,0 @@
#include "RangeSet.h"
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>
SETUP_LOG (RangeSet)
inline uint32 min(uint32 x, uint32 y) { return (x < y) ? x : y; }
inline uint32 max(uint32 x, uint32 y) { return (x > y) ? x : y; }
bool RangeSet::hasValue(uint32 v) const
{
return mRanges.find(v) != mRanges.end();
}
uint32 RangeSet::getFirst() const
{
const_iterator it = begin();
if (it == end())
return RangeSetAbsent;
return lower(it);
}
uint32 RangeSet::getNext(uint32 v) const
{
for (const_iterator it = begin(); it != end(); ++it)
{
if (upper(it) > v)
return max(v + 1, lower(it));
}
return RangeSetAbsent;
}
uint32 RangeSet::getLast() const
{
const_reverse_iterator it = rbegin();
if (it == rend())
return RangeSetAbsent;
return upper(it);
}
uint32 RangeSet::getPrev(uint32 v) const
{
for (const_reverse_iterator it = rbegin(); it != rend(); ++it)
{
if (lower(it) < v)
return min(v - 1, upper(it));
}
return RangeSetAbsent;
}
uint32 RangeSet::prevMissing(uint32 v) const
{ // largest number not in the set that is less than the given number
WriteLog (lsTRACE, RangeSet) << "prevMissing(" << v << ") " << toString();
for (const_reverse_iterator it = rbegin(); it != rend(); ++it)
{
if ((upper(it) + 1) < v)
return upper(it) + 1;
if (lower(it) == 0)
return RangeSetAbsent;
if ((lower(it) - 1) < v)
return lower(it) - 1;
}
if (v > 0)
return v - 1;
return RangeSetAbsent;
}
void RangeSet::setValue(uint32 v)
{
setRange(v, v);
}
void RangeSet::setRange(uint32 minV, uint32 maxV)
{
mRanges.add(boost::icl::discrete_interval<uint32>(minV, maxV + 1));
}
void RangeSet::clearValue(uint32 v)
{
clearRange(v, v);
}
void RangeSet::clearRange(uint32 minV, uint32 maxV)
{
mRanges.erase(boost::icl::discrete_interval<uint32>(minV, maxV + 1));
}
std::string RangeSet::toString() const
{
std::string ret;
for (const_iterator it = begin(); it != end(); ++it)
{
if (!ret.empty())
ret += ",";
if (lower(it) == upper(it))
ret += boost::lexical_cast<std::string>(lower(it));
else
ret += boost::lexical_cast<std::string>(lower(it)) + "-"
+ boost::lexical_cast<std::string>(upper(it));
}
if (ret.empty())
return "empty";
return ret;
}
BOOST_AUTO_TEST_SUITE(RangeSet_suite)
BOOST_AUTO_TEST_CASE(RangeSet_test)
{
WriteLog (lsTRACE, RangeSet) << "RangeSet test begins";
RangeSet r1, r2;
r1.setRange(1,10);
r1.clearValue(5);
r1.setRange(11, 20);
r2.setRange(1, 4);
r2.setRange(6, 10);
r2.setRange(10, 20);
if (r1.hasValue(5)) BOOST_FAIL("RangeSet fail");
if (!r2.hasValue(9)) BOOST_FAIL("RangeSet fail");
// TODO: Traverse functions must be tested
WriteLog (lsTRACE, RangeSet) << "RangeSet test complete";
}
BOOST_AUTO_TEST_SUITE_END()
// vim:ts=4

View File

@@ -1,83 +0,0 @@
#ifndef RANGESET__H
#define RANGESET__H
#include <list>
#include <string>
#include <boost/foreach.hpp>
#include <boost/icl/interval_set.hpp>
class RangeSet
{
public:
typedef boost::icl::interval_set<uint32> iRangeSet;
typedef iRangeSet::iterator iterator;
typedef iRangeSet::const_iterator const_iterator;
typedef iRangeSet::reverse_iterator reverse_iterator;
typedef iRangeSet::const_reverse_iterator const_reverse_iterator;
static const uint32 RangeSetAbsent = static_cast<uint32>(-1);
protected:
iRangeSet mRanges;
public:
RangeSet() { ; }
bool hasValue(uint32) const;
uint32 getFirst() const;
uint32 getNext(uint32) const;
uint32 getLast() const;
uint32 getPrev(uint32) const;
uint32 prevMissing(uint32) const; // largest number not in the set that is less than the given number
void setValue(uint32);
void setRange(uint32, uint32);
void clearValue(uint32);
void clearRange(uint32, uint32);
// iterator stuff
iterator begin() { return mRanges.begin(); }
iterator end() { return mRanges.end(); }
const_iterator begin() const { return mRanges.begin(); }
const_iterator end() const { return mRanges.end(); }
reverse_iterator rbegin() { return mRanges.rbegin(); }
reverse_iterator rend() { return mRanges.rend(); }
const_reverse_iterator rbegin() const { return mRanges.rbegin(); }
const_reverse_iterator rend() const { return mRanges.rend(); }
static uint32 lower(const_iterator& it) { return it->lower(); }
static uint32 upper(const_iterator& it) { return it->upper() - 1; }
static uint32 lower(const_reverse_iterator& it) { return it->lower(); }
static uint32 upper(const_reverse_iterator& it) { return it->upper() - 1; }
std::string toString() const;
};
inline RangeSet::const_iterator range_begin(const RangeSet& r) { return r.begin(); }
inline RangeSet::iterator range_begin(RangeSet& r) { return r.begin(); }
inline RangeSet::const_iterator range_end(const RangeSet& r) { return r.end(); }
inline RangeSet::iterator range_end(RangeSet& r) { return r.end(); }
namespace boost
{
template<> struct range_mutable_iterator<RangeSet>
{
typedef RangeSet::iterator type;
};
template<> struct range_const_iterator<RangeSet>
{
typedef RangeSet::const_iterator type;
};
}
#endif
// vim:ts=4