Debian Codegraph @

mcs/class/referencesource/mscorlib/system/collections/Concurrent/OrderablePartitioner.cs

raw
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 //
8 // OrderablePartitioner.cs
9 //
10 // <OWNER>Microsoft</OWNER>
11 //
12 //
13 //
14 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
15
16 using System;
17 using System.Collections.Generic;
18 using System.Security.Permissions;
19 using System.Threading;
20
21 namespace System.Collections.Concurrent
22 {
23
24 /// <summary>
25 /// Represents a particular manner of splitting an orderable data source into multiple partitions.
26 /// </summary>
27 /// <typeparam name="TSource">Type of the elements in the collection.</typeparam>
28 /// <remarks>
29 /// <para>
30 /// Each element in each partition has an integer index associated with it, which determines the relative
31 /// order of that element against elements in other partitions.
32 /// </para>
33 /// <para>
34 /// Inheritors of <see cref="OrderablePartitioner{TSource}"/> must adhere to the following rules:
35 /// <ol>
36 /// <li>All indices must be unique, such that there may not be duplicate indices. If all indices are not
37 /// unique, the output ordering may be scrambled.</li>
38 /// <li>All indices must be non-negative. If any indices are negative, consumers of the implementation
39 /// may throw exceptions.</li>
40 /// <li><see cref="GetPartitions"/> and <see cref="GetOrderablePartitions"/> should throw a
41 /// <see cref="T:System.ArgumentOutOfRangeException"/> if the requested partition count is less than or
42 /// equal to zero.</li>
43 /// <li><see cref="GetPartitions"/> and <see cref="GetOrderablePartitions"/> should always return a number
44 /// of enumerables equal to the requested partition count. If the partitioner runs out of data and cannot
45 /// create as many partitions as requested, an empty enumerator should be returned for each of the
46 /// remaining partitions. If this rule is not followed, consumers of the implementation may throw a <see
47 /// cref="T:System.InvalidOperationException"/>.</li>
48 /// <li><see cref="GetPartitions"/>, <see cref="GetOrderablePartitions"/>,
49 /// <see cref="GetDynamicPartitions"/>, and <see cref="GetOrderableDynamicPartitions"/>
50 /// should never return null. If null is returned, a consumer of the implementation may throw a
51 /// <see cref="T:System.InvalidOperationException"/>.</li>
52 /// <li><see cref="GetPartitions"/>, <see cref="GetOrderablePartitions"/>,
53 /// <see cref="GetDynamicPartitions"/>, and <see cref="GetOrderableDynamicPartitions"/>
54 /// should always return partitions that can fully and uniquely enumerate the input data source. All of
55 /// the data and only the data contained in the input source should be enumerated, with no duplication
56 /// that was not already in the input, unless specifically required by the particular partitioner's
57 /// design. If this is not followed, the output ordering may be scrambled.</li>
58 /// <li>If <see cref="KeysOrderedInEachPartition"/> returns true, each partition must return elements
59 /// with increasing key indices.</li>
60 /// <li>If <see cref="KeysOrderedAcrossPartitions"/> returns true, all the keys in partition numbered N
61 /// must be larger than all the keys in partition numbered N-1.</li>
62 /// <li>If <see cref="KeysNormalized"/> returns true, all indices must be monotonically increasing from
63 /// 0, though not necessarily within a single partition.</li>
64 /// </ol>
65 /// </para>
66 /// </remarks>
67 [HostProtection(Synchronization = true, ExternalThreading = true)]
68 public abstract class OrderablePartitioner<TSource> : Partitioner<TSource>
69 {
70 /// <summary>
71 /// Initializes a new instance of the <see cref="OrderablePartitioner{TSource}"/> class with the
72 /// specified constraints on the index keys.
73 /// </summary>
74 /// <param name="keysOrderedInEachPartition">
75 /// Indicates whether the elements in each partition are yielded in the order of
76 /// increasing keys.
77 /// </param>
78 /// <param name="keysOrderedAcrossPartitions">
79 /// Indicates whether elements in an earlier partition always come before
80 /// elements in a later partition. If true, each element in partition 0 has a smaller order key than
81 /// any element in partition 1, each element in partition 1 has a smaller order key than any element
82 /// in partition 2, and so on.
83 /// </param>
84 /// <param name="keysNormalized">
85 /// Indicates whether keys are normalized. If true, all order keys are distinct
86 /// integers in the range [0 .. numberOfElements-1]. If false, order keys must still be dictinct, but
87 /// only their relative order is considered, not their absolute values.
88 /// </param>
89 protected OrderablePartitioner(bool keysOrderedInEachPartition, bool keysOrderedAcrossPartitions, bool keysNormalized)
90 {
91 KeysOrderedInEachPartition = keysOrderedInEachPartition;
92 KeysOrderedAcrossPartitions = keysOrderedAcrossPartitions;
93 KeysNormalized = keysNormalized;
94 }
95
96 /// <summary>
97 /// Partitions the underlying collection into the specified number of orderable partitions.
98 /// </summary>
99 /// <remarks>
100 /// Each partition is represented as an enumerator over key-value pairs.
101 /// The value of the pair is the element itself, and the key is an integer which determines
102 /// the relative ordering of this element against other elements in the data source.
103 /// </remarks>
104 /// <param name="partitionCount">The number of partitions to create.</param>
105 /// <returns>A list containing <paramref name="partitionCount"/> enumerators.</returns>
106 public abstract IList<IEnumerator<KeyValuePair<long, TSource>>> GetOrderablePartitions(int partitionCount);
107
108 /// <summary>
109 /// Creates an object that can partition the underlying collection into a variable number of
110 /// partitions.
111 /// </summary>
112 /// <remarks>
113 /// <para>
114 /// The returned object implements the <see
115 /// cref="T:System.Collections.Generic.IEnumerable{TSource}"/> interface. Calling <see
116 /// cref="System.Collections.Generic.IEnumerable{TSource}.GetEnumerator">GetEnumerator</see> on the
117 /// object creates another partition over the sequence.
118 /// </para>
119 /// <para>
120 /// Each partition is represented as an enumerator over key-value pairs. The value in the pair is the element
121 /// itself, and the key is an integer which determines the relative ordering of this element against
122 /// other elements.
123 /// </para>
124 /// <para>
125 /// The <see cref="GetOrderableDynamicPartitions"/> method is only supported if the <see
126 /// cref="System.Collections.Concurrent.Partitioner{TSource}.SupportsDynamicPartitions">SupportsDynamicPartitions</see>
127 /// property returns true.
128 /// </para>
129 /// </remarks>
130 /// <returns>An object that can create partitions over the underlying data source.</returns>
131 /// <exception cref="NotSupportedException">Dynamic partitioning is not supported by this
132 /// partitioner.</exception>
133 public virtual IEnumerable<KeyValuePair<long, TSource>> GetOrderableDynamicPartitions()
134 {
135 throw new NotSupportedException(Environment.GetResourceString("Partitioner_DynamicPartitionsNotSupported"));
136 }
137
138 /// <summary>
139 /// Gets whether elements in each partition are yielded in the order of increasing keys.
140 /// </summary>
141 public bool KeysOrderedInEachPartition { get; private set; }
142
143 /// <summary>
144 /// Gets whether elements in an earlier partition always come before elements in a later partition.
145 /// </summary>
146 /// <remarks>
147 /// If <see cref="KeysOrderedAcrossPartitions"/> returns true, each element in partition 0 has a
148 /// smaller order key than any element in partition 1, each element in partition 1 has a smaller
149 /// order key than any element in partition 2, and so on.
150 /// </remarks>
151 public bool KeysOrderedAcrossPartitions { get; private set; }
152
153 /// <summary>
154 /// Gets whether order keys are normalized.
155 /// </summary>
156 /// <remarks>
157 /// If <see cref="KeysNormalized"/> returns true, all order keys are distinct integers in the range
158 /// [0 .. numberOfElements-1]. If the property returns false, order keys must still be dictinct, but
159 /// only their relative order is considered, not their absolute values.
160 /// </remarks>
161 public bool KeysNormalized { get; private set; }
162
163 /// <summary>
164 /// Partitions the underlying collection into the given number of ordered partitions.
165 /// </summary>
166 /// <remarks>
167 /// The default implementation provides the same behavior as <see cref="GetOrderablePartitions"/> except
168 /// that the returned set of partitions does not provide the keys for the elements.
169 /// </remarks>
170 /// <param name="partitionCount">The number of partitions to create.</param>
171 /// <returns>A list containing <paramref name="partitionCount"/> enumerators.</returns>
172 public override IList<IEnumerator<TSource>> GetPartitions(int partitionCount)
173 {
174 IList<IEnumerator<KeyValuePair<long, TSource>>> orderablePartitions = GetOrderablePartitions(partitionCount);
175
176 if (orderablePartitions.Count != partitionCount)
177 {
178 throw new InvalidOperationException("OrderablePartitioner_GetPartitions_WrongNumberOfPartitions");
179 }
180
181 IEnumerator<TSource>[] partitions = new IEnumerator<TSource>[partitionCount];
182 for (int i = 0; i < partitionCount; i++)
183 {
184 partitions[i] = new EnumeratorDropIndices(orderablePartitions[i]);
185 }
186 return partitions;
187 }
188
189 /// <summary>
190 /// Creates an object that can partition the underlying collection into a variable number of
191 /// partitions.
192 /// </summary>
193 /// <remarks>
194 /// <para>
195 /// The returned object implements the <see
196 /// cref="T:System.Collections.Generic.IEnumerable{TSource}"/> interface. Calling <see
197 /// cref="System.Collections.Generic.IEnumerable{TSource}.GetEnumerator">GetEnumerator</see> on the
198 /// object creates another partition over the sequence.
199 /// </para>
200 /// <para>
201 /// The default implementation provides the same behavior as <see cref="GetOrderableDynamicPartitions"/> except
202 /// that the returned set of partitions does not provide the keys for the elements.
203 /// </para>
204 /// <para>
205 /// The <see cref="GetDynamicPartitions"/> method is only supported if the <see
206 /// cref="System.Collections.Concurrent.Partitioner{TSource}.SupportsDynamicPartitions"/>
207 /// property returns true.
208 /// </para>
209 /// </remarks>
210 /// <returns>An object that can create partitions over the underlying data source.</returns>
211 /// <exception cref="NotSupportedException">Dynamic partitioning is not supported by this
212 /// partitioner.</exception>
213 public override IEnumerable<TSource> GetDynamicPartitions()
214 {
215 IEnumerable<KeyValuePair<long, TSource>> orderablePartitions = GetOrderableDynamicPartitions();
216 return new EnumerableDropIndices(orderablePartitions);
217 }
218
219 /// <summary>
220 /// Converts an enumerable over key-value pairs to an enumerable over values.
221 /// </summary>
222 private class EnumerableDropIndices : IEnumerable<TSource>, IDisposable
223 {
224 private readonly IEnumerable<KeyValuePair<long, TSource>> m_source;
225 public EnumerableDropIndices(IEnumerable<KeyValuePair<long, TSource>> source)
226 {
227 m_source = source;
228 }
229 public IEnumerator<TSource> GetEnumerator()
230 {
231 return new EnumeratorDropIndices(m_source.GetEnumerator());
232 }
233 IEnumerator IEnumerable.GetEnumerator()
234 {
235 return ((EnumerableDropIndices)this).GetEnumerator();
236 }
237 public void Dispose()
238 {
239 IDisposable d = m_source as IDisposable;
240 if (d != null)
241 {
242 d.Dispose();
243 }
244 }
245 }
246
247 private class EnumeratorDropIndices : IEnumerator<TSource>
248 {
249 private readonly IEnumerator<KeyValuePair<long, TSource>> m_source;
250 public EnumeratorDropIndices(IEnumerator<KeyValuePair<long, TSource>> source)
251 {
252 m_source = source;
253 }
254 public bool MoveNext()
255 {
256 return m_source.MoveNext();
257 }
258 public TSource Current
259 {
260 get
261 {
262 return m_source.Current.Value;
263 }
264 }
265 Object IEnumerator.Current
266 {
267 get
268 {
269 return ((EnumeratorDropIndices)this).Current;
270 }
271 }
272 public void Dispose()
273 {
274 m_source.Dispose();
275 }
276 public void Reset()
277 {
278 m_source.Reset();
279 }
280 }
281
282 }
283
284 }