[base] Move run_set to namespace base

This commit is contained in:
Ming-Hung Tsai
2020-07-27 10:48:54 +08:00
parent 71545e56c6
commit e62022a200
16 changed files with 19 additions and 19 deletions

View File

@@ -19,8 +19,8 @@
#ifndef BLOCK_COUNTER_H
#define BLOCK_COUNTER_H
#include "base/run_set.h"
#include "block.h"
#include "run_set.h"
//----------------------------------------------------------------

View File

@@ -19,7 +19,7 @@
#ifndef PERSISTENT_DATA_DATA_STRUCTURES_BITSET_H
#define PERSISTENT_DATA_DATA_STRUCTURES_BITSET_H
#include "persistent-data/run.h"
#include "base/run.h"
//----------------------------------------------------------------

View File

@@ -1,9 +1,9 @@
#ifndef PERSISTENT_DATA_DATA_STRUCTURES_DAMAGE_VISITOR_H
#define PERSISTENT_DATA_DATA_STRUCTURES_DAMAGE_VISITOR_H
#include "base/run.h"
#include "persistent-data/data-structures/btree.h"
#include "persistent-data/data-structures/btree_node_checker.h"
#include "persistent-data/run.h"
//----------------------------------------------------------------

View File

@@ -1,71 +0,0 @@
#ifndef PERSISTENT_DATA_RANGE_H
#define PERSISTENT_DATA_RANGE_H
#include <boost/optional.hpp>
#include <ostream>
//----------------------------------------------------------------
namespace base {
template <typename T>
class run {
public:
typedef boost::optional<T> maybe;
run() {
}
explicit run(T const &b)
: begin_(b) {
}
run(T const &b, T const &e)
: begin_(b),
end_(e) {
}
explicit run(maybe begin, maybe end)
: begin_(begin),
end_(end) {
}
bool operator ==(run<T> const &r) const {
return (begin_ == r.begin_ && end_ == r.end_);
}
bool contains(T const &v) const {
if (begin_ && v < *begin_)
return false;
if (end_ && v >= *end_)
return false;
return true;
}
maybe begin_;
maybe end_;
};
template <typename T>
std::ostream &
operator <<(std::ostream &out, run<T> const &r) {
if (r.begin_)
out << "[" << *r.begin_;
else
out << "[-";
out << ", ";
if (r.end_)
out << *r.end_ << "]";
else
out << "-]";
return out;
}
}
//----------------------------------------------------------------
#endif

View File

@@ -1,150 +0,0 @@
#ifndef PERSISTENT_DATA_H
#define PERSISTENT_DATA_H
#include "persistent-data/run.h"
#include <algorithm>
#include <set>
//----------------------------------------------------------------
namespace base {
template <typename T>
class run_set {
public:
void clear() {
runs_.clear();
}
void add(T const &b) {
add(run<T>(b, b + 1));
}
void add(T const &b, T const &e) {
add(run<T>(b, e));
}
void add(run<T> const &r_) {
run<T> r(r_);
if (runs_.size()) {
// Skip all blocks that end before r
const_iterator it = runs_.lower_bound(r);
if (it != runs_.begin())
--it;
while (it != runs_.end() && it->end_ < r.begin_)
++it;
// work out which runs overlap
if (it != runs_.end()) {
r.begin_ = min_maybe(it->begin_, r.begin_);
const_iterator first = it;
while (it != runs_.end() && it->begin_ <= r.end_) {
r.end_ = max_maybe(it->end_, r.end_);
++it;
}
// remove overlapping runs
runs_.erase(first, it);
}
}
runs_.insert(r);
}
void merge(run_set<T> const &rhs) {
for (const_iterator it = rhs.begin(); it != rhs.end(); ++it)
add(*it);
}
bool member(T const &v) const {
if (!runs_.size())
return false;
typename rset::const_iterator it = runs_.lower_bound(run<T>(v));
if (it != runs_.end() && it->begin_ == v)
return true;
if (it != runs_.begin()) {
it--;
return it->contains(v);
}
return false;
}
struct compare_begin {
bool operator ()(run<T> const &r1, run<T> const &r2) const {
return r1.begin_ < r2.begin_;
}
};
typedef std::set<run<T>, compare_begin> rset;
typedef typename rset::const_iterator const_iterator;
const_iterator begin() const {
return runs_.begin();
}
const_iterator end() const {
return runs_.end();
}
void negate() {
rset replacement;
if (runs_.begin() == runs_.end())
replacement.insert(run<T>());
else {
typename rset::const_iterator b = runs_.begin();
// Some versions of gcc give a spurious
// warning here. So we initialize it to
// get round it.
maybe last(0);
last = b->end_;
if (b->begin_)
replacement.insert(run<T>(maybe(), *(b->begin_)));
++b;
while (b != runs_.end()) {
replacement.insert(run<T>(last, b->begin_));
last = b->end_;
++b;
}
if (last)
replacement.insert(run<T>(last, maybe()));
}
runs_ = replacement;
}
private:
typedef typename run<T>::maybe maybe;
static maybe min_maybe(maybe const &m1, maybe const &m2) {
if (!m1 || !m2)
return maybe();
return maybe(std::min<T>(*m1, *m2));
}
static maybe max_maybe(maybe const &m1, maybe const &m2) {
if (!m1 || !m2)
return maybe();
return maybe(std::max<T>(*m1, *m2));
}
rset runs_;
};
}
//----------------------------------------------------------------
#endif

View File

@@ -1,8 +1,8 @@
#ifndef SPAN_ITERATOR_H
#define SPAN_ITERATOR_H
#include "base/run_set.h"
#include "persistent-data/space_map.h"
#include "persistent-data/run_set.h"
#include <set>

View File

@@ -19,9 +19,9 @@
#ifndef SPACE_MAP_H
#define SPACE_MAP_H
#include "base/run.h"
#include "persistent-data/block.h"
#include "persistent-data/block_counter.h"
#include "persistent-data/run.h"
#include <boost/optional.hpp>
#include <functional>