Created
May 6, 2013 18:09
-
-
Save Konard/5526917 to your computer and use it in GitHub Desktop.
Range is a generic class that represents a structure based on two borders (Minimum and Maximum).
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 Konard.Helpers | |
{ | |
public struct Range<T> : IComparable<Range<T>>, IEquatable<Range<T>> | |
where T : IComparable<T>, IEquatable<T> | |
{ | |
/// <summary> | |
/// Minimum value of the range | |
/// </summary> | |
public T Minimum; | |
/// <summary> | |
/// Maximum value of the range | |
/// </summary> | |
public T Maximum; | |
public Range(T minimum, T maximum) | |
{ | |
this.Minimum = minimum; | |
this.Maximum = maximum; | |
} | |
static public Range<T> Create(T minimum, T maximum) | |
{ | |
return new Range<T>(minimum, maximum); | |
} | |
public override int GetHashCode() | |
{ | |
int hash = 17; | |
hash = hash * 31 + this.Maximum.GetHashCode(); | |
hash = hash * 31 + this.Maximum.GetHashCode(); | |
return hash; | |
} | |
public override bool Equals(object other) | |
{ | |
return other is Range<T> ? Equals((Range<T>)other) : false; | |
} | |
public bool Equals(Range<T> other) | |
{ | |
return this.Minimum.Equals(other.Minimum) && | |
this.Maximum.Equals(other.Maximum); | |
} | |
/// <summary> | |
/// Determines if the range is valid | |
/// </summary> | |
/// <returns>True if range is valid, else false</returns> | |
public bool IsValid { get { return Minimum.CompareTo(Maximum) <= 0; } } | |
/// <summary> | |
/// Presents the Range in readable format | |
/// </summary> | |
/// <returns>String representation of the Range</returns> | |
public override string ToString() { return String.Format("[{0} - {1}]", Minimum, Maximum); } | |
/// <summary> | |
/// Determines if the provided value is inside the range | |
/// </summary> | |
/// <param name="value">The value to test</param> | |
/// <returns>True if the value is inside Range, else false</returns> | |
public bool Contains(T value) | |
{ | |
return (Minimum.CompareTo(value) <= 0) && (value.CompareTo(Maximum) <= 0); | |
} | |
/// <summary> | |
/// Determines if another range is inside the bounds of this range | |
/// </summary> | |
/// <param name="Range">The child range to test</param> | |
/// <returns>True if range is inside, else false</returns> | |
public bool Contains(Range<T> Range) | |
{ | |
return this.IsValid && Range.IsValid && this.Contains(Range.Minimum) && this.Contains(Range.Maximum); | |
} | |
/// <summary> | |
/// Determines if this Range is inside the bounds of another range | |
/// </summary> | |
/// <param name="Range">The parent range to test on</param> | |
/// <returns>True if range is inclusive, else false</returns> | |
public bool InstersectsWith(Range<T> Range) | |
{ | |
return this.IsValid && Range.IsValid && Range.Contains(this.Minimum) && Range.Contains(this.Maximum); | |
} | |
public int CompareTo(Range<T> other) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment