Documentation ¶
Overview ¶
Package set provides a set data structure that can be used to store unique elements. The set is implemented using a map[K]struct{} where K is a comparable type.
Index ¶
- type Set
- func (s Set[K]) Add(item ...K)
- func (s Set[K]) All() iter.Seq[K]
- func (s Set[K]) Contains(item K) bool
- func (s Set[K]) Difference(other Set[K]) Set[K]
- func (s Set[K]) Equal(other Set[K]) bool
- func (s Set[K]) Intersection(other Set[K]) Set[K]
- func (s Set[K]) IsEmpty() bool
- func (s Set[K]) Len() int
- func (s Set[K]) Remove(item K)
- func (s Set[K]) String() string
- func (s Set[K]) Union(other Set[K]) Set[K]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set[K comparable] map[K]struct{}
Set is a collection of unique elements.
func New ¶
func New[K comparable](items ...K) Set[K]
New creates a new set with the provided items.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s := set.New(1, 2, 3) for v := range s.All() { fmt.Println(v) } }
Output: 3 1 2
func (Set[K]) Add ¶
func (s Set[K]) Add(item ...K)
Add adds the provided items to the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s := set.New(1, 2, 3) s.Add(4) for v := range s.All() { fmt.Println(v) } }
Output: 3 1 2 4
func (Set[K]) All ¶
All returns an iter.Seq[K] of all elements in the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s := set.New(1, 2, 3) for v := range s.All() { fmt.Println(v) } }
Output: 3 1 2
func (Set[K]) Contains ¶
Contains returns true if the provided item is in the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s := set.New(1, 2, 3) fmt.Println(s.Contains(1)) }
Output: true
func (Set[K]) Difference ¶
Difference returns a new set with the difference of the provided set and the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s1 := set.New(1, 2, 3) s2 := set.New(3, 4, 5) difference := s1.Difference(s2) for v := range difference.All() { fmt.Println(v) } }
Output: 1 2
func (Set[K]) Equal ¶
Equal returns true if the provided set is equal to the set. Two sets are equal if they have the same elements.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s1 := set.New(1, 2, 3) s2 := set.New(1, 2, 3) fmt.Println(s1.Equal(s2)) }
Output: true
func (Set[K]) Intersection ¶
Intersection returns a new set with the intersection of the provided set and the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s1 := set.New(1, 2, 3) s2 := set.New(3, 4, 5) intersection := s1.Intersection(s2) for v := range intersection.All() { fmt.Println(v) } }
Output: 3
func (Set[K]) IsEmpty ¶
IsEmpty returns true if the set is empty.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s := set.New[int]() fmt.Println(s.IsEmpty()) s.Add(1) fmt.Println(s.IsEmpty()) }
Output: true false
func (Set[K]) Len ¶
Len returns the number of elements in the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s := set.New(1, 2, 3) fmt.Println(s.Len()) }
Output: 3
func (Set[K]) Remove ¶
func (s Set[K]) Remove(item K)
Remove removes the provided item from the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s := set.New(1, 2, 3) s.Remove(2) for v := range s.All() { fmt.Println(v) } }
Output: 3 1
func (Set[K]) String ¶
String returns a string representation of the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s := set.New(1) fmt.Println(s.String()) }
Output: {1}
func (Set[K]) Union ¶
Union returns a new set with the union of the provided set and the set.
Example ¶
package main import ( "fmt" "github.com/elordeiro/goext/containers/set" ) func main() { s1 := set.New(1, 2, 3) s2 := set.New(3, 4, 5) union := s1.Union(s2) for v := range union.All() { fmt.Println(v) } }
Output: 3 1 2 4 5