set

package
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 2 Imported by: 14

Documentation

Overview

Package set contains generic typesafe set operations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Of

type Of[T comparable] map[T]struct{}

Of is a set of elements of type T. It is called "Of" so that when qualified with this package name and instantiated with a member type, it reads naturally: e.g., set.Of[int].

The zero value of Of is not safe for use. Create one with New instead.

Example
package main

import (
	"fmt"

	"github.com/bobg/go-generics/v2/set"
)

func main() {
	s := set.New(1, 2, 3, 4, 5)
	fmt.Println("1 is in the set?", s.Has(1))
	fmt.Println("100 is in the set?", s.Has(100))
	s.Add(100)
	fmt.Println("100 is in the set?", s.Has(100))
	fmt.Println("set size is", s.Len())
	s.Del(100)
	fmt.Println("100 is in the set?", s.Has(100))
	fmt.Println("set size is", s.Len())
}
Output:

1 is in the set? true
100 is in the set? false
100 is in the set? true
set size is 6
100 is in the set? false
set size is 5

func Diff

func Diff[T comparable](s1, s2 Of[T]) Of[T]

Diff produces a new set containing the items in s1 that are not also in s2. Either set may be nil. The result is never nil (but may be empty).

Example
package main

import (
	"fmt"

	"github.com/bobg/go-generics/v2/set"
)

func main() {
	var (
		s1   = set.New(1, 2, 3, 4, 5)
		s2   = set.New(4, 5, 6, 7, 8)
		diff = set.Diff(s1, s2)
	)
	diff.Each(func(val int) { fmt.Println(val) })
}
Output:

1
2
3

func Intersect

func Intersect[T comparable](sets ...Of[T]) Of[T]

Intersect produces a new set containing only items that appear in all the given sets. The input may include nils, representing empty sets and therefore producing an empty (but non-nil) intersection.

Example
package main

import (
	"fmt"

	"github.com/bobg/go-generics/v2/set"
)

func main() {
	var (
		s1    = set.New(1, 2, 3, 4, 5)
		s2    = set.New(4, 5, 6, 7, 8)
		inter = set.Intersect(s1, s2)
	)
	inter.Each(func(val int) { fmt.Println(val) })
}
Output:

4
5

func New

func New[T comparable](vals ...T) Of[T]

New produces a new set containing the given values.

func Union

func Union[T comparable](sets ...Of[T]) Of[T]

Union produces a new set containing all the items in all the given sets. The input may include nils, representing empty sets. The result is never nil (but may be empty).

Example
package main

import (
	"fmt"

	"github.com/bobg/go-generics/v2/set"
)

func main() {
	var (
		s1    = set.New(1, 2, 3, 4, 5)
		s2    = set.New(4, 5, 6, 7, 8)
		union = set.Union(s1, s2)
	)
	union.Each(func(val int) { fmt.Println(val) })
}
Output:

1
2
3
4
5
6
7
8

func (Of[T]) Add

func (s Of[T]) Add(vals ...T)

Add adds the given values to the set. Items already present in the set are silently ignored.

func (Of[T]) Del

func (s Of[T]) Del(vals ...T)

Del removes the given items from the set. Items already absent from the set are silently ignored.

func (Of[T]) Each

func (s Of[T]) Each(f func(T))

func (Of[T]) Eachx

func (s Of[T]) Eachx(f func(T) error) error

Eachx calls a function on each element of the set in an indeterminate order. It is safe to add and remove items during a call to Each, but that can affect the sequence of values seen later during the same Eachx call. The set may be nil.

func (Of[T]) Equal

func (s Of[T]) Equal(other Of[T]) bool

Equal tests whether the set has the same membership as another. Either set may be nil.

func (Of[T]) Has

func (s Of[T]) Has(val T) bool

Has tells whether the given value is in the set. The set may be nil.

func (Of[T]) Iter

func (s Of[T]) Iter() iter.Of[T]

Iter produces an iterator over the members of the set, in an indeterminate order. The set may be nil.

func (Of[T]) Len

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

Len tells the number of distinct values in the set. The set may be nil.

func (Of[T]) Slice

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

Slice produces a new slice of the elements in the set. The slice is in an indeterminate order.

Jump to

Keyboard shortcuts

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