Skip to content

Commit 10a2d76

Browse files
committed
Reimplemented punch feature properly.
1 parent 8b5aac7 commit 10a2d76

File tree

8 files changed

+1392
-164
lines changed

8 files changed

+1392
-164
lines changed

README.md

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ Create a build folder, navigate there, run cmake and build the tree-tests target
6161
You might have to adapt the linker line for gtest, if you built it yourself and didn't install it into your system.
6262
If you want to generate the pretty drawings, install cairo, pull the submodule and pass INT_TREE_DRAW_EXAMPLES=on to the cmake command line to generate a drawings/make_drawings executeable.
6363

64+
Some features of this library require the presence of an optional type.
65+
If you are using C++17 and up, it will be std::optional.
66+
Otherwise you can specify INTERVAL_TREE_HAVE_BOOST_OPTIONAL to use boost::optional.
67+
And if neither, a reduced version of optional is provided in the library, not perfectly exchangeable with std::optional, but sufficient for the library to work.
68+
6469
## Draw Dot Graph
6570
This draws a dot graph of the tree:
6671
```c++
@@ -137,37 +142,73 @@ Options are:
137142

138143
## Members of IntervalTree<Interval>
139144

140-
- [Members of IntervalTree<Interval>](#members-of-intervaltreeinterval)
141-
- [iterator insert(interval_type const& ival)](#iterator-insertinterval_type-const-ival)
142-
- [iterator insert_overlap(interval_type const& ival, bool, bool)](#iterator-insert_overlapinterval_type-const-ival-bool-bool)
145+
- [interval-tree](#interval-tree)
146+
- [How an interval tree looks like:](#how-an-interval-tree-looks-like)
147+
- [Example](#example)
148+
- [Compile \& Run Testing](#compile--run-testing)
149+
- [Draw Dot Graph](#draw-dot-graph)
150+
- [Free Functions](#free-functions)
151+
- [interval\<NumericT, Kind\> make\_safe\_interval(NumericT border1, NumericT border2)](#intervalnumerict-kind-make_safe_intervalnumerict-border1-numerict-border2)
152+
- [draw\_dot\_graph(std::ostream\& os, interval\_tree\_t const\& tree, DrawOptions const\& options)](#draw_dot_graphstdostream-os-interval_tree_t-const-tree-drawoptions-const-options)
153+
- [Members of IntervalTree](#members-of-intervaltree)
154+
- [iterator insert(interval\_type const\& ival)](#iterator-insertinterval_type-const-ival)
155+
- [Parameters](#parameters)
156+
- [iterator insert\_overlap(interval\_type const\& ival, bool, bool)](#iterator-insert_overlapinterval_type-const-ival-bool-bool)
157+
- [Parameters](#parameters-1)
143158
- [iterator erase(iterator iter)](#iterator-eraseiterator-iter)
144-
- [size_type size() const](#size_type-size-const)
145-
- [(const)iterator find(interval_type const& ival)](#constiterator-findinterval_type-const-ival)
146-
- [(const)iterator find(interval_type const& ival, CompareFunctionT const& compare)](#constiterator-findinterval_type-const-ival-comparefunctiont-const-compare)
147-
- [(const)iterator find_all(interval_type const& ival, OnFindFunctionT const& on_find)](#constiterator-find_allinterval_type-const-ival-onfindfunctiont-const-on_find)
159+
- [Parameters](#parameters-2)
160+
- [size\_type size() const](#size_type-size-const)
161+
- [(const)iterator find(interval\_type const\& ival)](#constiterator-findinterval_type-const-ival)
162+
- [Parameters](#parameters-3)
163+
- [(const)iterator find(interval\_type const\& ival, CompareFunctionT const\& compare)](#constiterator-findinterval_type-const-ival-comparefunctiont-const-compare)
164+
- [Parameters](#parameters-4)
165+
- [(const)iterator find\_all(interval\_type const\& ival, OnFindFunctionT const\& on\_find)](#constiterator-find_allinterval_type-const-ival-onfindfunctiont-const-on_find)
166+
- [Parameters](#parameters-5)
148167
- [Example](#example-1)
149-
- [(const)iterator find_all(interval_type const& ival, OnFindFunctionT const& on_find, CompareFunctionT const& compare)](#constiterator-find_allinterval_type-const-ival-onfindfunctiont-const-on_find-comparefunctiont-const-compare)
150-
- [(const)iterator find_next_in_subtree(iterator from, interval_type const& ival)](#constiterator-find_next_in_subtreeiterator-from-interval_type-const-ival)
151-
- [(const)iterator find_next_in_subtree(iterator from, interval_type const& ival, CompareFunctionT const& compare)](#constiterator-find_next_in_subtreeiterator-from-interval_type-const-ival-comparefunctiont-const-compare)
152-
- [(const)iterator overlap_find(interval_type const& ival, bool exclusive)](#constiterator-overlap_findinterval_type-const-ival-bool-exclusive)
153-
- [(const)iterator overlap_find_all(interval_type const& ival, OnFindFunctionT const& on_find, bool exclusive)](#constiterator-overlap_find_allinterval_type-const-ival-onfindfunctiont-const-on_find-bool-exclusive)
168+
- [(const)iterator find\_all(interval\_type const\& ival, OnFindFunctionT const\& on\_find, CompareFunctionT const\& compare)](#constiterator-find_allinterval_type-const-ival-onfindfunctiont-const-on_find-comparefunctiont-const-compare)
169+
- [Parameters](#parameters-6)
170+
- [(const)iterator find\_next\_in\_subtree(iterator from, interval\_type const\& ival)](#constiterator-find_next_in_subtreeiterator-from-interval_type-const-ival)
171+
- [Parameters](#parameters-7)
172+
- [(const)iterator find\_next\_in\_subtree(iterator from, interval\_type const\& ival, CompareFunctionT const\& compare)](#constiterator-find_next_in_subtreeiterator-from-interval_type-const-ival-comparefunctiont-const-compare)
173+
- [Parameters](#parameters-8)
174+
- [(const)iterator overlap\_find(interval\_type const\& ival, bool exclusive)](#constiterator-overlap_findinterval_type-const-ival-bool-exclusive)
175+
- [Parameters](#parameters-9)
176+
- [(const)iterator overlap\_find\_all(interval\_type const\& ival, OnFindFunctionT const\& on\_find, bool exclusive)](#constiterator-overlap_find_allinterval_type-const-ival-onfindfunctiont-const-on_find-bool-exclusive)
177+
- [Parameters](#parameters-10)
154178
- [Example](#example-2)
155-
- [(const)iterator overlap_find_next_in_subtree(interval_type const& ival, bool exclusive)](#constiterator-overlap_find_next_in_subtreeinterval_type-const-ival-bool-exclusive)
156-
- [interval_tree& deoverlap()](#interval_tree-deoverlap)
179+
- [(const)iterator overlap\_find\_next\_in\_subtree(interval\_type const\& ival, bool exclusive)](#constiterator-overlap_find_next_in_subtreeinterval_type-const-ival-bool-exclusive)
180+
- [Parameters](#parameters-11)
181+
- [interval\_tree\& deoverlap()](#interval_tree-deoverlap)
157182
- [After deoverlap](#after-deoverlap)
158-
- [interval_tree& deoverlap_copy()](#interval_tree-deoverlap_copy)
159-
- [interval_tree punch(interval_type const& ival)](#interval_tree-punchinterval_type-const-ival)
160-
- [After punching (with [0, 50])](#after-punching-with-0-50)
161-
- [interval_tree punch()](#interval_tree-punch)
183+
- [interval\_tree deoverlap\_copy()](#interval_tree-deoverlap_copy)
184+
- [interval\_tree punch(interval\_type const\& ival)](#interval_tree-punchinterval_type-const-ival)
185+
- [After punching (with \[0, 50\])](#after-punching-with-0-50)
186+
- [interval\_tree punch()](#interval_tree-punch)
162187
- [bool empty() const noexcept](#bool-empty-const-noexcept)
163188
- [iterator begin()](#iterator-begin)
164189
- [iterator end()](#iterator-end)
165190
- [iterator cbegin()](#iterator-cbegin)
166191
- [iterator cend()](#iterator-cend)
167-
- [reverse_iterator rbegin()](#reverse_iterator-rbegin)
168-
- [reverse_iterator rend()](#reverse_iterator-rend)
169-
- [reverse_iterator crbegin()](#reverse_iterator-crbegin)
170-
- [reverse_iterator crend()](#reverse_iterator-crend)
192+
- [reverse\_iterator rbegin()](#reverse_iterator-rbegin)
193+
- [reverse\_iterator rend()](#reverse_iterator-rend)
194+
- [reverse\_iterator crbegin()](#reverse_iterator-crbegin)
195+
- [reverse\_iterator crend()](#reverse_iterator-crend)
196+
- [Members of Interval](#members-of-interval)
197+
- [using value\_type](#using-value_type)
198+
- [using interval\_kind](#using-interval_kind)
199+
- [friend bool operator==(interval const\& lhs, interval const\& other)](#friend-bool-operatorinterval-const-lhs-interval-const-other)
200+
- [friend bool operator!=(interval const\& lhs, interval const\& other)](#friend-bool-operatorinterval-const-lhs-interval-const-other-1)
201+
- [value\_type low() const](#value_type-low-const)
202+
- [value\_type high() const](#value_type-high-const)
203+
- [\[\[deprecated\]\] bool overlaps(value\_type l, value\_type h) const](#deprecated-bool-overlapsvalue_type-l-value_type-h-const)
204+
- [bool overlaps\_exclusive(value\_type l, value\_type h) const](#bool-overlaps_exclusivevalue_type-l-value_type-h-const)
205+
- [bool overlaps(interval const\& other) const](#bool-overlapsinterval-const-other-const)
206+
- [bool overlaps\_exclusive(interval const\& other) const](#bool-overlaps_exclusiveinterval-const-other-const)
207+
- [bool within(value\_type value) const](#bool-withinvalue_type-value-const)
208+
- [bool within(interval const\& other) const](#bool-withininterval-const-other-const)
209+
- [value\_type operator-(interval const\& other) const](#value_type-operator-interval-const-other-const)
210+
- [value\_type size() const](#value_type-size-const)
211+
- [interval join(interval const\& other) const](#interval-joininterval-const-other-const)
171212

172213
### iterator insert(interval_type const& ival)
173214
Adds an interval into the tree.

drawings/example_drawings.hpp

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include <interval-tree/draw.hpp>
44

5+
#include <random>
56
#include <string>
7+
#include <iostream>
68

79
static void drawDocExample()
810
{
@@ -31,7 +33,7 @@ static void drawFromTests1()
3133

3234
interval_tree_t<int> tree;
3335

34-
std::vector <interval_tree_t<int>::interval_type> intervalCollection;
36+
std::vector<interval_tree_t<int>::interval_type> intervalCollection;
3537

3638
intervalCollection.push_back({-51, 11});
3739
intervalCollection.push_back({26, 68});
@@ -67,8 +69,120 @@ static void drawFromTests1()
6769
drawTree("drawings/from_tests_1_deoverlapped.png", tree, false, false);
6870
}
6971

72+
static void drawLargeOverlapFree()
73+
{
74+
using namespace lib_interval_tree;
75+
76+
interval_tree_t<int> tree;
77+
78+
for (int i = 0; i < 30; ++i)
79+
{
80+
tree.insert({i * 10, i * 10 + 5});
81+
}
82+
83+
drawTree("drawings/large_overlap_free.png", tree, false, false);
84+
85+
std::vector<std::pair<int, int>> intervals;
86+
for (int i = 0; i < 30; ++i)
87+
{
88+
intervals.emplace_back(i * 10, i * 10 + 5);
89+
}
90+
91+
// insert shuffled into new tree
92+
interval_tree_t<int> tree2;
93+
std::mt19937 rng(std::random_device{}());
94+
std::shuffle(intervals.begin(), intervals.end(), rng);
95+
for (const auto& interval : intervals)
96+
{
97+
tree2.insert({interval.first, interval.second});
98+
}
99+
drawTree("drawings/large_overlap_free_shuffled.png", tree2, false, false);
100+
}
101+
102+
static void drawOpenPunchExample()
103+
{
104+
constexpr int iterations = 5;
105+
using namespace lib_interval_tree;
106+
107+
interval_tree<interval<int, open>> tree;
108+
109+
// insert shuffled into new tree
110+
std::vector<std::pair<int, int>> intervals;
111+
for (int i = 0; i < iterations; ++i)
112+
{
113+
intervals.emplace_back(i * 10, i * 10 + 5);
114+
}
115+
116+
std::mt19937 rng(std::random_device{}());
117+
std::shuffle(intervals.begin(), intervals.end(), rng);
118+
for (const auto& interval : intervals)
119+
{
120+
tree.insert({interval.first, interval.second});
121+
}
122+
123+
drawTree("drawings/open_punch_source.png", tree, false, false);
124+
const auto punched = tree.punch({-10, iterations * 10 + 10});
125+
drawTree("drawings/open_punched.png", punched, false, false);
126+
}
127+
128+
static void drawClosedPunchExample()
129+
{
130+
constexpr int iterations = 5;
131+
using namespace lib_interval_tree;
132+
133+
interval_tree<interval<int, closed>> tree;
134+
135+
// insert shuffled into new tree
136+
std::vector<std::pair<int, int>> intervals;
137+
for (int i = 0; i < iterations; ++i)
138+
{
139+
intervals.emplace_back(i * 10, i * 10 + 5);
140+
}
141+
142+
std::mt19937 rng(std::random_device{}());
143+
std::shuffle(intervals.begin(), intervals.end(), rng);
144+
for (const auto& interval : intervals)
145+
{
146+
tree.insert({interval.first, interval.second});
147+
}
148+
149+
drawTree("drawings/closed_punch_source.png", tree, false, false);
150+
const auto punched = tree.punch({-10, iterations * 10 + 10});
151+
drawTree("drawings/closed_punched.png", punched, false, false);
152+
}
153+
154+
static void drawFloatPunchExample()
155+
{
156+
constexpr int iterations = 5;
157+
using namespace lib_interval_tree;
158+
159+
interval_tree<interval<float, closed>> tree;
160+
161+
// insert shuffled into new tree
162+
std::vector<std::pair<float, float>> intervals;
163+
for (int i = 0; i < iterations; ++i)
164+
{
165+
intervals.emplace_back(i * 10.0f, i * 10.0f + 5.0f);
166+
}
167+
168+
std::mt19937 rng(std::random_device{}());
169+
std::shuffle(intervals.begin(), intervals.end(), rng);
170+
for (const auto& interval : intervals)
171+
{
172+
tree.insert({interval.first, interval.second});
173+
}
174+
175+
drawTree("drawings/float_punch_source.png", tree, false, false);
176+
const auto punched = tree.punch({-10.0f, iterations * 10.0f + 10.0f});
177+
drawTree("drawings/float_punched.png", punched, false, false);
178+
}
179+
70180
static void drawAll()
71181
{
72182
drawDocExample();
73183
drawFromTests1();
184+
drawLargeOverlapFree();
185+
drawOpenPunchExample();
186+
drawClosedPunchExample();
187+
drawFloatPunchExample();
74188
}

0 commit comments

Comments
 (0)