I'm making an interval collection extension of the famous C# library C5. The IInterval
interface defines an interval with comparable endpoints (irrelevant members removed):
public interface IInterval<T> where T : IComparable<T>
{
T Low { get; }
T High { get; }
}
This works well in general, since interval endpoints can be anything comparable like integers, dates, or even strings.
However, it is sometimes favorable to be able to calculate the duration of an interval. The interval [3:5)
has a duration of 2, and the interval [1PM, 9PM)
has a duration of 8 hours. This is not possible with comparables, since it only gives us the order of elements, not their distance, e.g. it is difficult to give the distance between two strings. The endpoint type basically has to be interval-scaled values.
Is there an interface like IComparable<T>
, that allows me to compare endpoints in general, but also do stuff like subtracting two endpoints to get a duration, and adding a duration to a low endpoint to get the high endpoint that could be used for an inheriting interface, IDurationInterval<T> : IInterval<T>
for instance?
Or more concise: is there an interface for interval-scaled values?