Created
December 3, 2015 21:30
-
-
Save kntajus/2f61d6af08b15e95cb6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Diuturnal { | |
[Serializable] | |
public class Range<T> where T : IComparable<T> { | |
private T _start; | |
private T _end; | |
public Range(T first, T second) { | |
if (first.CompareTo(second) > 0) { | |
_start = second; | |
_end = first; | |
} | |
else { | |
_start = first; | |
_end = second; | |
} | |
} | |
public T Start { | |
get { return _start; } | |
} | |
public T End { | |
get { return _end; } | |
} | |
public bool Contains(T value) { | |
return (_start.CompareTo(value) <= 0 && value.CompareTo(_end) <= 0); | |
} | |
public bool Overlaps(Range<T> other) { | |
return Range.Overlap<T>(this, other); | |
} | |
} | |
public static class Range { | |
public static bool Overlap<T>(Range<T> first, Range<T> second) where T : IComparable<T> { | |
return second.End.CompareTo(first.Start) >= 0 && first.End.CompareTo(second.Start) >= 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment