Documentation ¶
Overview ¶
Set is a generic set type based on a map.
Set supports all the map methods, functions that apply to maps (e.g., len()), and has its own often more convenient API.
See New for how to create empty or populated sets.
Index ¶
- Variables
- type Set
- func (me Set[T]) Add(elements ...T)
- func (me Set[T]) Clear()
- func (me Set[T]) Contains(element T) bool
- func (me Set[T]) Copy() Set[T]
- func (me Set[T]) Delete(elements ...T)
- func (me Set[T]) Difference(other Set[T]) Set[T]
- func (me Set[T]) Equal(other Set[T]) bool
- func (me Set[T]) Intersection(other Set[T]) Set[T]
- func (me Set[T]) IsDisjoint(other Set[T]) bool
- func (me Set[T]) IsEmpty() bool
- func (me Set[T]) IsSubsetOf(other Set[T]) bool
- func (me Set[T]) IsSupersetOf(other Set[T]) bool
- func (me Set[T]) String() string
- func (me Set[T]) SymmetricDifference(other Set[T]) Set[T]
- func (me Set[T]) ToSlice() []T
- func (me Set[T]) ToSortedSlice() []T
- func (me Set[T]) Union(other Set[T]) Set[T]
- func (me Set[T]) Unite(other Set[T])
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Version string // This module's version.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set[T comparable] map[T]struct{}
func New ¶
func New[T comparable](elements ...T) Set[T]
New returns a new set containing the given elements (if any). If no elements are given, the type must be specified since it can't be inferred.
Example ¶
package main import ( "fmt" "github.com/mark-summerfield/gset" ) func main() { a := gset.New[int]() // Type must be specified since it can't be inferred. fmt.Println("a:", a) b := gset.New(8, 7, 4, 3, 8, 2, 1) fmt.Println("b:", b) s := []string{"De", "A", "Fgh", "C", "B"} c := gset.New(s...) fmt.Println("c:", c) }
Output: a: {} b: {1 2 3 4 7 8} c: {"A" "B" "C" "De" "Fgh"}
func (Set[T]) Clear ¶
func (me Set[T]) Clear()
Clear deletes all the elements to make this an empty set.
func (Set[T]) Contains ¶
Contains returns true if element is in the set; otherwise returns false. Alternatively, use map syntax.
Example ¶
package main import ( "fmt" "github.com/mark-summerfield/gset" ) func main() { count := 0 s := gset.New("X", "Y", "Z") if s.Contains("Y") { count += 1 } // Alternatively, use map syntax: if _, ok := s["Y"]; ok { count += 1 } fmt.Println(count, count == 2) }
Output: 2 true
func (Set[T]) Delete ¶
func (me Set[T]) Delete(elements ...T)
Delete deletes the given element(s) from the set.
func (Set[T]) Difference ¶
Difference returns a new set that contains the elements which are in this set that are not in the other set.
func (Set[T]) Equal ¶
Equal returns true if this set has the same elements as the other set; otherwise returns false.
func (Set[T]) Intersection ¶
Intersection returns a new set that contains the elements this set has in common with the other set.
func (Set[T]) IsDisjoint ¶
IsDisjoint returns true if this set has no elements in common with the other set; otherwise returns false.
func (Set[T]) IsEmpty ¶ added in v0.4.0
IsEmpty returns true if the set is empty; otherwise returns false. This is just a convenience for len(s) == 0.
func (Set[T]) IsSubsetOf ¶ added in v1.1.0
IsSubsetOf returns true if this set is a subset of the other set, i.e., if every member of this set is in the other set; otherwise returns false.
func (Set[T]) IsSupersetOf ¶ added in v1.2.0
IsSupersetOf returns true if this set is a superset of the other set, i.e., if every member of the other set is in this set; otherwise returns false.
func (Set[T]) String ¶
String returns a human readable string representation of the set. If len(s) <= 100, returns "{e1 e2 ... eN}" with elements sorted by <; otherwise returns "{…N elements…}" where N is len(s).
Example ¶
package main import ( "fmt" "github.com/mark-summerfield/gset" ) func main() { s := gset.New("one", "two", "three", "four", "five", "six") fmt.Println(s) }
Output: {"five" "four" "one" "six" "three" "two"}
func (Set[T]) SymmetricDifference ¶
SymmetricDifference returns a new set that contains the elements which are in this set or the other set—but not in both sets.
func (Set[T]) ToSlice ¶
func (me Set[T]) ToSlice() []T
ToSlice returns this set's elements as a slice. For iteration either use this, or if you only need one value at a time, use map syntax with a for loop. See also [ToSortedSlice].
Example ¶
package main import ( "fmt" "github.com/mark-summerfield/gset" ) func main() { total1 := 0 s := gset.New(2, 3, 5, 7, 11, 13) slice := s.ToSlice() // Copies the lot for _, v := range slice { total1 += v } total2 := 0 // Alternatively, one value at a time using map syntax: for v := range s { total2 += v } fmt.Println(total1, total2, total1 == total2) }
Output: 41 41 true
func (Set[T]) ToSortedSlice ¶ added in v0.7.0
func (me Set[T]) ToSortedSlice() []T
ToSortedSlice returns this set's elements as a slice with the elements sorted using <. For iteration either use this, or if you only need one value at a time, use map syntax with a for loop. See also [ToSlice].
func (Set[T]) Union ¶
Union returns a new set that contains the elements from this set and from the other set (with no duplicates of course). See also Set.Unite.
Example ¶
package main import ( "fmt" "github.com/mark-summerfield/gset" ) func main() { s := gset.New(23, 19, 17, 13, 11) t := gset.New(2, 3, 5, 7, 11, 13) u := s.Union(t) v := t.Union(s) fmt.Println(u.Equal(v), u, t) }
Output: true {2 3 5 7 11 13 17 19 23} {2 3 5 7 11 13}
func (Set[T]) Unite ¶
Unite adds all the elements from other that aren't already in this set to this set. See also Set.Union.
Example ¶
package main import ( "fmt" "github.com/mark-summerfield/gset" ) func main() { s := gset.New(23, 19, 17, 13, 11) s.Unite(gset.New(2, 3, 5, 7, 11, 13)) fmt.Println(s) }
Output: {2 3 5 7 11 13 17 19 23}