2 // EnumerablePartitioner.cs
5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 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
27 #if NET_4_0 || BOOTSTRAP_NET_4_0
30 using System.Threading.Tasks;
31 using System.Collections.Generic;
33 namespace System.Collections.Concurrent
35 // Represent a chunk partitioner
36 internal class EnumerablePartitioner<T> : OrderablePartitioner<T>
38 IEnumerable<T> source;
40 const int InitialPartitionSize = 1;
41 const int PartitionMultiplier = 2;
43 int initialPartitionSize;
44 int partitionMultiplier;
47 readonly object syncLock = new object ();
49 public EnumerablePartitioner (IEnumerable<T> source)
50 : this (source, InitialPartitionSize, PartitionMultiplier)
55 // This is used to get striped partitionning (for Take and Skip for instance
56 public EnumerablePartitioner (IEnumerable<T> source, int initialPartitionSize, int partitionMultiplier)
57 : base (true, false, true)
60 this.initialPartitionSize = initialPartitionSize;
61 this.partitionMultiplier = partitionMultiplier;
64 public override IList<IEnumerator<KeyValuePair<long, T>>> GetOrderablePartitions (int partitionCount)
66 if (partitionCount <= 0)
67 throw new ArgumentOutOfRangeException ("partitionCount");
69 IEnumerator<KeyValuePair<long, T>>[] enumerators
70 = new IEnumerator<KeyValuePair<long, T>>[partitionCount];
72 IEnumerator<T> src = source.GetEnumerator ();
74 for (int i = 0; i < enumerators.Length; i++) {
75 enumerators[i] = GetPartitionEnumerator (src);
81 IEnumerator<KeyValuePair<long, T>> GetPartitionEnumerator (IEnumerator<T> src)
83 int count = initialPartitionSize;
84 List<T> list = new List<T> ();
93 for (int i = 0; i < count; i++) {
94 if (!src.MoveNext ()) {
101 list.Add (src.Current);
108 for (int i = 0; i < list.Count; i++)
109 yield return new KeyValuePair<long, T> (ind + i, list[i]);
111 count *= partitionMultiplier;