A set implementation for Go using generics.
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.
This module contains two set implementations:
- A generic implementation using interface{} values, for all versions of Go. This is fixed to v1.0.0. No more updates will happen.
- An implementation using generics in the v2 directory. This requires Go 1.18 or later.
This documentation is for the version 1 of the package. See v2/README.md for the documentation of the version 2 package.
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)
}
All operations are invoked in an object oriented style. Only the Add, Remove and Clear methods modify the receiver.
This package is released under the MIT license. The full license text can be found in the LICENSE file.