Skip to content

Commit 3abad36

Browse files
committed
Find functions no longer start at the root and dont include their given iterator.
1 parent 978a914 commit 3abad36

File tree

4 files changed

+108
-10
lines changed

4 files changed

+108
-10
lines changed

draw.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "interval_types.hpp"
44
#include "interval_tree.hpp"
55

6-
#include "cairo-wrap/cairo_wrap.hpp"
6+
#include <cairo-wrap/cairo_wrap.hpp>
77

88
#include <string>
99
#include <sstream>

interval_tree.hpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ namespace lib_interval_tree
277277
return counter;
278278
}
279279

280+
/**
281+
* Returns the lower bound of the interval of this node
282+
*/
283+
value_type low() const
284+
{
285+
return interval_.low();
286+
}
287+
288+
/**
289+
* Returns the upper bound of the interval of this node
290+
*/
291+
value_type high() const
292+
{
293+
return interval_.high();
294+
}
295+
280296
private:
281297
void set_interval(interval_type const& ival)
282298
{
@@ -432,7 +448,7 @@ namespace lib_interval_tree
432448
if (node_)
433449
return node_->interval();
434450
else
435-
throw std::out_of_range("interval_tree_iterator out of bounds");
451+
throw std::out_of_range("dereferencing interval_tree_iterator out of bounds");
436452
}
437453

438454
/**
@@ -799,15 +815,15 @@ namespace lib_interval_tree
799815
}
800816

801817
/**
802-
* Finds the next exact match INCLUDING from.
818+
* Finds the next exact match EXCLUDING from.
803819
*
804-
* @param from The iterator to search from, INCLUSIVE!
820+
* @param from The iterator to search from, EXCLUCISVE!
805821
* @param ival The interval to find an exact match for within the tree.
806822
* @param compare A comparison function to use.
807823
*/
808824
iterator find_next(iterator from, interval_type const& ival)
809825
{
810-
return find_next(from, ival, [](auto const& lhs, auto const& rhs){return lhs == rhs;});
826+
return find_next(++from, ival, [](auto const& lhs, auto const& rhs){return lhs == rhs;});
811827
}
812828

813829
/**
@@ -820,21 +836,21 @@ namespace lib_interval_tree
820836
{
821837
if (root_ == nullptr)
822838
return end();
823-
return iterator{overlap_find_i(root_, ival, exclusive), this};
839+
return iterator{overlap_find_i(begin().node_, ival, exclusive), this};
824840
}
825841

826842
/**
827-
* Finds the next interval that overlaps with ival INCLUDING from.
843+
* Finds the next interval that overlaps with ival
828844
*
829-
* @param from The iterator to start from, INCLUSIVE!
845+
* @param from The iterator to start from, EXCLUSIVE!
830846
* @param ival The interval to find an overlap for within the tree.
831847
* @param exclusive Exclude edges?
832848
*/
833849
iterator overlap_find_next(iterator from, interval_type const& ival, bool exclusive = false)
834850
{
835851
if (root_ == nullptr)
836852
return end();
837-
return iterator{overlap_find_i(from.node_, ival, exclusive), this};
853+
return iterator{overlap_find_i((++from).node_, ival, exclusive), this};
838854
}
839855

840856
/**

tests/overlap_tests.hpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#pragma once
2+
3+
#include "test_utility.hpp"
4+
5+
class OverlapFindTests
6+
: public ::testing::Test
7+
{
8+
public:
9+
using types = IntervalTypes <int>;
10+
protected:
11+
IntervalTypes <int>::tree_type tree;
12+
};
13+
14+
TEST_F(OverlapFindTests, WillReturnEndIfTreeIsEmpty)
15+
{
16+
EXPECT_EQ(tree.overlap_find({2, 7}), std::end(tree));
17+
}
18+
19+
TEST_F(OverlapFindTests, WillNotFindOverlapWithRootIfItDoesntOverlap)
20+
{
21+
tree.insert({0, 1});
22+
EXPECT_EQ(tree.overlap_find({2, 7}), std::end(tree));
23+
}
24+
25+
TEST_F(OverlapFindTests, WillFindOverlapWithRoot)
26+
{
27+
tree.insert({2, 4});
28+
EXPECT_EQ(tree.overlap_find({2, 7}), std::begin(tree));
29+
}
30+
31+
TEST_F(OverlapFindTests, WillFindOverlapWithRootIfMatchingExactly)
32+
{
33+
tree.insert({2, 7});
34+
EXPECT_EQ(tree.overlap_find({2, 7}), std::begin(tree));
35+
}
36+
37+
TEST_F(OverlapFindTests, WillFindOverlapWithRootIfTouching)
38+
{
39+
tree.insert({2, 7});
40+
EXPECT_EQ(tree.overlap_find({7, 9}), std::begin(tree));
41+
}
42+
43+
TEST_F(OverlapFindTests, WillFindMultipleOverlaps)
44+
{
45+
tree.insert({ 0, 5 });
46+
tree.insert({ 5, 10 });
47+
tree.insert({ 10, 15 });
48+
tree.insert({ 15, 20 });
49+
50+
auto iter = tree.overlap_find({0, 9}), end = tree.end();
51+
EXPECT_EQ(iter->low(), 0);
52+
EXPECT_EQ(iter->high(), 5);
53+
54+
iter = tree.overlap_find_next(iter, {0, 9});
55+
EXPECT_EQ(iter->low(), 5);
56+
EXPECT_EQ(iter->high(), 10);
57+
58+
iter = tree.overlap_find_next(iter, {0, 9});
59+
EXPECT_EQ(iter, end);
60+
}
61+
62+
TEST_F(OverlapFindTests, WillFindMultipleOverlapsIfTouching)
63+
{
64+
tree.insert({ 0, 5 });
65+
tree.insert({ 5, 10 });
66+
tree.insert({ 10, 15 });
67+
tree.insert({ 15, 20 });
68+
69+
auto iter = tree.overlap_find({5, 5}), end = tree.end();
70+
EXPECT_EQ(iter->low(), 0);
71+
EXPECT_EQ(iter->high(), 5);
72+
73+
iter = tree.overlap_find_next(iter, {5, 5});
74+
EXPECT_EQ(iter->low(), 5);
75+
EXPECT_EQ(iter->high(), 10);
76+
77+
iter = tree.overlap_find_next(iter, {5, 5});
78+
EXPECT_EQ(iter, end);
79+
}
80+
81+

tests/tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
// following headers expect to be included after gtest headers and interval_tree
88
#include "interval_tests.hpp"
99
#include "insert_tests.hpp"
10-
#include "erase_tests.hpp"
10+
#include "erase_tests.hpp"
11+
#include "overlap_tests.hpp"
1112

1213
int main(int argc, char** argv)
1314
{

0 commit comments

Comments
 (0)