Documentation
¶
Overview ¶
Package set provides a set data structure.
Index ¶
- type Set
- func (s *Set[T]) Add(element T) *Set[T]
- func (s *Set[T]) Clear() *Set[T]
- func (s Set[T]) Copy() Set[T]
- func (s Set[T]) Difference(another Set[T]) Set[T]
- func (s *Set[T]) Discard(element T) bool
- func (s Set[T]) Empty() bool
- func (s Set[T]) Equal(another Set[T]) bool
- func (s Set[T]) Has(element T) bool
- func (s Set[T]) Intersection(another Set[T]) Set[T]
- func (s Set[T]) IsDisjoint(another Set[T]) bool
- func (s Set[T]) IsSubset(another Set[T]) bool
- func (s Set[T]) IsSuperset(another Set[T]) bool
- func (s Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Pop() (element T, err error)
- func (s Set[T]) Size() int
- func (s Set[T]) String() string
- func (s Set[T]) SymmetricDifference(another Set[T]) Set[T]
- func (s Set[T]) Union(another Set[T]) Set[T]
- func (s *Set[T]) UnmarshalJSON(data []byte) (err error)
- func (s *Set[T]) Update(another 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] map[T]bool
Set is a generic type representing a set data structure with elements of type T.
Example ¶
s := Of(1, 2, 3, 4, 5, 5, 4, 3, 2, 1) fmt.Println(s.Size())
Output: 5
func Of ¶
func Of[T comparable](elements ...T) Set[T]
Of creates and returns a new set containing the given elements.
func (*Set[T]) Add ¶
Add inserts the specified element into the set and returns a pointer to the modified set.
Example ¶
s := Of[int]() fmt.Println(s.Has(1)) s.Add(1) fmt.Println(s.Has(1))
Output: false true
func (*Set[T]) Clear ¶
Clear removes all elements from the set and returns a pointer to the empty set.
Example ¶
s := Of(1, 2) fmt.Println(s.Empty()) s.Clear() fmt.Println(s.Empty()) fmt.Println(s)
Output: false true {}
func (Set[T]) Copy ¶
Copy creates and returns a new set containing the same elements as the original set.
Example ¶
s1 := Of("apple", "banana", "orange") s2 := s1.Copy() fmt.Println(s1.Equal(s2))
Output: true
func (Set[T]) Difference ¶
Difference returns a new set that contains elements from the receiver set that are not in the given set
Example ¶
s1 := Of("a", "b", "c") s2 := Of("b", "c", "d") fmt.Println(s1.Difference(s2)) fmt.Println(s2.Difference(s1))
Output: {a} {d}
func (*Set[T]) Discard ¶
Discard removes the specified element from the set and returns true if the element was present, false otherwise.
Example ¶
s := Of(1, 2, 3) fmt.Println(s.Has(2)) s.Discard(2) fmt.Println(s.Has(2))
Output: true false
func (Set[T]) Empty ¶
Empty returns true if the set is empty, otherwise false.
Example ¶
s := Of(1, 2, 3) s2 := Of[int]() fmt.Println(s.Empty()) fmt.Println(s2.Empty())
Output: false true
func (Set[T]) Equal ¶
Equal checks if the set is equal to another set by comparing the size and elements of the sets, and returns true if they are equal, otherwise false.
Example ¶
set1 := Of("a", "b", "c", "a") set2 := Of("c", "b", "a", "c") fmt.Println(set1.Equal(set2))
Output: true
func (Set[T]) Has ¶
Has checks if the set contains the given element and returns true if found, otherwise false.
Example ¶
s := Of(1, 2, 3) fmt.Println(s.Has(1)) fmt.Println(s.Has(4))
Output: true false
func (Set[T]) Intersection ¶
Intersection returns a new set that contains elements that are common to both the receiver set and the given set
Example ¶
s1 := Of("a", "b", "c") s2 := Of("e", "c", "d") fmt.Println(s1.Intersection(s2))
Output: {c}
func (Set[T]) IsDisjoint ¶
IsDisjoint checks if the set is disjoint with another set and returns true if they have no common elements, otherwise false.
Example ¶
set1 := Of("a", "b", "c") set2 := Of("d", "e", "f") set3 := Of("a", "e", "f") fmt.Println(set1.IsDisjoint(set2)) fmt.Println(set1.IsDisjoint(set3))
Output: true false
func (Set[T]) IsSubset ¶
IsSubset checks if the set is a subset of another set and returns true if every element in the set is also in the other set, otherwise false.
Example ¶
set1 := Of("a", "b") set2 := Of("a", "b", "c") set3 := Of("a", "c") fmt.Println(set1.IsSubset(set2)) fmt.Println(set1.IsSubset(set3))
Output: true false
func (Set[T]) IsSuperset ¶
IsSuperset checks if the set is a superset of another set and returns true if it contains every element of the other set, otherwise false.
Example ¶
set1 := Of("a", "b", "c") set2 := Of("a", "b") set3 := Of("a", "e") fmt.Println(set1.IsSuperset(set2)) fmt.Println(set1.IsSuperset(set3))
Output: true false
func (Set[T]) MarshalJSON ¶
MarshalJSON converts the set into JSON format.
func (*Set[T]) Pop ¶
Pop removes and returns an arbitrary element from the set, along with an error if the set is empty.
Example ¶
s := Of(1) element, _ := s.Pop() fmt.Println(element)
Output: 1
func (Set[T]) Size ¶
Size returns the number of elements in the set.
Example ¶
s := Of(1, 2, 3, 4, 5) fmt.Println(s.Size())
Output: 5
func (Set[T]) SymmetricDifference ¶
SymmetricDifference returns a new set that contains elements that are in either of the sets, but not in both
Example ¶
s1 := Of("a", "b", "c") s2 := Of("b", "c", "d") sd1 := s1.SymmetricDifference(s2) sd2 := s2.SymmetricDifference(s1) sd := Of("a", "d") fmt.Println(sd1.Equal(sd2)) fmt.Println(sd1.Equal(sd))
Output: true true
func (Set[T]) Union ¶
Union returns a new set that contains all the unique elements from both the receiver set and the given set
Example ¶
s1 := Of("a", "b", "c") s2 := Of("b", "c", "d") s3 := Of("a", "b", "c", "d") union := s1.Union(s2) fmt.Println(union.Equal(s3))
Output: true
func (*Set[T]) UnmarshalJSON ¶
UnmarshalJSON populates the set with data from JSON format.