Documentation ¶
Overview ¶
Package mapset implements a basic set type using a built-in map.
The Set type is a thin wrapper on a built-in Go map, so a Set is not safe for concurrent use without external synchronization.
Example ¶
package main import ( "fmt" "strings" "github.com/creachadair/mds/mapset" ) func main() { s := mapset.New(strings.Fields("a man a plan")...) // Add individual elements. s.Add("panama", "canal") // Add the contents of another set. t := mapset.New("plan", "for", "the", "future") s.AddAll(t) // Remove items and convert to a slice. elts := s.Remove("a", "an", "the", "for").Slice() // Clone and make other changes. u := s.Clone().Remove("future", "plans") // Do some basic comparisons. fmt.Println("t intersects u:", t.Intersects(u)) fmt.Println("t equals u:", t.Equals(u)) fmt.Println() // The slice is unordered, so impose some discipline. fmt.Println(strings.Join(elts, "\n")) }
Output: t intersects u: true t equals u: false canal future man panama plan
Index ¶
- type Set
- func (s *Set[T]) Add(items ...T) Set[T]
- func (s *Set[T]) AddAll(t Set[T]) Set[T]
- func (s Set[T]) Append(vs []T) []T
- func (s Set[T]) Clear() Set[T]
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Equals(t Set[T]) bool
- func (s Set[T]) Has(t T) bool
- func (s Set[T]) HasAll(ts ...T) bool
- func (s Set[T]) HasAny(ts ...T) bool
- func (s Set[T]) Intersects(t Set[T]) bool
- func (s Set[T]) IsEmpty() bool
- func (s Set[T]) IsSubset(t Set[T]) bool
- func (s Set[T]) Len() int
- func (s Set[T]) Pop() T
- func (s Set[T]) Remove(items ...T) Set[T]
- func (s Set[T]) RemoveAll(t Set[T]) Set[T]
- func (s Set[T]) Slice() []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set[T comparable] map[T]struct{}
A Set represents a set of distinct values. It is implemented via the built-in map type, and the underlying map can also be used directly to add and remove items and to iterate the contents.
func Intersect ¶
func Intersect[T comparable](ss ...Set[T]) Set[T]
Intersect constructs a new set containing the intersection of the specified sets. The result is never nil, even if the given sets are empty.
func Keys ¶ added in v0.2.3
func Keys[T comparable, U any](m map[T]U) Set[T]
Keys constructs a new Set containing the keys of m. The result is never nil, even if m is empty.
Example ¶
package main import ( "fmt" "strings" "github.com/creachadair/mds/mapset" ) func main() { s := mapset.Keys(map[string]int{ "apple": 1, "pear": 2, "plum": 3, "cherry": 4, }) fmt.Println(strings.Join(s.Slice(), "\n")) }
Output: apple cherry pear plum
func New ¶
func New[T comparable](items ...T) Set[T]
New constructs a set of the specified items. The result is never nil, even if no items are provided.
func NewSize ¶
func NewSize[T comparable](n int) Set[T]
NewSize constructs a new empty set preallocated to have space for n items.
func Values ¶ added in v0.2.3
func Values[T, U comparable](m map[T]U) Set[U]
Values constructs a new Set containing the values of m. The result is never nil, even if m is empty.
Example ¶
package main import ( "fmt" "github.com/creachadair/mds/mapset" ) func main() { s := mapset.Values(map[string]int{ "apple": 5, "pear": 4, "plum": 4, "cherry": 6, }) for _, v := range s.Slice() { fmt.Println(v) } }
Output: 4 5 6
func (Set[T]) Append ¶ added in v0.14.2
func (s Set[T]) Append(vs []T) []T
Append appends the elements of s to the specified slice in arbitrary order, and returns the resulting slice. If cap(vs) ≥ len(s) this will not allocate.
func (Set[T]) Clone ¶
Clone returns a new set with the same contents as s. The value returned is never nil.
func (Set[T]) HasAll ¶ added in v0.1.0
HasAll reports whether s contains all the elements of ts. It is semantically equivalent to ts.IsSubset(s), but does not construct an intermediate set. It returns true if len(ts) == 0.
func (Set[T]) HasAny ¶
HasAny reports whether s contains any element of ts. It is semantically equivalent to ts.Intersects(s), but does not construct an intermediate set. It returns false if len(ts) == 0.
func (Set[T]) Intersects ¶
Intersects reports whether s and t share any elements in common.
func (Set[T]) Pop ¶
func (s Set[T]) Pop() T
Pop removes and returns an arbitrary element of s, if s is non-empty. If s is empty, it returns a zero value.