-
Notifications
You must be signed in to change notification settings - Fork 1
/
HashRange.cs
49 lines (44 loc) · 1.58 KB
/
HashRange.cs
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
namespace ConsistentHashing
{
/// <summary>
/// Represents a range of hashes in the consistent hash ring.
/// </summary>
public struct HashRange
{
/// <summary>
/// Creates a hash range for (startExclusive, endInclusive].
/// </summary>
/// <param name="startExclusive">The start of the range (exclusive).</param>
/// <param name="endInclusive">The end of the range (inclusive).</param>
public HashRange(uint startExclusive, uint endInclusive)
{
this.StartExclusive = startExclusive;
this.EndInclusive = endInclusive;
}
/// <summary>
/// Gets the exclusive start of the hash range.
/// </summary>
public uint StartExclusive { get; }
/// <summary>
/// Gets the inclusive end of the hash range.
/// </summary>
public uint EndInclusive { get; }
/// <summary>
/// Checks whether the specified hash is contained in the range.
/// </summary>
/// <param name="hash">The hash to check.</param>
/// <returns>True if the hash is in the range and false otherwise.</returns>
public bool Contains(uint hash)
{
if (this.StartExclusive > this.EndInclusive)
{
return hash > this.StartExclusive || hash <= this.EndInclusive;
}
return hash > this.StartExclusive && hash <= this.EndInclusive;
}
public override string ToString()
{
return $"({this.StartExclusive}, {this.EndInclusive}]";
}
}
}