Skip to content

Instantly share code, notes, and snippets.

@Kntajus Kntajus/Range.cs
Created Dec 3, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.