Documentation
¶
Overview ¶
Package set provides a generic set implementation for Go.
See http://en.wikipedia.org/wiki/Set_%28mathematics%29 for a full discussion of sets.
This package implements a set as a map without values. Keys can have any arbitrary type, as long as there is equality defined on it.
Examples
a := set.NewInit(1, 3, 5, 7, 9)
b := set.NewInit(2, 4, 6, 8, 10)
fmt.Println(a, b)
union := a.Union(b)
intersect := a.Intersect(b)
difference := a.Diff(b)
fmt.Println(union, intersect, difference)
a.Add(2)
b.Add(5)
fmt.Println(a.Intersect(b).Contains(2))
ch, _ := a.Iterator()
for x := range ch {
fmt.Println(x)
}
Rationale ¶
All operations are invoked in an object oriented style. Only the Add, Remove and Clear methods modify the receiver.
License ¶
This package is released under the GNU Lesser General Public License, Version 3. The full license text can be found in the LICENSE file of the source code distribution.
Index ¶
- Constants
- type AnySlice
- type Set
- func (s Set) Add(e ...interface{})
- func (s Set) Clear()
- func (s Set) Contains(e ...interface{}) bool
- func (s Set) Copy() Set
- func (s Set) Diff(t Set) Set
- func (s Set) Intersect(t ...Set) Set
- func (s Set) IsEmpty() bool
- func (s Set) IsEqual(t Set) bool
- func (s Set) IsSubsetOf(t Set) bool
- func (s Set) IsSupersetOf(t Set) bool
- func (s Set) Iterator() (<-chan interface{}, chan<- struct{})
- func (s Set) Len() int
- func (s Set) List() AnySlice
- func (s Set) Remove(e ...interface{})
- func (s Set) SortedList() AnySlice
- func (s Set) String() string
- func (s Set) SymDiff(t Set) Set
- func (s Set) Union(t ...Set) Set
Constants ¶
const VERSION = "0.1"
VERSION is the version number of the set package
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnySlice ¶
type AnySlice []interface{}
AnySlice is a slice type for arbitrary values, which implements the sort.Interface. We use it for returning a list of (sorted or unsorted) set elements.
type Set ¶
type Set map[interface{}]struct{}
The Set is implemented as a map without values.
func NewInit ¶
func NewInit(e ...interface{}) Set
NewInit creates a new set and initializes it with the argument values.
func (Set) Contains ¶
Contains checks if a set contains one or more elements. The return value is true only if all given elements are in the set.
func (Set) Diff ¶
Diff returns a new set which represents the difference of two sets. The sets themselves are not modified.
func (Set) Intersect ¶
Intersect returns a new set which represents the intersection of two or more sets. The sets themselves are not modified.
func (Set) IsSubsetOf ¶
IsSubsetOf returns true if the set s is a subset of the set t, e.g. if all elements of s are also in t.
func (Set) IsSupersetOf ¶
IsSupersetOf returns true if the set s is a superset of the set t, e.g. if all elements of t are also in s.
func (Set) Iterator ¶
func (s Set) Iterator() (<-chan interface{}, chan<- struct{})
Iterator returns a channel that can be used to iterate over the set. A second "done" channel can be used to preliminarily terminate the iteration by closing the done channel.
func (Set) Remove ¶
func (s Set) Remove(e ...interface{})
Remove removes one or more elements from the given set.
func (Set) SortedList ¶
SortedList returns a sorted list of the set elements in a slice.