1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
//
// QueryJoinNode.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2010 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if NET_4_0
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace System.Linq.Parallel.QueryNodes
{
internal class QueryJoinNode<TFirst, TSecond, TKey, TResult> : QueryMuxNode<TFirst, TSecond, TResult>
{
struct VSlot<T>
{
public readonly bool HasValue;
public readonly T Value;
public VSlot (T value)
{
HasValue = true;
Value = value;
}
}
Func<TFirst, TKey> firstKeySelector;
Func<TSecond, TKey> secondKeySelector;
Func<TFirst, TSecond, TResult> resultSelector;
IEqualityComparer<TKey> comparer;
internal QueryJoinNode (QueryBaseNode<TFirst> first,
QueryBaseNode<TSecond> second,
Func<TFirst, TKey> firstKeySelector,
Func<TSecond, TKey> secondKeySelector,
Func<TFirst, TSecond, TResult> resultSelector,
IEqualityComparer<TKey> comparer) : base (first, second)
{
this.firstKeySelector = firstKeySelector;
this.secondKeySelector = secondKeySelector;
this.resultSelector = resultSelector;
this.comparer = comparer;
}
internal override IEnumerable<TResult> GetSequential ()
{
return Parent.GetSequential ().Join (Second.GetSequential (),
firstKeySelector,
secondKeySelector,
resultSelector,
comparer);
}
internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
{
var first = Parent.GetEnumerables (options);
var second = Second.GetEnumerables (options);
if (first.Count != second.Count)
throw new InvalidOperationException ("Internal size mismatch");
var store = new TemporaryArea<TKey, Tuple<VSlot<TFirst>, VSlot<TSecond>>> (comparer);
return first
.Select ((f, i) => GetEnumerable (f, second[i], store, firstKeySelector, secondKeySelector, resultSelector))
.ToList ();
}
internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
{
var first = Parent.GetOrderedEnumerables (options);
var second = Second.GetOrderedEnumerables (options);
if (first.Count != second.Count)
throw new InvalidOperationException ("Internal size mismatch");
var store = new TemporaryArea<TKey, Tuple<VSlot<KeyValuePair<long, TFirst>>, VSlot<KeyValuePair<long, TSecond>>>> (comparer);
return first
.Select ((f, i) => GetEnumerable<KeyValuePair<long, TFirst>, KeyValuePair<long, TSecond>, KeyValuePair<long, TResult>> (f,
second[i],
store,
(e) => firstKeySelector (e.Value),
(e) => secondKeySelector (e.Value),
(e1, e2) => new KeyValuePair<long, TResult> (e1.Key, resultSelector (e1.Value, e2.Value))))
.ToList ();
}
IEnumerable<T> GetEnumerable<U, V, T> (IEnumerable<U> first,
IEnumerable<V> second,
TemporaryArea<TKey, Tuple<VSlot<U>, VSlot<V>>> store,
Func<U, TKey> fKeySelect,
Func<V, TKey> sKeySelect,
Func<U, V, T> resultor)
{
IEnumerator<U> eFirst = first.GetEnumerator ();
IEnumerator<V> eSecond = second.GetEnumerator ();
try {
bool fstHasCurrent = false, sndHasCurrent = false;
Tuple<VSlot<U>, VSlot<V>> kvp;
while ((fstHasCurrent = eFirst.MoveNext ()) & (sndHasCurrent = eSecond.MoveNext ())) {
U e1 = eFirst.Current;
V e2 = eSecond.Current;
TKey key1 = fKeySelect (e1);
TKey key2 = sKeySelect (e2);
if (comparer.Equals (key1, key2)) {
yield return resultor (e1, e2);
continue;
}
do {
if (store.TryRemove (key1, out kvp) && kvp.Item2.HasValue) {
yield return resultor (e1, kvp.Item2.Value);
break;
}
} while (!store.TryAdd (key1, Tuple.Create (new VSlot<U> (e1), new VSlot<V> ())));
do {
if (store.TryRemove (key2, out kvp) && kvp.Item1.HasValue) {
yield return resultor (kvp.Item1.Value, e2);
break;
}
} while (!store.TryAdd (key2, Tuple.Create (new VSlot<U> (), new VSlot<V> (e2))));
}
if (fstHasCurrent) {
do {
U e1 = eFirst.Current;
TKey key1 = fKeySelect (e1);
do {
if (store.TryRemove (key1, out kvp) && kvp.Item2.HasValue) {
yield return resultor (e1, kvp.Item2.Value);
break;
}
} while (!store.TryAdd (key1, Tuple.Create (new VSlot<U> (e1), new VSlot<V> ())));
} while (eFirst.MoveNext ());
}
if (sndHasCurrent) {
do {
V e2 = eSecond.Current;
TKey key2 = sKeySelect (e2);
do {
if (store.TryRemove (key2, out kvp) && kvp.Item1.HasValue) {
yield return resultor (kvp.Item1.Value, e2);
break;
}
} while (!store.TryAdd (key2, Tuple.Create (new VSlot<U> (), new VSlot<V> (e2))));
} while (eSecond.MoveNext ());
}
} finally {
eFirst.Dispose ();
eSecond.Dispose ();
}
}
}
}
#endif
|