Yet more parameter manual fixup
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / Partitioner.cs
1 // 
2 // Partitioner.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 using System;
28 using System.Collections.Generic;
29
30 #if NET_4_0 || BOOTSTRAP_NET_4_0
31
32 namespace System.Collections.Concurrent
33 {
34         using Partitioners;
35
36         public static class Partitioner
37         {
38                 public static OrderablePartitioner<TSource> Create<TSource> (IEnumerable<TSource> source)
39                 {
40                         IList<TSource> tempIList = source as IList<TSource>;
41                         if (tempIList != null)
42                                 return Create (tempIList, true);
43                         
44                         return new EnumerablePartitioner<TSource> (source);
45                 }
46                 
47                 public static OrderablePartitioner<TSource> Create<TSource> (TSource[] array, bool loadBalance)
48                 {
49                         return Create ((IList<TSource>)array, loadBalance);
50                 }
51                 
52                 public static OrderablePartitioner<TSource> Create<TSource> (IList<TSource> list, bool loadBalance)
53                 {
54                         return new ListPartitioner<TSource> (list);
55                 }
56                 
57                 public static OrderablePartitioner<Tuple<int, int>> Create (int fromInclusive,
58                                                                             int toExclusive)
59                 {
60                         // This formula that is somewhat non-straighforward was guessed based on MS output
61                         int rangeSize = (toExclusive - fromInclusive) / (Environment.ProcessorCount * 3);
62                         if (rangeSize < 1)
63                                 rangeSize = 1;
64
65                         return Create (fromInclusive, toExclusive, rangeSize);
66                 }
67
68                 public static OrderablePartitioner<Tuple<int, int>> Create (int fromInclusive,
69                                                                             int toExclusive,
70                                                                             int rangeSize)
71                 {
72                         if (fromInclusive >= toExclusive)
73                                 throw new ArgumentOutOfRangeException ("toExclusive");
74                         if (rangeSize <= 0)
75                                 throw new ArgumentOutOfRangeException ("rangeSize");
76
77                         return new UserRangePartitioner (fromInclusive, toExclusive, rangeSize);
78                 }
79
80                 public static OrderablePartitioner<Tuple<long, long>> Create (long fromInclusive,
81                                                                               long toExclusive)
82                 {
83                         long rangeSize = (toExclusive - fromInclusive) / (Environment.ProcessorCount * 3);
84                         if (rangeSize < 1)
85                                 rangeSize = 1;
86
87                         return Create (fromInclusive, toExclusive, rangeSize);
88                 }
89
90                 public static OrderablePartitioner<Tuple<long, long>> Create (long fromInclusive,
91                                                                               long toExclusive,
92                                                                               long rangeSize)
93                 {
94                         if (rangeSize <= 0)
95                                 throw new ArgumentOutOfRangeException ("rangeSize");
96                         if (fromInclusive >= toExclusive)
97                                 throw new ArgumentOutOfRangeException ("toExclusive");
98
99                         return new UserLongRangePartitioner (fromInclusive, toExclusive, rangeSize);
100                 }
101         }
102         
103         public abstract class Partitioner<TSource>
104         {
105                 protected Partitioner ()
106                 {
107                         
108                 }
109                 
110                 public virtual IEnumerable<TSource> GetDynamicPartitions ()
111                 {
112                         if (!SupportsDynamicPartitions)
113                                 throw new NotSupportedException ();
114                         
115                         return null;
116                 }
117                 
118                 public abstract IList<IEnumerator<TSource>> GetPartitions (int partitionCount);
119                 
120                 public virtual bool SupportsDynamicPartitions {
121                         get {
122                                 return false;
123                         }
124                 }
125         }
126 }
127 #endif