Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Core / System / Linq / Parallel / QueryOperators / QueryResults.cs
1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 //
8 // QueryResults.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     /// The QueryResults{T} is a class representing the results of the query. There may
22     /// be different ways the query results can be manipulated. Currently, two ways are
23     /// supported:
24     ///
25     /// 1. Open the query results as a partitioned stream by calling GivePartitionedStream
26     ///    and pass a generic action as an argument.
27     ///    
28     /// 2. Access individual elements of the results list by calling GetElement(index) and
29     ///    ElementsCount. This method of accessing the query results is available only if
30     ///    IsIndexible return true. 
31     /// </summary>
32     /// <typeparam name="T"></typeparam>
33     internal abstract class QueryResults<T> : IList<T>
34     {
35         //-----------------------------------------------------------------------------------
36         // Gets the query results represented as a partitioned stream. Instead of returning
37         // the PartitionedStream, we instead call recipient.Receive<TKey>(...). That way,
38         // the code that receives the partitioned stream has access to the TKey type.
39         //
40         // Arguments:
41         //    recipient - the object that the partitioned stream will be passed to
42         //
43
44         internal abstract void GivePartitionedStream(IPartitionedStreamRecipient<T> recipient);
45
46         //-----------------------------------------------------------------------------------
47         // Returns whether the query results are indexible. If this property is true, the
48         // user can call GetElement(index) and ElementsCount. If it is false, both
49         // GetElement(index) and ElementsCount should throw InvalidOperationException.
50         //
51
52         internal virtual bool IsIndexible
53         {
54             get { return false; }
55         }
56
57         //-----------------------------------------------------------------------------------
58         // Returns index-th element in the query results
59         //
60         // Assumptions:
61         //    IsIndexible returns true
62         //    0 <= index < ElementsCount
63         //    
64
65         internal virtual T GetElement(int index)
66         {
67             Contract.Assert(false, "GetElement property is not supported by non-indexible query results");
68             throw new NotSupportedException();
69         }
70
71         //-----------------------------------------------------------------------------------
72         // Returns the number of elements in the query results
73         //
74         // Assumptions:
75         //    IsIndexible returns true
76         //   
77
78         internal virtual int ElementsCount
79         {
80             get
81             {
82                 Contract.Assert(false, "ElementsCount property is not supported by non-indexible query results");
83                 throw new NotSupportedException();
84             }
85         }
86
87         //
88         // An assortment of methods we need to support in order to implement the IList interface
89         //
90
91         int IList<T>.IndexOf(T item)
92         {
93             throw new NotSupportedException();
94         }
95
96         void IList<T>.Insert(int index, T item)
97         {
98             throw new NotSupportedException();
99         }
100
101         void IList<T>.RemoveAt(int index)
102         {
103             throw new NotSupportedException();
104         }
105
106         public T this[int index]
107         {
108             get { return GetElement(index); }
109             set
110             {
111                 throw new NotSupportedException();
112             }
113         }
114
115         void ICollection<T>.Add(T item)
116         {
117             throw new NotSupportedException();
118         }
119
120         void ICollection<T>.Clear()
121         {
122             throw new NotSupportedException();
123         }
124
125         bool ICollection<T>.Contains(T item)
126         {
127             throw new NotSupportedException();
128         }
129
130         void ICollection<T>.CopyTo(T[] array, int arrayIndex)
131         {
132             throw new NotSupportedException();
133         }
134
135         public int Count
136         {
137             get { return ElementsCount; }
138         }
139
140         bool ICollection<T>.IsReadOnly
141         {
142             get { return true; }
143         }
144
145         bool ICollection<T>.Remove(T item)
146         {
147             throw new NotSupportedException();
148         }
149
150         IEnumerator<T> IEnumerable<T>.GetEnumerator()
151         {
152             for (int index = 0; index < Count; index++)
153             {
154                 yield return this[index];
155             }
156         }
157
158         IEnumerator IEnumerable.GetEnumerator()
159         {
160             return ((IEnumerable<T>)this).GetEnumerator();
161         }
162     }
163 }