run_set.negate()

This commit is contained in:
Joe Thornber 2013-07-08 23:21:48 +01:00
parent 3be8376256
commit 31686fbb17
2 changed files with 82 additions and 1 deletions

View File

@ -87,6 +87,33 @@ namespace base {
return runs_.end(); return runs_.end();
} }
void negate() {
rset replacement;
if (runs_.begin() == runs_.end())
replacement.insert(run<T>());
else {
auto b = runs_.begin();
maybe 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: private:
typedef typename run<T>::maybe maybe; typedef typename run<T>::maybe maybe;
@ -104,7 +131,7 @@ namespace base {
return maybe(std::max<T>(*m1, *m2)); return maybe(std::max<T>(*m1, *m2));
} }
std::set<run<T>, compare_begin> runs_; rset runs_;
}; };
} }

View File

@ -16,6 +16,18 @@ namespace {
(arg.end_ == static_cast<unsigned>(e)); (arg.end_ == static_cast<unsigned>(e));
} }
MATCHER(EqAll, "") {
return !arg.begin_ && !arg.end_;
}
MATCHER_P(EqOpenBegin, e, "") {
return !arg.begin_ && (arg.end_ == static_cast<unsigned>(e));
}
MATCHER_P(EqOpenEnd, b, "") {
return (arg.begin_ == static_cast<unsigned>(b)) && !arg.end_;
}
class RunSetTests : public Test { class RunSetTests : public Test {
}; };
} }
@ -176,4 +188,46 @@ TEST_F(RunSetTests, merge_discrete_sets)
ASSERT_THAT(++(++(rs1.begin())), Eq(rs1.end())); ASSERT_THAT(++(++(rs1.begin())), Eq(rs1.end()));
} }
TEST_F(RunSetTests, negate_empty)
{
run_set<unsigned> rs;
rs.negate();
ASSERT_THAT(*rs.begin(), EqAll());
ASSERT_THAT(++rs.begin(), Eq(rs.end()));
}
TEST_F(RunSetTests, negate_single)
{
run_set<unsigned> rs;
rs.add(5, 10);
rs.negate();
ASSERT_THAT(*rs.begin(), EqOpenBegin(5));
ASSERT_THAT(*(++rs.begin()), EqOpenEnd(10));
ASSERT_THAT(++(++rs.begin()), Eq(rs.end()));
}
TEST_F(RunSetTests, negate_double)
{
run_set<unsigned> rs;
rs.add(5, 10);
rs.add(15, 20);
rs.negate();
ASSERT_THAT(*rs.begin(), EqOpenBegin(5));
ASSERT_THAT(*(++rs.begin()), EqRun(10, 15));
ASSERT_THAT(*(++(++rs.begin())), EqOpenEnd(20));
ASSERT_THAT(++(++(++rs.begin())), Eq(rs.end()));
}
TEST_F(RunSetTests, negate_negate)
{
run_set<unsigned> rs;
rs.add(5, 10);
rs.add(15, 20);
rs.negate();
rs.negate();
ASSERT_THAT(*rs.begin(), EqRun(5, 10));
ASSERT_THAT(*(++rs.begin()), EqRun(15, 20));
ASSERT_THAT(++(++rs.begin()), Eq(rs.end()));
}
//---------------------------------------------------------------- //----------------------------------------------------------------