Skip to content

hweidner/set

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT GoDocs Go Reference Go Report Card

set

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.

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 MIT license. The full license text can be found in the LICENSE file.

About

A generic set implementation for Go.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages