Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interval data type for C# .NET?

Tags:

c#

.net

types

I'm looking an interval data type for .NET 4.0. For example the interval (a,b], all point x such that a<x<=b.

What i would like to be able to do are create intervals with the following properites:

  • Closed and open ends
  • Unbounded intervals, fully unbounded, and right/left unbounded.

With these I would like to do thing like:

  • Check if a point is in a interval.
  • Check if two intervals overlap.
  • Merge two overlapping intervals to a single interval.
  • Check if a collection of intervals covers a single interval.
  • Etc :)

Would be nice if I could work with both numerical datatype and datetimes.

I know that the logic is pretty straight forward, but I see no reason that I would be the first one to need such a thing either.

like image 448
gSpotTornado Avatar asked Nov 11 '10 17:11

gSpotTornado


People also ask

What is an interval data type?

Interval data is a type of data which is measured along a scale, in which each point is placed at an equal distance (interval) from one another. Interval data is one of the two types of discrete data.

Is time a interval data type?

The INTERVAL data type stores a value that represents a span of time. INTERVAL types are divided into two classes: year-month intervals and day-time intervals.

What is interval data type in SQL?

A datetime or interval data type is stored as a decimal number with a scale factor of zero and a precision equal to the number of digits that its qualifier implies. When you know the precision and scale, you know the storage format.


2 Answers

EDIT 2019: As of C# 8.0/.NET Core 3.x/.NET Standard 2.1 there is now a System.Range that provides minimal interval functionality with endpoints. I'm leaving the rest of this answer as-is.

As others have stated, there are no integrated interval type. Depending on the needs of your project, a simple Tuple<T1, T2> or call to Enumerable.Range with a few additional lines of code might suffice. The HashSet<T> contains set operation methods, such as UnionWith, IntersectWith and more, but still stores all the items, not just the endpoints.

Many implementations can be found online. There is the basic generic Range class part of the Microsoft Research Dynamic Data Display project and another from Kevin Gadd. The AForge project contains a non-generic IntInterval/DoubleInterval implementation. Other (1, 2) SO questions might also be of interest. Andy Clymer has an interesting dynamically compiled implementation on his blog. More complete solutions can be found on CodeProject, in Jon Skeet's book and From Russia with Love. There seems to be a few (1, 2) commercial solutions as well. I've seen others before that I can't find at the moment.

Whatever you do, please watch out when using a generic interval type. It's actually hard to write a correct monolithic generic interval class because integer and floating point intervals have different mathematical properties. For example all integer intervals can be represented with closed endpoints and the pair [1,2] [3,6] can be considered as contiguous, equivalent to [1,6]. None of this is true with floating points intervals. See Wikipedia for details. A group of classes might be better, with an abstract generic base class and typed derived classes IntInterval or DoubleInterval to implement the different behaviors.

Aside from the math, there are a few more implementation difficulties with generic interval types. It's not possible to easily do arithmetic with generics in C#, and there is floating point NaN and rounding errors to take care of. See the Boost library documentation for Interval<T> for more on this. (A lot of it translates to C# and .NET.) Luckily many operations can be done with just IComparable<T>.

As I mentioned before, the choice of what is appropriate in terms of functionality and correctness all depends on the requirements of your projects.

like image 159
Djof Avatar answered Oct 04 '22 08:10

Djof


To get you started:

public class Interval<T> where T : struct, IComparable {     public T? Start { get; set; }     public T? End { get; set; }      public Interval(T? start, T? end)     {         Start = start;         End = end;     }      public bool InRange(T value)     {         return ((!Start.HasValue || value.CompareTo(Start.Value) > 0) &&                 (!End.HasValue || End.Value.CompareTo(value) > 0));     } } 
like image 34
Stu Avatar answered Oct 04 '22 08:10

Stu