@@ -37,13 +37,19 @@ namespace lib_interval_tree
3737 * Constructs an interval. low MUST be smaller than high.
3838 */
3939#ifndef INTERVAL_TREE_SAFE_INTERVALS
40+ #if __cplusplus >= 201703L
41+ constexpr
42+ #endif
4043 interval (value_type low, value_type high)
4144 : low_{low}
4245 , high_{high}
4346 {
4447 assert (low <= high);
4548 }
4649#else
50+ #if __cplusplus >= 201703L
51+ constexpr
52+ #endif
4753 interval (value_type low, value_type high)
4854 : low_{std::min (low, high)}
4955 , high_{std::max (low, high)}
@@ -173,6 +179,9 @@ namespace lib_interval_tree
173179 * Creates a safe interval that puts the lower bound left automatically.
174180 */
175181 template <typename numerical_type, typename interval_kind_ = closed>
182+ #if __cplusplus >= 201703L
183+ constexpr
184+ #endif
176185 interval <numerical_type, interval_kind_> make_safe_interval (numerical_type lhs, numerical_type rhs)
177186 {
178187 return interval <numerical_type, interval_kind_>{std::min (lhs, rhs), std::max (lhs, rhs)};
@@ -621,6 +630,7 @@ namespace lib_interval_tree
621630 using iterator = interval_tree_iterator <node_type>;
622631 using const_iterator = const_interval_tree_iterator <node_type>;
623632 using size_type = long long ;
633+ using this_type = interval_tree<interval_type>;
624634
625635 public:
626636 friend const_interval_tree_iterator <node_type>;
@@ -854,14 +864,14 @@ namespace lib_interval_tree
854864 {
855865 if (root_ == nullptr )
856866 return ;
857- find_all_i<iterator>(root_, ival, on_find, compare);
867+ find_all_i<this_type, iterator>(this , root_, ival, on_find, compare);
858868 }
859869 template <typename FunctionT, typename CompareFunctionT>
860870 void find_all (interval_type const & ival, FunctionT const & on_find, CompareFunctionT const & compare) const
861871 {
862872 if (root_ == nullptr )
863873 return ;
864- find_all_i<const_iterator>(root_, ival, on_find, compare);
874+ find_all_i<this_type, const_iterator>(this , root_, ival, on_find, compare);
865875 }
866876
867877 template <typename FunctionT>
@@ -950,19 +960,19 @@ namespace lib_interval_tree
950960 if (root_ == nullptr )
951961 return ;
952962 if (exclusive)
953- overlap_find_all_i<true , iterator>(root_, ival, on_find);
963+ overlap_find_all_i<this_type, true , iterator>(this , root_, ival, on_find);
954964 else
955- overlap_find_all_i<false , iterator>(root_, ival, on_find);
965+ overlap_find_all_i<this_type, false , iterator>(this , root_, ival, on_find);
956966 }
957967 template <typename FunctionT>
958968 void overlap_find_all (interval_type const & ival, FunctionT const & on_find, bool exclusive = false ) const
959969 {
960970 if (root_ == nullptr )
961971 return ;
962972 if (exclusive)
963- 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);
964974 else
965- 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);
966976 }
967977
968978 /* *
@@ -1114,36 +1124,43 @@ namespace lib_interval_tree
11141124 return nullptr ;
11151125 };
11161126
1117- template <typename IteratorT, typename FunctionT, typename ComparatorFunctionT>
1118- 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+ )
11191136 {
11201137 if (compare (ptr->interval (), ival))
11211138 {
1122- if (!on_find (IteratorT{ptr, this }))
1139+ if (!on_find (IteratorT{ptr, self }))
11231140 return false ;
11241141 }
11251142 if (ptr->left_ && ival.high () <= ptr->left_ ->max ())
11261143 {
11271144 // no right? can only continue left
11281145 if (!ptr->right_ || ival.low () > ptr->right_ ->max ())
1129- 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);
11301147
1131- 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))
11321149 return false ;
11331150 }
11341151 if (ptr->right_ && ival.high () <= ptr->right_ ->max ())
11351152 {
11361153 if (!ptr->left_ || ival.low () > ptr->left_ ->max ())
1137- 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);
11381155
1139- 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))
11401157 return false ;
11411158 }
11421159 return true ;
11431160 }
11441161
11451162 template <typename ComparatorFunctionT>
1146- 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
11471164 {
11481165 if (compare (ptr->interval (), ival))
11491166 return ptr;
@@ -1153,7 +1170,7 @@ namespace lib_interval_tree
11531170
11541171 // excludes ptr
11551172 template <typename ComparatorFunctionT>
1156- 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
11571174 {
11581175 if (ptr->left_ && ival.high () <= ptr->left_ ->max ())
11591176 {
@@ -1178,7 +1195,7 @@ namespace lib_interval_tree
11781195 }
11791196
11801197 template <bool Exclusive>
1181- 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
11821199 {
11831200#if __cplusplus >= 201703L
11841201 if constexpr (Exclusive)
@@ -1198,8 +1215,14 @@ namespace lib_interval_tree
11981215 return overlap_find_i_ex<Exclusive>(ptr, ival);
11991216 }
12001217
1201- template <bool Exclusive, typename IteratorT, typename FunctionT>
1202- 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+ )
12031226 {
12041227#if __cplusplus >= 201703L
12051228 if constexpr (Exclusive)
@@ -1209,7 +1232,7 @@ namespace lib_interval_tree
12091232 {
12101233 if (ptr->interval ().overlaps_exclusive (ival))
12111234 {
1212- if (!on_find (IteratorT{ptr, this }))
1235+ if (!on_find (IteratorT{ptr, self }))
12131236 {
12141237 return false ;
12151238 }
@@ -1219,7 +1242,7 @@ namespace lib_interval_tree
12191242 {
12201243 if (ptr->interval ().overlaps (ival))
12211244 {
1222- if (!on_find (IteratorT{ptr, this }))
1245+ if (!on_find (IteratorT{ptr, self }))
12231246 {
12241247 return false ;
12251248 }
@@ -1230,25 +1253,25 @@ namespace lib_interval_tree
12301253 // no right? can only continue left
12311254 // or interval low is bigger than max of right branch.
12321255 if (!ptr->right_ || ival.low () > ptr->right_ ->max ())
1233- 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);
12341257
1235- 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))
12361259 return false ;
12371260 }
12381261 if (ptr->right_ && ptr->right_ ->max () >= ival.low ())
12391262 {
12401263 if (!ptr->left_ || ival.low () > ptr->right_ ->max ())
1241- 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);
12421265
1243- 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))
12441267 return false ;
12451268 }
12461269 return true ;
12471270 }
12481271
12491272 // excludes ptr
12501273 template <bool Exclusive>
1251- 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
12521275 {
12531276 if (ptr->left_ && ptr->left_ ->max () >= ival.low ())
12541277 {
0 commit comments