14 template <
class Scalar,
typename Value>
26 template <
class Scalar,
typename Value>
31 template <
class Scalar,
typename Value>
36 template <
class Scalar,
typename Value>
38 out <<
"Interval(" << i.
start <<
", " << i.
stop <<
"): " << i.
value;
42 template <
class Scalar,
class Value>
67 std::unique_ptr<IntervalTree>
clone()
const {
68 return std::unique_ptr<IntervalTree>(
new IntervalTree(*
this));
89 std::size_t maxbucket = 512, Scalar leftextent = 0, Scalar rightextent = 0)
93 const auto minmaxStop = std::minmax_element(ivals.begin(), ivals.end(), IntervalStopCmp());
94 const auto minmaxStart = std::minmax_element(ivals.begin(), ivals.end(), IntervalStartCmp());
96 center = (minmaxStart.first->start + minmaxStop.second->stop) / 2;
98 if (leftextent == 0 && rightextent == 0) {
100 std::sort(ivals.begin(), ivals.end(), IntervalStartCmp());
102 assert(std::is_sorted(ivals.begin(), ivals.end(), IntervalStartCmp()));
104 if (depth == 0 || (ivals.size() < minbucket && ivals.size() < maxbucket)) {
105 std::sort(ivals.begin(), ivals.end(), IntervalStartCmp());
113 if (leftextent || rightextent) {
115 rightp = rightextent;
117 leftp = ivals.front().start;
118 rightp = std::max_element(ivals.begin(), ivals.end(), IntervalStopCmp())->stop;
124 for (
typename interval_vector::const_iterator i = ivals.begin(); i != ivals.end(); ++i) {
137 if (!lefts.empty()) {
140 if (!rights.empty()) {
149 template <
class UnaryFunction>
150 void visit_near(
const Scalar& start,
const Scalar& stop, UnaryFunction f)
const {
165 template <
class UnaryFunction>
171 template <
class UnaryFunction>
183 template <
class UnaryFunction>
184 void visit_contained(
const Scalar& start,
const Scalar& stop, UnaryFunction f)
const {
222 template <
class UnaryFunction>
233 std::pair<Scalar, Scalar>
extent()
const {
235 std::pair<Scalar, Scalar>
x{std::numeric_limits<Scalar>::max(),
236 std::numeric_limits<Scalar>::min()};
252 std::pair<bool, std::pair<Scalar, Scalar>>
is_valid()
const {
253 const auto minmaxStop =
255 const auto minmaxStart =
258 std::pair<bool, std::pair<Scalar, Scalar>> result = {
259 true, {std::numeric_limits<Scalar>::max(), std::numeric_limits<Scalar>::min()}};
261 result.second.first = std::min(result.second.first, minmaxStart.first->start);
262 result.second.second = std::min(result.second.second, minmaxStop.second->stop);
265 auto valid =
left->is_valid();
266 result.first &= valid.first;
267 result.second.first = std::min(result.second.first, valid.second.first);
268 result.second.second = std::min(result.second.second, valid.second.second);
272 if (valid.second.second >=
center) {
273 result.first =
false;
278 auto valid =
right->is_valid();
279 result.first &= valid.first;
280 result.second.first = std::min(result.second.first, valid.second.first);
281 result.second.second = std::min(result.second.second, valid.second.second);
285 if (valid.second.first <=
center) {
286 result.first =
false;
291 result.first =
false;
298 std::unique_ptr<IntervalTree>
left;
299 std::unique_ptr<IntervalTree>
right;