Skip to content

Commit f9996e4

Browse files
author
Tim Ebbeke
committed
improved const correctness on find methods
1 parent b043ee4 commit f9996e4

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

interval_tree.hpp

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ namespace lib_interval_tree
630630
using iterator = interval_tree_iterator <node_type>;
631631
using const_iterator = const_interval_tree_iterator <node_type>;
632632
using size_type = long long;
633+
using this_type = interval_tree<interval_type>;
633634

634635
public:
635636
friend const_interval_tree_iterator <node_type>;
@@ -863,14 +864,14 @@ namespace lib_interval_tree
863864
{
864865
if (root_ == nullptr)
865866
return;
866-
find_all_i<iterator>(root_, ival, on_find, compare);
867+
find_all_i<this_type, iterator>(this, root_, ival, on_find, compare);
867868
}
868869
template <typename FunctionT, typename CompareFunctionT>
869870
void find_all(interval_type const& ival, FunctionT const& on_find, CompareFunctionT const& compare) const
870871
{
871872
if (root_ == nullptr)
872873
return;
873-
find_all_i<const_iterator>(root_, ival, on_find, compare);
874+
find_all_i<this_type, const_iterator>(this, root_, ival, on_find, compare);
874875
}
875876

876877
template <typename FunctionT>
@@ -959,19 +960,19 @@ namespace lib_interval_tree
959960
if (root_ == nullptr)
960961
return;
961962
if (exclusive)
962-
overlap_find_all_i<true, iterator>(root_, ival, on_find);
963+
overlap_find_all_i<this_type, true, iterator>(this, root_, ival, on_find);
963964
else
964-
overlap_find_all_i<false, iterator>(root_, ival, on_find);
965+
overlap_find_all_i<this_type, false, iterator>(this, root_, ival, on_find);
965966
}
966967
template <typename FunctionT>
967968
void overlap_find_all(interval_type const& ival, FunctionT const& on_find, bool exclusive = false) const
968969
{
969970
if (root_ == nullptr)
970971
return;
971972
if (exclusive)
972-
overlap_find_all_i<true, const_iterator>(root_, ival, on_find);
973+
overlap_find_all_i<this_type, true, const_iterator>(this, root_, ival, on_find);
973974
else
974-
overlap_find_all_i<false, const_iterator>(root_, ival, on_find);
975+
overlap_find_all_i<this_type, false, const_iterator>(this, root_, ival, on_find);
975976
}
976977

977978
/**
@@ -1123,36 +1124,43 @@ namespace lib_interval_tree
11231124
return nullptr;
11241125
};
11251126

1126-
template <typename IteratorT, typename FunctionT, typename ComparatorFunctionT>
1127-
bool find_all_i(node_type* ptr, interval_type const& ival, FunctionT const& on_find, ComparatorFunctionT const& compare)
1127+
template <typename ThisType, typename IteratorT, typename FunctionT, typename ComparatorFunctionT>
1128+
static bool find_all_i
1129+
(
1130+
typename std::conditional<std::is_same<IteratorT, iterator>::value, ThisType, ThisType const>::type* self,
1131+
node_type* ptr,
1132+
interval_type const& ival,
1133+
FunctionT const& on_find,
1134+
ComparatorFunctionT const& compare
1135+
)
11281136
{
11291137
if (compare(ptr->interval(), ival))
11301138
{
1131-
if (!on_find(IteratorT{ptr, this}))
1139+
if (!on_find(IteratorT{ptr, self}))
11321140
return false;
11331141
}
11341142
if (ptr->left_ && ival.high() <= ptr->left_->max())
11351143
{
11361144
// no right? can only continue left
11371145
if (!ptr->right_ || ival.low() > ptr->right_->max())
1138-
return find_all_i<IteratorT>(ptr->left_, ival, on_find, compare);
1146+
return find_all_i<ThisType, IteratorT>(self, ptr->left_, ival, on_find, compare);
11391147

1140-
if (!find_all_i<IteratorT>(ptr->left_, ival, on_find, compare))
1148+
if (!find_all_i<ThisType, IteratorT>(self, ptr->left_, ival, on_find, compare))
11411149
return false;
11421150
}
11431151
if (ptr->right_ && ival.high() <= ptr->right_->max())
11441152
{
11451153
if (!ptr->left_ || ival.low() > ptr->left_->max())
1146-
return find_all_i<IteratorT>(ptr->right_, ival, on_find, compare);
1154+
return find_all_i<ThisType, IteratorT>(self, ptr->right_, ival, on_find, compare);
11471155

1148-
if (!find_all_i<IteratorT>(ptr->right_, ival, on_find, compare))
1156+
if (!find_all_i<ThisType, IteratorT>(self, ptr->right_, ival, on_find, compare))
11491157
return false;
11501158
}
11511159
return true;
11521160
}
11531161

11541162
template <typename ComparatorFunctionT>
1155-
node_type* find_i(node_type* ptr, interval_type const& ival, ComparatorFunctionT const& compare)
1163+
node_type* find_i(node_type* ptr, interval_type const& ival, ComparatorFunctionT const& compare) const
11561164
{
11571165
if (compare(ptr->interval(), ival))
11581166
return ptr;
@@ -1162,7 +1170,7 @@ namespace lib_interval_tree
11621170

11631171
// excludes ptr
11641172
template <typename ComparatorFunctionT>
1165-
node_type* find_i_ex(node_type* ptr, interval_type const& ival, ComparatorFunctionT const& compare)
1173+
node_type* find_i_ex(node_type* ptr, interval_type const& ival, ComparatorFunctionT const& compare) const
11661174
{
11671175
if (ptr->left_ && ival.high() <= ptr->left_->max())
11681176
{
@@ -1187,7 +1195,7 @@ namespace lib_interval_tree
11871195
}
11881196

11891197
template <bool Exclusive>
1190-
node_type* overlap_find_i(node_type* ptr, interval_type const& ival)
1198+
node_type* overlap_find_i(node_type* ptr, interval_type const& ival) const
11911199
{
11921200
#if __cplusplus >= 201703L
11931201
if constexpr (Exclusive)
@@ -1207,8 +1215,14 @@ namespace lib_interval_tree
12071215
return overlap_find_i_ex<Exclusive>(ptr, ival);
12081216
}
12091217

1210-
template <bool Exclusive, typename IteratorT, typename FunctionT>
1211-
bool overlap_find_all_i(node_type* ptr, interval_type const& ival, FunctionT const& on_find)
1218+
template <typename ThisType, bool Exclusive, typename IteratorT, typename FunctionT>
1219+
static bool overlap_find_all_i
1220+
(
1221+
typename std::conditional<std::is_same<IteratorT, iterator>::value, ThisType, ThisType const>::type* self,
1222+
node_type* ptr,
1223+
interval_type const& ival,
1224+
FunctionT const& on_find
1225+
)
12121226
{
12131227
#if __cplusplus >= 201703L
12141228
if constexpr (Exclusive)
@@ -1218,7 +1232,7 @@ namespace lib_interval_tree
12181232
{
12191233
if (ptr->interval().overlaps_exclusive(ival))
12201234
{
1221-
if (!on_find(IteratorT{ptr, this}))
1235+
if (!on_find(IteratorT{ptr, self}))
12221236
{
12231237
return false;
12241238
}
@@ -1228,7 +1242,7 @@ namespace lib_interval_tree
12281242
{
12291243
if (ptr->interval().overlaps(ival))
12301244
{
1231-
if (!on_find(IteratorT{ptr, this}))
1245+
if (!on_find(IteratorT{ptr, self}))
12321246
{
12331247
return false;
12341248
}
@@ -1239,25 +1253,25 @@ namespace lib_interval_tree
12391253
// no right? can only continue left
12401254
// or interval low is bigger than max of right branch.
12411255
if (!ptr->right_ || ival.low() > ptr->right_->max())
1242-
return overlap_find_all_i<Exclusive, IteratorT>(ptr->left_, ival, on_find);
1256+
return overlap_find_all_i<ThisType, Exclusive, IteratorT>(self, ptr->left_, ival, on_find);
12431257

1244-
if (!overlap_find_all_i<Exclusive, IteratorT>(ptr->left_, ival, on_find))
1258+
if (!overlap_find_all_i<ThisType, Exclusive, IteratorT>(self, ptr->left_, ival, on_find))
12451259
return false;
12461260
}
12471261
if (ptr->right_ && ptr->right_->max() >= ival.low())
12481262
{
12491263
if (!ptr->left_ || ival.low() > ptr->right_->max())
1250-
return overlap_find_all_i<Exclusive, IteratorT>(ptr->right_, ival, on_find);
1264+
return overlap_find_all_i<ThisType, Exclusive, IteratorT>(self, ptr->right_, ival, on_find);
12511265

1252-
if (!overlap_find_all_i<Exclusive, IteratorT>(ptr->right_, ival, on_find))
1266+
if (!overlap_find_all_i<ThisType, Exclusive, IteratorT>(self, ptr->right_, ival, on_find))
12531267
return false;
12541268
}
12551269
return true;
12561270
}
12571271

12581272
// excludes ptr
12591273
template <bool Exclusive>
1260-
node_type* overlap_find_i_ex(node_type* ptr, interval_type const& ival)
1274+
node_type* overlap_find_i_ex(node_type* ptr, interval_type const& ival) const
12611275
{
12621276
if (ptr->left_ && ptr->left_->max() >= ival.low())
12631277
{

0 commit comments

Comments
 (0)