I need to keep a collection on integers in the range 0 to 65535 so that I can quickly do the following:
- Insert a new integer
- Insert a range of contiguous integers
- Remove an integer
- Remove all integers below an integer
- Test if an integer is present
My data has the property that it often contains runs of integers in the collection. For example, the collection might at one point in time be:
{ 121, 122, 123, 124, 3201, 3202, 5897, 8912, 8913, 8914, 18823, 18824, 40891 }
The simplest approach is just to use a balanced binary tree like the C++ std::set, however, using that, I am not leveraging the fact that I often have runs of numbers. Perhaps it would be better to store a collection of ranges? But that means a range needs to be able to be broken up if an integer in its middle is removed, or joined together if the space between two ranges in filled in.
Are there any existing data structures that would be well suited for this problem?