@@ -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+
3672Function 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
485508Function 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 ()
0 commit comments