Documentation
¶
Overview ¶
Package set provides a set data structure.
Index ¶
- type Set
- func (s *Set) Add(element any) *Set
- func (s *Set) Clear() *Set
- func (s Set) Copy() Set
- func (s Set) Difference(another Set) Set
- func (s *Set) Discard(element any) bool
- func (s Set) Empty() bool
- func (s Set) Equal(another Set) bool
- func (s Set) Has(element any) bool
- func (s Set) Intersection(another Set) Set
- func (s Set) IsDisjoint(another Set) bool
- func (s Set) IsSubset(another Set) bool
- func (s Set) IsSuperset(another Set) bool
- func (s Set) MarshalJSON() ([]byte, error)
- func (s *Set) Pop() (element any, err error)
- func (s Set) Size() int
- func (s Set) String() string
- func (s Set) SymmetricDifference(another Set) Set
- func (s Set) Union(another Set) Set
- func (s *Set) UnmarshalJSON(data []byte) (err error)
- func (s *Set) Update(another Set) *Set
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
Set is a set data structure.
Example ¶
s := Of(1, 2, 3, 4, 5, 5, 4, 3, 2, 1) fmt.Println(s.Size())
Output: 5
func (*Set) Add ¶
Add adds the specified element to the set.
Example ¶
s := Of() fmt.Println(s.Has(1)) s.Add(1) fmt.Println(s.Has(1))
Output: false true
func (*Set) Clear ¶
Clear removes all elements from the set.
Example ¶
s := Of(1, 2) fmt.Println(s.Empty()) s.Clear() fmt.Println(s.Empty()) fmt.Println(s)
Output: false true {}
func (Set) Copy ¶
Copy creates a new set with the same elements as the original set and returns it.
Example ¶
s1 := Of("apple", "banana", "orange")
s2 := s1.Copy()
fmt.Println(s1.Equal(s2))
Output: true
func (Set) Difference ¶
Difference returns a new set containing elements that are present in the receiver set but not in the another 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) Discard ¶
Discard removes the specified element from the set and returns true if the element was present.
Example ¶
s := Of(1, 2, 3) fmt.Println(s.Has(2)) s.Discard(2) fmt.Println(s.Has(2))
Output: true false
func (Set) Empty ¶
Empty checks if the set is empty.
Example ¶
s := Of(1, 2, 3) s2 := Of() fmt.Println(s.Empty()) fmt.Println(s2.Empty())
Output: false true
func (Set) Equal ¶
Equal checks if the set is equal to another set.
Example ¶
set1 := Of("a", "b", "c", "a")
set2 := Of("c", "b", "a", "c")
fmt.Println(set1.Equal(set2))
Output: true
func (Set) Has ¶
Has checks if the set contains the given element.
Example ¶
s := Of(1, 2, 3) fmt.Println(s.Has(1)) fmt.Println(s.Has(4))
Output: true false
func (Set) Intersection ¶
Intersection returns a new set containing elements that are present both in the receiver set and another set.
Example ¶
s1 := Of("a", "b", "c")
s2 := Of("e", "c", "d")
fmt.Println(s1.Intersection(s2))
Output: {c}
func (Set) IsDisjoint ¶
IsDisjoint checks if the set is disjoint from another set.
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) IsSubset ¶
IsSubset checks if the set is a subset of another set.
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) IsSuperset ¶
IsSuperset checks if the set is a superset of another set.
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) MarshalJSON ¶
MarshalJSON customizes the JSON encoding for the set.
func (*Set) Pop ¶
Pop removes and returns an arbitrary element from the set.
Example ¶
s := Of(1) element, _ := s.Pop() fmt.Println(element)
Output: 1
func (Set) 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) SymmetricDifference ¶
SymmetricDifference returns a new set containing elements that are present in either the receiver set or another set, 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) Union ¶
Union returns a new set containing all elements from both the receiver set and another 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) UnmarshalJSON ¶
UnmarshalJSON customizes the JSON decoding for the set.