forked from ekg/intervaltree
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix intervaltree min max #3
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
BenFrantzDale
merged 4 commits into
Formlabs:master
from
BenFrantzDale:fix-intervaltree-min-max
Apr 3, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 { | ||
|
|
@@ -160,15 +164,27 @@ class IntervalTree { | |
|
|
||
| Scalar min() const { | ||
| assert(!empty()); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are the fixes. |
||
| if (left) { return left->min(); } | ||
| return std::min_element(intervals.begin(), intervals.end(), | ||
| IntervalStartCmp())->start; | ||
| 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 { | ||
| assert(!empty()); | ||
| if (right) { return right->max(); } | ||
| return std::max_element(intervals.begin(), intervals.end(), | ||
| IntervalStopCmp())->stop; | ||
| 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> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,3 +186,43 @@ int main(int argc, char**argv) { | |
|
|
||
| return Catch::Session().run( argc, argv ); | ||
| } | ||
|
|
||
| TEST_CASE( "Test min/max" ) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tests the fixes. |
||
| 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 ); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just scoping