Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active December 21, 2023 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinGrote/afadca1e8798bfc23450df5494621888 to your computer and use it in GitHub Desktop.
Save JustinGrote/afadca1e8798bfc23450df5494621888 to your computer and use it in GitHub Desktop.
Equatable and HashSets Demo
using namespace System.Collections.Generic
class Pet {
[string]$Name
[string]$Type
hidden [int] GetHashCode() {
$uniqueString = $this.Name + $this.Type
return $uniqueString.GetHashCode()
}
hidden [bool] Equals($other) {
return $this.GetHashCode() -eq $other.GetHashCode()
}
}
class Person {
[string]$Name
[HashSet[Pet]]$Pets = @{}
hidden [int] GetHashCode() {
return $this.Name.GetHashCode()
}
hidden [bool] Equals($other) {
return $this.GetHashCode() -eq $other.GetHashCode() -and $this.Pets.SetEquals($other.Pets)
}
hidden static [Person] op_Addition([Person] $person, [Pet] $pet) {
if (-not $person.Pets.Add($pet)) {
throw "$($pet.Name) [$($Pet.Type)] already belongs to $($person.Name)"
}
return $person
}
}
class House {
[string]$Address
[HashSet[Person]]$People = @{}
hidden [int] GetHashCode() {
return $this.Address.GetHashCode()
}
hidden [bool] Equals($other) {
return $this.GetHashCode() -eq $other.GetHashCode() -and $this.People.SetEquals($other.People)
}
hidden static [House] op_Addition([House] $house, [Person] $person) {
if (-not $house.People.Add($person)) {
throw "$($person.Name) already lives here"
}
return $house
}
}
#Tests:
$HouseA = [house]@{Address = 'testaddress' }
$HouseB = [house]@{Address = 'testaddress' }
$HouseC = [house]@{Address = 'differentAddress' }
$HouseD = [house]@{Address = 'testaddress' }
$John = [Person]@{Name = 'John' }
$JohnClone = [Person]@{Name = 'John' }
$Jane = [Person]@{Name = 'Jane' }
$Spot = [Pet]@{Name = 'Spot'; Type = 'Dog' }
$Kitty = [Pet]@{Name = 'Kitty'; Type = 'Cat' }
$HouseA -eq $HouseB #True
$HouseA -ne $HouseC #Because the address is different
$HouseA += $John #Add John to HouseA
try {
$HouseA += $John #Errors that John already is here
} catch {
Write-Warning $_.Exception.InnerException.Message
}
try {
$HouseA += $Spot #Cannot Add pets to the house directly. You could have logic here that adds the pet to all people in the house.
} catch {
Write-Warning $_.Exception.InnerException.Message
}
$HouseA -ne $HouseB #True because John lives in one but not the other
$HouseB += $John #Add John to HouseB
$HouseA -eq $HouseB #True because now John lives in both
$John -eq $JohnClone #True because their names are the same
$John -ne $Jane #True because they have different names
$John += $Spot #Add Spot to John
$John -ne $JohnClone #Untrue now because even tho same names, different pets
try {
$John += $Spot #Errors that John already has spot
} catch {
Write-Warning $_.Exception.InnerException.Message
}
$HouseD += $JohnClone
$HouseA -ne $HouseD #False because even though John and JohnClone have the same name, they have different pets, which results in them not equaling each other on the hashset comparison
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment