dsa-go/hash_set/main.go

52 lines
784 B
Go
Raw Permalink Normal View History

2024-09-15 12:00:16 +00:00
package main
import (
"fmt"
"go-dsa/hash_set/v2/hash_set"
)
type Person struct {
Name string
Age int
}
// Hash returns the has of a person, it conforms to the hash_set.Hashable interface
2024-09-15 12:00:16 +00:00
func (p Person) Hash() string {
return fmt.Sprintf("%s-%d", p.Name, p.Age)
}
func main() {
mySet := hash_set.NewSet[Person, string]()
// Add
mySet.Add(Person{
Name: "Batman",
Age: 28,
})
mySet.Add(Person{
Name: "Robin",
Age: 16,
})
mySet.Add(Person{
Name: "Batman",
Age: 28,
})
// Print
fmt.Printf("%s\n", mySet.String())
// Contains
result := mySet.Contains(Person{
Name: "Batman",
Age: 28,
})
fmt.Printf("Set contains batman %v\n", result)
// Deletion
mySet.Delete(Person{
Name: "Batman",
Age: 28,
})
fmt.Printf("%s\n", mySet.String())
}