【问题标题】:boost disjoint interval sets提升不相交区间集
【发布时间】:2015-06-26 13:12:17
【问题描述】:

我正在尝试将 boost:disjoint_sets 用于由以下结构表示的非重叠间隔(在我的情况下,集合中的间隔必须在其成员之间没有交集):

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);
  }
};

假设我有一个间隔列表,例如([1,3]、[4,5]、[6,10]、[8,9])。我初始化一个不相交集如下:

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

然后我在列表的所有元素中执行 make_set,这将导致 4 个不相交的集合,对吗?在此之后,我执行以下操作:

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;

我不明白的是为什么最后一个 cout 说我只有一套,在我的理解中应该说我有 2 套 {[1,3], [4,5], [6,10 ]} 和 {[8,9]}。

谁能帮我理解发生了什么?

【问题讨论】:

    标签: c++ boost intervals disjoint-sets


    【解决方案1】:

    我不完全确定我明白你想要做什么,但请看看 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";
        }
    }
    

    输出

    {[1,3]}
    {[1,3][4,5]}
    {[1,3][4,5][6,10]}
    {[1,3][4,5][6,8)[8,9](9,10]}
    

    【讨论】:

    • 事实并非如此。在我的情况下,区间 [8,9] 不能与 [6,10] 位于同一集合中,因为它们彼此相交。但我会看看 Boost ICL。
    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多