Some dictionary extensions.
[IEnumerableExtras.git] / C# / EnumerableCompare.cs
blob1df82cce8228554fcf5592460e6d100ddd9c998f
1 using System;
2 using System.Collections.Generic;
4 namespace IEnumerableExtras
6 /// <summary>
7 /// Compare <see cref="IEnumerable{T}"/> extension.
8 /// </summary>
9 public static class EnumerableCompare
11 /// <summary>
12 /// Compare this instance of <see cref="IEnumerable{TValue}"/> with another instance
13 /// if same type using equivalence function and produce a list of items present
14 /// in both collections and a list of items missing from second collection.
15 /// </summary>
16 /// <typeparam name="TValue"></typeparam>
17 /// <param name="a"></param>
18 /// <param name="b"></param>
19 /// <param name="missingFromOther"></param>
20 /// <param name="equivFunc"></param>
21 /// <returns></returns>
22 public static IEnumerable<Pair<TValue>> Compare< TValue >( this IEnumerable<TValue> a,
23 IEnumerable<TValue> b,
24 out IEnumerable<TValue> missingFromOther,
25 Func<TValue, TValue, bool> equivFunc )
27 List<Pair<TValue>> common = new List<Pair<TValue>>();
28 List<TValue> missing = new List<TValue>();
30 foreach ( TValue fromA in a )
32 bool found = false;
34 foreach ( TValue fromB in b )
36 if ( equivFunc( fromA, fromB ) )
38 common.Add( new Pair<TValue>( fromA, fromB ) );
39 found = true;
40 break;
44 if ( !found )
46 missing.Add( fromA );
50 missingFromOther = missing;
51 return common;