AFAIK không có điều nào trong .NET. Sẽ là thú vị mặc dù, để đến với một thực hiện chung chung.
Xây dựng BCL loại phạm vi chất lượng chung là rất nhiều công việc, nhưng nó có thể trông giống như thế này:
public enum RangeBoundaryType
{
Inclusive = 0,
Exclusive
}
public struct Range<T> : IComparable<Range<T>>, IEquatable<Range<T>>
where T : struct, IComparable<T>
{
public Range(T min, T max) :
this(min, RangeBoundaryType.Inclusive,
max, RangeBoundaryType.Inclusive)
{
}
public Range(T min, RangeBoundaryType minBoundary,
T max, RangeBoundaryType maxBoundary)
{
this.Min = min;
this.Max = max;
this.MinBoundary = minBoundary;
this.MaxBoundary = maxBoundary;
}
public T Min { get; private set; }
public T Max { get; private set; }
public RangeBoundaryType MinBoundary { get; private set; }
public RangeBoundaryType MaxBoundary { get; private set; }
public bool Contains(Range<T> other)
{
// TODO
}
public bool OverlapsWith(Range<T> other)
{
// TODO
}
public override string ToString()
{
return string.Format("Min: {0} {1}, Max: {2} {3}",
this.Min, this.MinBoundary, this.Max, this.MaxBoundary);
}
public override int GetHashCode()
{
return this.Min.GetHashCode() << 256^this.Max.GetHashCode();
}
public bool Equals(Range<T> other)
{
return
this.Min.CompareTo(other.Min) == 0 &&
this.Max.CompareTo(other.Max) == 0 &&
this.MinBoundary == other.MinBoundary &&
this.MaxBoundary == other.MaxBoundary;
}
public static bool operator ==(Range<T> left, Range<T> right)
{
return left.Equals(right);
}
public static bool operator !=(Range<T> left, Range<T> right)
{
return !left.Equals(right);
}
public int CompareTo(Range<T> other)
{
if (this.Min.CompareTo(other.Min) != 0)
{
return this.Min.CompareTo(other.Min);
}
if (this.Max.CompareTo(other.Max) != 0)
{
this.Max.CompareTo(other.Max);
}
if (this.MinBoundary != other.MinBoundary)
{
return this.MinBoundary.CompareTo(other.Min);
}
if (this.MaxBoundary != other.MaxBoundary)
{
return this.MaxBoundary.CompareTo(other.MaxBoundary);
}
return 0;
}
}
Tại sao điều này lại bị giảm giá? – jason
Các downvotes về điều này là khó hiểu tôi. – jason
Tôi cũng có hai nhược điểm không giải thích được. Khá khó chịu. 1 từ tôi để bù đắp. – Steven