开发者

Reduce ranges of doubles in C++

I've seen various approaches in other languages for how to do this, but it seems like a common enough problem that it seems like there should be a standard way to do this.

I want to collapse a list of float ranges to its smallest representation.

2.45-3.72 3.16-6.55 7.23-8.96 8.95-10.27

would become:

2.45-6.55 7.23-10.27

Is th开发者_StackOverflowere a function or library in C++ somewhere that I can give my list to, and it will perform the reduction for me? I could easily write my own, but why bother if it already exists?


Sound simple:

  • start with list of ranges
  • let current = first range in the list
  • while there's a next range after current:
    • if next overlaps current:
      • extend current to also contain next
      • remove next from the list
    • else:
      • advance current one forward

That should do the trick.


Boost has an interval library. This doesn't explicitly contain exactly what you need, but might make it easier to write it.

Personally, looking at the documentation, it worries me that the library does not attempt to let you clearly distinguish between closed and open intervals. But this may not be important for your purpose.


I don't think there will be any solution provided by the Standard, it's way too specific.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜