5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 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.Threading;
29 using System.Collections;
30 using System.Collections.Generic;
32 namespace System.Linq.Parallel.QueryNodes
34 internal class QuerySelectManyNode<TSource, TCollection, TResult> : QueryStreamNode<TResult, TSource>
36 Func<TSource, IEnumerable<TCollection>> collectionSelector;
37 Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed;
38 Func<TSource, TCollection, TResult> resultSelector;
40 internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
41 Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed,
42 Func<TSource, TCollection, TResult> resultSelector)
45 this.collectionSelectorIndexed = collectionSelectorIndexed;
46 this.resultSelector = resultSelector;
49 internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
50 Func<TSource, IEnumerable<TCollection>> collectionSelector,
51 Func<TSource, TCollection, TResult> resultSelector)
52 : base (parent, false)
54 this.collectionSelector = collectionSelector;
55 this.resultSelector = resultSelector;
58 internal override IEnumerable<TResult> GetSequential ()
60 IEnumerable<TSource> source = Parent.GetSequential ();
63 source.SelectMany (collectionSelectorIndexed, resultSelector) :
64 source.SelectMany (collectionSelector, resultSelector);
67 internal override IList<IEnumerable<TResult>> GetEnumerablesIndexed (QueryOptions options)
69 return Parent.GetOrderedEnumerables (options)
70 .Select ((i) => GetEnumerableInternal (i,
71 (kv) => collectionSelectorIndexed (kv.Value, (int)kv.Key),
72 (e, c) => resultSelector (e.Value, c)))
76 internal override IList<IEnumerable<TResult>> GetEnumerablesNonIndexed (QueryOptions options)
78 return Parent.GetEnumerables (options)
79 .Select ((i) => GetEnumerableInternal (i,
81 (e, c) => resultSelector (e, c)))
85 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
87 var source = Parent.GetOrderedEnumerables (options);
88 var sizeRequests = new SizeRequest[source.Count];
91 Barrier barrier = new Barrier (source.Count, delegate (Barrier b) {
92 Array.Sort (sizeRequests, KeyComparator);
93 for (int i = 0; i < b.ParticipantCount; ++i) {
94 if (sizeRequests[i].Key == -1)
96 sizeRequests[i].Key = deviation;
97 deviation += sizeRequests[i].Size;
102 .Select ((i, ind) => GetOrderedEnumerableInternal (i, sizeRequests, ind, barrier))
106 IEnumerable<TResult> GetEnumerableInternal<T> (IEnumerable<T> source,
107 Func<T, IEnumerable<TCollection>> collectionner,
108 Func<T, TCollection, TResult> packer)
110 foreach (T element in source)
111 foreach (TCollection item in collectionner (element))
112 yield return packer (element, item);
115 IEnumerable<KeyValuePair<long, TResult>> GetOrderedEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source,
116 SizeRequest[] sizeRequests,
120 IEnumerator<KeyValuePair<long, TSource>> enumerator = source.GetEnumerator ();
121 bool isIndexed = IsIndexed;
125 KeyValuePair<long, TSource> element;
126 IEnumerable<TCollection> collection;
128 if (enumerator.MoveNext ()) {
129 element = enumerator.Current;
130 collection = isIndexed ?
131 collectionSelectorIndexed (element.Value, (int)element.Key) :
132 collectionSelector (element.Value);
134 var count = GetCount (ref collection);
136 sizeRequests[index].Update (element.Key, count, collection, element.Value);
139 barrier.SignalAndWait ();
141 long i = sizeRequests[index].Key;
142 collection = sizeRequests[index].Collection;
143 var elementValue = sizeRequests[index].ElementValue;
145 sizeRequests[index].Clear ();
150 foreach (TCollection item in collection)
151 yield return new KeyValuePair<long, TResult> (i++, resultSelector (elementValue, item));
154 barrier.RemoveParticipant ();
155 enumerator.Dispose ();
159 /* If getting Count is a O(1) operation (i.e. actual is a ICollection) then return it immediatly
160 * if not process the IEnumerable into a List and return the Count from that (i.e. enumerable
161 * processing will only happen once in case of e.g. a Linq query)
163 static int GetCount<T> (ref IEnumerable<T> actual)
165 ICollection coll = actual as ICollection;
169 var foo = actual.ToList ();
175 static int KeyComparator (SizeRequest e1, SizeRequest e2)
177 return e1.Key.CompareTo (e2.Key);
184 public IEnumerable<TCollection> Collection;
185 public TSource ElementValue;
187 public void Update (long k, int s, IEnumerable<TCollection> c, TSource ev)
200 ElementValue = default (TSource);