I have a method that gets a number of objects of this class
class Range<T>
{
public T Start;
public T End;
}
In my case T
is DateTime
, but lets use int
for simplicity. I would like a method that collapses those ranges into ones that cover the same "area" but that do not overlap.
So if I had the following ranges
- 1 to 5
- 3 to 9
- 11 to 15
- 12 to 14
- 13 to 20
The method should give me
Guess it would be called a union? I imagine the method signature could look something like this:
public static IEnumerable<Range<T>> Collapse<T>(
this IEnumerable<Range<T>>,
IComparable<T> comparer)
{
...
}
I have looked at some other questions here that are kind of similar, but I haven't found an implementation of this yet. This answer and some other answers to the same question describes algorithms, but I am not quite sure if I understand the algorithms. Not especially good at implementing algorithms either, so I was hoping someone here could help me out.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…