Skip to content

Commit fbb0756

Browse files
committed
HashSet changes
1 parent bfd6875 commit fbb0756

File tree

5 files changed

+133
-132
lines changed

5 files changed

+133
-132
lines changed

ListFunctions.psm1

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,42 @@ Function BuildPredicate()
3333
}
3434
}
3535

36+
Function NewEqualityComparer() {
37+
[CmdletBinding()]
38+
param (
39+
[Parameter(Mandatory = $false)]
40+
[Alias("Type", "t")]
41+
[ValidateScript( { $_ -is [type] -or $_ -is [string] })]
42+
[object] $GenericType = "[object]",
43+
44+
[Parameter(Mandatory=$true)]
45+
[scriptblock] $EqualityScript,
46+
47+
[Parameter(Mandatory=$true)]
48+
[scriptblock] $HashCodeScript
49+
)
50+
51+
$ms = [regex]::Matches($EqualityScript, '\$(x|y)(\s|\.)', "IgnoreCase")
52+
if ($ms | Assert-Any -Condition { $_.Success }) {
53+
$replace1 = [regex]::Replace($EqualityScript, '\$x(\s|\.)', '$args[0]$1', "IgnoreCase")
54+
$replace2 = [regex]::Replace($replace1, '\$y(\s|\.)', '$args[1]$1', "IgnoreCase")
55+
$EqualityScript = [scriptblock]::Create($replace2)
56+
}
57+
58+
if ($HashCodeScript -match '\$[_](\.|\s)') {
59+
$HashCodeScript = [scriptblock]::Create([regex]::Replace($HashCodeScript, '\$[_](\.|\s)', '$args[0]$1'))
60+
}
61+
62+
if ($GenericType -is [type]) {
63+
$GenericType = $GenericType.FullName
64+
}
65+
66+
New-Object -TypeName "ListFunctions.ScriptBlockComparer[$GenericType]" -Property @{
67+
EqualityTester = $EqualityScript
68+
HashCodeScript = $HashCodeScript
69+
}
70+
}
71+
3672
Function Assert-All()
3773
{
3874
<#
@@ -413,73 +449,60 @@ Function Find-LastIndexOf()
413449
}
414450
}
415451

416-
Function New-EqualityComparer() {
417-
[CmdletBinding()]
452+
Function New-HashSet() {
453+
454+
[CmdletBinding(DefaultParameterSetName = "None")]
418455
param (
419456
[Parameter(Mandatory = $false)]
457+
[ValidateRange(0, [int]::MaxValue)]
458+
[int] $Capacity = 0,
459+
460+
[Parameter(Mandatory = $false, Position = 0)]
420461
[Alias("Type", "t")]
421462
[ValidateScript( { $_ -is [type] -or $_ -is [string] })]
463+
[ValidateNotNull()]
422464
[object] $GenericType = "[object]",
423465

424-
[Parameter(Mandatory=$true)]
466+
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
467+
[object[]] $InputObject,
468+
469+
[Parameter(Mandatory = $true, ParameterSetName = "WithCustomEqualityComparer")]
425470
[scriptblock] $EqualityScript,
426471

427-
[Parameter(Mandatory=$true)]
472+
[Parameter(Mandatory = $true, ParameterSetName = "WithCustomEqualityComparer")]
428473
[scriptblock] $HashCodeScript
429474
)
475+
Begin {
430476

431-
if ($GenericType -is [type]) {
432-
$GenericType = $GenericType.FullName
433-
}
434-
435-
New-Object -TypeName "ListFunctions.ScriptBlockComparer[$GenericType]" -Property @{
436-
EqualityTester = $EqualityScript
437-
HashCodeScript = $HashCodeScript
438-
}
439-
}
440-
441-
Function New-HashSet() {
442-
[CmdletBinding()]
443-
param (
444-
[Parameter(Mandatory = $false)]
445-
[int] $Capacity = 1,
477+
if ($GenericType -is [type]) {
478+
$private:type = $GenericType
479+
$GenericType = $GenericType.FullName
480+
}
446481

447-
[Parameter(Mandatory = $false, Position = 0)]
448-
[Alias("Type", "t")]
449-
[ValidateScript( { $_ -is [type] -or $_ -is [string] })]
450-
[object] $GenericType = "[object]",
482+
if ($PSCmdlet.ParameterSetName -eq "WithCustomEqualityComparer") {
483+
$comparer = NewEqualityComparer -GenericType $GenericType -EqualityScript $EqualityScript -HashCodeScript $HashCodeScript
484+
}
451485

452-
[Parameter(Mandatory = $false)]
453-
[ValidateScript({
454-
$true -in @(
455-
$_.GetType().ImplementedInterfaces.FullName | Foreach-Object {
456-
$_ -like "System.Collections.Generic.IEqualityComparer*"
457-
}
458-
)
459-
})]
460-
[object] $EqualityComparer
461-
)
486+
$set = New-Object -TypeName "System.Collections.Generic.HashSet[$GenericType]"($Capacity, $comparer)
487+
488+
if ($null -eq $type) {
489+
$private:type = $set.GetType().GenericTypeArguments | Select-Object -First 1
490+
}
462491

463-
if ($GenericType -is [type]) {
464-
$GenericType = $GenericType.FullName
492+
Write-Verbose "HashSet - GenericType $($private:type.FullName)"
493+
$private:type = $private:type.MakeArrayType()
465494
}
495+
Process {
466496

467-
if ($PSBoundParameters.ContainsKey("Capacity") -and -not $PSBoundParameters.ContainsKey("EqualityComparer"))
468-
{
469-
$set = New-Object -TypeName "System.Collections.Generic.Hashset[$GenericType]"($Capacity)
470-
}
471-
elseif (-not $PSBoundParameters.ContainsKey("Capacity") -and $PSBoundParameters.ContainsKey("EqualityComparer"))
472-
{
473-
$set = New-Object -TypeName "System.Collections.Generic.Hashset[$GenericType]"($EqualityComparer)
474-
}
475-
elseif ($PSBoundParameters.ContainsKey("Capacity") -and $PSBoundParameters.ContainsKey("EqualityComparer"))
476-
{
477-
$set = New-Object -TypeName "System.Collections.Generic.Hashset[$GenericType]"($Capacity, $EqualityComparer)
497+
if ($PSBoundParameters.ContainsKey("InputObject")) {
498+
499+
$set.UnionWith(($InputObject -as $private:type))
500+
}
478501
}
479-
else {
480-
$set = New-Object -TypeName "System.Collections.Generic.Hashset[$GenericType]"($Capacity)
502+
End {
503+
504+
, $set
481505
}
482-
, $set
483506
}
484507

485508
Function New-List() {
@@ -493,9 +516,10 @@ Function New-List() {
493516

494517
[Parameter(Mandatory = $false, Position = 0)]
495518
[Alias("c", "cap")]
496-
[int] $Capacity = 1,
519+
[ValidateRange(0, [int]::MaxValue)]
520+
[int] $Capacity = 0,
497521

498-
[Parameter(Mandatory = $false, ValueFromPipeline=$true)]
522+
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
499523
[object[]] $InputObject
500524
)
501525
Begin {
@@ -505,17 +529,11 @@ Function New-List() {
505529
$GenericType = $GenericType.FullName
506530
}
507531

508-
if ($PSBoundParameters.ContainsKey("Capacity")) {
509-
$private:list = New-Object "System.Collections.Generic.List[$GenericType]"($Capacity)
510-
}
511-
else {
512-
$private:list = New-Object "System.Collections.Generic.List[$GenericType]"
513-
514-
}
532+
$private:list = New-Object "System.Collections.Generic.List[$GenericType]"($Capacity)
515533
Write-Verbose "List - Created with 'Capacity': $($private:list.Capacity)"
516534

517535
if ($null -eq $type) {
518-
$private:type = $private:list.GetType().GenericTypeArguments[0]
536+
$private:type = $private:list.GetType().GenericTypeArguments | Select-Object -First 1
519537
}
520538
Write-Verbose "List - GenericType: $($private:type.FullName)"
521539
$private:type = $private:type.MakeArrayType()
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Function New-EqualityComparer() {
1+
Function NewEqualityComparer() {
22
[CmdletBinding()]
33
param (
44
[Parameter(Mandatory = $false)]
@@ -13,6 +13,17 @@ Function New-EqualityComparer() {
1313
[scriptblock] $HashCodeScript
1414
)
1515

16+
$ms = [regex]::Matches($EqualityScript, '\$(x|y)(\s|\.)', "IgnoreCase")
17+
if ($ms | Assert-Any -Condition { $_.Success }) {
18+
$replace1 = [regex]::Replace($EqualityScript, '\$x(\s|\.)', '$args[0]$1', "IgnoreCase")
19+
$replace2 = [regex]::Replace($replace1, '\$y(\s|\.)', '$args[1]$1', "IgnoreCase")
20+
$EqualityScript = [scriptblock]::Create($replace2)
21+
}
22+
23+
if ($HashCodeScript -match '\$[_](\.|\s)') {
24+
$HashCodeScript = [scriptblock]::Create([regex]::Replace($HashCodeScript, '\$[_](\.|\s)', '$args[0]$1'))
25+
}
26+
1627
if ($GenericType -is [type]) {
1728
$GenericType = $GenericType.FullName
1829
}

Public/Find-LastObject.ps1.old

Lines changed: 0 additions & 35 deletions
This file was deleted.

Public/New-HashSet.ps1

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
11
Function New-HashSet() {
2-
[CmdletBinding()]
2+
3+
[CmdletBinding(DefaultParameterSetName = "None")]
34
param (
45
[Parameter(Mandatory = $false)]
5-
[int] $Capacity = 1,
6+
[ValidateRange(0, [int]::MaxValue)]
7+
[int] $Capacity = 0,
68

79
[Parameter(Mandatory = $false, Position = 0)]
810
[Alias("Type", "t")]
911
[ValidateScript( { $_ -is [type] -or $_ -is [string] })]
12+
[ValidateNotNull()]
1013
[object] $GenericType = "[object]",
1114

12-
[Parameter(Mandatory = $false)]
13-
[ValidateScript({
14-
$true -in @(
15-
$_.GetType().ImplementedInterfaces.FullName | Foreach-Object {
16-
$_ -like "System.Collections.Generic.IEqualityComparer*"
17-
}
18-
)
19-
})]
20-
[object] $EqualityComparer
15+
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
16+
[object[]] $InputObject,
17+
18+
[Parameter(Mandatory = $true, ParameterSetName = "WithCustomEqualityComparer")]
19+
[scriptblock] $EqualityScript,
20+
21+
[Parameter(Mandatory = $true, ParameterSetName = "WithCustomEqualityComparer")]
22+
[scriptblock] $HashCodeScript
2123
)
24+
Begin {
2225

23-
if ($GenericType -is [type]) {
24-
$GenericType = $GenericType.FullName
25-
}
26+
if ($GenericType -is [type]) {
27+
$private:type = $GenericType
28+
$GenericType = $GenericType.FullName
29+
}
2630

27-
if ($PSBoundParameters.ContainsKey("Capacity") -and -not $PSBoundParameters.ContainsKey("EqualityComparer"))
28-
{
29-
$set = New-Object -TypeName "System.Collections.Generic.Hashset[$GenericType]"($Capacity)
30-
}
31-
elseif (-not $PSBoundParameters.ContainsKey("Capacity") -and $PSBoundParameters.ContainsKey("EqualityComparer"))
32-
{
33-
$set = New-Object -TypeName "System.Collections.Generic.Hashset[$GenericType]"($EqualityComparer)
31+
if ($PSCmdlet.ParameterSetName -eq "WithCustomEqualityComparer") {
32+
$comparer = NewEqualityComparer -GenericType $GenericType -EqualityScript $EqualityScript -HashCodeScript $HashCodeScript
33+
}
34+
35+
$set = New-Object -TypeName "System.Collections.Generic.HashSet[$GenericType]"($Capacity, $comparer)
36+
37+
if ($null -eq $type) {
38+
$private:type = $set.GetType().GenericTypeArguments | Select-Object -First 1
39+
}
40+
41+
Write-Verbose "HashSet - GenericType $($private:type.FullName)"
42+
$private:type = $private:type.MakeArrayType()
3443
}
35-
elseif ($PSBoundParameters.ContainsKey("Capacity") -and $PSBoundParameters.ContainsKey("EqualityComparer"))
36-
{
37-
$set = New-Object -TypeName "System.Collections.Generic.Hashset[$GenericType]"($Capacity, $EqualityComparer)
44+
Process {
45+
46+
if ($PSBoundParameters.ContainsKey("InputObject")) {
47+
48+
$set.UnionWith(($InputObject -as $private:type))
49+
}
3850
}
39-
else {
40-
$set = New-Object -TypeName "System.Collections.Generic.Hashset[$GenericType]"($Capacity)
51+
End {
52+
53+
, $set
4154
}
42-
, $set
4355
}

Public/New-List.ps1

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ Function New-List() {
99

1010
[Parameter(Mandatory = $false, Position = 0)]
1111
[Alias("c", "cap")]
12-
[int] $Capacity = 1,
12+
[ValidateRange(0, [int]::MaxValue)]
13+
[int] $Capacity = 0,
1314

14-
[Parameter(Mandatory = $false, ValueFromPipeline=$true)]
15+
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
1516
[object[]] $InputObject
1617
)
1718
Begin {
@@ -21,17 +22,11 @@ Function New-List() {
2122
$GenericType = $GenericType.FullName
2223
}
2324

24-
if ($PSBoundParameters.ContainsKey("Capacity")) {
25-
$private:list = New-Object "System.Collections.Generic.List[$GenericType]"($Capacity)
26-
}
27-
else {
28-
$private:list = New-Object "System.Collections.Generic.List[$GenericType]"
29-
30-
}
25+
$private:list = New-Object "System.Collections.Generic.List[$GenericType]"($Capacity)
3126
Write-Verbose "List - Created with 'Capacity': $($private:list.Capacity)"
3227

3328
if ($null -eq $type) {
34-
$private:type = $private:list.GetType().GenericTypeArguments[0]
29+
$private:type = $private:list.GetType().GenericTypeArguments | Select-Object -First 1
3530
}
3631
Write-Verbose "List - GenericType: $($private:type.FullName)"
3732
$private:type = $private:type.MakeArrayType()

0 commit comments

Comments
 (0)