c++boostboost-icl

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

Solution

  • 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;