Documentation ¶
Overview ¶
Package set provides a common set based on generics.
Index ¶
- type Set
- func (s Set[T]) Add(elements ...T)
- func (s Set[T]) Clear()
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Contains(element T) bool
- func (s Set[T]) Difference(others ...Set[T]) Set[T]
- func (s Set[T]) DifferenceUpdate(others ...Set[T])
- func (s Set[T]) Equal(other Set[T]) bool
- func (s Set[T]) Intersection(others ...Set[T]) Set[T]
- func (s Set[T]) IntersectionUpdate(others ...Set[T])
- func (s Set[T]) Pop() (element T, ok bool)
- func (s Set[T]) Range(f func(element T))
- func (s Set[T]) Remove(elements ...T)
- func (s Set[T]) Size() int
- func (s Set[T]) Slice() []T
- func (s Set[T]) String() string
- func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]
- func (s Set[T]) SymmetricDifferenceUpdate(other Set[T])
- func (s Set[T]) Union(others ...Set[T]) Set[T]
- func (s Set[T]) UnionUpdate(others ...Set[T])
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is a set type.
Example ¶
intset1 := NewSet(1, 2, 3) intset2 := NewSet(7, 8, 9) strset := NewSet("a", "b", "c") intset1.Add(3, 4, 5) strset.Add("c", "d", "e") fmt.Println(intset1.Size(), strset.Size()) fmt.Println(intset1.Contains(4), intset1.Contains(5), intset1.Contains(6)) fmt.Println(strset.Contains("d"), strset.Contains("e"), strset.Contains("z")) // 5 5 // true true false // true true false intset1.Remove(1, 2, 9) strset.Remove("a", "b", "z") fmt.Println(intset1.Size(), strset.Size()) fmt.Println(intset1.Contains(1), intset1.Contains(2)) fmt.Println(strset.Contains("a"), strset.Contains("b")) // 3 3 // false false // false false list1 := intset1.Slice() fmt.Println(list1[0]) fmt.Println(list1[1]) fmt.Println(list1[2]) // 3 // 4 // 5 list2 := strset.Slice() fmt.Println(list2[0]) fmt.Println(list2[1]) fmt.Println(list2[2]) // c // d // e union := intset1.Union(intset2) diff := intset1.Difference(intset2) inter := intset1.Intersection(intset2) sdiff := intset1.SymmetricDifference(intset2) fmt.Println(union.Size(), diff.Size(), inter.Size(), sdiff.Size()) // 6 3 0 6 fmt.Println(union.Difference(intset1).Equal(intset2)) fmt.Println(union.Equal(sdiff)) // true // true union.Range(func(element int) { fmt.Println(element) }) for _, v := range union.Slice() { fmt.Println(v) } // 3 // 4 // 5 // 7 // 8 // 9
Output: 5 5 true true false true true false 3 3 false false false false 3 4 5 c d e 6 3 0 6 true true 3 4 5 7 8 9 3 4 5 7 8 9
func NewSet ¶
func NewSet[T comparable](elements ...T) Set[T]
NewSet returns a new Set.
If the element is string, it will ignore the empty string.
func NewSetFromSet ¶
func NewSetFromSet[T comparable](sets ...Set[T]) Set[T]
NewSetFromSet returns a new Set.
func (Set[T]) Difference ¶
Difference returns a new set with elements in the set that are not in the others.
func (Set[T]) DifferenceUpdate ¶
DifferenceUpdate updates the set, removing the elements found in others.
func (Set[T]) Intersection ¶
Intersection returns a new set with elements common to the set and all others.
func (Set[T]) IntersectionUpdate ¶
IntersectionUpdate updates the set, keeping only elements found in it and all others.
func (Set[T]) Range ¶
func (s Set[T]) Range(f func(element T))
Range travels all the elements of the set.
func (Set[T]) Remove ¶
func (s Set[T]) Remove(elements ...T)
Remove removes the elements from the set.
func (Set[T]) SymmetricDifference ¶
SymmetricDifference returns a new set with elements in either the set or other but not both.
func (Set[T]) SymmetricDifferenceUpdate ¶
SymmetricDifferenceUpdate updates the set, keeping only elements found in either set, but not in both.
func (Set[T]) UnionUpdate ¶
UnionUpdate updates the set, adding the elements from all others.