New tests.
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / Partitioners / EnumerablePartitioner.cs
1 // 
2 // EnumerablePartitioner.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
8 // 
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:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
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
25 // THE SOFTWARE.
26
27 #if NET_4_0 || BOOTSTRAP_NET_4_0
28
29 using System;
30 using System.Threading.Tasks;
31 using System.Collections.Generic;
32
33 namespace System.Collections.Concurrent
34 {
35         // Represent a chunk partitioner
36         internal class EnumerablePartitioner<T> : OrderablePartitioner<T>
37         {
38                 IEnumerable<T> source;
39                 
40                 const int InitialPartitionSize = 1;
41                 const int PartitionMultiplier = 2;
42                 
43                 int initialPartitionSize;
44                 int partitionMultiplier;
45                 
46                 int index = 0;
47                 readonly object syncLock = new object ();
48                 
49                 public EnumerablePartitioner (IEnumerable<T> source)
50                         : this (source, InitialPartitionSize, PartitionMultiplier)
51                 {
52
53                 }
54                 
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)
58                 {
59                         this.source = source;
60                         this.initialPartitionSize = initialPartitionSize;
61                         this.partitionMultiplier = partitionMultiplier;
62                 }
63                 
64                 public override IList<IEnumerator<KeyValuePair<long, T>>> GetOrderablePartitions (int partitionCount)
65                 {
66                         if (partitionCount <= 0)
67                                 throw new ArgumentOutOfRangeException ("partitionCount");
68                         
69                         IEnumerator<KeyValuePair<long, T>>[] enumerators
70                                 = new IEnumerator<KeyValuePair<long, T>>[partitionCount];
71                         
72                         IEnumerator<T> src = source.GetEnumerator ();
73                         
74                         for (int i = 0; i < enumerators.Length; i++) {
75                                 enumerators[i] = GetPartitionEnumerator (src);
76                         }
77                         
78                         return enumerators;
79                 }
80                 
81                 IEnumerator<KeyValuePair<long, T>> GetPartitionEnumerator (IEnumerator<T> src)
82                 {
83                         int count = initialPartitionSize;
84                         List<T> list = new List<T> ();
85                         
86                         while (true) {
87                                 list.Clear ();
88                                 int ind = -1;
89                                 
90                                 lock (syncLock) {
91                                         ind = index;
92                                         
93                                         for (int i = 0; i < count; i++) {
94                                                 if (!src.MoveNext ()) {
95                                                         if (list.Count == 0)
96                                                                 yield break;
97                                                         else
98                                                                 break;
99                                                 }
100                                                 
101                                                 list.Add (src.Current);
102                                                 index++;
103                                         }                                       
104                                 }
105                                 
106                                 
107                                 
108                                 for (int i = 0; i < list.Count; i++)
109                                         yield return new KeyValuePair<long, T> (ind + i, list[i]);
110                                 
111                                 count *= partitionMultiplier;
112                         }
113                 }                                  
114         }
115 }
116 #endif