2 // OrderablePartitioner.cs
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
34 public abstract class OrderablePartitioner<TSource> : Partitioner<TSource>
36 bool keysOrderedInEachPartition;
37 bool keysOrderedAcrossPartitions;
40 protected OrderablePartitioner (bool keysOrderedInEachPartition,
41 bool keysOrderedAcrossPartitions,
42 bool keysNormalized) : base ()
44 this.keysOrderedInEachPartition = keysOrderedInEachPartition;
45 this.keysOrderedAcrossPartitions = keysOrderedAcrossPartitions;
46 this.keysNormalized = keysNormalized;
49 public override IEnumerable<TSource> GetDynamicPartitions ()
51 foreach (KeyValuePair<long, TSource> item in GetOrderableDynamicPartitions ())
52 yield return item.Value;
55 public override IList<IEnumerator<TSource>> GetPartitions (int partitionCount)
57 IEnumerator<TSource>[] temp = new IEnumerator<TSource>[partitionCount];
58 IList<IEnumerator<KeyValuePair<long, TSource>>> enumerators
59 = GetOrderablePartitions (partitionCount);
61 for (int i = 0; i < enumerators.Count; i++)
62 temp[i] = GetProxyEnumerator (enumerators[i]);
67 IEnumerator<TSource> GetProxyEnumerator (IEnumerator<KeyValuePair<long, TSource>> enumerator)
69 while (enumerator.MoveNext ())
70 yield return enumerator.Current.Value;
73 public abstract IList<IEnumerator<KeyValuePair<long, TSource>>> GetOrderablePartitions(int partitionCount);
75 public virtual IEnumerable<KeyValuePair<long, TSource>> GetOrderableDynamicPartitions()
77 if (!SupportsDynamicPartitions)
78 throw new NotSupportedException ();
84 public bool KeysOrderedInEachPartition {
86 return keysOrderedInEachPartition;
90 public bool KeysOrderedAcrossPartitions {
92 return keysOrderedAcrossPartitions;
96 public bool KeysNormalized {
98 return keysNormalized;