Documentation ¶
Overview ¶
set is giving Set structure.
Example (AddRemove) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/set" ) func main() { a := set.Of(1, 3, 5, 7, 9) a.Add(2) a.Add(4) a.Remove(3) a.Remove(5) a.Foreach(func(i int) { fmt.Print(i, " ") }) fmt.Println() }
Output: 1 2 4 7 9
Example (Contains) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/set" ) func main() { a := set.Of(1, 3, 5, 7, 9) fmt.Println(a.Contains(4)) }
Output: false
Example (Intersect) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/set" ) func main() { a := set.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) b := set.Of(2, 4, 6, 8, 10) c := a.Intersect(b) c.Foreach(func(i int) { fmt.Print(i, " ") }) fmt.Println() }
Output: 2 4 6 8 10
Example (Subtract) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/set" ) func main() { a := set.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) b := set.Of(2, 4, 6, 8, 10) c := a.Subtract(b) c.Foreach(func(i int) { fmt.Print(i, " ") }) fmt.Println() }
Output: 1 3 5 7 9
Example (Union) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/set" ) func main() { a := set.Of(1, 3, 5, 7, 9) b := set.Of(2, 4, 6, 8, 10) c := a.Union(b) c.Foreach(func(i int) { fmt.Print(i, " ") }) fmt.Println() }
Output: 1 2 3 4 5 6 7 8 9 10
Index ¶
- func ToSlice[T comparable](s Set[T]) []T
- type Set
- func (s Set[T]) Add(v T)
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Contains(v T) bool
- func (s1 Set[T]) Equal(s2 Set[T]) bool
- func (s1 Set[T]) Foreach(f func(T))
- func (s1 Set[T]) Intersect(s2 Set[T]) Set[T]
- func (s1 Set[T]) IsDisjoint(s2 Set[T]) bool
- func (s1 Set[T]) IsSubset(s2 Set[T]) bool
- func (s1 Set[T]) IsSuperset(s2 Set[T]) bool
- func (s Set[T]) Remove(v T)
- func (s1 Set[T]) Subtract(s2 Set[T]) Set[T]
- func (s1 Set[T]) Union(s2 Set[T]) Set[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToSlice ¶
func ToSlice[T comparable](s Set[T]) []T
ToSlice returns a slice with the elements of the Set.
Types ¶
type Set ¶
type Set[T comparable] map[T]struct{}
Set is a map[T]struct{} with additional methods.
func FromMapKey ¶
func FromMapKey[K comparable, V any](m map[K]V) Set[K]
FromMapKey returns a new Set with keys from a map.
func FromMapValue ¶
func FromMapValue[K comparable, V comparable](m map[K]V) Set[V]
FromMapValue returns a new Set with values from a map.
func FromSlice ¶
func FromSlice[T comparable](s []T) Set[T]
FromSlice returns a new Set from a slice.
func (Set[T]) Foreach ¶
func (s1 Set[T]) Foreach(f func(T))
Foreach calls the given function for each element in the Set.
func (Set[T]) Intersect ¶
Intersect returns a new Set with the intersection of this Set and the given Set.
func (Set[T]) IsDisjoint ¶
IsDisjoint returns true if this Set and the given Set have no elements in common.
func (Set[T]) IsSuperset ¶
IsSuperset returns true if this Set is a superset of the given Set.
Click to show internal directories.
Click to hide internal directories.