Documentation
¶
Overview ¶
Package set implements a set data structure and common set theory operations.
Index ¶
- type Set
- func (set *Set[T]) Add(element T)
- func (set *Set[T]) Difference(other *Set[T]) *Set[T]
- func (set *Set[T]) Intersection(other *Set[T]) *Set[T]
- func (set *Set[T]) Remove(element T)
- func (set *Set[T]) String() string
- func (set *Set[T]) SymmetricDifference(other *Set[T]) *Set[T]
- func (set *Set[T]) Union(other *Set[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 comparable] struct { base.Collection[T] }
Set is an implementation of a set data structure. For usage examples see the tests.
func NewSet ¶
func NewSet[T comparable]() *Set[T]
NewSet returns a new empty set.
Example ¶
fmt.Println(NewSet[int]())
Output: []
func NewSetFromSlice ¶
func NewSetFromSlice[T comparable](slice []T) *Set[T]
NewSetFromSlice returns a new set with the elements of the slice. The slice is not modified and the duplicates are removed.
Example ¶
fmt.Println(NewSetFromSlice[int]([]int{2, 2}))
Output: [2]
func (*Set[T]) Add ¶
func (set *Set[T]) Add(element T)
Add adds the element to the set.
Example ¶
s := NewSet[int]() s.Add(1) fmt.Println(s)
Output: [1]
func (*Set[T]) Difference ¶
Difference returns a new set with elements in the set that are not in the others. The sets are not modified.
Example ¶
s := NewSetFromSlice[int]([]int{1, 2, 3})
o := NewSetFromSlice[int]([]int{2, 3, 40})
fmt.Println(s.Difference(o))
Output: [1]
func (*Set[T]) Intersection ¶
Intersection returns a new set with elements in the set and the others. The sets are not modified.
Example ¶
s := NewSetFromSlice[int]([]int{1, 2, 3})
o := NewSetFromSlice[int]([]int{2, 30, 40})
fmt.Println(s.Intersection(o))
Output: [2]
func (*Set[T]) Remove ¶
func (set *Set[T]) Remove(element T)
Remove removes the element from the set.
Example ¶
s := NewSetFromSlice[int]([]int{1, 2})
s.Remove(1)
fmt.Println(s)
Output: [2]
func (*Set[T]) String ¶
String returns a string representation of the set.
Example ¶
s := NewSetFromSlice[int]([]int{1})
fmt.Println(s.String())
Output: [1]
func (*Set[T]) SymmetricDifference ¶
SymmetricDifference returns a new set with elements in the set or the others but not in both. The sets are not modified.
Example ¶
s := NewSetFromSlice[int]([]int{2, 30})
o := NewSetFromSlice[int]([]int{2, 30, 1})
fmt.Println(s.SymmetricDifference(o))
Output: [1]