Skip to content

Commit 7783d85

Browse files
committed
Fixed interval kinds for floating point.
1 parent 9188575 commit 7783d85

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ There are 6 types of intervals:
323323
- right_open: [a, b)
324324
- closed: [a, b]
325325
- closed_adjacent: [a, b] (counts adjacent intervals as overlapping)
326-
- dynamic: Can be any of the above, depending on the input.
326+
- dynamic: Can be any of the above, depending on the input. Not supported for floating point.
327327

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

include/interval-tree/interval_types.hpp

+43-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
#include "feature_test.hpp"
44

5-
#ifdef LIB_INTERVAL_TREE_CONCEPTS
6-
# include <type_traits>
7-
#endif
8-
95
#include <cmath>
106
#include <algorithm>
7+
#include <utility>
8+
#include <type_traits>
119

1210
namespace lib_interval_tree
1311
{
@@ -69,10 +67,23 @@ namespace lib_interval_tree
6967
}
7068

7169
template <typename numerical_type>
72-
static inline numerical_type size(numerical_type low, numerical_type high)
70+
static inline typename std::enable_if<!std::is_floating_point<numerical_type>::value, numerical_type>::type
71+
size(numerical_type low, numerical_type high)
7372
{
7473
return high - low + 1;
7574
}
75+
76+
template <typename numerical_type>
77+
#ifdef LIB_INTERVAL_TREE_CONCEPTS
78+
requires std::is_floating_point_v<numerical_type>
79+
static inline numerical_type
80+
#else
81+
static inline typename std::enable_if<std::is_floating_point<numerical_type>::value, numerical_type>::type
82+
#endif
83+
size(numerical_type low, numerical_type high)
84+
{
85+
return high - low;
86+
}
7687
};
7788
// ()
7889
struct open
@@ -90,27 +101,49 @@ namespace lib_interval_tree
90101
}
91102

92103
template <typename numerical_type>
93-
static inline numerical_type size(numerical_type low, numerical_type high)
104+
static inline typename std::enable_if<!std::is_floating_point<numerical_type>::value, numerical_type>::type
105+
size(numerical_type low, numerical_type high)
94106
{
95107
return high - low - 1;
96108
}
109+
110+
template <typename numerical_type>
111+
#ifdef LIB_INTERVAL_TREE_CONCEPTS
112+
requires std::is_floating_point_v<numerical_type>
113+
static inline numerical_type
114+
#else
115+
static inline typename std::enable_if<std::is_floating_point<numerical_type>::value, numerical_type>::type
116+
#endif
117+
size(numerical_type low, numerical_type high)
118+
{
119+
return high - low;
120+
}
97121
};
98122
/// [] and adjacent counts as overlapping
99123
struct closed_adjacent
100124
{
101125
template <typename numerical_type>
126+
#ifdef LIB_INTERVAL_TREE_CONCEPTS
127+
requires std::is_integral_v<numerical_type>
128+
#endif
102129
static inline bool within(numerical_type low, numerical_type high, numerical_type p)
103130
{
104131
return (low <= p) && (p <= high);
105132
}
106133

107134
template <typename numerical_type>
135+
#ifdef LIB_INTERVAL_TREE_CONCEPTS
136+
requires std::is_integral_v<numerical_type>
137+
#endif
108138
static inline bool overlaps(numerical_type l1, numerical_type h1, numerical_type l2, numerical_type h2)
109139
{
110140
return (l1 <= (h2 + 1)) && ((l2 - 1) <= h1);
111141
}
112142

113143
template <typename numerical_type>
144+
#ifdef LIB_INTERVAL_TREE_CONCEPTS
145+
requires std::is_integral_v<numerical_type>
146+
#endif
114147
static inline numerical_type size(numerical_type low, numerical_type high)
115148
{
116149
return high - low + 1;
@@ -123,6 +156,10 @@ namespace lib_interval_tree
123156
open,
124157
closed_adjacent
125158
};
159+
160+
/**
161+
* @brief Do not use for floating point types
162+
*/
126163
class dynamic
127164
{
128165
public:

0 commit comments

Comments
 (0)