31d072f3b071abf8a82c98833e6647b11160e028
[mono.git] / mcs / class / referencesource / System.Core / System / Linq / Parallel / Enumerables / ParallelEnumerableWrapper.cs
1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 //
8 // ParallelEnumerableWrapper.cs
9 //
10 // <OWNER>Microsoft</OWNER>
11 //
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
13
14 using System.Collections;
15 using System.Collections.Generic;
16 using System.Diagnostics.Contracts;
17
18 namespace System.Linq.Parallel
19 {
20     /// <summary>
21     /// A simple implementation of the ParallelQuery{object} interface which wraps an
22     /// underlying IEnumerable, such that it can be used in parallel queries.
23     /// </summary>
24     internal class ParallelEnumerableWrapper : ParallelQuery<object>
25     {
26
27         private readonly IEnumerable m_source; // The wrapped enumerable object.
28
29         //-----------------------------------------------------------------------------------
30         // Instantiates a new wrapper object.
31         //
32
33         internal ParallelEnumerableWrapper(Collections.IEnumerable source)
34             : base(QuerySettings.Empty)
35         {
36             Contract.Assert(source != null);
37             m_source = source;
38         }
39
40         internal override IEnumerator GetEnumeratorUntyped()
41         {
42             return m_source.GetEnumerator();
43         }
44
45         public override IEnumerator<object> GetEnumerator()
46         {
47             return new EnumerableWrapperWeakToStrong(m_source).GetEnumerator();
48         }
49     }
50     
51     /// <summary>
52     /// A simple implementation of the ParallelQuery{T} interface which wraps an
53     /// underlying IEnumerable{T}, such that it can be used in parallel queries.
54     /// </summary>
55     /// <typeparam name="T"></typeparam>
56     internal class ParallelEnumerableWrapper<T> : ParallelQuery<T>
57     {
58
59         private readonly IEnumerable<T> m_wrappedEnumerable; // The wrapped enumerable object.
60
61         //-----------------------------------------------------------------------------------
62         // Instantiates a new wrapper object.
63         //
64         // Arguments:
65         //     wrappedEnumerable   - the underlying enumerable object being wrapped
66         //
67         // Notes:
68         //     The analysisOptions and degreeOfParallelism settings are optional.  Passing null
69         //     indicates that the system defaults should be used instead.
70         //
71
72         internal ParallelEnumerableWrapper(IEnumerable<T> wrappedEnumerable)
73             : base(QuerySettings.Empty)
74         {
75             Contract.Assert(wrappedEnumerable != null, "wrappedEnumerable must not be null.");
76
77             m_wrappedEnumerable = wrappedEnumerable;
78         }
79
80         //-----------------------------------------------------------------------------------
81         // Retrieves the wrapped enumerable object.
82         //
83
84         internal IEnumerable<T> WrappedEnumerable
85         {
86             get { return m_wrappedEnumerable; }
87         }
88
89         //-----------------------------------------------------------------------------------
90         // Implementations of GetEnumerator that just delegate to the wrapped enumerable.
91         //
92
93         public override IEnumerator<T> GetEnumerator()
94         {
95             Contract.Assert(m_wrappedEnumerable != null);
96             return m_wrappedEnumerable.GetEnumerator();
97         }
98     }
99 }