Skip to content

Instantly share code, notes, and snippets.

@kntajus
Created December 3, 2015 21:30
Show Gist options
  • Save kntajus/2f61d6af08b15e95cb6d to your computer and use it in GitHub Desktop.
Save kntajus/2f61d6af08b15e95cb6d to your computer and use it in GitHub Desktop.
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