17 using System.Collections.Generic;
18 using System.Collections.Immutable;
19 using System.Linq.Expressions;
39 where TCollection : ICollection<T>,
new()
41 var result =
new TCollection();
42 var evaluator = comparison.GetEvaluator<T>();
43 foreach (var value
in values)
45 if (evaluator(value, reference))
62 SortedDictionary<TKey, TValue> values,
66 SortedDictionary<TKey, TValue> result;
67 if (comparison.
Type == ExpressionType.NotEqual)
69 result =
new SortedDictionary<TKey, TValue>(values);
70 result.Remove(reference);
74 result =
new SortedDictionary<TKey, TValue>();
75 if (comparison.
Type == ExpressionType.Equal)
78 if (values.TryGetValue(reference, out value))
80 result.Add(reference, value);
88 var breakAfterFailure =
92 var evaluator = comparison.GetEvaluator<TKey>();
93 foreach (var kvp
in values)
95 if (evaluator(kvp.Key, reference))
97 result.Add(kvp.Key, kvp.Value);
99 else if (breakAfterFailure)
116 ImmutableSortedDictionary<TKey, TValue> values,
120 if (comparison.
Type == ExpressionType.NotEqual)
122 return values.Remove(reference);
125 var result = ImmutableSortedDictionary<TKey, TValue>.Empty;
126 if (comparison.
Type == ExpressionType.Equal)
129 if (values.TryGetValue(reference, out value))
131 result = result.Add(reference, value);
139 var breakAfterFailure =
143 var evaluator = comparison.GetEvaluator<TKey>();
144 foreach (var kvp
in values)
146 if (evaluator(kvp.Key, reference))
148 result = result.Add(kvp.Key, kvp.Value);
150 else if (breakAfterFailure)
165 public static Tuple<ImmutableSortedDictionary<TKey, TValue>, ImmutableSortedDictionary<TKey, TValue>>
SplitBy<TKey, TValue>(
167 ImmutableSortedDictionary<TKey, TValue> values,
171 var matches = ImmutableSortedDictionary<TKey, TValue>.Empty;
172 var removed = ImmutableSortedDictionary<TKey, TValue>.Empty;
174 if (comparison.
Type == ExpressionType.NotEqual)
176 var match = values.Remove(reference);
178 return Tuple.Create(match, removed);
181 if (comparison.
Type == ExpressionType.Equal)
184 if (values.TryGetValue(reference, out value))
186 matches = matches.Add(reference, value);
188 return Tuple.Create(matches, removed);
192 return Tuple.Create(ImmutableSortedDictionary<TKey, TValue>.Empty, values);
195 var evaluator = comparison.GetEvaluator<TKey>();
196 foreach (var kvp
in values)
198 if (evaluator(kvp.Key, reference))
200 matches = matches.Add(kvp.Key, kvp.Value);
204 removed = removed.Add(kvp.Key, kvp.Value);
208 return Tuple.Create(matches, removed);