This page provides an example of how to use the interval map. For information on what an interval map is and how it behaves, please see Overview over the Data Structures.
The code can also be found in the examples directory.
#include "ygg.hpp"
#include <vector>
class Node : public IMapNodeBase<int, int> {
public:
int lower;
int upper;
int value;
Node(int lower_in, int upper_in, int value_in) : lower(lower_in), upper(upper_in), value(value_in)
{}
};
class NodeTraits : public IMapNodeTraits<Node> {
public:
using key_type = int;
using value_type = int;
static key_type get_lower(const Node & n) {
return n.lower;
}
static key_type get_upper(const Node & n) {
return n.upper;
}
static value_type get_value(const Node & n) {
return n.value;
}
};
using IMap = IntervalMap<Node, NodeTraits>;
int main(int argc, char **argv) {
(void)argc;
(void)argv;
std::vector<Node> nodes;
nodes.emplace_back(0, 1, 42);
nodes.emplace_back(2, 3, 23);
nodes.emplace_back(3, 10, 23);
nodes.emplace_back(5, 20, 47);
IMap m;
for (auto & n : nodes) {
m.insert(n);
}
for (auto it = m.begin(); it != m.end(); ++it) {
std::cout << "Segment from " << it.get_lower() << " to " << it.get_upper() << ": aggregate "
"value " << it.get_value() << "\n";
}
return 0;
}