Documentation ¶
Overview ¶
Package multiset implements a map-based multiset data structure.
Index ¶
- type Multiset
- func (m *Multiset) Add(s ...interface{})
- func (m *Multiset) AddCount(e interface{}, c int) int
- func (m *Multiset) Clear()
- func (m *Multiset) Contains(s ...interface{}) bool
- func (m *Multiset) Count(e interface{}) int
- func (m *Multiset) DistinctElements() []interface{}
- func (m *Multiset) Equals(o *Multiset) bool
- func (m *Multiset) IsEmpty() bool
- func (m *Multiset) Iter() <-chan interface{}
- func (m *Multiset) Remove(s ...interface{}) bool
- func (m *Multiset) RemoveCount(e interface{}, c int) int
- func (m *Multiset) Size() int
- func (m *Multiset) String() string
- func (m *Multiset) ToSlice() []interface{}
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Multiset ¶
Multiset represents the multiset data structure.
func New ¶
func New(l ...interface{}) *Multiset
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 a b]
func (*Multiset) Add ¶
func (m *Multiset) Add(s ...interface{})
Add adds a single occurrence of each of the specified elements to this multiset.
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 a a b b c d]
func (*Multiset) AddCount ¶
AddCount adds the stated number of occurrences to the specified element of this multiset. Returns the number of occurrence of the element before the operation; possibly zero. The number of occurrences provided may be zero or negative value, in which case no change will be made.
Example ¶
m := New() m.AddCount("d", 1) m.AddCount("a", 3) m.AddCount("c", 1) m.AddCount("b", 2) var list []string for elem := range m.Iter() { list = append(list, elem.(string)) } sort.Strings(list) fmt.Println(list)
Output: [a a a b b c d]
func (*Multiset) Clear ¶
func (m *Multiset) Clear()
Clear removes all of the elements from this multiset. The collection will be empty after this method returns.
Example ¶
m := New("b", "b", "c", "d") m.Clear() fmt.Println(m)
Output: []
func (*Multiset) Contains ¶
Contains determines whether this multiset contains at least one occurrence of each of the specified elements. If at least one occurrence present returns true; otherwise false.
Example ¶
m := New("b", "b", "c", "d") fmt.Println(m.Contains("b", "c", "d"))
Output: true
func (*Multiset) Count ¶
Count returns the number of occurrences of the specified element if present in this multiset; zero otherwise.
Example ¶
m := New("b", "b", "c", "d") fmt.Println(m.Count("b"))
Output: 2
func (*Multiset) DistinctElements ¶
func (m *Multiset) DistinctElements() []interface{}
DistinctElements returns a slice containing only distinct elements of this multiset.
Example ¶
m := New() m.AddCount("a", 3) m.AddCount("b", 2) var list []string distinct := m.DistinctElements() for _, elem := range distinct { list = append(list, elem.(string)) } sort.Strings(list) fmt.Println(list)
Output: [a b]
func (*Multiset) Equals ¶
Equals compares the specified multiset with this multiset for equality. Returns true if this multiset contains equal elements with equal counts; false otherwise.
Example ¶
m1 := New("b", "b", "c", "d") m2 := New("c", "b", "d", "b") fmt.Println(m1.Equals(m2))
Output: true
func (*Multiset) IsEmpty ¶
IsEmpty returns true if this multiset contains no elements; false otherwise.
Example ¶
m := New("b", "b", "c", "d") fmt.Println(m.IsEmpty())
Output: false
func (*Multiset) Iter ¶
func (m *Multiset) Iter() <-chan interface{}
Iter returns a channel of elements that can be ranged over.
Example ¶
m := New("a", "a", "a") for elem := range m.Iter() { fmt.Println(elem) }
Output: a a a
func (*Multiset) Remove ¶
Remove removes a single occurrence of each of the specified elements from this multiset, if present. Returns true if this multiset changed as a result of the call
Example ¶
m := New("a", "a", "a", "b", "b") m.Remove("a", "a") fmt.Println(m)
Output: [a b b]
func (*Multiset) RemoveCount ¶
RemoveCount removes a number of occurrences of the specified element from this multiset. Returns the count of occurrence of the element before the operation; possibly zero. The number of occurrences provided may be zero or negative value, in which case no change will be made.
Example ¶
m := New("a", "a", "a", "b", "b") m.RemoveCount("a", 3) fmt.Println(m)
Output: [b b]
func (*Multiset) Size ¶
Size returns the total number of elements in this multiset.
Example ¶
m := New("b", "b", "c", "d") fmt.Println(m.Size())
Output: 4