Quick start¶
This section demonstrates a simple use case of storing non-overlapping ranged
values and performing queries using flat_segment_tree.
First, we need to instantiate a concrete type from the template:
using fst_type = mdds::flat_segment_tree<long, int>;
then create an instance of this type:
// Define the begin and end points of the whole segment, and the default
// value.
fst_type db(0, 500, 0);
Here, the first and second arguments specify the lower and upper boundaries of the whole segment. The third argument specifies the value for the empty segments. What this line does is to create a new instance and initializes it with one initial segment ranging from 0 to 500 with a value of 0:
The new instance is initialized with an initial segment.¶
Internally, this initial range is represented by two leaf nodes, with the first one storing the start key and the value for the segment both of which happen to be 0 in this example, and the second one storing the end key of 500. Note that the end key of a segment is not inclusive.
The following lines insert two new segments into this structure:
db.insert_front(10, 20, 10);
db.insert_back(50, 70, 15);
The first line inserts a segment ranging from 10 to 20 with a value of 10, and the second line from 50 to 70 with a value of 15:
Two new segments have been inserted.¶
You can insert a new segment either via insert_front()
or insert_back(). The end result will be
the same regardless of which method you use; the difference is that
insert_front() begins its search for
the insertion point from the first node associated with the minimum key value,
whereas insert_back() starts its search
from the last node associated with the maximum key value.
After the insertions, the tree now contains a total of six leaf nodes to represent all stored segments. Note that one leaf node typically represents both the end of a segment and the start of the adjacent segment that comes after it, unless it’s either the first or the last node.
The next line inserts another segment ranging from 60 to 65 having a value of 5:
db.insert_back(60, 65, 5);
As this new segment overlaps with the existing segment of 50 to 70, it will cut into a middle part of that segment to make room for the new segment. At this point, the tree contains a total of eight leaf nodes representing seven segments:
A new segment has been inserted that overlaps an existing non-empty segment.¶
Next, we are going to query the value associated with a key of 15 via
search():
// Perform linear search. This doesn't require the tree to be built ahead
// of time.
if (auto it = db.search(15); it != db.end())
{
auto segment = it.to_segment();
std::cout << "The value at 15 is " << segment->value
<< ", and this segment spans from " << segment->start << " to "
<< segment->end << std::endl;
}
Executing this code will yield the following output:
The value at 15 is 10, and this segment spans from 10 to 20
One thing to note is that the search()
method performs a linear search which involves traversing only through the
leaf nodes in this data structure in order to find the target segment. As
such, the worst-case lookup performance is directly proportional to the number
of leaf nodes.
There is another way to perform the query with better worse-case performance,
that is through search_tree() as seen in
the following code:
// Don't forget to build tree before calling search_tree().
db.build_tree();
// Perform tree search. Tree search is generally a lot faster than linear
// search, but requires the tree to be built ahead of time.
if (auto it = db.search_tree(62); it != db.end())
{
auto segment = it.to_segment();
std::cout << "The value at 62 is " << segment->value
<< ", and this segment spans from " << segment->start << " to "
<< segment->end << std::endl;
}
The signature of the search_tree() method
is identical to that of the search() method
except for the name. This code generates the following output:
The value at 62 is 5, and this segment spans from 60 to 65
Query via search_tree() generally performs
better since it traverses through the search tree to find the target segment.
But it does require the search tree to be built ahead of time by calling
build_tree(). Please be aware that if the
segments have been modified after the tree was last built, you will have to rebuild
the tree by calling build_tree().
Warning
You need to build the tree by calling build_tree()
before performing a tree-based search via search_tree().
If the segments have been modified after the tree was last built, you will have to
rebuild the tree by calling build_tree() again.