#include <iostream>
#include <boost/icl/split_interval_map.hpp>
using namespace std;
using namespace boost::icl;
int main()
{
split_interval_map<double, int> intervals;
intervals.add(make_pair(interval<double>::closed(0.,1.),0));
intervals.add(make_pair(interval<double>::closed(1.,2.),1));
intervals.add(make_pair(interval<double>::closed(3.,4.),2));
intervals.add(make_pair(interval<double>::closed(2.,4.),3));
intervals.add(make_pair(interval<double>::closed(1.5,3.5),4));
std::vector<double> probes = { 0.23, 1., 1.33 , 1.57, 3.49, 3.51 };
for(auto probe : probes)
{
std::cout << std::endl<< "probe " << probe << std::endl;
auto lower = intervals.lower_bound(interval<double>::closed(probe, probe));
auto upper = intervals.upper_bound(interval<double>::closed(probe, probe));
while(lower != upper)
{
std::cout << lower->second << " ";
++lower;
}
}
}
- What i get are the indices added up. But i'm looking for all the values (
ints) of the interval containing 'probe'. (intersection?)
- I could achieve this with
std::set<int> as value, but in the documentation it is stated, that this has a huge impact on performance. Seems like split_interval_map contains that information but i don't know how to retrieve it it.
- I need only a highly efficient lookup like in this example. I don't need the intersecting interval ranges anymore. Is boost icl too heavy for this?
You get all the values (the co-domain values) combined using the combiner of your choosing. For an arithmetic type, that implies summation.
If your co-domain is an index, clearly summation is not meaningful combiner, and you should choose something else.
As always, correct goes before performance. If it's what you need, it's what you need.
Not with the chosen co-domain: the combiner loses the original information if intervals overlap (and you use
add, notset).You could use
equal_rangeinstead oflower_bound/upper_bound:Live On Coliru
Prints
Observations:
These intervals subtly overlap.
[0, 1]and[1, 2]have[1,1]in common. Did you really meanleft_open? ([0, 1)and[1, 2)have no overlap).If you were surprised by the fact that this combines the values already in the overlapping interval(s), did you mean to replace them?
Alternatives, Ideas:
You could do the intersection with the set of probes at once:
Live On Coliru
Prints:
To (maybe?) optimize that a little:
Live On Coliru
If the sets are going to be small, consider specifying small_vector for that flat_set's sequence type:
All else just still works: Live On Coliru
Completely OUT-OF-THE-BOX: are you modeling geometric regions? Like intervals on a timeline? Or just line-segments on an axis? In that case, consider
boost::geometry::index::rtree<>