4

Does the interval_map (in icl) library provide support for deletion? Can I look up the range based on iterator and delete the range?

============ party.cpp from boost example ===============

partyp->add( // add and element
  make_pair( 
    interval<ptime>::right_open(
      time_from_string("2008-05-20 19:30"), 
      time_from_string("2008-05-20 23:00")), 
    mary_harry));

party += // element addition can also be done via operator +=
  make_pair( 
    interval<ptime>::right_open(
      time_from_string("2008-05-20 20:10"), 
      time_from_string("2008-05-21 00:00")), 
    diana_susan);

party +=
  make_pair( 
    interval<ptime>::right_open(
      time_from_string("2008-05-20 22:15"), 
      time_from_string("2008-05-21 00:30")), 
    peter);

========== My question is can i add a remove statement like

 party -= 
        interval<ptime>::right_open(
          time_from_string("2008-05-20 20:10"), 
          time_from_string("2008-05-21 00:00"));

I just want to remove the range. Any method is fine.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
helloworld
  • 79
  • 7

1 Answers1

1

I know this is an old post, but I had the same question and I've finally found an answer.

Looking at the docs I would guess that the Subtractability of an interval_map and the aggregate on overlap capabilities guarantee that the substraction works as a delete operation.

It turned out to be a very fruitful concept to propagate the addition or subtraction to the interval_map's associated values in cases where the insertion of an interval value pair into an interval_map resulted in a collision of the inserted interval value pair with interval value pairs, that are already in the interval_map. This operation propagation is called aggregate on overlap.

Let's say I have to match intervals of unix timestamps to some record ids (integers). Working from this answer I've come up with this MWE:

// interval_map_mwe.cpp
#include <map>
#include <set>
#include <climits>

#include <boost/icl/interval.hpp>
#include <boost/icl/interval_map.hpp>

// Set of IDs that cover a time interval
typedef std::set<unsigned int> IDSet_t;
// interval tree from intervals of timestamps to a set of ids
typedef boost::icl::interval_map<time_t, IDSet_t> IMap_t;
// a time interval
typedef boost::icl::interval<time_t> Interval_t;

#include <iostream>
// from https://stackoverflow.com/a/22027957
inline std::ostream& operator<< (std::ostream& S, const IDSet_t& X)
{
    S << '(';
    for (IDSet_t::const_iterator it = X.begin(); it != X.end(); ++it) {
        if (it != X.begin()) {
            S << ',';
        }
        S << *it;
    }
    S << ')';
    return S;
}

int main(int argc, const char *argv[])
{
    (void)argc; // suppress warning
    (void)argv; // suppress warning

    IMap_t m;

    IDSet_t s;
    s.insert(1);
    s.insert(2);
    m += std::make_pair(Interval_t::right_open(100, 200), s);

    s = IDSet_t();
    s.insert(3);
    s.insert(4);
    m += std::make_pair(Interval_t::right_open(200, 300), s);

    s = IDSet_t();
    s.insert(5);
    s.insert(6);
    m += std::make_pair(Interval_t::right_open(150, 250), s);

    std::cout << "Initial map: " << std::endl;
    std::cout << m << std::endl;

    // find operation
    IMap_t::const_iterator it = m.find(175);

    std::cout << "Interval that covers 175: ";
    std::cout << it->first << std::endl;
    std::cout << "Ids in interval: " << it->second << std::endl;

    // partially remove 5 from interval (160,180)
    s = IDSet_t();
    s.insert(5);
    m -= std::make_pair(Interval_t::right_open(160, 180), s);

    std::cout << "map with 5 partially removed:" << std::endl;
    std::cout << m << std::endl;

    // completelly remove 6
    s = IDSet_t();
    s.insert(6);
    // Note: maybe the range of the interval could be shorter if you can somehow obtain the minimum and maximum times
    m -= std::make_pair(Interval_t::right_open(0, UINT_MAX), s);

    std::cout << "map without 6: " << std::endl;
    std::cout << m << std::endl;

    // remove a time interval
    m -= Interval_t::right_open(160, 170);

    std::cout << "map with time span removed: " << std::endl;
    std::cout << m << std::endl;

    return 0;
}

Compiling with g++ 4.4.7:

g++ -Wall -Wextra -std=c++98 -I /usr/include/boost148/ interval_map_mwe.cpp

The output I get is

Initial map:
{([100,150)->{1 2 })([150,200)->{1 2 5 6 })([200,250)->{3 4 5 6 })([250,300)->{3 4 })}
Interval that covers 175: [150,200)
Ids in interval: (1,2,5,6)
map with 5 partially removed:
{([100,150)->{1 2 })([150,160)->{1 2 5 6 })([160,180)->{1 2 6 })([180,200)->{1 2 5 6 })([200,250)->{3 4 5 6 })([250,300)->{3 4 })}
map without 6:
{([100,150)->{1 2 })([150,160)->{1 2 5 })([160,180)->{1 2 })([180,200)->{1 2 5 })([200,250)->{3 4 5 })([250,300)->{3 4 })}
map with time span removed:
{([100,150)->{1 2 })([150,160)->{1 2 5 })([170,180)->{1 2 })([180,200)->{1 2 5 })([200,250)->{3 4 5 })([250,300)->{3 4 })}

Note: The numbers in the MWE can be considered random. I find it easier to reason about the example with small numbers.

miravalls
  • 365
  • 1
  • 7