-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathWithout.cs
87 lines (79 loc) · 4.74 KB
/
Without.cs
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
using System.Collections.Generic;
using System.Linq;
namespace ExtraLinq
{
public static partial class EnumerableExtensions
{
/// <summary>
/// Returns all elements of <paramref name="source"/> without <paramref name="elements"/>.
/// Does not throw an exception if <paramref name="source"/> does not contain <paramref name="elements"/>.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{TSource}"/> to remove the specified elements from.</param>
/// <param name="elements">The elements to remove.</param>
/// <returns>
/// All elements of <paramref name="source"/> except <paramref name="elements"/>.
/// </returns>
public static IEnumerable<TSource> Without<TSource>(this IEnumerable<TSource> source, params TSource[] elements)
{
return Without(source, (IEnumerable<TSource>)elements);
}
/// <summary>
/// Returns all elements of <paramref name="source"/> without <paramref name="elements"/>.
/// Does not throw an exception if <paramref name="source"/> does not contain <paramref name="elements"/>.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{TSource}"/> to remove the specified elements from.</param>
/// <param name="elements">The elements to remove.</param>
/// <returns>
/// All elements of <paramref name="source"/> except <paramref name="elements"/>.
/// </returns>
public static IEnumerable<TSource> Without<TSource>(this IEnumerable<TSource> source, IEnumerable<TSource> elements)
{
ThrowIf.Argument.IsNull(source, "source");
ThrowIf.Argument.IsNull(elements, "elements");
return WithoutIterator(source, elements, EqualityComparer<TSource>.Default);
}
/// <summary>
/// Returns all elements of <paramref name="source"/> without <paramref name="elements"/> using the specified equality comparer to compare values.
/// Does not throw an exception if <paramref name="source"/> does not contain <paramref name="elements"/>.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{TSource}"/> to remove the specified elements from.</param>
/// <param name="equalityComparer">The equality comparer to use.</param>
/// <param name="elements">The elements to remove.</param>
/// <returns>
/// All elements of <paramref name="source"/> except <paramref name="elements"/>.
/// </returns>
public static IEnumerable<TSource> Without<TSource>(this IEnumerable<TSource> source,
IEqualityComparer<TSource> equalityComparer, params TSource[] elements)
{
return Without(source, equalityComparer, (IEnumerable<TSource>)elements);
}
/// <summary>
/// Returns all elements of <paramref name="source"/> without <paramref name="elements"/> using the specified equality comparer to compare values.
/// Does not throw an exception if <paramref name="source"/> does not contain <paramref name="elements"/>.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{TSource}"/> to remove the specified elements from.</param>
/// <param name="equalityComparer">The equality comparer to use.</param>
/// <param name="elements">The elements to remove.</param>
/// <returns>
/// All elements of <paramref name="source"/> except <paramref name="elements"/>.
/// </returns>
public static IEnumerable<TSource> Without<TSource>(this IEnumerable<TSource> source,
IEqualityComparer<TSource> equalityComparer, IEnumerable<TSource> elements)
{
ThrowIf.Argument.IsNull(source, "source");
ThrowIf.Argument.IsNull(elements, "elements");
ThrowIf.Argument.IsNull(equalityComparer, "equalityComparer");
return WithoutIterator(source, elements, equalityComparer);
}
private static IEnumerable<TSource> WithoutIterator<TSource>(IEnumerable<TSource> source,
IEnumerable<TSource> elementsToRemove, IEqualityComparer<TSource> comparer)
{
HashSet<TSource> elementsToRemoveSet = new HashSet<TSource>(elementsToRemove, comparer);
return source.Where(elem => !elementsToRemoveSet.Contains(elem));
}
}
}