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
28 using System.Collections.Generic;
30 #if NET_4_0 || BOOTSTRAP_NET_4_0
32 namespace System.Collections.Concurrent
36 public static class Partitioner
38 public static OrderablePartitioner<TSource> Create<TSource> (IEnumerable<TSource> source)
40 IList<TSource> tempIList = source as IList<TSource>;
41 if (tempIList != null)
42 return Create (tempIList, true);
44 return new EnumerablePartitioner<TSource> (source);
47 public static OrderablePartitioner<TSource> Create<TSource> (TSource[] source, bool loadBalance)
49 return Create ((IList<TSource>)source, loadBalance);
52 public static OrderablePartitioner<TSource> Create<TSource> (IList<TSource> source, bool loadBalance)
54 return new ListPartitioner<TSource> (source);
57 public static OrderablePartitioner<Tuple<int, int>> Create (int fromInclusive,
60 // This formula that is somewhat non-straighforward was guessed based on MS output
61 int rangeSize = (toExclusive - fromInclusive) / (Environment.ProcessorCount * 3);
65 return Create (fromInclusive, toExclusive, rangeSize);
68 public static OrderablePartitioner<Tuple<int, int>> Create (int fromInclusive,
72 if (fromInclusive >= toExclusive)
73 throw new ArgumentOutOfRangeException ("toExclusive");
75 throw new ArgumentOutOfRangeException ("rangeSize");
77 return new UserRangePartitioner (fromInclusive, toExclusive, rangeSize);
80 public static OrderablePartitioner<Tuple<long, long>> Create (long fromInclusive,
83 long rangeSize = (toExclusive - fromInclusive) / (Environment.ProcessorCount * 3);
87 return Create (fromInclusive, toExclusive, rangeSize);
90 public static OrderablePartitioner<Tuple<long, long>> Create (long fromInclusive,
95 throw new ArgumentOutOfRangeException ("rangeSize");
96 if (fromInclusive >= toExclusive)
97 throw new ArgumentOutOfRangeException ("toExclusive");
99 return new UserLongRangePartitioner (fromInclusive, toExclusive, rangeSize);
103 public abstract class Partitioner<TSource>
105 protected Partitioner ()
110 public virtual IEnumerable<TSource> GetDynamicPartitions ()
112 if (!SupportsDynamicPartitions)
113 throw new NotSupportedException ();
118 public abstract IList<IEnumerator<TSource>> GetPartitions (int partitionCount);
120 public virtual bool SupportsDynamicPartitions {