This page provides an example of how to use the dynamic segment tree. For information on what a dynamic segment tree is and how it behaves, please see Introduction to the Dynamic Segment Tree.
The code can also be found in the examples directory.
#include "../src/ygg.hpp"
class Interval
public:
double lower;
double upper;
int value;
Interval(double lower_in, double upper_in, int value_in)
: lower(lower_in), upper(upper_in), value(value_in){};
};
public:
using key_type = double;
using value_type = int;
static key_type
get_lower(const Interval & n)
{
return n.lower;
}
static key_type
get_upper(const Interval & n)
{
return n.upper;
}
static value_type
get_value(const Interval & n)
{
return n.value;
}
static bool
is_lower_closed(const Interval & n)
{
(void)n;
return true;
};
static bool
is_upper_closed(const Interval & n)
{
(void)n;
return false;
};
};
DefaultOptions, TreeSelector>;
int
main(int argc, char ** argv)
{
(void)argc;
(void)argv;
MyTree t;
std::vector<Interval> intervals;
intervals.emplace_back(0.0, 10.0, 1);
intervals.emplace_back(0.5, 10.0, 2);
intervals.emplace_back(10.0, 15.0, 3);
intervals.emplace_back(12.0, 20.0, 8);
for (auto & interval : intervals) {
t.insert(interval);
}
for (auto point : std::vector<double>{0, 0.5, 5, 10, 14, 15}) {
std::cout << "Point: " << point << "\t| Aggregate Value: " << t.query(point)
<< "\n";
}
std::cout << "\n==============================\n\n";
for (auto event : t) {
std::cout << "At point " << event.get_point();
if (event.is_closed()) {
std::cout << " (inclusive) ";
} else {
std::cout << " (exclusive) ";
}
std::cout << " there ";
if (event.is_start()) {
std::cout << " starts ";
} else {
std::cout << " ends ";
}
auto interval = static_cast<const Interval *>(event.get_interval());
std::cout << "the interval [" << interval->lower << "," << interval->upper
<< ")\n";
}
std::cout << "\n==============================\n\n";
std::cout << "Maximum Value: " << t.get_combined<MCombiner>() << "\n";
auto combiner = t.get_combiner<MCombiner>();
std::cout << "Maximum occurs between " << combiner.get_left_border()
<< " and " << combiner.get_right_border() << "\n";
std::cout << "Maximum over [0, 10): " << t.get_combined<MCombiner>(0, 10)
<< "\n";
std::cout << "Maximum over [10, 12): " << t.get_combined<MCombiner>(10, 12)
<< "\n";
combiner = t.get_combiner<MCombiner>(0, 10);
std::cout << "Maximum interval in the range [0,10) is between "
<< combiner.get_left_border() << " and "
<< combiner.get_right_border() << "\n";
std::cout << "Maximum over [10, 12]: "
<< t.get_combined<MCombiner>(10, 12, true, true) << "\n";
return 0;
}