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
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
35 internal class QuerySetNode<TSource> : QueryMuxNode<TSource, TSource, TSource>
37 readonly SetInclusion setInclusion;
38 readonly IEqualityComparer<TSource> comparer;
40 internal QuerySetNode (SetInclusion setInclusion, IEqualityComparer<TSource> comparer,
41 QueryBaseNode<TSource> first, QueryBaseNode<TSource> second)
42 : base (first, second)
44 this.setInclusion = setInclusion;
45 this.comparer = comparer;
48 internal override IEnumerable<TSource> GetSequential ()
50 var first = Parent.GetSequential ();
51 var second = Second == null ? null : Second.GetSequential ();
53 // We try to do some guessing based on the default
54 switch (setInclusion) {
55 case SetInclusionDefaults.Union:
56 return first.Union (second, comparer);
57 case SetInclusionDefaults.Intersect:
58 return first.Intersect (second, comparer);
59 case SetInclusionDefaults.Except:
60 return first.Except (second, comparer);
61 case SetInclusionDefaults.Distinct:
62 return first.Distinct (comparer);
65 // Default is we return the bare source enumerable
69 internal override IList<IEnumerable<TSource>> GetEnumerables (QueryOptions options)
71 var first = Parent.GetEnumerables (options);
72 var second = Second.GetEnumerables (options);
74 var checker = new ConcurrentSkipList<TSource> (comparer);
75 InitConcurrentSkipList (checker, second, (e) => e);
78 .Select ((f, i) => GetEnumerable<TSource> (f, second[i], checker, (e) => e))
82 internal override IList<IEnumerable<KeyValuePair<long, TSource>>> GetOrderedEnumerables (QueryOptions options)
84 var first = Parent.GetOrderedEnumerables (options);
85 var second = Second.GetOrderedEnumerables (options);
87 var checker = new ConcurrentSkipList<TSource> (comparer);
88 InitConcurrentSkipList (checker, second, (e) => e.Value);
91 .Select ((f, i) => GetEnumerable<KeyValuePair<long, TSource>> (f, second[i], checker, (e) => e.Value))
95 void InitConcurrentSkipList<TExtract> (ConcurrentSkipList<TSource> checker,
96 IList<IEnumerable<TExtract>> feeds,
97 Func<TExtract, TSource> extractor)
99 if ((setInclusion & SetInclusion.Preload) == 0)
102 foreach (IEnumerable<TExtract> feed in feeds)
103 foreach (TExtract item in feed)
104 checker.TryAdd (extractor (item));
107 IEnumerable<TExtract> GetEnumerable<TExtract> (IEnumerable<TExtract> first,
108 IEnumerable<TExtract> second,
109 ConcurrentSkipList<TSource> checker,
110 Func<TExtract, TSource> extractor)
112 IEnumerator<TExtract> eFirst = first.GetEnumerator ();
113 IEnumerator<TExtract> eSecond = second == null ? null : second.GetEnumerator ();
115 IEnumerator<TExtract> current = eFirst;
116 bool outInclusion = (setInclusion & SetInclusion.Out) > 0;
117 bool preload = (setInclusion & SetInclusion.Preload) > 0;
118 bool relaxed = (setInclusion & SetInclusion.Relaxed) > 0;
121 while (current != null) {
122 while (current.MoveNext ()) {
123 bool result = relaxed ?
124 checker.Contains (extractor (current.Current)) : checker.TryAdd (extractor (current.Current));
126 if ((result && outInclusion)
127 || (!result && !outInclusion))
128 yield return current.Current;
131 if (current == eFirst && !preload)