0

I'm trying to use boost:disjoint_sets for non overlaping intervals (in my case the intervals in a set must have no intersection between its members) that are represented by the following struct:

struct dataum {
  int x,y;
  bool operator< (const dataum& o) const { return   y < o.x ; }
  bool operator==(const dataum& o) const {
    if(x <= o.x && o.x <= y && y <= o.y )
        return true;
    if(x <= o.x && o.y <= y)
        return true;
    if(o.x <= x && y <= o.y)
        return true;
    if(o.x <= x && x <= o.y && o.y <= y )
        return true;
    return false;
  }
  bool operator!=(const dataum& o) const {
    return !operator==(o);
  }
};

Let's say I have a list of intervals e.g. ([1,3], [4,5], [6,10], [8,9]). I initialize a disjoint set as below:

    boost::disjoint_sets<
    associative_property_map<std::map<dataum,int>>,
    associative_property_map<std::map<dataum,dataum>> > ds(
            make_assoc_property_map(rank),
            make_assoc_property_map(parent));

Then I perform a make_set in all elements of my list, which will result in 4 disjoint sets, right? After this, I perform the following operations:

std::cout << ds.count_sets(S.begin(), S.end()) << std::endl;
ds.union_set(S[0],S[1]);
ds.union_set(S[1],S[2]);
std::cout << ds.count_sets(S.begin(), S.end()) << std::endl;

What I don't understand is why the last cout says that I have only one set, which in my understanding should say that I have 2 sets {[1,3], [4,5], [6,10]} and {[8,9]}.

Can anyone help me to understand what's happening ?

1 Answers1

0

I'm not completely sure I get what you're trying to do, but do have a look at Boost ICL:

Live On Coliru

#include <boost/icl/split_interval_set.hpp>
#include <iostream>

using dset   = boost::icl::split_interval_set<int>;
using dataum = dset::interval_type;


int main() {
    dset data;
    for (auto i : {
            dataum::closed (1,3),
            dataum::closed (4,5),
            dataum::closed (6,10),
            dataum::closed (8,9),
            })
    {
        data.insert(i);
        std::cout << data << "\n";
    }
}

Output

{[1,3]}
{[1,3][4,5]}
{[1,3][4,5][6,10]}
{[1,3][4,5][6,8)[8,9](9,10]}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • that's not the case. In my case the interval [8,9] cannot be in the same set as [6,10] since they intersect each other. But I'll take a look at Boost ICL. – Rafael Mathias Apr 21 '15 at 03:42