Switch from manually creating result sets to creating them with a Linq query. Replaced some type with var. Split indexed and non indexed call in two different virtual method call.
internal override IList<IEnumerable<KeyValuePair<long, object>>> GetOrderedEnumerables (QueryOptions options)
{
- IList<IEnumerable<KeyValuePair<long, T>>> src = Parent.GetOrderedEnumerables (options);
- IEnumerable<KeyValuePair<long, object>>[] result = new IEnumerable<KeyValuePair<long, object>> [src.Count];
-
- for (int i = 0; i < src.Count; i++)
- result[i] = src[i].Select ((e) => new KeyValuePair<long, object> (e.Key, (object)e.Value));
-
- return result;
+ return Parent.GetOrderedEnumerables (options)
+ .Select ((i) => i.Select ((e) => new KeyValuePair<long, object> (e.Key, (object)e.Value)))
+ .ToArray ();
}
internal override IList<IEnumerable<object>> GetEnumerables (QueryOptions options)
{
- IList<IEnumerable<T>> src = Parent.GetEnumerables (options);
- IEnumerable<object>[] result = new IEnumerable<object> [src.Count];
-
- for (int i = 0; i < src.Count; i++)
- result[i] = src[i].Cast<object> ();
-
- return result;
+ return Parent.GetEnumerables (options)
+ .Select ((i) => i.Cast<object> ())
+ .ToArray ();
}
}
}
internal override IList<IEnumerable<TSource>> GetEnumerables (QueryOptions options)
{
- IList<IEnumerable<TSource>> first = Parent.GetEnumerables (options);
- IList<IEnumerable<TSource>> second = Second.GetEnumerables (options);
+ var second = Second.GetEnumerables (options);
- IEnumerable<TSource>[] result = new IEnumerable<TSource>[first.Count];
-
- for (int i = 0; i < result.Length; i++)
- result[i] = CombineEnumerables (first[i], second[i]);
-
- return result;
+ return Parent.GetEnumerables (options)
+ .Select ((f, i) => CombineEnumerables (f, second[i]))
+ .ToArray ();
}
internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
{
- IList<IEnumerable<KeyValuePair<long, TSource>>> first = Parent.GetOrderedEnumerables (options);
- IList<IEnumerable<KeyValuePair<long, TSource>>> second = Second.GetOrderedEnumerables (options);
-
- IEnumerable<KeyValuePair<long, TSource>>[] result = new IEnumerable<KeyValuePair<long, TSource>>[first.Count];
-
- for (int i = 0; i < result.Length; i++)
- result[i] = CombineEnumerables (first[i], second[i]);
+ var second = Second.GetOrderedEnumerables (options);
- return result;
+ return Parent.GetOrderedEnumerables (options)
+ .Select ((f, i) => CombineEnumerables (f, second[i]))
+ .ToArray ();
}
internal override IEnumerable<TSource> GetSequential ()
{
IList<IEnumerable<TSource>> enumerables = Parent.GetEnumerables (options);
CountdownEvent evt = new CountdownEvent (enumerables.Count);
- IEnumerable<TSource>[] result = new IEnumerable<TSource>[enumerables.Count];
-
- for (int i = 0; i < enumerables.Count; i++)
- result[i] = GetEnumerableInternal<TSource> (enumerables[i], evt,
- (s) => s);
-
- return result;
+
+ return enumerables
+ .Select ((e) => GetEnumerableInternal<TSource> (e,
+ evt,
+ (s) => s))
+ .ToArray ();
}
internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
{
IList<IEnumerable<KeyValuePair<long, TSource>>> enumerables = Parent.GetOrderedEnumerables (options);
CountdownEvent evt = new CountdownEvent (enumerables.Count);
- IEnumerable<KeyValuePair<long, TSource>>[] result = new IEnumerable<KeyValuePair<long, TSource>>[enumerables.Count];
-
- for (int i = 0; i < enumerables.Count; i++)
- result[i] = GetEnumerableInternal<KeyValuePair<long, TSource>> (enumerables[i], evt,
- (s) => new KeyValuePair<long, TSource> (0, s));
-
- return result;
+
+ return enumerables
+ .Select ((e) => GetEnumerableInternal<KeyValuePair<long, TSource>> (e,
+ evt,
+ (s) => new KeyValuePair<long, TSource> (0, s)))
+ .ToArray ();
}
IEnumerable<TSecond> GetEnumerableInternal<TSecond> (IEnumerable<TSecond> source,
}
internal override IList<IEnumerable<KeyValuePair<long, IGrouping<TKey, TElement>>>> GetOrderedEnumerables (QueryOptions options)
- {
- //ConcurrentLookup<TKey, TElement> lookup = new ConcurrentLookup<TKey, TElement> ();
-
- //ParallelExecuter.ProcessAndBlock (Parent, (e) => lookup.Add (keySelector (e), elementSelector (e)));
-
+ {
throw new System.NotImplementedException();
}
{
IEnumerable<TSource> source = Parent.GetSequential ();
- if (IsIndexed)
- return source.SelectMany (collectionSelectorIndexed, resultSelector);
- else
- return source.SelectMany (collectionSelector, resultSelector);
+ return IsIndexed ?
+ source.SelectMany (collectionSelectorIndexed, resultSelector) :
+ source.SelectMany (collectionSelector, resultSelector);
}
- internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
+ internal override IList<IEnumerable<TResult>> GetEnumerablesIndexed (QueryOptions options)
{
- IEnumerable<TResult>[] result = null;
-
- if (IsIndexed) {
- IList<IEnumerable<KeyValuePair<long, TSource>>> enumerables = Parent.GetOrderedEnumerables (options);
- result = new IEnumerable<TResult>[enumerables.Count];
-
- for (int i = 0; i < enumerables.Count; i++)
- result[i] = GetEnumerableInternal<KeyValuePair<long, TSource>> (enumerables[i],
- (kv) => collectionSelectorIndexed (kv.Value, (int)kv.Key),
- (e, c) => resultSelector (e.Value, c));
- } else {
- IList<IEnumerable<TSource>> enumerables = Parent.GetEnumerables (options);
- result = new IEnumerable<TResult>[enumerables.Count];
-
- for (int i = 0; i < enumerables.Count; i++)
- result[i] = GetEnumerableInternal<TSource> (enumerables[i],
- collectionSelector,
- (e, c) => resultSelector (e, c));
- }
-
- return result;
+ return Parent.GetOrderedEnumerables (options)
+ .Select ((i) => GetEnumerableInternal (i,
+ (kv) => collectionSelectorIndexed (kv.Value, (int)kv.Key),
+ (e, c) => resultSelector (e.Value, c)))
+ .ToArray ();
+ }
+
+ internal override IList<IEnumerable<TResult>> GetEnumerablesNonIndexed (QueryOptions options)
+ {
+ return Parent.GetEnumerables (options)
+ .Select ((i) => GetEnumerableInternal (i,
+ collectionSelector,
+ (e, c) => resultSelector (e, c)))
+ .ToArray ();
}
// This one is gonna be tricky
internal override IEnumerable<TResult> GetSequential ()
{
- if (IsIndexed)
- return Parent.GetSequential ().Select (indexedSelector);
- else
- return Parent.GetSequential ().Select (selector);
+ return IsIndexed ?
+ Parent.GetSequential ().Select (indexedSelector) :
+ Parent.GetSequential ().Select (selector);
}
- internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
+ internal override IList<IEnumerable<TResult>> GetEnumerablesIndexed (QueryOptions options)
{
- if (IsIndexed) {
- IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
- IEnumerable<TResult>[] results = new IEnumerable<TResult>[sources.Count];
- for (int i = 0; i < results.Length; i++)
- results[i] = sources[i].Select ((e) => indexedSelector (e.Value, (int)e.Key));
- return results;
- } else {
- IList<IEnumerable<TSource>> sources = Parent.GetEnumerables (options);
- IEnumerable<TResult>[] results = new IEnumerable<TResult>[sources.Count];
+ return Parent.GetOrderedEnumerables (options)
+ .Select ((i) => i.Select ((e) => indexedSelector (e.Value, (int)e.Key)))
+ .ToArray ();
+ }
- for (int i = 0; i < results.Length; i++)
- results[i] = sources[i].Select (selector);
- return results;
- }
+ internal override IList<IEnumerable<TResult>> GetEnumerablesNonIndexed (QueryOptions options)
+ {
+ return Parent.GetEnumerables (options)
+ .Select ((i) => i.Select (selector))
+ .ToArray ();
}
internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
{
- IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
- IEnumerable<KeyValuePair<long, TResult>>[] results = new IEnumerable<KeyValuePair<long, TResult>>[sources.Count];
-
- if (IsIndexed) {
- for (int i = 0; i < results.Length; i++)
- results[i] = sources[i].
- Select ((e) => new KeyValuePair<long, TResult> (e.Key, indexedSelector (e.Value, (int)e.Key)));
- } else {
- for (int i = 0; i < results.Length; i++)
- results[i] = sources[i].
- Select ((e) => new KeyValuePair<long, TResult> (e.Key, selector (e.Value)));
- }
-
- return results;
+ return Parent.GetOrderedEnumerables (options)
+ .Select ((i) =>
+ IsIndexed ?
+ i.Select ((e) => new KeyValuePair<long, TResult> (e.Key, indexedSelector (e.Value, (int)e.Key))) :
+ i.Select ((e) => new KeyValuePair<long, TResult> (e.Key, selector (e.Value))))
+ .ToArray ();
}
}
}
internal override IEnumerable<TSource> GetSequential ()
{
- IEnumerable<TSource> first = Parent.GetSequential ();
- IEnumerable<TSource> second = Second == null ? null : Second.GetSequential ();
+ var first = Parent.GetSequential ();
+ var second = Second == null ? null : Second.GetSequential ();
// We try to do some guessing based on the default
switch (setInclusion) {
internal override IList<IEnumerable<TSource>> GetEnumerables (QueryOptions options)
{
- IList<IEnumerable<TSource>> first = Parent.GetEnumerables (options);
- IList<IEnumerable<TSource>> second = Second.GetEnumerables (options);
-
- IEnumerable<TSource>[] result = new IEnumerable<TSource>[first.Count];
- ConcurrentSkipList<TSource> checker = new ConcurrentSkipList<TSource> (comparer);
+ var first = Parent.GetEnumerables (options);
+ var second = Second.GetEnumerables (options);
+ var checker = new ConcurrentSkipList<TSource> (comparer);
InitConcurrentSkipList (checker, second, (e) => e);
- for (int i = 0; i < result.Length; i++)
- result[i] = GetEnumerable<TSource> (first[i], second[i], checker, (e) => e);
-
- return result;
+ return first
+ .Select ((f, i) => GetEnumerable<TSource> (f, second[i], checker, (e) => e))
+ .ToArray ();
}
internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
{
- IList<IEnumerable<KeyValuePair<long, TSource>>> first = Parent.GetOrderedEnumerables (options);
- IList<IEnumerable<KeyValuePair<long, TSource>>> second = Second.GetOrderedEnumerables (options);
+ var first = Parent.GetOrderedEnumerables (options);
+ var second = Second.GetOrderedEnumerables (options);
- IEnumerable<KeyValuePair<long, TSource>>[] result = new IEnumerable<KeyValuePair<long, TSource>>[first.Count];
- ConcurrentSkipList<TSource> checker = new ConcurrentSkipList<TSource> (comparer);
-
+ var checker = new ConcurrentSkipList<TSource> (comparer);
InitConcurrentSkipList (checker, second, (e) => e.Value);
- for (int i = 0; i < result.Length; i++)
- result[i] = GetEnumerable<KeyValuePair<long, TSource>> (first[i], second[i], checker, (e) => e.Value);
-
- return result;
+ return first
+ .Select ((f, i) => GetEnumerable<KeyValuePair<long, TSource>> (f, second[i], checker, (e) => e.Value))
+ .ToArray ();
}
void InitConcurrentSkipList<TExtract> (ConcurrentSkipList<TSource> checker,
this.isIndexed = isIndexed;
}
+ internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
+ {
+ if (IsIndexed)
+ return GetEnumerablesIndexed (options);
+ else
+ return GetEnumerablesNonIndexed (options);
+ }
+
+ internal virtual IList<IEnumerable<T>> GetEnumerablesIndexed (QueryOptions options)
+ {
+ return null;
+ }
+
+ internal virtual IList<IEnumerable<T>> GetEnumerablesNonIndexed (QueryOptions options)
+ {
+ return null;
+ }
+
public override void Visit (INodeVisitor visitor)
{
visitor.Visit<T, TParent> (this);
return parent.Where (predicate);
}
- internal override IList<IEnumerable<TSource>> GetEnumerables (QueryOptions options)
+ internal override IList<IEnumerable<TSource>> GetEnumerablesIndexed (QueryOptions options)
{
- if (IsIndexed) {
- IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
- IEnumerable<TSource>[] results = new IEnumerable<TSource>[sources.Count];
-
- for (int i = 0; i < results.Length; i++)
- results[i] =
- sources[i].Where ((e) => indexedPredicate (e.Value, (int)e.Key)).Select ((e) => e.Value);
- return results;
- } else {
- IList<IEnumerable<TSource>> sources = Parent.GetEnumerables (options);
- IEnumerable<TSource>[] results = new IEnumerable<TSource>[sources.Count];
-
- for (int i = 0; i < results.Length; i++)
- results[i] = sources[i].Where (predicate);
- return results;
- }
+ return Parent.GetOrderedEnumerables (options)
+ .Select ((i) => i.Where ((e) => indexedPredicate (e.Value, (int)e.Key)).Select ((e) => e.Value))
+ .ToArray ();
+ }
+
+ internal override IList<IEnumerable<TSource>> GetEnumerablesNonIndexed (QueryOptions options)
+ {
+ return Parent.GetEnumerables (options)
+ .Select ((i) => i.Where (predicate))
+ .ToArray ();
}
internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
{
IList<IEnumerable<KeyValuePair<long, TSource>>> sources = Parent.GetOrderedEnumerables (options);
- IEnumerable<KeyValuePair<long, TSource>>[] results = new IEnumerable<KeyValuePair<long, TSource>>[sources.Count];
- Tuple<TSource, long, bool>[] store = new Tuple<TSource, long, bool>[results.Length];
+ Tuple<TSource, long, bool>[] store = new Tuple<TSource, long, bool>[sources.Count];
long lastIndex = 0;
- Barrier barrier = new Barrier (results.Length, delegate (Barrier b) {
+ Barrier barrier = new Barrier (sources.Count, delegate (Barrier b) {
// Sort the store
Array.Sort (store, ArraySortMethod);
lastIndex += i;
});
- for (int j = 0; j < results.Length; j++)
- results[j] = GetEnumerator (sources[j], barrier, store, j);
-
- return results;
+ return sources
+ .Select ((s, i) => GetEnumerator (s, barrier, store, i))
+ .ToArray ();
}
static int ArraySortMethod (Tuple<TSource, long, bool> lhs, Tuple<TSource, long, bool> rhs)
internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
{
- IList<IEnumerable<TFirst>> first = Parent.GetEnumerables (options);
- IList<IEnumerable<TSecond>> second = Second.GetEnumerables (options);
+ var first = Parent.GetEnumerables (options);
+ var second = Second.GetEnumerables (options);
if (first.Count != second.Count)
throw new InvalidOperationException ("Internal size mismatch");
- IEnumerable<TResult>[] result = new IEnumerable<TResult>[first.Count];
-
- for (int i = 0; i < result.Length; i++)
- result[i] = GetEnumerable (first[i], second[i]);
-
- return result;
+ return first
+ .Select ((f, i) => GetEnumerable (f, second[i]))
+ .ToArray ();
}
IEnumerable<TResult> GetEnumerable (IEnumerable<TFirst> first, IEnumerable<TSecond> second)
IEnumerator<TFirst> eFirst = first.GetEnumerator ();
IEnumerator<TSecond> eSecond = second.GetEnumerator ();
- while (eFirst.MoveNext ()) {
- if (!eSecond.MoveNext ())
- yield break;
+ try {
+ while (eFirst.MoveNext ()) {
+ if (!eSecond.MoveNext ())
+ yield break;
- yield return resultSelector (eFirst.Current, eSecond.Current);
+ yield return resultSelector (eFirst.Current, eSecond.Current);
+ }
+ } finally {
+ eFirst.Dispose ();
+ eSecond.Dispose ();
}
}
internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
{
- IList<IEnumerable<KeyValuePair<long, TFirst>>> first = Parent.GetOrderedEnumerables (options);
- IList<IEnumerable<KeyValuePair<long, TSecond>>> second = Second.GetOrderedEnumerables (options);
+ var first = Parent.GetOrderedEnumerables (options);
+ var second = Second.GetOrderedEnumerables (options);
if (first.Count != second.Count)
throw new InvalidOperationException ("Internal size mismatch");
- IEnumerable<KeyValuePair<long, TResult>>[] result = new IEnumerable<KeyValuePair<long, TResult>>[first.Count];
-
- KeyValuePair<long, TFirst>[] store1 = new KeyValuePair<long, TFirst>[result.Length];
- KeyValuePair<long, TSecond>[] store2 = new KeyValuePair<long, TSecond>[result.Length];
+ var store1 = new KeyValuePair<long, TFirst>[first.Count];
+ var store2 = new KeyValuePair<long, TSecond>[second.Count];
- Barrier barrier = new Barrier (result.Length, delegate {
+ var barrier = new Barrier (first.Count, delegate {
Array.Sort (store1, (e1, e2) => e1.Key.CompareTo (e2.Key));
Array.Sort (store2, (e1, e2) => e1.Key.CompareTo (e2.Key));
});
- for (int i = 0; i < result.Length; i++)
- result[i] = GetEnumerable (first[i], second[i], i, store1, store2, barrier);
-
- return result;
+ return first
+ .Select ((f, i) => GetEnumerable (f, second[i], i , store1, store2, barrier))
+ .ToArray ();
}
IEnumerable<KeyValuePair<long, TResult>> GetEnumerable (IEnumerable<KeyValuePair<long, TFirst>> first,