Skip to content

Commit

Permalink
Merge pull request #35 from 5cript/feat/interval-kinds
Browse files Browse the repository at this point in the history
Made interval types meaningful.
  • Loading branch information
5cript authored Aug 2, 2024
2 parents a661ab1 + 9f9bad2 commit ba04ef5
Show file tree
Hide file tree
Showing 10 changed files with 1,342 additions and 156 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
uses: actions/checkout@v2

- name: CMake Configure
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 }}
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 }}

- name: Build
run: cmake --build ${{github.workspace}}/build
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ if(INT_TREE_DRAW_EXAMPLES)
endif()
if (INT_TREE_ENABLE_TESTS)
add_subdirectory(tests)
endif()
if (INT_TREE_BUILD_EXAMPLES)
add_subdirectory(example)
endif()
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ int main()
{
using namespace lib_interval_tree;

// interval_tree <interval <int>>;
interval_tree_t <int> tree;
// interval_tree<interval<int>>; // closed by default
// interval_tree<interval<int, open>>;
// interval_tree<interval<int, closed>>;
// interval_tree<interval<int, left_open>>;
// interval_tree<interval<int, right_open>>;
// interval_tree<interval<int, closed_adjacent>>; // counts adjacent intervals as overlapping
interval_tree_t<int> tree;

tree.insert(make_safe_interval<int>(21, 16)); // make_safe_interval swaps low and high if not in right order.
tree.insert({8, 9});
Expand All @@ -42,6 +47,12 @@ int main()
{
std::cout << "[" << i.low() << ", " << i.high() << "]\n";
}

using lib_interval_tree::open;
// dynamic has some logic overhead.
interval_tree<interval<int, dynamic>> dynamicIntervals;
dynamicIntervals.insert({0, 1, interval_border::closed, interval_border::open});
dynamicIntervals.insert({7, 5, interval_border::open, interval_border::closed_adjacent});
}
```

Expand Down Expand Up @@ -315,7 +326,18 @@ Returns a past the end const_iterator in reverse.
**Returns**: past the end const_iterator.

## Members of Interval
___You can implement your own interval if you provide all the same functions.___
___You can implement your own interval if you provide the same functions, except (within, operator-, size, operator!=).___

There are 6 types of intervals:
- open: (a, b)
- left_open: (a, b]
- right_open: [a, b)
- closed: [a, b]
- closed_adjacent: [a, b] (counts adjacent intervals as overlapping)
- dynamic: Can be any of the above, depending on the input. Not supported for floating point.

Which can be picked with the second template parameter of interval:
`lib_interval_tree::interval<int, lib_interval_tree::open>`

- [Members of Interval](#members-of-interval)
- [using value_type](#using-value_type)
Expand All @@ -324,7 +346,7 @@ ___You can implement your own interval if you provide all the same functions.___
- [friend bool operator!=(interval const& lhs, interval const& other)](#friend-bool-operatorinterval-const-lhs-interval-const-other-1)
- [value_type low() const](#value_type-low-const)
- [value_type high() const](#value_type-high-const)
- [bool overlaps(value_type l, value_type h) const](#bool-overlapsvalue_type-l-value_type-h-const)
- [\[\[deprecated\]\] bool overlaps(value_type l, value_type h) const](#bool-overlapsvalue_type-l-value_type-h-const)
- [bool overlaps_exclusive(value_type l, value_type h) const](#bool-overlaps_exclusivevalue_type-l-value_type-h-const)
- [bool overlaps(interval const& other) const](#bool-overlapsinterval-const-other-const)
- [bool overlaps_exclusive(interval const& other) const](#bool-overlaps_exclusiveinterval-const-other-const)
Expand All @@ -346,22 +368,23 @@ Comparison operator.
Lower bound.
### value_type high() const
Upper bound.
### bool overlaps(value_type l, value_type h) const
### \[\[deprecated\]\] bool overlaps(value_type l, value_type h) const
Overlap these bounds with this interval (closed)?
Is deprecated because the overlapping only works with closed intervals.
### bool overlaps_exclusive(value_type l, value_type h) const
Overlap these bounds with this interval excluding borders?
### bool overlaps(interval const& other) const
Like overlaps with lower and upper bound.
### bool overlaps_exclusive(interval const& other) const
Like overlaps with lower and upper bound.
### bool within(value_type value) const
Is the value within the interval (closed)?
Is the value within the interval?
### bool within(interval const& other) const
Is the interval within the interval?
### value_type operator-(interval const& other) const
Calculates the distance between the two intervals.
Overlapping intervals have 0 distance.
### value_type size() const
Returns high - low.
Returns The amount of elements in the interval when integral, or the distance between the 2 bounds when floating point.
### interval join(interval const& other) const
Joins 2 intervals and whatever is inbetween.
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(from_readme)
7 changes: 7 additions & 0 deletions example/from_readme/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_executable(from_readme main.cpp)
target_link_libraries(from_readme interval-tree)

set_target_properties(from_readme
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/interval_tree/examples"
)
39 changes: 39 additions & 0 deletions example/from_readme/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// #include <interval-tree/draw.hpp> // to draw tree. this is not header only anymore.
#include <interval-tree/interval_tree.hpp>

int main()
{
using namespace lib_interval_tree;

// interval_tree<interval<int>>; // closed by default
// interval_tree<interval<int, open>>;
// interval_tree<interval<int, closed>>;
// interval_tree<interval<int, left_open>>;
// interval_tree<interval<int, right_open>>;
// interval_tree<interval<int, closed_adjacent>>; // counts adjacent intervals as overlapping
interval_tree_t<int> tree;

tree.insert(make_safe_interval<int>(21, 16)); // make_safe_interval swaps low and high if not in right order.
tree.insert({8, 9});
tree.insert({25, 30});
tree.insert({5, 8});
tree.insert({15, 23});
tree.insert({17, 19});
tree.insert({26, 26});
tree.insert({0, 3});
tree.insert({6, 10});
tree.insert({19, 20});

tree.deoverlap();

for (auto const& i : tree)
{
std::cout << "[" << i.low() << ", " << i.high() << "]\n";
}

using lib_interval_tree::open;
// dynamic has some logic overhead.
interval_tree<interval<int, dynamic>> dynamicIntervals;
dynamicIntervals.insert({0, 1, interval_border::closed, interval_border::open});
dynamicIntervals.insert({7, 5, interval_border::open, interval_border::closed_adjacent});
}
26 changes: 21 additions & 5 deletions include/interval-tree/feature_test.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#pragma once

#if defined(__cpp_concepts) && __cpp_concepts >= 202002L
# define LIB_INTERVAL_TREE_CONCEPTS
// define LIB_INTERVAL_TREE_CONCEPTS to override the concepts feature test
#ifndef LIB_INTERVAL_TREE_CONCEPTS
# if defined(__cpp_concepts) && __cpp_concepts >= 202002L
# define LIB_INTERVAL_TREE_CONCEPTS
# endif
#endif

namespace lib_interval_tree
{
}
// define LIB_INTERVAL_TREE_DEPRECATED if __attribute__((deprecated)) is not supported
#ifndef LIB_INTERVAL_TREE_DEPRECATED
# if __has_cpp_attribute(depreacted)
# define LIB_INTERVAL_TREE_DEPRECATED [[deprecated]]
# else
# define LIB_INTERVAL_TREE_DEPRECATED __attribute__((deprecated))
# endif
#endif

#ifndef LIB_INTERVAL_TREE_FALLTHROUGH
# if __has_cpp_attribute(fallthrough)
# define LIB_INTERVAL_TREE_FALLTHROUGH [[fallthrough]]
# else
# define LIB_INTERVAL_TREE_FALLTHROUGH __attribute__((fallthrough))
# endif
#endif
Loading

0 comments on commit ba04ef5

Please sign in to comment.