Skip to content

Commit

Permalink
Fixed merge issue and added simple tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Aug 22, 2021
2 parents 7e6fa11 + 9a0e504 commit f5bff66
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 41 deletions.
46 changes: 10 additions & 36 deletions interval_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,22 +1125,15 @@ namespace lib_interval_tree
if (ptr->left_ && ival.high() <= ptr->left_->max())
{
// no right? can only continue left
if (!ptr->right_)
return find_all_i<IteratorT>(ptr->left_, ival, on_find, compare);

// upper bounds higher than what is contained right? continue left
if (ival.high() > ptr->right_->max())
if (!ptr->right_ || ival.low() > ptr->right_->max())
return find_all_i<IteratorT>(ptr->left_, ival, on_find, compare);

if (!find_all_i<IteratorT>(ptr->left_, ival, on_find, compare))
return false;
}
if (ptr->right_ && ival.high() <= ptr->right_->max())
{
if (!ptr->left_)
return find_all_i<IteratorT>(ptr->right_, ival, on_find, compare);

if (ival.high() > ptr->left_->max())
if (!ptr->left_ || ival.low() > ptr->left_->max())
return find_all_i<IteratorT>(ptr->right_, ival, on_find, compare);

if (!find_all_i<IteratorT>(ptr->right_, ival, on_find, compare))
Expand All @@ -1165,11 +1158,7 @@ namespace lib_interval_tree
if (ptr->left_ && ival.high() <= ptr->left_->max())
{
// no right? can only continue left
if (!ptr->right_)
return find_i(ptr->left_, ival, compare);

// upper bounds higher than what is contained right? continue left
if (ival.high() > ptr->right_->max())
if (!ptr->right_ || ival.low() > ptr->right_->max())
return find_i(ptr->left_, ival, compare);

auto* res = find_i(ptr->left_, ival, compare);
Expand All @@ -1178,10 +1167,7 @@ namespace lib_interval_tree
}
if (ptr->right_ && ival.high() <= ptr->right_->max())
{
if (!ptr->left_)
return find_i(ptr->right_, ival, compare);

if (ival.high() > ptr->left_->max())
if (!ptr->left_ || ival.low() > ptr->left_->max())
return find_i(ptr->right_, ival, compare);

auto* res = find_i(ptr->right_, ival, compare);
Expand Down Expand Up @@ -1242,22 +1228,16 @@ namespace lib_interval_tree
if (ptr->left_ && ptr->left_->max() >= ival.low())
{
// no right? can only continue left
if (!ptr->right_)
return overlap_find_all_i<Exclusive, IteratorT>(ptr->left_, ival, on_find);

// upper bounds higher than what is contained right? continue left
if (ival.high() > ptr->right_->max())
// or interval low is bigger than max of right branch.
if (!ptr->right_ || ival.low() > ptr->right_->max())
return overlap_find_all_i<Exclusive, IteratorT>(ptr->left_, ival, on_find);

if (!overlap_find_all_i<Exclusive, IteratorT>(ptr->left_, ival, on_find))
return false;
}
if (ptr->right_ && ptr->right_->max() >= ival.low())
{
if (!ptr->left_)
return overlap_find_all_i<Exclusive, IteratorT>(ptr->right_, ival, on_find);

if (ival.high() > ptr->left_->max())
if (!ptr->left_ || ival.low() > ptr->right_->max())
return overlap_find_all_i<Exclusive, IteratorT>(ptr->right_, ival, on_find);

if (!overlap_find_all_i<Exclusive, IteratorT>(ptr->right_, ival, on_find))
Expand All @@ -1273,11 +1253,8 @@ namespace lib_interval_tree
if (ptr->left_ && ptr->left_->max() >= ival.low())
{
// no right? can only continue left
if (!ptr->right_)
return overlap_find_i<Exclusive>(ptr->left_, ival);

// upper bounds higher than what is contained right? continue left
if (ival.high() > ptr->right_->max())
// or upper bounds higher than what is contained right? continue left.
if (!ptr->right_ || ival.low() > ptr->right_->max())
return overlap_find_i<Exclusive>(ptr->left_, ival);

auto* res = overlap_find_i<Exclusive>(ptr->left_, ival);
Expand All @@ -1286,10 +1263,7 @@ namespace lib_interval_tree
}
if (ptr->right_ && ptr->right_->max() >= ival.low())
{
if (!ptr->left_)
return overlap_find_i<Exclusive>(ptr->right_, ival);

if (ival.high() > ptr->left_->max())
if (!ptr->left_ || ival.low() > ptr->left_->max())
return overlap_find_i<Exclusive>(ptr->right_, ival);

auto* res = overlap_find_i<Exclusive>(ptr->right_, ival);
Expand Down
6 changes: 4 additions & 2 deletions tests/find_tests.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <ctime>
#include <random>
#include <cmath>

class FindTests
: public ::testing::Test
{
Expand All @@ -8,7 +12,6 @@ class FindTests
protected:
IntervalTypes <int>::tree_type tree;
std::default_random_engine gen;
std::uniform_int_distribution <int> distSmall{-500, 500};
std::uniform_int_distribution <int> distLarge{-50000, 50000};
};

Expand Down Expand Up @@ -134,4 +137,3 @@ TEST_F(FindTests, CanFindAllElementsBackInStrictlyAscendingOverlappingIntervals)
ASSERT_NE(tree.find(ival), std::end(tree));
}
}

64 changes: 64 additions & 0 deletions tests/float_overlap_tests.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include "test_utility.hpp"

#include <algorithm>

class FloatOverlapFindTests
: public ::testing::Test
{
public:
using types = IntervalTypes <double>;
protected:
IntervalTypes <double>::tree_type tree;
};

TEST_F(FloatOverlapFindTests, FloatOverlapTest)
{
tree.insert(lib_interval_tree::make_safe_interval<double>(-1.483529864195180e+00, -1.296053859335657e+00));
tree.insert(lib_interval_tree::make_safe_interval<double>(-1.308996938995747e+00, -1.127801743538376e+00));
tree.insert(lib_interval_tree::make_safe_interval<double>(-1.134464013796314e+00, -9.562870818388700e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(-9.599310885968813e-01, -7.834918877708545e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(-7.853981633974484e-01, -6.090750919515169e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(-6.108652381980154e-01, -4.348738075675338e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(-4.363323129985824e-01, -2.608478200480425e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(-2.617993877991495e-01, -8.693606119038631e-02));
tree.insert(lib_interval_tree::make_safe_interval<double>(-8.726646259971654e-02, 8.726646259971654e-02));
tree.insert(lib_interval_tree::make_safe_interval<double>(8.693606119038631e-02, 2.617993877991493e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(2.608478200480422e-01, 4.363323129985823e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(4.348738075675337e-01, 6.108652381980154e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(6.090750919515169e-01, 7.853981633974484e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(7.834918877708545e-01, 9.599310885968813e-01));
tree.insert(lib_interval_tree::make_safe_interval<double>(9.562870818388700e-01, 1.134464013796314e+00)); //
tree.insert(lib_interval_tree::make_safe_interval<double>(1.127801743538376e+00, 1.308996938995747e+00)); //
tree.insert(lib_interval_tree::make_safe_interval<double>(1.296053859335657e+00, 1.483529864195180e+00)); //

double lat0 = 1.040893537045970;
double lat1 = 1.570796326794897;

std::vector <std::pair<double, double>> vecOverlapsA;
lib_interval_tree::interval <double> intSource({lat0, lat1});
for (auto const& iter : tree)
{
if (iter.overlaps(intSource))
vecOverlapsA.push_back({iter.low(), iter.high()});
}

std::vector <std::pair<double, double>> vecOverlapsB;
tree.overlap_find_all
(
{lat0, lat1},
[&vecOverlapsB](lib_interval_tree::interval_tree_t<double>::iterator iter)
{
vecOverlapsB.push_back({iter->low(), iter->high()});
return true;
},
false
);

std::sort(std::begin(vecOverlapsA), std::end(vecOverlapsA));
std::sort(std::begin(vecOverlapsB), std::end(vecOverlapsB));

ASSERT_EQ(vecOverlapsA.size(), vecOverlapsB.size());
EXPECT_THAT(vecOverlapsA, ::testing::ContainerEq(vecOverlapsB));
}
7 changes: 4 additions & 3 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

#include "../interval_tree.hpp"
#include "typedefs.hpp"
#include "example_drawings.hpp"
//#include "example_drawings.hpp"

// following headers expect to be included after gtest headers and interval_tree
#include "interval_tests.hpp"
#include "insert_tests.hpp"
#include "erase_tests.hpp"
#include "find_tests.hpp"
#include "overlap_find_tests.hpp"

#include "overlap_find_tests.hpp"
#include "float_overlap_tests.hpp"

int main(int argc, char** argv)
{
#ifdef INTERVAL_TREE_DO_DRAWINGS
Expand Down

0 comments on commit f5bff66

Please sign in to comment.