From 8523314a7fe0edb9ceef446c5e2d52382b576089 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Tue, 9 Jul 2013 10:36:30 +0100 Subject: [PATCH] [run_set] improve run merging --- persistent-data/run_set.h | 8 ++++++-- unit-tests/run_set_t.cc | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/persistent-data/run_set.h b/persistent-data/run_set.h index 4ecd137..ef8135a 100644 --- a/persistent-data/run_set.h +++ b/persistent-data/run_set.h @@ -12,6 +12,10 @@ namespace base { template class run_set { public: + void clear() { + runs_.clear(); + } + void add(T const &b) { add(run(b, b + 1)); } @@ -28,14 +32,14 @@ namespace base { const_iterator it = runs_.cbegin(); // Skip all blocks that end before r - while (it != runs_.end() && it->end_ <= r.begin_) + 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_) { + while (it != runs_.end() && it->begin_ <= r.end_) { r.end_ = max_maybe(it->end_, r.end_); ++it; } diff --git a/unit-tests/run_set_t.cc b/unit-tests/run_set_t.cc index 02ce070..01a3cbb 100644 --- a/unit-tests/run_set_t.cc +++ b/unit-tests/run_set_t.cc @@ -48,6 +48,39 @@ TEST_F(RunSetTests, add_single_blocks) rs.add(9u); } +TEST_F(RunSetTests, add_adjacent_single) +{ + run_set rs; + + rs.add(3); + rs.add(4); + + ASSERT_THAT(*rs.begin(), EqRun(3, 5)); + ASSERT_THAT(++rs.begin(), Eq(rs.end())); +} + +TEST_F(RunSetTests, add_adjacent_single_other_way_round) +{ + run_set rs; + + rs.add(4); + rs.add(3); + + ASSERT_THAT(*rs.begin(), EqRun(3, 5)); + ASSERT_THAT(++rs.begin(), Eq(rs.end())); +} + +TEST_F(RunSetTests, many_single_blocks) +{ + run_set rs; + + for (unsigned i = 1; i < 100000; i++) + rs.add(i); + + ASSERT_THAT(*rs.begin(), EqRun(1, 100000)); + ASSERT_THAT(++rs.begin(), Eq(rs.end())); +} + TEST_F(RunSetTests, add_runs) { run_set rs;