从boost :: icl :: interval_set中删除离散的闭合间隔

时间:2015-10-21 17:52:38

标签: c++ boost intervals

  boost::icl::interval_set<uint> test_set;
  test_set.insert(boost::icl::discrete_interval<uint>::closed(10u, 20u));
  test_set.insert(boost::icl::discrete_interval<uint>::closed(21u, 30u)); //should merge to single interval
  test_set.insert(boost::icl::discrete_interval<uint>::closed(15u, 25u)); //should not change
  test_set.erase(boost::icl::discrete_interval<uint>::closed(12u, 18u)); //should split in two intervals
  uint i1min = test_set.begin()->lower();
  uint i1max = test_set.begin()->upper();
  uint i2min = (++test_set.begin())->lower();
  uint i2max = (++test_set.begin())->upper();
  std::cout<<i1min<<"\n";
  std::cout<<i1max<<"\n";
  std::cout<<i2min<<"\n";
  std::cout<<i2max<<"\n";

由于我正在添加和减去闭合间隔,我希望得到以下输出:

10
11
19
30

但我得到了:

10
12
18
30

为什么我要删除的间隔的端点仍然存在?这是减去封闭间隔的预期行为还是我做错了什么?

1 个答案:

答案 0 :(得分:4)

从您的情况中减去一个闭合间隔,在您的情况下导致两个半开间隔。如果你考虑它,它也与连续间隔的行为一致。

您可以使用以下代码对此进行测试:

 std::cout << test_set.begin()->bounds() << "\n";
 std::cout << (++test_set.begin())->bounds() << "\n";

此输出:[)(]表示生成的时间间隔是半开的,这意味着1218不包含在您的时间间隔集中。

要获取(可能是开放的)离散间隔的第一个/最后一个元素,请使用函数firstlast(docs here):

uint i1min = boost::icl::first(*test_set.begin());
uint i1max = boost::icl::last(*test_set.begin());
uint i2min = boost::icl::first(*(++test_set.begin()));
uint i2max = boost::icl::last(*(++test_set.begin()));