Normalize line endings.
[mono.git] / mcs / class / System.Core / System.Linq / Internal / QueryNodes / QuerySelectManyNode.cs
1 #if NET_4_0
2 //
3 // QueryConcatNode.cs
4 //
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 //
8 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27
28 using System;
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
32
33 namespace System.Linq
34 {
35         internal class QuerySelectManyNode<TSource, TCollection, TResult> : QueryStreamNode<TResult, TSource>
36         {
37                 Func<TSource, IEnumerable<TCollection>> collectionSelector;
38                 Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed;
39                 Func<TSource, TCollection, TResult> resultSelector;
40                 
41                 internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
42                                               Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed,
43                                               Func<TSource, TCollection, TResult> resultSelector)
44                         : base (parent, true)
45                 {
46                         this.collectionSelectorIndexed = collectionSelectorIndexed;
47                         this.resultSelector = resultSelector;
48                 }
49                 
50                 internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
51                                               Func<TSource, IEnumerable<TCollection>> collectionSelector,
52                                               Func<TSource, TCollection, TResult> resultSelector)
53                         : base (parent, false)
54                 {
55                         this.collectionSelector = collectionSelector;
56                         this.resultSelector = resultSelector;
57                 }
58                 
59                 internal override IEnumerable<TResult> GetSequential ()
60                 {
61                         IEnumerable<TSource> source = Parent.GetSequential ();
62                         
63                         if (IsIndexed)
64                                 return source.SelectMany (collectionSelectorIndexed, resultSelector);
65                         else
66                                 return source.SelectMany (collectionSelector, resultSelector);
67                 }
68                 
69                 internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
70                 {
71                         IEnumerable<TResult>[] result = null;
72                         
73                         if (IsIndexed) {
74                                 IList<IEnumerable<KeyValuePair<long, TSource>>> enumerables = Parent.GetOrderedEnumerables (options);
75                                 result = new IEnumerable<TResult>[enumerables.Count];
76                                 
77                                 for (int i = 0; i < enumerables.Count; i++)
78                                         result[i] = GetEnumerableInternal<KeyValuePair<long, TSource>> (enumerables[i],
79                                                                                                         (kv) => collectionSelectorIndexed (kv.Value, (int)kv.Key),
80                                                                                                         (e, c) => resultSelector (e.Value, c));
81                         } else {
82                                 IList<IEnumerable<TSource>> enumerables = Parent.GetEnumerables (options);
83                                 result = new IEnumerable<TResult>[enumerables.Count];
84                                 
85                                 for (int i = 0; i < enumerables.Count; i++)
86                                         result[i] = GetEnumerableInternal<TSource> (enumerables[i],
87                                                                                     collectionSelector,
88                                                                                     (e, c) => resultSelector (e, c));
89                         }
90                         
91                         return result;
92                 }
93                 
94                 // This one is gonna be tricky
95                 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
96                 {
97                         throw new NotImplementedException ();
98                 }
99                 
100                 IEnumerable<TResult> GetEnumerableInternal<T> (IEnumerable<T> source,
101                                                                Func<T, IEnumerable<TCollection>> collectionner,
102                                                                Func<T, TCollection, TResult> packer)
103                 {
104                         foreach (T element in source)
105                                 foreach (TCollection item in collectionner (element))
106                                         yield return packer (element, item);
107                 }
108                 
109                 IEnumerable<KeyValuePair<long, TResult>> GetOrderedEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source,
110                                                                                        Barrier barrier)
111                 {
112                         foreach (KeyValuePair<long, TSource> element in source) {
113                                 IEnumerable<TCollection> collection = collectionSelectorIndexed (element.Value, (int)element.Key);
114                                 
115                                 foreach (TCollection item in collection)
116                                         yield return new KeyValuePair<long, TResult> (-1, resultSelector (element.Value, item));
117                         }
118                 }
119         }
120 }
121
122 #endif