Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.Core / System.Linq.Parallel.QueryNodes / QueryWhereNode.cs
index fc6e007678138ad2025816ce6df76fa7faf136e4..fe9ca8a90edfa3c3cd003e19cc5a61cfbec87ff1 100644 (file)
@@ -24,7 +24,7 @@
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 
-#if NET_4_0
+#if NET_4_0 || MOBILE
 using System;
 using System.Linq;
 using System.Threading;
@@ -63,14 +63,14 @@ namespace System.Linq.Parallel.QueryNodes
                internal override IList<IEnumerable<TSource>> GetEnumerablesIndexed (QueryOptions options)
                {
                        return Parent.GetOrderedEnumerables (options)
-                               .Select ((i) => i.Where ((e) => indexedPredicate (e.Value, (int)e.Key)).Select ((e) => e.Value))
+                               .Select (i => i.Where (e => indexedPredicate (e.Value, (int)e.Key)).Select (e => e.Value))
                                .ToList ();
                }
 
                internal override IList<IEnumerable<TSource>> GetEnumerablesNonIndexed (QueryOptions options)
                {
                        return Parent.GetEnumerables (options)
-                               .Select ((i) => i.Where (predicate))
+                               .Select (i => i.Where (predicate))
                                .ToList ();
                }
 
@@ -78,20 +78,18 @@ namespace System.Linq.Parallel.QueryNodes
                {
                        IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
 
-                       Tuple<TSource, long, bool>[] store = new Tuple<TSource, long, bool>[sources.Count];
+                       ProcessingSlot[] store = new ProcessingSlot[sources.Count];
+                       Comparison<ProcessingSlot> arrayComparison = ArraySortMethod;
                        long lastIndex = 0;
 
                        Barrier barrier = new Barrier (sources.Count, delegate (Barrier b) {
                                // Sort the store
-                               Array.Sort (store, ArraySortMethod);
+                               Array.Sort (store, arrayComparison);
 
                                // Reassign a good index
                                int i = 0;
-
-                               for (i = 0; i < store.Length && store[i].Item3; i++) {
-                                       Tuple<TSource, long, bool> old = store[i];
-                                       store[i] = Tuple.Create (old.Item1, lastIndex + i, old.Item3);
-                               }
+                               for (i = 0; i < store.Length && store[i].IsValid; ++i)
+                                       store[i].Index = lastIndex + i;
 
                                // Update lastIndex for next round
                                lastIndex += i;
@@ -102,43 +100,42 @@ namespace System.Linq.Parallel.QueryNodes
                                .ToList ();
                }
 
-               static int ArraySortMethod (Tuple<TSource, long, bool> lhs, Tuple<TSource, long, bool> rhs)
+               static int ArraySortMethod (ProcessingSlot lhs, ProcessingSlot rhs)
                {
-                       if (lhs.Item3 && !rhs.Item3)
+                       if (lhs.IsValid && !rhs.IsValid)
                                return -1;
-                       if (!lhs.Item3 && rhs.Item3)
+                       if (!lhs.IsValid && rhs.IsValid)
                                return 1;
-                       if (!lhs.Item3 && !rhs.Item3)
+                       if (!lhs.IsValid && !rhs.IsValid)
                                return 0;
 
-                       return (lhs.Item2 < rhs.Item2) ? -1 : 1;
+                       return lhs.Index < rhs.Index ? -1 : 1;
                }
 
                IEnumerable<KeyValuePair<long, TSource>> GetEnumerator (IEnumerable<KeyValuePair<long, TSource>> source,
                                                                        Barrier barrier,
                                                                        CancellationToken token,
-                                                                       Tuple<TSource, long, bool>[] store, int index)
+                                                                       ProcessingSlot[] store, int index)
                {
                        IEnumerator<KeyValuePair<long, TSource>> current = source.GetEnumerator ();
-                       bool result;
-                       Tuple<TSource, long, bool> reset = Tuple.Create (default (TSource), long.MaxValue, false);
+                       bool isIndexed = IsIndexed;
 
                        try {
                                while (current.MoveNext ()) {
                                        KeyValuePair<long, TSource> curr = current.Current;
 
-                                       result = IsIndexed ? indexedPredicate (curr.Value, (int)curr.Key) : predicate (curr.Value);
-                                       store[index] = Tuple.Create (curr.Value, curr.Key, result);
+                                       bool result = isIndexed ? indexedPredicate (curr.Value, (int)curr.Key) : predicate (curr.Value);
+                                       store[index].Update (curr.Value, curr.Key, result);
 
                                        barrier.SignalAndWait (token);
 
-                                       Tuple<TSource, long, bool> value = store [index];
+                                       var value = store [index];
 
-                                       if (value.Item3)
-                                               yield return new KeyValuePair<long, TSource> (value.Item2, value.Item1);
+                                       if (value.IsValid)
+                                               yield return new KeyValuePair<long, TSource> (value.Index, value.Value);
 
                                        // Reset
-                                       store[index] = reset;
+                                       store[index].Clear ();
                                }
                        } finally {
                                // Remove our participation
@@ -146,6 +143,25 @@ namespace System.Linq.Parallel.QueryNodes
                                current.Dispose ();
                        }
                }
+
+               struct ProcessingSlot
+               {
+                       public TSource Value;
+                       public long Index;
+                       public bool IsValid;
+
+                       public void Update (TSource v, long i, bool t)
+                       {
+                               Value = v;
+                               Index = i;
+                               IsValid = t;
+                       }
+
+                       public void Clear ()
+                       {
+                               Update (default (TSource), long.MaxValue, false);
+                       }
+               }
        }
 }
 #endif