2 // UserRangePartitioner.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 using System.Threading;
31 using System.Collections.Generic;
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;