Skip to content
Open
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
40 changes: 34 additions & 6 deletions IntervalTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ class IntervalTree {
, right(nullptr)
{
--depth;
const auto minmaxStop = std::minmax_element(ivals.begin(), ivals.end(),
IntervalStopCmp());
const auto minmaxStart = std::minmax_element(ivals.begin(), ivals.end(),
IntervalStartCmp());
if (!ivals.empty()) {
center = (minmaxStart.first->start + minmaxStop.second->stop) / 2;
{ // This scope is because afterward we sort and so these two iterators
// will stop meaing what they look say they mean, so we want them to go out of scope.
const auto minmaxStop = std::minmax_element(ivals.begin(), ivals.end(),
IntervalStopCmp());
const auto minmaxStart = std::minmax_element(ivals.begin(), ivals.end(),
IntervalStartCmp());
if (!ivals.empty()) {
center = (minmaxStart.first->start + minmaxStop.second->stop) / 2;
}
}
if (leftextent == 0 && rightextent == 0) {
// sort intervals by start
Expand All @@ -112,6 +115,7 @@ class IntervalTree {
if (depth == 0 || (ivals.size() < minbucket && ivals.size() < maxbucket)) {
std::sort(ivals.begin(), ivals.end(), IntervalStartCmp());
intervals = std::move(ivals);
intervals.shrink_to_fit();
assert(is_valid().first);
return;
} else {
Expand Down Expand Up @@ -158,6 +162,30 @@ class IntervalTree {
assert(is_valid().first);
}

Scalar min() const {
assert(!empty());
auto result = std::numeric_limits<Scalar>::max(); // If all else fails, return max as our min.
if (left) { result = left->min(); }
if (intervals.empty()) {
return result;
}
assert(std::is_sorted(intervals.begin(), intervals.end(), IntervalStartCmp()));
result = std::min(result, intervals.front().start); // These are sorted by start pos.
return result;
}
Scalar max() const {
Copy link
Contributor Author

@BenFrantzDale BenFrantzDale Apr 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct, or do we need to do max of right->max() and everything in this tree node?

assert(!empty());
auto result = std::numeric_limits<Scalar>::lowest(); // If all else fails, return lowest as our max.
if (right) { result = right->max(); }
if (intervals.empty()) {
return result;
}
// Our intervals are sorted by start pos, not end pos, so we have to check them all:
result = std::max(result,
std::max_element(intervals.begin(), intervals.end(),
IntervalStopCmp())->stop);
return result;
}
// Call f on all intervals near the range [start, stop]:
template <class UnaryFunction>
void visit_near(const Scalar& start, const Scalar& stop, UnaryFunction f) const {
Expand Down
40 changes: 40 additions & 0 deletions interval_tree_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,43 @@ int main(int argc, char**argv) {

return Catch::Session().run( argc, argv );
}

TEST_CASE( "Test min/max" ) {
typedef IntervalTree<int, int> ITree;
{
ITree::interval_vector sanityIntervals;
sanityIntervals.push_back(ITree::interval(-100, 100, 0));
sanityIntervals.push_back(ITree::interval(-90, -80, 1));
ITree sanityTree(std::move(sanityIntervals), 16, 1, 1);
REQUIRE( sanityTree.min() == -100 );
REQUIRE( sanityTree.max() == 100 );
}

{
ITree::interval_vector sanityIntervals;
sanityIntervals.push_back(ITree::interval(-100, 100, 0));
sanityIntervals.push_back(ITree::interval(-90, -80, 1));
sanityIntervals.push_back(ITree::interval(-10, 10, 2));
ITree sanityTree(std::move(sanityIntervals), 16, 1, 1);
REQUIRE( sanityTree.min() == -100 );
REQUIRE( sanityTree.max() == 100 );
}
{
ITree::interval_vector sanityIntervals;
sanityIntervals.push_back(ITree::interval(-100, 100, 0));
sanityIntervals.push_back(ITree::interval(-90, -80, 1));
sanityIntervals.push_back(ITree::interval(-10, 10, 2));
ITree sanityTree(std::move(sanityIntervals), 16, 1, 1);
REQUIRE( sanityTree.min() == -100 );
REQUIRE( sanityTree.max() == 100 );
}
{
ITree::interval_vector sanityIntervals;
sanityIntervals.push_back(ITree::interval(-100, 100, 0));
sanityIntervals.push_back(ITree::interval(80, 90, 2));
ITree sanityTree(std::move(sanityIntervals), 16, 1, 1);
REQUIRE( sanityTree.min() == -100 );
REQUIRE( sanityTree.max() == 100 );
}

}