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] = new ProxyEnumerator (enumerators[i]);
68 IEnumerator<TSource> GetProxyEnumerator (IEnumerator<KeyValuePair<long, TSource>> enumerator)
70 while (enumerator.MoveNext ())
71 yield return enumerator.Current.Value;
74 public abstract IList<IEnumerator<KeyValuePair<long, TSource>>> GetOrderablePartitions(int partitionCount);
76 public virtual IEnumerable<KeyValuePair<long, TSource>> GetOrderableDynamicPartitions()
78 if (!SupportsDynamicPartitions)
79 throw new NotSupportedException ();
84 public bool KeysOrderedInEachPartition {
86 return keysOrderedInEachPartition;
90 public bool KeysOrderedAcrossPartitions {
92 return keysOrderedAcrossPartitions;
96 public bool KeysNormalized {
98 return keysNormalized;
102 class ProxyEnumerator : IEnumerator<TSource>, IDisposable
104 IEnumerator<KeyValuePair<long, TSource>> internalEnumerator;
106 internal ProxyEnumerator (IEnumerator<KeyValuePair<long, TSource>> enumerator)
108 internalEnumerator = enumerator;
111 public void Dispose ()
113 internalEnumerator.Dispose ();
116 public bool MoveNext ()
118 if (!internalEnumerator.MoveNext ())
121 Current = internalEnumerator.Current.Value;
128 internalEnumerator.Reset ();
131 object IEnumerator.Current {
137 public TSource Current {