33 namespace System.Collections.Concurrent.Partitioners
35 internal class UserRangePartitioner : OrderablePartitioner<Tuple<int, int>>
39 readonly
int rangeSize;
41 public UserRangePartitioner (
int start,
int end,
int rangeSize) : base (true, true, true)
45 this.rangeSize = rangeSize;
48 public override IList<IEnumerator<KeyValuePair<long, Tuple<int, int>>>> GetOrderablePartitions (
int partitionCount)
50 if (partitionCount <= 0)
51 throw new ArgumentOutOfRangeException (
"partitionCount");
54 Func<int> getNextIndex = () => Interlocked.Increment (ref currentIndex) - 1;
56 var enumerators =
new IEnumerator<KeyValuePair<long, Tuple<int, int>>>[partitionCount];
57 for (
int i = 0; i < partitionCount; i++)
58 enumerators[i] = GetEnumerator (getNextIndex);
63 IEnumerator<KeyValuePair<long, Tuple<int, int>>> GetEnumerator (Func<int> getNextIndex)
66 int index = getNextIndex ();
67 int sliceStart = index * rangeSize + start;
69 if (sliceStart >= end)
72 yield
return new KeyValuePair<long, Tuple<int, int>> (index,
Tuple.Create (sliceStart, Math.Min (end, sliceStart + rangeSize)));
73 sliceStart += rangeSize;
78 internal class UserLongRangePartitioner : OrderablePartitioner<Tuple<long, long>>
82 readonly
long rangeSize;
84 public UserLongRangePartitioner (
long start,
long end,
long rangeSize) : base (true, true, true)
88 this.rangeSize = rangeSize;
91 public override IList<IEnumerator<KeyValuePair<long, Tuple<long, long>>>> GetOrderablePartitions (
int partitionCount)
93 if (partitionCount <= 0)
94 throw new ArgumentOutOfRangeException (
"partitionCount");
96 long currentIndex = 0;
97 Func<long> getNextIndex = () => Interlocked.Increment (ref currentIndex) - 1;
99 var enumerators =
new IEnumerator<KeyValuePair<long, Tuple<long, long>>>[partitionCount];
100 for (
int i = 0; i < partitionCount; i++)
101 enumerators[i] = GetEnumerator (getNextIndex);
106 IEnumerator<KeyValuePair<long, Tuple<long, long>>> GetEnumerator (Func<long> getNextIndex)
109 long index = getNextIndex ();
110 long sliceStart = index * rangeSize + start;
112 if (sliceStart >= end)
115 yield
return new KeyValuePair<long, Tuple<long, long>> (index,
Tuple.Create (sliceStart, Math.Min (end, sliceStart + rangeSize)));
116 sliceStart += rangeSize;