forked from patriklindstrom/Powershell-pasen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUse-SetOperatorOnHashTable.ps1
More file actions
57 lines (53 loc) · 2.45 KB
/
Copy pathUse-SetOperatorOnHashTable.ps1
File metadata and controls
57 lines (53 loc) · 2.45 KB
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
$CSharpDictLinqOpLib = @"
using System.Collections;
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); ;
}
public static Dictionary<string,string> HashtableToDictionary(Hashtable table)
{
return table
.Cast<DictionaryEntry>()
.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString());
}
}
}
"@
Add-Type -TypeDefinition $CSharpDictLinqOpLib -Language CSharp
$testhash1 = @{"1" = "Adam"; "3"= "Cesar"; "4"= "David"};
$testhash2 = @{"1" = "Anna"; "3"= "Cesar"; "2"= "Bertil"};
$Set1 = [SetToolbox.SetOperator]::HashtableToDictionary($testhash1)
$Set2 = [SetToolbox.SetOperator]::HashtableToDictionary($testhash2)
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