Skip to content

Commit 10c74a5

Browse files
authored
Merge pull request #53 from 5cript/feat/punch_again
Reimplement punch & address some unsigned underflow concerns.
2 parents 12bf0d5 + 00ad98f commit 10c74a5

File tree

12 files changed

+2365
-181
lines changed

12 files changed

+2365
-181
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ project(interval-tree)
66

77
add_library(interval-tree INTERFACE)
88

9+
if(INT_TREE_USE_OPTIONAL_POLYFILL)
10+
target_compile_definitions(interval-tree INTERFACE
11+
-DINTERVAL_TREE_USE_OPTIONAL_POLYFILL
12+
)
13+
endif()
14+
915
target_include_directories(interval-tree INTERFACE ./include)
1016

1117
if(INT_TREE_DRAW_EXAMPLES)

README.md

Lines changed: 87 additions & 28 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,75 @@ 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+
- [Before punching (closed\_adjacent intervals)](#before-punching-closed_adjacent-intervals)
186+
- [After punching (with \[-10, 60\])](#after-punching-with--10-60)
187+
- [interval\_tree punch()](#interval_tree-punch)
162188
- [bool empty() const noexcept](#bool-empty-const-noexcept)
163189
- [iterator begin()](#iterator-begin)
164190
- [iterator end()](#iterator-end)
165191
- [iterator cbegin()](#iterator-cbegin)
166192
- [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)
193+
- [reverse\_iterator rbegin()](#reverse_iterator-rbegin)
194+
- [reverse\_iterator rend()](#reverse_iterator-rend)
195+
- [reverse\_iterator crbegin()](#reverse_iterator-crbegin)
196+
- [reverse\_iterator crend()](#reverse_iterator-crend)
197+
- [Members of Interval](#members-of-interval)
198+
- [using value\_type](#using-value_type)
199+
- [using interval\_kind](#using-interval_kind)
200+
- [friend bool operator==(interval const\& lhs, interval const\& other)](#friend-bool-operatorinterval-const-lhs-interval-const-other)
201+
- [friend bool operator!=(interval const\& lhs, interval const\& other)](#friend-bool-operatorinterval-const-lhs-interval-const-other-1)
202+
- [value\_type low() const](#value_type-low-const)
203+
- [value\_type high() const](#value_type-high-const)
204+
- [\[\[deprecated\]\] bool overlaps(value\_type l, value\_type h) const](#deprecated-bool-overlapsvalue_type-l-value_type-h-const)
205+
- [bool overlaps\_exclusive(value\_type l, value\_type h) const](#bool-overlaps_exclusivevalue_type-l-value_type-h-const)
206+
- [bool overlaps(interval const\& other) const](#bool-overlapsinterval-const-other-const)
207+
- [bool overlaps\_exclusive(interval const\& other) const](#bool-overlaps_exclusiveinterval-const-other-const)
208+
- [bool within(value\_type value) const](#bool-withinvalue_type-value-const)
209+
- [bool within(interval const\& other) const](#bool-withininterval-const-other-const)
210+
- [value\_type operator-(interval const\& other) const](#value_type-operator-interval-const-other-const)
211+
- [value\_type size() const](#value_type-size-const)
212+
- [interval join(interval const\& other) const](#interval-joininterval-const-other-const)
213+
- [slice\_type slice(interval const\& other) const](#slice_type-sliceinterval-const-other-const)
171214

172215
### iterator insert(interval_type const& ival)
173216
Adds an interval into the tree.
@@ -326,13 +369,20 @@ Same as deoverlap, but not inplace
326369

327370
---
328371
### interval_tree punch(interval_type const& ival)
329-
Removes all intervals from `ival` and produces a tree that contains the remaining intervals.
330-
**The tree must be deoverlapped, or the result is undefined.**
331-
`ival` is expected to encompass the entire interval range.
372+
Cuts the intervals of the tree out of the given interval. Like a cookie cutter cuts out of dough.
373+
This will return a new interval_tree containing the gaps between the intervals in the tree and the given interval.
374+
Closed adjacent intervals are treated as exclusive on the borders. [0,5]a[6,10]a will not produce another interval between 5 and 6 as they are considered within the intervals and nothing fits inbetween.
375+
Regular closed intervals will not behave like this, so [0,5][6,10] will produce a new interval [5,6].
376+
Open intervals with integral numbers will also not produce the gap (5, 6), because (5, 6) is empty for integers, not for floats.
377+
378+
**IMPORTANT! The tree must be deoverlapped, or the result is undefined.**
379+
`ival` can be any subrange of the tree, including encompassing the whole tree.
332380

333381
**Returns**: A new interval_tree containing the gaps.
334-
### After punching (with [0, 50])
335-
![AfterPunch](https://cloud.githubusercontent.com/assets/6238896/24613645/2dbf72e8-1889-11e7-813f-6d16fe0ad327.png)
382+
### Before punching (closed_adjacent intervals)
383+
![BeforePunch](https://private-user-images.githubusercontent.com/6238896/471147224-5c631e00-dea4-4b75-a3bf-6fdd8ec1440b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NTM1NjI1MzQsIm5iZiI6MTc1MzU2MjIzNCwicGF0aCI6Ii82MjM4ODk2LzQ3MTE0NzIyNC01YzYzMWUwMC1kZWE0LTRiNzUtYTNiZi02ZmRkOGVjMTQ0MGIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDcyNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTA3MjZUMjAzNzE0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDQ0NWIwMTcwMjZhNDA1YmUwNGI1YTIzNTBhZTQ5OTNhMWFiOTU5ZmU0N2E3NDI0NTQ0MzYwODA4N2E2MGFiZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.P5zLeXg0-9bd20Thj6pfq_WxriMn4GC_lDSLzzGKMbw)
384+
### After punching (with [-10, 60])
385+
![AfterPunch](https://private-user-images.githubusercontent.com/6238896/471147227-5c226d1d-d544-4a43-89a4-b3545145107d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NTM1NjI1MzQsIm5iZiI6MTc1MzU2MjIzNCwicGF0aCI6Ii82MjM4ODk2LzQ3MTE0NzIyNy01YzIyNmQxZC1kNTQ0LTRhNDMtODlhNC1iMzU0NTE0NTEwN2QucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDcyNiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTA3MjZUMjAzNzE0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NmE2ZDUzMjU2ZTNjZWQ0Y2QzYjQ3ZGUyYjgyNWM2NDViYTAxMTdlY2RjYmQyMzg4OWFmZDlhMWU5YjY4NjlmZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.Infe9i281LDOEC5GeBFuLHVE6Xjqw7KvcUo-gv3hjpk)
336386

337387
---
338388
### interval_tree punch()
@@ -395,7 +445,7 @@ Returns a past the end const_iterator in reverse.
395445
**Returns**: past the end const_iterator.
396446

397447
## Members of Interval
398-
___You can implement your own interval if you provide the same functions, except (operator-, size, operator!=).___
448+
___You can implement your own interval if you provide the same functions, except (slice, operator-, size, operator!=).___
399449

400450
There are 6 types of intervals:
401451
- open: (a, b)
@@ -457,3 +507,12 @@ Overlapping intervals have 0 distance.
457507
Returns The amount of elements in the interval when integral, or the distance between the 2 bounds when floating point.
458508
### interval join(interval const& other) const
459509
Joins 2 intervals and whatever is inbetween.
510+
### slice_type<interval> slice(interval const& other) const
511+
Removes other from this interval returning what is remaining.
512+
The range of other going beyond the range of this is ignored.
513+
Returns a struct with 2 members: left_slice and right_slice.
514+
[ this interval ]
515+
[left][other][right]
516+
517+
When the intervals are closed, adjacent results are differenty by 1.
518+
[0, 9].slice([5, 19]) => left: [0, 4], right: nullopt

cmake/options.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
option(INT_TREE_DRAW_EXAMPLES "Draws some examples in a subdirectory. run make_drawable.sh before this" OFF)
2-
option(INT_TREE_ENABLE_TESTS "Enable tests?" OFF)
2+
option(INT_TREE_ENABLE_TESTS "Enable tests?" OFF)
3+
# You generally do not want to turn this on, unless you are testing the library.
4+
option(INT_TREE_USE_OPTIONAL_POLYFILL "Use optional polyfill?" OFF)

drawings/example_drawings.hpp

Lines changed: 142 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,147 @@ 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 drawAdjacentClosedPunchExample()
155+
{
156+
constexpr int iterations = 5;
157+
using namespace lib_interval_tree;
158+
159+
interval_tree<interval<int, closed_adjacent>> tree;
160+
161+
// insert shuffled into new tree
162+
std::vector<std::pair<int, int>> intervals;
163+
for (int i = 0; i < iterations; ++i)
164+
{
165+
intervals.emplace_back(i * 10, i * 10 + 5);
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/closed_adjacent_punch_source.png", tree, false, false);
176+
const auto punched = tree.punch({-10, iterations * 10 + 10});
177+
drawTree("drawings/closed_adjacent_punched.png", punched, false, false);
178+
}
179+
180+
static void drawFloatPunchExample()
181+
{
182+
constexpr int iterations = 5;
183+
using namespace lib_interval_tree;
184+
185+
interval_tree<interval<float, closed>> tree;
186+
187+
// insert shuffled into new tree
188+
std::vector<std::pair<float, float>> intervals;
189+
for (int i = 0; i < iterations; ++i)
190+
{
191+
intervals.emplace_back(i * 10.0f, i * 10.0f + 5.0f);
192+
}
193+
194+
std::mt19937 rng(std::random_device{}());
195+
std::shuffle(intervals.begin(), intervals.end(), rng);
196+
for (const auto& interval : intervals)
197+
{
198+
tree.insert({interval.first, interval.second});
199+
}
200+
201+
drawTree("drawings/float_punch_source.png", tree, false, false);
202+
const auto punched = tree.punch({-10.0f, iterations * 10.0f + 10.0f});
203+
drawTree("drawings/float_punched.png", punched, false, false);
204+
}
205+
70206
static void drawAll()
71207
{
72208
drawDocExample();
73209
drawFromTests1();
210+
drawLargeOverlapFree();
211+
drawOpenPunchExample();
212+
drawClosedPunchExample();
213+
drawAdjacentClosedPunchExample();
214+
drawFloatPunchExample();
74215
}

0 commit comments

Comments
 (0)