Partitioner.cs
Go to the documentation of this file.
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 #if NET_4_0
28 
29 using System;
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 #if NET_4_5
103  [MonoTODO]
104  public static OrderablePartitioner<TSource> Create<TSource> (IEnumerable<TSource> source,
105  EnumerablePartitionerOptions partitionerOptions)
106  {
107  throw new NotImplementedException ();
108  }
109 #endif
110  }
111 
112  public abstract class Partitioner<TSource>
113  {
114  protected Partitioner ()
115  {
116 
117  }
118 
119  public virtual IEnumerable<TSource> GetDynamicPartitions ()
120  {
121  if (!SupportsDynamicPartitions)
122  throw new NotSupportedException ();
123 
124  return null;
125  }
126 
127  public abstract IList<IEnumerator<TSource>> GetPartitions (int partitionCount);
128 
129  public virtual bool SupportsDynamicPartitions {
130  get {
131  return false;
132  }
133  }
134  }
135 }
136 #endif
virtual IEnumerable< TSource > GetDynamicPartitions()
Definition: Partitioner.cs:119
static OrderablePartitioner< Tuple< long, long > > Create(long fromInclusive, long toExclusive, long rangeSize)
Definition: Partitioner.cs:90
static OrderablePartitioner< Tuple< int, int > > Create(int fromInclusive, int toExclusive)
Definition: Partitioner.cs:57
static OrderablePartitioner< Tuple< int, int > > Create(int fromInclusive, int toExclusive, int rangeSize)
Definition: Partitioner.cs:68
static OrderablePartitioner< Tuple< long, long > > Create(long fromInclusive, long toExclusive)
Definition: Partitioner.cs:80