Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does boost interval_map have operator [] or .at() method?

I'm using interval_map from BOOST library.

typedef set<int> Tpopulations;    
interval_map<int, Tpopulations> populations;

Say I have this in populations

[1006311,1006353)   1611,1653,
[1006353,1006432)   1031,1611,1653,
[1006432,1006469]   1031,1387,1523,1611,1653,
(1006469,1006484]   1031,1387,1611,1653,
(1006484,1006496]   1031,1387,1611,
(1006496,1006506]   1031,1611,
(1006506,1006547]   1031,

Now I want to find out what is mapped on some number: I would expect something like:

cout << populations[1006313];  // 1611,1653

or

cout << populations.at(1006313);  // 1611,1653

However I seem not to find any such a method.

Do I really need to define anoher interval map as "window" and do intersection? Something like:

interval_map<int, Tpopulations> window;
set<int>empty_set;
window +=(make_pair(1006313,empty_set));
cout << populations & window
like image 624
user2848463 Avatar asked Oct 05 '13 01:10

user2848463


2 Answers

No, the boost::icl::interval_map doesn't contain these element access functions. However you can do what you want, using the find function.

typedef std::set<int> Tpopulations;
typedef boost::icl::interval_map<int, Tpopulations> IMap;
typedef boost::icl::interval<int> Interval;
...
IMap m;
m += std::make_pair(Interval::right_open(1006311, 1006353), Tpopulations({1611, 1653}));
...
IMap::const_iterator it = m.find(1006313);
cout << it->first << endl;
...

The code above will give you interval, which contains the number 1006313. In order to send std::set<int> to the cout you'll need additional operator:

inline std::ostream& operator<< (std::ostream& S, const Tpopulations& X)
{
  S << '(';
  for (ISet::const_iterator it = X.cbegin(); it != X.cend(); ++it)
  {
    if (it != X.cbegin()) S << ',';
    S << *it;
  }
  S << ')';
  return S;
}

Then the line below will print what you want:

cout << it->second << endl;
like image 187
HEKTO Avatar answered Oct 04 '22 16:10

HEKTO


Yes, a simple solution would be to find your mapped element with (). However, to do so you have to set your map trait=total_absorber so you cover the whole range. Here is the code:

interval_map<int, Tpopulations, icl::total_absorber> populations;
Tpopulations valSet = populations(1006313);

You would then iterate over valSet or override the operator<< as @HEKTO mentioned above to actually print out your data.

Check the boost documents for selection on interval_map. This method also gives the best expected performance of O(log(N))

like image 25
bschwagg Avatar answered Oct 04 '22 17:10

bschwagg