Documentation ¶
Overview ¶
Package hashset implements an immutable set by using a hashdict in the background.
Index ¶
- type Set
- func (s Set[T]) Add(elems ...T) Set[T]
- func (s Set[T]) Contains(elem T) bool
- func (s Set[T]) EqHash() hash.EqHash[T]
- func (s Set[T]) Intersect(other iterable.Iterable[T]) Set[T]
- func (s Set[T]) Iterator() iterable.Iterator[T]
- func (s Set[T]) Minus(other iterable.Iterable[T]) Set[T]
- func (s Set[T]) Remove(elems ...T) Set[T]
- func (s Set[T]) String() string
- func (s Set[T]) Union(other iterable.Iterable[T]) Set[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set[T any] struct { // contains filtered or unexported fields }
func (Set[T]) Add ¶
Add elements to the set
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" "github.com/peterzeller/go-fun/set/hashset" ) func main() { s1 := hashset.New(hash.String(), "a", "b", "c") s2 := s1.Add("x", "y") fmt.Printf("s1 = %v\n", s1) fmt.Printf("s2 = %v\n", s2) }
Output: s1 = [b, a, c] s2 = [b, x, a, c, y]
func (Set[T]) Contains ¶
Contains checks if the set contains an element
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" "github.com/peterzeller/go-fun/set/hashset" ) func main() { s := hashset.New(hash.String(), "a", "b", "c") fmt.Printf("contains a: %v\n", s.Contains("a")) fmt.Printf("contains x: %v\n", s.Contains("x")) }
Output: contains a: true contains x: false
func (Set[T]) Intersect ¶
Intersect two sets, returning only elements contained in both
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" "github.com/peterzeller/go-fun/set/hashset" ) func main() { s1 := hashset.New(hash.String(), "a", "b", "c") s2 := hashset.New(hash.String(), "b", "c", "d") s3 := s1.Intersect(s2) fmt.Printf("s1 = %v\n", s1) fmt.Printf("s2 = %v\n", s2) fmt.Printf("s3 = %v\n", s3) }
Output: s1 = [b, a, c] s2 = [b, c, d] s3 = [b, c]
func (Set[T]) Iterator ¶
Iterator for the set
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" "github.com/peterzeller/go-fun/set/hashset" ) func main() { s := hashset.New(hash.Num[int](), 1, 2, 3) it := s.Iterator() sum := 0 for { v, ok := it.Next() if !ok { break } sum += v } fmt.Printf("sum = %v\n", sum) }
Output: sum = 6
func (Set[T]) Minus ¶
Minus removes all elements from the other set from this set
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" "github.com/peterzeller/go-fun/set/hashset" ) func main() { s1 := hashset.New(hash.String(), "a", "b", "c", "d") s2 := hashset.New(hash.String(), "b", "d", "e") s3 := s1.Minus(s2) fmt.Printf("s1 = %v\n", s1) fmt.Printf("s2 = %v\n", s2) fmt.Printf("s3 = %v\n", s3) }
Output: s1 = [b, a, c, d] s2 = [e, b, d] s3 = [a, c]
func (Set[T]) Remove ¶
Remove elements from the set
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" "github.com/peterzeller/go-fun/set/hashset" ) func main() { s1 := hashset.New(hash.String(), "a", "b", "c") s2 := s1.Remove("c") fmt.Printf("s1 = %v\n", s1) fmt.Printf("s2 = %v\n", s2) }
Output: s1 = [b, a, c] s2 = [b, a]
func (Set[T]) Union ¶
Union of two sets, returning elements that return in either of the sets
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/hash" "github.com/peterzeller/go-fun/set/hashset" ) func main() { s1 := hashset.New(hash.String(), "a", "b", "c") s2 := hashset.New(hash.String(), "b", "d") s3 := s1.Union(s2) fmt.Printf("s1 = %v\n", s1) fmt.Printf("s2 = %v\n", s2) fmt.Printf("s3 = %v\n", s3) }
Output: s1 = [b, a, c] s2 = [b, d] s3 = [b, a, c, d]
Click to show internal directories.
Click to hide internal directories.