Is there any interval container in C#?
I'm interested in that it combines overlapping internals, and that I can find intervals that are not in the container but are in a specific interval.
Is there any interval container in C#?
I'm interested in that it combines overlapping internals, and that I can find intervals that are not in the container but are in a specific interval.
Someone by the name of Jani Giannoudis has posted a .NET time period library on CodeProject that might fit your needs.
You should implement a class like below:
public class Interval
{
public long Start {get;set;}
public long End{get;set;}
public bool IsIn(Interval interval)
{
return Start >= interval.Start && End < interval.End;
}
public Interval Intersection(Interval interval)
{
if (interval == null)
return false;
if (IsIn(interval))
return interval;
if (interval.IsIn(this))
return this;
if ....
}
public Interval Union(Interval interval)
{....}
public bool IsIn(List<Interval> intervals)
{
return intrvals.Any(x=>IsIn(x));
}
public List<Interval> Intersect(List<Interval> intervals)
{....}
public List<Interval> Union(List<Interval> intervals)
{....}
}
Edit: As @zmbq mentioned in comments, this can be done by struct, struct is more trivial way in this situations, I personally used class to simply deal with empty intervals (In fact if start - end >= 0, then interval is empty, instead of using predifined empty interval, I think we can set it to null. but I think this is syntax suger.
No. You should be able to use LINQ to perform such check on simple array of pairs.