Skip to content

Commit ba04ef5

Browse files
authored
Merge pull request #35 from 5cript/feat/interval-kinds
Made interval types meaningful.
2 parents a661ab1 + 9f9bad2 commit ba04ef5

File tree

10 files changed

+1342
-156
lines changed

10 files changed

+1342
-156
lines changed

.github/workflows/build_and_test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
uses: actions/checkout@v2
4949

5050
- name: CMake Configure
51-
run: cmake -B ${{github.workspace}}/build -G"Ninja" -DINT_TREE_ENABLE_TESTS=on -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_COMPILER=${{ matrix.compiler.cxx }} -DCMAKE_CXX_STANDARD=${{ matrix.cxx_standard }}
51+
run: cmake -B ${{github.workspace}}/build -G"Ninja" -DINT_TREE_BUILD_EXAMPLES=on -DINT_TREE_ENABLE_TESTS=on -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_COMPILER=${{ matrix.compiler.cxx }} -DCMAKE_CXX_STANDARD=${{ matrix.cxx_standard }}
5252

5353
- name: Build
5454
run: cmake --build ${{github.workspace}}/build

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ if(INT_TREE_DRAW_EXAMPLES)
1414
endif()
1515
if (INT_TREE_ENABLE_TESTS)
1616
add_subdirectory(tests)
17+
endif()
18+
if (INT_TREE_BUILD_EXAMPLES)
19+
add_subdirectory(example)
1720
endif()

README.md

+30-7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ int main()
2222
{
2323
using namespace lib_interval_tree;
2424

25-
// interval_tree <interval <int>>;
26-
interval_tree_t <int> tree;
25+
// interval_tree<interval<int>>; // closed by default
26+
// interval_tree<interval<int, open>>;
27+
// interval_tree<interval<int, closed>>;
28+
// interval_tree<interval<int, left_open>>;
29+
// interval_tree<interval<int, right_open>>;
30+
// interval_tree<interval<int, closed_adjacent>>; // counts adjacent intervals as overlapping
31+
interval_tree_t<int> tree;
2732

2833
tree.insert(make_safe_interval<int>(21, 16)); // make_safe_interval swaps low and high if not in right order.
2934
tree.insert({8, 9});
@@ -42,6 +47,12 @@ int main()
4247
{
4348
std::cout << "[" << i.low() << ", " << i.high() << "]\n";
4449
}
50+
51+
using lib_interval_tree::open;
52+
// dynamic has some logic overhead.
53+
interval_tree<interval<int, dynamic>> dynamicIntervals;
54+
dynamicIntervals.insert({0, 1, interval_border::closed, interval_border::open});
55+
dynamicIntervals.insert({7, 5, interval_border::open, interval_border::closed_adjacent});
4556
}
4657
```
4758

@@ -315,7 +326,18 @@ Returns a past the end const_iterator in reverse.
315326
**Returns**: past the end const_iterator.
316327

317328
## Members of Interval
318-
___You can implement your own interval if you provide all the same functions.___
329+
___You can implement your own interval if you provide the same functions, except (within, operator-, size, operator!=).___
330+
331+
There are 6 types of intervals:
332+
- open: (a, b)
333+
- left_open: (a, b]
334+
- right_open: [a, b)
335+
- closed: [a, b]
336+
- closed_adjacent: [a, b] (counts adjacent intervals as overlapping)
337+
- dynamic: Can be any of the above, depending on the input. Not supported for floating point.
338+
339+
Which can be picked with the second template parameter of interval:
340+
`lib_interval_tree::interval<int, lib_interval_tree::open>`
319341

320342
- [Members of Interval](#members-of-interval)
321343
- [using value_type](#using-value_type)
@@ -324,7 +346,7 @@ ___You can implement your own interval if you provide all the same functions.___
324346
- [friend bool operator!=(interval const& lhs, interval const& other)](#friend-bool-operatorinterval-const-lhs-interval-const-other-1)
325347
- [value_type low() const](#value_type-low-const)
326348
- [value_type high() const](#value_type-high-const)
327-
- [bool overlaps(value_type l, value_type h) const](#bool-overlapsvalue_type-l-value_type-h-const)
349+
- [\[\[deprecated\]\] bool overlaps(value_type l, value_type h) const](#bool-overlapsvalue_type-l-value_type-h-const)
328350
- [bool overlaps_exclusive(value_type l, value_type h) const](#bool-overlaps_exclusivevalue_type-l-value_type-h-const)
329351
- [bool overlaps(interval const& other) const](#bool-overlapsinterval-const-other-const)
330352
- [bool overlaps_exclusive(interval const& other) const](#bool-overlaps_exclusiveinterval-const-other-const)
@@ -346,22 +368,23 @@ Comparison operator.
346368
Lower bound.
347369
### value_type high() const
348370
Upper bound.
349-
### bool overlaps(value_type l, value_type h) const
371+
### \[\[deprecated\]\] bool overlaps(value_type l, value_type h) const
350372
Overlap these bounds with this interval (closed)?
373+
Is deprecated because the overlapping only works with closed intervals.
351374
### bool overlaps_exclusive(value_type l, value_type h) const
352375
Overlap these bounds with this interval excluding borders?
353376
### bool overlaps(interval const& other) const
354377
Like overlaps with lower and upper bound.
355378
### bool overlaps_exclusive(interval const& other) const
356379
Like overlaps with lower and upper bound.
357380
### bool within(value_type value) const
358-
Is the value within the interval (closed)?
381+
Is the value within the interval?
359382
### bool within(interval const& other) const
360383
Is the interval within the interval?
361384
### value_type operator-(interval const& other) const
362385
Calculates the distance between the two intervals.
363386
Overlapping intervals have 0 distance.
364387
### value_type size() const
365-
Returns high - low.
388+
Returns The amount of elements in the interval when integral, or the distance between the 2 bounds when floating point.
366389
### interval join(interval const& other) const
367390
Joins 2 intervals and whatever is inbetween.

example/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(from_readme)

example/from_readme/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_executable(from_readme main.cpp)
2+
target_link_libraries(from_readme interval-tree)
3+
4+
set_target_properties(from_readme
5+
PROPERTIES
6+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/interval_tree/examples"
7+
)

example/from_readme/main.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// #include <interval-tree/draw.hpp> // to draw tree. this is not header only anymore.
2+
#include <interval-tree/interval_tree.hpp>
3+
4+
int main()
5+
{
6+
using namespace lib_interval_tree;
7+
8+
// interval_tree<interval<int>>; // closed by default
9+
// interval_tree<interval<int, open>>;
10+
// interval_tree<interval<int, closed>>;
11+
// interval_tree<interval<int, left_open>>;
12+
// interval_tree<interval<int, right_open>>;
13+
// interval_tree<interval<int, closed_adjacent>>; // counts adjacent intervals as overlapping
14+
interval_tree_t<int> tree;
15+
16+
tree.insert(make_safe_interval<int>(21, 16)); // make_safe_interval swaps low and high if not in right order.
17+
tree.insert({8, 9});
18+
tree.insert({25, 30});
19+
tree.insert({5, 8});
20+
tree.insert({15, 23});
21+
tree.insert({17, 19});
22+
tree.insert({26, 26});
23+
tree.insert({0, 3});
24+
tree.insert({6, 10});
25+
tree.insert({19, 20});
26+
27+
tree.deoverlap();
28+
29+
for (auto const& i : tree)
30+
{
31+
std::cout << "[" << i.low() << ", " << i.high() << "]\n";
32+
}
33+
34+
using lib_interval_tree::open;
35+
// dynamic has some logic overhead.
36+
interval_tree<interval<int, dynamic>> dynamicIntervals;
37+
dynamicIntervals.insert({0, 1, interval_border::closed, interval_border::open});
38+
dynamicIntervals.insert({7, 5, interval_border::open, interval_border::closed_adjacent});
39+
}
+21-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
#pragma once
22

3-
#if defined(__cpp_concepts) && __cpp_concepts >= 202002L
4-
# define LIB_INTERVAL_TREE_CONCEPTS
3+
// define LIB_INTERVAL_TREE_CONCEPTS to override the concepts feature test
4+
#ifndef LIB_INTERVAL_TREE_CONCEPTS
5+
# if defined(__cpp_concepts) && __cpp_concepts >= 202002L
6+
# define LIB_INTERVAL_TREE_CONCEPTS
7+
# endif
58
#endif
69

7-
namespace lib_interval_tree
8-
{
9-
}
10+
// define LIB_INTERVAL_TREE_DEPRECATED if __attribute__((deprecated)) is not supported
11+
#ifndef LIB_INTERVAL_TREE_DEPRECATED
12+
# if __has_cpp_attribute(depreacted)
13+
# define LIB_INTERVAL_TREE_DEPRECATED [[deprecated]]
14+
# else
15+
# define LIB_INTERVAL_TREE_DEPRECATED __attribute__((deprecated))
16+
# endif
17+
#endif
18+
19+
#ifndef LIB_INTERVAL_TREE_FALLTHROUGH
20+
# if __has_cpp_attribute(fallthrough)
21+
# define LIB_INTERVAL_TREE_FALLTHROUGH [[fallthrough]]
22+
# else
23+
# define LIB_INTERVAL_TREE_FALLTHROUGH __attribute__((fallthrough))
24+
# endif
25+
#endif

0 commit comments

Comments
 (0)