Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a collection for storing discrete intervals?

Tags:

c++

algorithm

stl

I need to store discrete ranges in a set, joining adjacent ranges upon insertion. Is there a structure in STL which already has such functionality?

I have tried boost::intervals, but it's quite heavy and a bit of an overkill for what I'm trying to do.

For example, assume the set is empty and the the following elements are inserted:

[64, 96]
[0, 4]
[11, 15]
[5, 10]

The expected contents of the interval set should be as follows:

[0, 15]
[64, 96]
like image 612
sklopec Avatar asked Apr 12 '19 07:04

sklopec


1 Answers

This is a well known question. There is a wikipedia page on possible solutions to your question. Of course in the C++ STL you could implement a solution based on the Naive approach, explained in wikipedia, using a std::map because a map is a Red-Black Tree which is a type of Binary Search Tree.

like image 171
Max Avatar answered Oct 04 '22 19:10

Max