mapset

package
v0.14.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2024 License: BSD-3-Clause Imports: 1 Imported by: 7

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

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]) Add

func (s *Set[T]) Add(items ...T) Set[T]

Add adds the specified items to the set and returns s.

func (*Set[T]) AddAll

func (s *Set[T]) AddAll(t Set[T]) Set[T]

AddAll adds all the elements of set t to s and returns s.

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]) Clear

func (s Set[T]) Clear() Set[T]

Clear removes all elements from s and returns s.

func (Set[T]) Clone

func (s Set[T]) Clone() Set[T]

Clone returns a new set with the same contents as s. The value returned is never nil.

func (Set[T]) Equals

func (s Set[T]) Equals(t Set[T]) bool

Equals reports whether s and t contain exactly the same elements.

func (Set[T]) Has

func (s Set[T]) Has(t T) bool

Has reports whether t is present in the set.

func (Set[T]) HasAll added in v0.1.0

func (s Set[T]) HasAll(ts ...T) bool

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

func (s Set[T]) HasAny(ts ...T) bool

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

func (s Set[T]) Intersects(t Set[T]) bool

Intersects reports whether s and t share any elements in common.

func (Set[T]) IsEmpty

func (s Set[T]) IsEmpty() bool

IsEmpty reports whether s is empty.

func (Set[T]) IsSubset

func (s Set[T]) IsSubset(t Set[T]) bool

IsSubset reports whether s is a subset of t.

func (Set[T]) Len

func (s Set[T]) Len() int

Len reports the number of elements in s.

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.

func (Set[T]) Remove

func (s Set[T]) Remove(items ...T) Set[T]

Remove removes the specified items from the set and returns s.

func (Set[T]) RemoveAll

func (s Set[T]) RemoveAll(t Set[T]) Set[T]

RemoveAll removes all the elements of set t from s and returns s.

func (Set[T]) Slice

func (s Set[T]) Slice() []T

Slice returns a slice of the contents of s in arbitrary order. It is a shorthand for Append.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL