Merge remote branch 'upstream/master'
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / Partitioner.cs
index 24987ae73bc058ad7c15a32bc6e1165a3df1a0b7..92b5665712cfbf2eea2fd2db9d956672ecd3d4ee 100644 (file)
@@ -1,4 +1,3 @@
-#if NET_4_0
 // 
 // Partitioner.cs
 //  
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 
+#if NET_4_0
+
 using System;
 using System.Collections.Generic;
 
 namespace System.Collections.Concurrent
 {
+       using Partitioners;
+
        public static class Partitioner
        {
                public static OrderablePartitioner<TSource> Create<TSource> (IEnumerable<TSource> source)
@@ -41,44 +44,59 @@ namespace System.Collections.Concurrent
                        return new EnumerablePartitioner<TSource> (source);
                }
                
-               public static OrderablePartitioner<TSource> Create<TSource> (TSource[] source, bool loadBalance)
+               public static OrderablePartitioner<TSource> Create<TSource> (TSource[] array, bool loadBalance)
                {
-                       return Create ((IList<TSource>)source, loadBalance);
+                       return Create ((IList<TSource>)array, loadBalance);
                }
                
-               public static OrderablePartitioner<TSource> Create<TSource> (IList<TSource> source, bool loadBalance)
+               public static OrderablePartitioner<TSource> Create<TSource> (IList<TSource> list, bool loadBalance)
                {
-                       return new ListPartitioner<TSource> (source);
+                       return new ListPartitioner<TSource> (list);
                }
                
-               [MonoTODO("What range size is supposed to be in context and what the result returned looks like")]
                public static OrderablePartitioner<Tuple<int, int>> Create (int fromInclusive,
-                                                                            int toExclusive)
+                                                                           int toExclusive)
                {
-                       return Create (fromInclusive, toExclusive, 1);
+                       // This formula that is somewhat non-straighforward was guessed based on MS output
+                       int rangeSize = (toExclusive - fromInclusive) / (Environment.ProcessorCount * 3);
+                       if (rangeSize < 1)
+                               rangeSize = 1;
+
+                       return Create (fromInclusive, toExclusive, rangeSize);
                }
-               
-               [MonoTODO("What range size is supposed to be in context and what the result returned looks like")]
+
                public static OrderablePartitioner<Tuple<int, int>> Create (int fromInclusive,
-                                                                            int toExclusive,
-                                                                            int rangeSize)
+                                                                           int toExclusive,
+                                                                           int rangeSize)
                {
-                       throw new NotImplementedException ();
+                       if (fromInclusive >= toExclusive)
+                               throw new ArgumentOutOfRangeException ("toExclusive");
+                       if (rangeSize <= 0)
+                               throw new ArgumentOutOfRangeException ("rangeSize");
+
+                       return new UserRangePartitioner (fromInclusive, toExclusive, rangeSize);
                }
-               
-               [MonoTODO("What range size is supposed to be in context and what the result returned looks like")]
+
                public static OrderablePartitioner<Tuple<long, long>> Create (long fromInclusive,
-                                                                              long toExclusive)
+                                                                             long toExclusive)
                {
-                       return Create (fromInclusive, toExclusive, 1);
+                       long rangeSize = (toExclusive - fromInclusive) / (Environment.ProcessorCount * 3);
+                       if (rangeSize < 1)
+                               rangeSize = 1;
+
+                       return Create (fromInclusive, toExclusive, rangeSize);
                }
-               
-               [MonoTODO("What range size is supposed to be in context and what the result returned looks like")]
+
                public static OrderablePartitioner<Tuple<long, long>> Create (long fromInclusive,
-                                                                              long toExclusive,
-                                                                              long rangeSize)
+                                                                             long toExclusive,
+                                                                             long rangeSize)
                {
-                       throw new NotImplementedException ();
+                       if (rangeSize <= 0)
+                               throw new ArgumentOutOfRangeException ("rangeSize");
+                       if (fromInclusive >= toExclusive)
+                               throw new ArgumentOutOfRangeException ("toExclusive");
+
+                       return new UserLongRangePartitioner (fromInclusive, toExclusive, rangeSize);
                }
        }