forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange1.cs
More file actions
120 lines (104 loc) · 3.42 KB
/
Range1.cs
File metadata and controls
120 lines (104 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simple.Data
{
public struct Range<T> : IRange, IEquatable<Range<T>>
where T : IComparable<T>
{
private static readonly EqualityComparer<T> EqualityComparer = EqualityComparer<T>.Default;
private readonly T _start;
private readonly T _end;
public Range(T start, T end)
: this()
{
_start = start;
_end = end;
}
public T End
{
get { return _end; }
}
IEnumerable<object> IRange.AsEnumerable()
{
return AsEnumerable().Cast<object>();
}
public IEnumerable<T> AsEnumerable()
{
var end = _end;
return AsEnumerable(_ => end);
}
public IEnumerable<T> AsEnumerable(Func<T,T> step)
{
T current = _start;
do
{
yield return current;
current = step(current);
} while (current.CompareTo(_end) <= 0);
}
public T Start
{
get { return _start; }
}
public override string ToString()
{
return string.Format("({0}..{1})", _start, _end);
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(Range<T> other)
{
return EqualityComparer.Equals(other._start, _start) && EqualityComparer.Equals(other._end, _end);
}
/// <summary>
/// Indicates whether this instance and a specified object are equal.
/// </summary>
/// <returns>
/// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
/// </returns>
/// <param name="obj">Another object to compare to. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj.GetType() != typeof(Range<T>)) return false;
return Equals((Range<T>)obj);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
/// <filterpriority>2</filterpriority>
public override int GetHashCode()
{
unchecked
{
return (_start.GetHashCode() * 397) ^ _end.GetHashCode();
}
}
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);
}
object IRange.Start
{
get { return Start; }
}
object IRange.End
{
get { return End; }
}
}
}