run_list
This commit is contained in:
parent
f0bc22cfdd
commit
6417af5daa
50
run_list.h
Normal file
50
run_list.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef RUN_LIST_H
|
||||
#define RUN_LIST_H
|
||||
|
||||
#include <set>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace base {
|
||||
template <typename T>
|
||||
struct run {
|
||||
run(T const &b, T const &e)
|
||||
: b_(b),
|
||||
e_(e) {
|
||||
}
|
||||
|
||||
bool operator< (run const &rhs) const {
|
||||
return b_ < rhs.b_;
|
||||
}
|
||||
|
||||
T b_, e_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class run_list {
|
||||
public:
|
||||
run_list()
|
||||
: invert_(false) {
|
||||
}
|
||||
|
||||
void add_run(T const &b, T const &e);
|
||||
void sub_run(T const &b, T const &e);
|
||||
bool in_run(T const &key) const;
|
||||
|
||||
void invert();
|
||||
void add(run_list<T> const &rl);
|
||||
void sub(run_list<T> const &rl);
|
||||
|
||||
private:
|
||||
bool in_run_(T const &key) const;
|
||||
|
||||
bool invert_;
|
||||
std::set<run<T> > runs_;
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#include "run_list.tcc"
|
||||
|
||||
#endif
|
148
run_list.tcc
Normal file
148
run_list.tcc
Normal file
@ -0,0 +1,148 @@
|
||||
#include <iostream> // FIXME: remove
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
using namespace base;
|
||||
using namespace boost;
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
bool overlaps_ordered(run<T> const &lhs, run<T> const &rhs) {
|
||||
return rhs.b_ < lhs.e_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool overlaps(run<T> const &lhs, run<T> const &rhs) {
|
||||
if (lhs.b_ <= rhs.b_)
|
||||
return overlaps_ordered(lhs, rhs);
|
||||
else
|
||||
return overlaps_ordered(rhs, lhs);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
boost::optional<run<T> >
|
||||
merge_ordered_runs_if_overlapping(run<T> const &lhs, run<T> const &rhs) {
|
||||
typedef optional<run<T> > result;
|
||||
|
||||
if (lhs.e_ < rhs.e_)
|
||||
return result(run<T>(lhs.b_, rhs.e_));
|
||||
|
||||
if (lhs.e_ <= rhs.e_)
|
||||
return result(lhs);
|
||||
|
||||
return result();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
boost::optional<run<T> >
|
||||
merge_if_overlapping(run<T> const &lhs, run<T> const &rhs) {
|
||||
if (lhs.b_ <= rhs.b_)
|
||||
return merge_ordered_runs_if_overlapping(lhs, rhs);
|
||||
else
|
||||
return merge_ordered_runs_if_overlapping(rhs, lhs);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
pair<typename set<run<T> >::const_iterator,
|
||||
typename set<run<T> >::const_iterator>
|
||||
overlapping_range(set<run<T> > const &runs, run<T> const &r) {
|
||||
// FIXME: slow, but correct implementation first
|
||||
typedef typename set<run<T> >::const_iterator cit;
|
||||
|
||||
for (cit b = runs.begin(); b != runs.end(); ++b) {
|
||||
if (overlaps(*b, r)) {
|
||||
cit e = b;
|
||||
++e;
|
||||
while (overlaps(*e, r))
|
||||
++e;
|
||||
|
||||
return make_pair(b, e);
|
||||
}
|
||||
}
|
||||
|
||||
return make_pair(runs.end(), runs.end());
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
run_list<T>::add_run(T const &b, T const &e)
|
||||
{
|
||||
using namespace std;
|
||||
typedef typename set<run<T> >::const_iterator cit;
|
||||
|
||||
run<T> r(b, e);
|
||||
pair<cit, cit> range = overlapping_range(runs_, r);
|
||||
for (cit it = range.first; it != range.second; ++it) {
|
||||
optional<run<T> > mr = merge_if_overlapping(r, *it);
|
||||
if (mr)
|
||||
r = *mr;
|
||||
}
|
||||
|
||||
runs_.erase(range.first, range.second);
|
||||
runs_.insert(r);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
run_list<T>::sub_run(T const &b, T const &e)
|
||||
{
|
||||
// FIXME: finish
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
run_list<T>::in_run_(T const &key) const
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
run<T> r(key, key + 1);
|
||||
typename set<run<T> >::const_iterator it = runs_.lower_bound(r);
|
||||
|
||||
if (it != runs_.end() && it->b_ == key)
|
||||
return true;
|
||||
|
||||
--it;
|
||||
if (it == runs_.end())
|
||||
return false;
|
||||
|
||||
return it->b_ <= key && it->e_ > key;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
run_list<T>::in_run(T const &key) const
|
||||
{
|
||||
if (invert_)
|
||||
return !in_run_(key);
|
||||
else
|
||||
return in_run_(key);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
run_list<T>::invert()
|
||||
{
|
||||
invert_ = !invert_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
run_list<T>::add(run_list<T> const &rl)
|
||||
{
|
||||
// FIXME: finish
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
run_list<T>::sub(run_list<T> const &rl)
|
||||
{
|
||||
// FIXME: finish
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
@ -3,6 +3,7 @@ TEST_SOURCE=\
|
||||
unit-tests/btree_t.cc \
|
||||
unit-tests/cache_t.cc \
|
||||
unit-tests/endian_t.cc \
|
||||
unit-tests/run_list_t.cc \
|
||||
unit-tests/space_map_t.cc \
|
||||
unit-tests/space_map_disk_t.cc \
|
||||
unit-tests/transaction_manager_t.cc
|
||||
@ -23,6 +24,9 @@ unit-tests/btree_t: unit-tests/btree_t.o $(OBJECTS)
|
||||
unit-tests/cache_t: unit-tests/cache_t.o $(OBJECTS)
|
||||
g++ $(CPPFLAGS) -o $@ $+ $(LIBS)
|
||||
|
||||
unit-tests/run_list_t: unit-tests/run_list_t.o $(OBJECTS)
|
||||
g++ $(CPPFLAGS) -o $@ $+ $(LIBS)
|
||||
|
||||
unit-tests/space_map_t: unit-tests/space_map_t.o $(OBJECTS)
|
||||
g++ $(CPPFLAGS) -o $@ $+ $(LIBS)
|
||||
|
||||
|
186
unit-tests/run_list_t.cc
Normal file
186
unit-tests/run_list_t.cc
Normal file
@ -0,0 +1,186 @@
|
||||
#include "run_list.h"
|
||||
|
||||
#define BOOST_TEST_MODULE RunListTests
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
using namespace base;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
void check_run(run_list<unsigned> const &rl,
|
||||
unsigned b, unsigned e, bool in) {
|
||||
for (unsigned i = b; i < e; i++)
|
||||
BOOST_CHECK(rl.in_run(i) == in);
|
||||
}
|
||||
|
||||
bool id(bool b) {
|
||||
return b;
|
||||
}
|
||||
|
||||
bool negate(bool b) {
|
||||
return !b;
|
||||
}
|
||||
|
||||
void _test_added_runs_are_recorded(bool invert) {
|
||||
run_list<unsigned> rl;
|
||||
rl.add_run(10, 87);
|
||||
|
||||
if (invert)
|
||||
rl.invert();
|
||||
bool (*op)(bool) = invert ? negate : id;
|
||||
|
||||
check_run(rl, 0, 10, op(false));
|
||||
check_run(rl, 10, 87, op(true));
|
||||
check_run(rl, 87, 100, op(false));
|
||||
}
|
||||
|
||||
void _test_multiple_runs_are_recorded(bool invert) {
|
||||
run_list<unsigned> rl;
|
||||
|
||||
rl.add_run(10, 87);
|
||||
rl.add_run(100, 111);
|
||||
rl.add_run(678, 789);
|
||||
|
||||
if (invert)
|
||||
rl.invert();
|
||||
bool (*op)(bool) = invert ? negate : id;
|
||||
|
||||
check_run(rl, 0, 10, op(false));
|
||||
check_run(rl, 10, 87, op(true));
|
||||
check_run(rl, 87, 100, op(false));
|
||||
check_run(rl, 100, 111, op(true));
|
||||
check_run(rl, 111, 678, op(false));
|
||||
check_run(rl, 678, 789, op(true));
|
||||
check_run(rl, 789, 1000, op(false));
|
||||
}
|
||||
|
||||
void _test_overlap_tail(bool invert) {
|
||||
run_list<unsigned> rl;
|
||||
|
||||
rl.add_run(10, 87);
|
||||
rl.add_run(50, 96);
|
||||
|
||||
if (invert)
|
||||
rl.invert();
|
||||
bool (*op)(bool) = invert ? negate : id;
|
||||
|
||||
check_run(rl, 0, 10, op(false));
|
||||
check_run(rl, 10, 96, op(true));
|
||||
check_run(rl, 96, 100, op(false));
|
||||
}
|
||||
|
||||
void _test_overlap_head(bool invert) {
|
||||
run_list<unsigned> rl;
|
||||
|
||||
rl.add_run(50, 96);
|
||||
rl.add_run(10, 87);
|
||||
|
||||
if (invert)
|
||||
rl.invert();
|
||||
bool (*op)(bool) = invert ? negate : id;
|
||||
|
||||
check_run(rl, 0, 10, op(false));
|
||||
check_run(rl, 10, 96, op(true));
|
||||
check_run(rl, 96, 100, op(false));
|
||||
}
|
||||
|
||||
void _test_overlap_completely(bool invert) {
|
||||
run_list<unsigned> rl;
|
||||
|
||||
rl.add_run(40, 60);
|
||||
rl.add_run(10, 87);
|
||||
|
||||
if (invert)
|
||||
rl.invert();
|
||||
bool (*op)(bool) = invert ? negate : id;
|
||||
|
||||
check_run(rl, 0, 10, op(false));
|
||||
check_run(rl, 10, 87, op(true));
|
||||
check_run(rl, 87, 100, op(false));
|
||||
}
|
||||
|
||||
void _test_overlap_many(bool invert) {
|
||||
run_list<unsigned> rl;
|
||||
|
||||
rl.add_run(20, 30);
|
||||
rl.add_run(35, 40);
|
||||
rl.add_run(45, 50);
|
||||
rl.add_run(55, 60);
|
||||
rl.add_run(10, 87);
|
||||
|
||||
if (invert)
|
||||
rl.invert();
|
||||
bool (*op)(bool) = invert ? negate : id;
|
||||
|
||||
check_run(rl, 0, 10, op(false));
|
||||
check_run(rl, 10, 87, op(true));
|
||||
check_run(rl, 87, 100, op(false));
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_added_runs_are_recorded)
|
||||
{
|
||||
_test_added_runs_are_recorded(false);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_multiple_runs_are_recorded)
|
||||
{
|
||||
_test_multiple_runs_are_recorded(false);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overlap_tail)
|
||||
{
|
||||
_test_overlap_tail(false);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overlap_head)
|
||||
{
|
||||
_test_overlap_head(false);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overlap_completely)
|
||||
{
|
||||
_test_overlap_completely(false);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overlap_many)
|
||||
{
|
||||
_test_overlap_many(false);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_added_runs_are_recorded_inverted)
|
||||
{
|
||||
_test_added_runs_are_recorded(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_multiple_runs_are_recorded_inverted)
|
||||
{
|
||||
_test_multiple_runs_are_recorded(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overlap_tail_inverted)
|
||||
{
|
||||
_test_overlap_tail(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overlap_head_inverted)
|
||||
{
|
||||
_test_overlap_head(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overlap_completely_inverted)
|
||||
{
|
||||
_test_overlap_completely(true);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_overlap_many_inverted)
|
||||
{
|
||||
_test_overlap_many(true);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
Loading…
Reference in New Issue
Block a user