Documentation ¶
Overview ¶
Package set implements a map-based set data structure.
Index ¶
- type Set
- func (s *Set) Add(l ...interface{})
- func (s *Set) Clear()
- func (s *Set) Contains(l ...interface{}) bool
- func (s *Set) Diff(o *Set) *Set
- func (s *Set) Equals(o *Set) bool
- func (s *Set) Intersect(o *Set) *Set
- func (s *Set) IsEmpty() bool
- func (s *Set) Iter() <-chan interface{}
- func (s *Set) Remove(l ...interface{}) bool
- func (s *Set) Size() int
- func (s *Set) String() string
- func (s *Set) Subset(o *Set) bool
- func (s *Set) Superset(o *Set) bool
- func (s *Set) SymDiff(o *Set) *Set
- func (s *Set) ToSlice() []interface{}
- func (s *Set) Union(o *Set) *Set
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set represents the set data structure.
func New ¶
func New(l ...interface{}) *Set
New creates and returns a reference to an empty set. Operations on the resulting set are thread-safe.
Example ¶
m := New("a", "a", "b") var list []string for elem := range m.Iter() { list = append(list, elem.(string)) } sort.Strings(list) fmt.Println(list)
Output: [a b]
func (*Set) Add ¶
func (s *Set) Add(l ...interface{})
Add adds occurrences of each of the specified elements to this set.
Example ¶
m := New("a", "a", "a") m.Add("b", "b", "c", "d") var list []string for elem := range m.Iter() { list = append(list, elem.(string)) } sort.Strings(list) fmt.Println(list)
Output: [a b c d]
func (*Set) Clear ¶
func (s *Set) Clear()
Clear removes all of the elements from this set. The collection will be empty after this method returns.
Example ¶
m := New("b", "b", "c", "d") m.Clear() fmt.Println(m)
Output: []
func (*Set) Contains ¶
Contains determines whether this set contains the occurrence of each of the specified elements. If at least one occurrence missing returns false; otherwise true.
Example ¶
m := New("b", "b", "c", "d") fmt.Println(m.Contains("b", "c", "d"))
Output: true
func (*Set) Diff ¶ added in v1.1.0
Diff returns a new set with items in the current set but not in the other set.
Example ¶
m1 := New("a", "b", "c") m2 := New("c", "d", "e") m := m1.Diff(m2) var list []string for elem := range m.Iter() { list = append(list, elem.(string)) } sort.Strings(list) fmt.Println(list)
Output: [a b]
func (*Set) Equals ¶
Equals compares the specified set with this set for equality. Returns true if this set contains equal elements; false otherwise.
Example ¶
m1 := New("b", "b", "c", "d") m2 := New("c", "b", "d", "b") fmt.Println(m1.Equals(m2))
Output: true
func (*Set) Intersect ¶ added in v1.1.0
Intersect returns a new set with items that exist only in both sets.
Example ¶
m1 := New("a", "b", "c") m2 := New("c", "d", "e") m := m1.Intersect(m2) var list []string for elem := range m.Iter() { list = append(list, elem.(string)) } sort.Strings(list) fmt.Println(list)
Output: [c]
func (*Set) IsEmpty ¶
IsEmpty returns true if this set contains no elements; false otherwise.
Example ¶
m := New("b", "b", "c", "d") fmt.Println(m.IsEmpty())
Output: false
func (*Set) Iter ¶
func (s *Set) Iter() <-chan interface{}
Iter returns a channel of elements that can be ranged over.
Example ¶
m := New("a", "a", "b") for elem := range m.Iter() { fmt.Println(elem) }
Output: a b
func (*Set) Remove ¶
Remove removes each of the specified elements from this set, if present. Returns true if this set changed as a result of the call
Example ¶
m := New("a", "a", "a", "b", "b") m.Remove("a", "a") fmt.Println(m)
Output: [b]
func (*Set) Size ¶
Size returns the total number of elements in this set.
Example ¶
m := New("b", "b", "c", "d") fmt.Println(m.Size())
Output: 3
func (*Set) Subset ¶ added in v1.1.0
Subset determines if every item in the other set is in this set.
Example ¶
m1 := New("a", "b", "c") m2 := New("c", "b") fmt.Println(m2.Subset(m1))
Output: true
func (*Set) Superset ¶ added in v1.1.0
Superset determines if every item of this set is in the other set.
Example ¶
m1 := New("a", "b", "c") m2 := New("c", "b") fmt.Println(m1.Superset(m2))
Output: true
func (*Set) SymDiff ¶ added in v1.1.0
SymDiff returns a new set with items in the current set or the other set but not in both.
Example ¶
m1 := New("a", "b", "c") m2 := New("c", "d", "e") m := m1.SymDiff(m2) var list []string for elem := range m.Iter() { list = append(list, elem.(string)) } sort.Strings(list) fmt.Println(list)
Output: [a b d e]
func (*Set) ToSlice ¶
func (s *Set) ToSlice() []interface{}
ToSlice returns a slice containing all elements of this set.
func (*Set) Union ¶ added in v1.1.0
Union returns a new set with all items in both sets.
Example ¶
m1 := New("a", "b", "c") m2 := New("c", "d", "e") m := m1.Union(m2) var list []string for elem := range m.Iter() { list = append(list, elem.(string)) } sort.Strings(list) fmt.Println(list)
Output: [a b c d e]