Skip to content

Intervals may join into parts #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Otherwise merge the interval with the one being overlapped.
#### Parameters
* `ival` An interval
* `exclusive` Exclude borders from overlap check. Defaults to false.
* `mergeSetOverlapping` If the result of interval::join is a collection of intervals, shall each be inserted with more overlap searches? Defaults to false

**Returns**: An iterator to the inserted element.

Expand Down
43 changes: 39 additions & 4 deletions include/interval-tree/interval_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,17 +767,18 @@ namespace lib_interval_tree
*
* @param ival The interval
* @param exclusive Exclude borders.
* @param mergeSetOverlapping If the result of interval::join is a collection of intervals, shall each be inserted with more overlap searches?
*/
iterator insert_overlap(interval_type const& ival, bool exclusive = false)
iterator insert_overlap(interval_type const& ival, bool exclusive = false, bool mergeSetOverlapping = false)
{
auto iter = overlap_find(ival, exclusive);
if (iter == end())
return insert(ival);
else
{
auto merged = iter->interval().join(ival);
erase(iter);
return insert(merged);
auto mergeSet = iter->interval().join(ival);
erase(iter);
return insert_merge_set(mergeSet, mergeSetOverlapping);
}
}

Expand Down Expand Up @@ -1160,6 +1161,40 @@ namespace lib_interval_tree
return nullptr;
};

template <typename MergeSet>
iterator insert_merge_set(MergeSet const& merge_set, bool mergeSetOverlapping)
{
if (mergeSetOverlapping)
{
for (auto iter = merge_set.begin(), end = merge_set.end(); iter != end;)
{
auto next = iter;
if (++next == end)
return insert_overlap(*iter);
else
insert_overlap(*iter);
iter = std::move(next);
}
return end();
}
else
{
for (auto iter = merge_set.begin(), end = merge_set.end(); iter != end;)
{
auto next = iter;
if (++next == end)
return insert(*iter);
else
insert(*iter);
iter = std::move(next);
}
return end();
}
}
iterator insert_merge_set(interval_type const& interval, bool) {
return insert(interval);
}

void clear_subtree(node_type* node)
{
if (node)
Expand Down
26 changes: 22 additions & 4 deletions tests/insert_tests.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once

#include "test_utility.hpp"
#include "multi_join_interval.hpp"

#include <ctime>
#include <random>
#include <cmath>



class InsertTests
: public ::testing::Test
{
Expand All @@ -17,17 +20,17 @@ class InsertTests
std::default_random_engine gen;
std::uniform_int_distribution <int> distSmall{-500, 500};
std::uniform_int_distribution <int> distLarge{-50000, 50000};
};

};
TEST_F(InsertTests, InsertIntoEmpty1)
{
auto inserted_interval = types::interval_type{0, 16};

tree.insert(inserted_interval);
EXPECT_EQ(*tree.begin(), inserted_interval);
EXPECT_EQ(tree.size(), 1);
}

}
TEST_F(InsertTests, InsertIntoEmpty2)
{
auto inserted_interval = types::interval_type{-45, 16};
Expand Down Expand Up @@ -80,3 +83,18 @@ TEST_F(InsertTests, RBPropertyInsertTest)

testRedBlackPropertyViolation(tree);
}

TEST_F(InsertTests, IntervalsMayReturnMultipleIntervalsForJoin)
{
using interval_type = multi_join_interval <int>;
using tree_type = lib_interval_tree::interval_tree<interval_type>;

auto multiJoinTree = tree_type{};

multiJoinTree.insert({0, 1});
multiJoinTree.insert_overlap({0, 2});

EXPECT_EQ(multiJoinTree.size(), 2);
EXPECT_EQ(*multiJoinTree.begin(), (interval_type{0, 1})) << multiJoinTree.begin()->low() << multiJoinTree.begin()->high();
EXPECT_EQ(*++multiJoinTree.begin(), (interval_type{1, 2}));
}
103 changes: 103 additions & 0 deletions tests/multi_join_interval.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#pragma once

#include <interval-tree/interval_types.hpp>
#include <algorithm>
#include <vector>

template <typename numerical_type, typename interval_kind_ = lib_interval_tree::closed>
struct multi_join_interval
{
public:
using value_type = numerical_type;
using interval_kind = interval_kind_;

#ifndef INTERVAL_TREE_SAFE_INTERVALS
#if __cplusplus >= 201703L
constexpr
#endif
multi_join_interval(value_type low, value_type high)
: low_{low}
, high_{high}
{
if (low > high)
throw std::invalid_argument("Low border is not lower or equal to high border.");
}
#else
#if __cplusplus >= 201703L
constexpr
#endif
multi_join_interval(value_type low, value_type high)
: low_{std::min(low, high)}
, high_{std::max(low, high)}
{
}
#endif
virtual ~multi_join_interval() = default;
friend bool operator==(multi_join_interval const& lhs, multi_join_interval const& other)
{
return lhs.low_ == other.low_ && lhs.high_ == other.high_;
}
friend bool operator!=(multi_join_interval const& lhs, multi_join_interval const& other)
{
return lhs.low_ != other.low_ || lhs.high_ != other.high_;
}
value_type low() const
{
return low_;
}
value_type high() const
{
return high_;
}
bool overlaps(value_type l, value_type h) const
{
return low_ <= h && l <= high_;
}
bool overlaps_exclusive(value_type l, value_type h) const
{
return low_ < h && l < high_;
}
bool overlaps(multi_join_interval const& other) const
{
return overlaps(other.low_, other.high_);
}
bool overlaps_exclusive(multi_join_interval const& other) const
{
return overlaps_exclusive(other.low_, other.high_);
}
bool within(value_type value) const
{
return interval_kind::within(low_, high_, value);
}
bool within(multi_join_interval const& other) const
{
return low_ <= other.low_ && high_ >= other.high_;
}
value_type operator-(multi_join_interval const& other) const
{
if (overlaps(other))
return 0;
if (high_ < other.low_)
return other.low_ - high_;
else
return low_ - other.high_;
}
value_type size() const
{
return high_ - low_;
}
std::vector<multi_join_interval> join(multi_join_interval const& other) const
{
const auto min = std::min(low_, other.low_);
const auto max = std::max(high_, other.high_);
const auto avg = (min + max) / 2;
return {
{min, avg},
{avg, max},
};
}

protected:
value_type low_;
value_type high_;
};