-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUse-SetOperator.ps1
52 lines (50 loc) · 2.19 KB
/
Use-SetOperator.ps1
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
$CSharpDictLinqOpLib = @"
using System.Collections.Generic;
using System.Linq;
namespace SetToolbox
{
public class DictCompareOnKeyOnly : IEqualityComparer<KeyValuePair<string, string>>
{
public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
{
return x.Key.Equals(y.Key);
}
public int GetHashCode(KeyValuePair<string, string> obj)
{
return obj.Key.GetHashCode();
}
}
public static class SetOperator
{
static public Dictionary<string, string> Union(Dictionary<string, string> setA, Dictionary<string, string> setB )
{
return setA.Union(setB, new DictCompareOnKeyOnly()).ToDictionary(ld => ld.Key, ld => ld.Value); ;
}
static public Dictionary<string, string> Except(Dictionary<string, string> setA, Dictionary<string, string> setB)
{
return setA.Except(setB, new DictCompareOnKeyOnly()).ToDictionary(ld => ld.Key, ld => ld.Value); ;
}
static public Dictionary<string, string> InterSect(Dictionary<string, string> setA, Dictionary<string, string> setB)
{
return setA.Intersect(setB, new DictCompareOnKeyOnly()).ToDictionary(ld => ld.Key, ld => ld.Value); ;
}
}
}
"@
$Set1 = New-Object "System.Collections.Generic.Dictionary[string,string]"
$Set2 = New-Object "System.Collections.Generic.Dictionary[string,string]"
$Set1.Add(1,"Adam")
$Set1.Add(3,"Caesar")
$Set1.Add(4,"David")
$Set2.Add(1,"Adam")
$Set2.Add(3,"Caesar")
$Set2.Add(2,"Bertil")
Add-Type -TypeDefinition $CSharpDictLinqOpLib -Language CSharp
Write-host "Union - What is in all the sets" -ForegroundColor Red
[SetToolbox.SetOperator]::Union($Set1,$Set2) | Format-Table
Write-host "Except - What is the difference between the sets Set1 - Set2" -ForegroundColor Cyan
[SetToolbox.SetOperator]::Except($Set1,$Set2) |Format-Table
Write-host "Except - What is the difference between the sets Set2 - Set1" -ForegroundColor DarkCyan
[SetToolbox.SetOperator]::Except($Set2,$Set1) |Format-Table
Write-host "InterSect - What do the sets have in common" -ForegroundColor DarkMagenta
[SetToolbox.SetOperator]::InterSect($Set1,$Set2) |Format-Table