Documentation
¶
Overview ¶
Package readyset provides generic sets for Go, with the usual set logic: union, intersection, difference, symmetric difference, and subset, superset and disjointness checks.
Set holds any comparable type, and two elements are the same element when they are equal:
a := readyset.New(1, 2, 3)
b := readyset.New(3, 4)
fmt.Println(a.Union(b)) // {1, 2, 3, 4}
fmt.Println(a.Intersection(b)) // {3}
For elements that are not comparable, or that are identified by only part of their value such as an ID, use github.com/kastolars/readyset/keyed.
Sets print with their elements in a stable order, and encode to and decode from JSON as arrays. Iteration order (Set.Slice) is random, as with Go maps.
Index ¶
- func Sorted[T cmp.Ordered](s Set[T]) []T
- type Set
- func (s *Set[T]) Add(elements ...T)
- func (s *Set[T]) AddSet(other Set[T])
- func (s Set[T]) All() iter.Seq[T]
- func (s *Set[T]) Clear()
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Contains(element T) bool
- func (s Set[T]) Difference(other Set[T]) Set[T]
- func (s Set[T]) Equal(other Set[T]) bool
- func (s Set[T]) Intersection(other Set[T]) Set[T]
- func (s Set[T]) IsDisjoint(other Set[T]) bool
- func (s Set[T]) IsEmpty() bool
- func (s Set[T]) IsSubset(other Set[T]) bool
- func (s Set[T]) IsSuperset(other Set[T]) bool
- func (s Set[T]) Len() int
- func (s Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Remove(elements ...T)
- func (s *Set[T]) RemoveSet(other Set[T])
- func (s Set[T]) Slice() []T
- func (s Set[T]) String() string
- func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]
- func (s Set[T]) Union(other Set[T]) Set[T]
- func (s *Set[T]) UnmarshalJSON(data []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Sorted ¶ added in v0.2.0
Sorted returns the elements of s as a new slice in ascending order. It works for any ordered element type: integers, floats and strings. NaN, if present, sorts before every other float.
Sorted is a function rather than a method because only an ordered element type can be sorted, and a method cannot add that constraint to Set. It is available when building with Go 1.21 or later.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
// Slice returns elements in random order; Sorted gives a stable one.
ids := readyset.New(30, 10, 20, 10)
fmt.Println(readyset.Sorted(ids))
}
Output: [10 20 30]
Types ¶
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is an unordered collection of unique elements.
The zero value is an empty set that is ready to use. Methods that modify a set have pointer receivers, so call them on an addressable variable. Copying a non-empty Set copies a reference to the same underlying storage; use Set.Clone for an independent copy.
A Set is not safe for concurrent use.
func New ¶
func New[T comparable](elements ...T) Set[T]
New returns a Set containing elements. Duplicates are ignored.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
s := readyset.New(3, 1, 2, 3)
fmt.Println(s.Len(), s.Contains(2), s.Contains(9))
}
Output: 3 true false
func (*Set[T]) Add ¶
func (s *Set[T]) Add(elements ...T)
Add inserts elements into the set. Elements already present are ignored.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
var s readyset.Set[string] // the zero value is ready to use
s.Add("a", "b")
s.Add("b", "c")
fmt.Println(s.Len())
}
Output: 3
func (*Set[T]) AddSet ¶ added in v0.2.0
AddSet inserts every element of other into the set. It is the in-place counterpart of Set.Union: afterwards s holds what s.Union(other) would have returned, and other is not modified.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
all := readyset.New(1, 2)
all.AddSet(readyset.New(2, 3))
fmt.Println(all)
}
Output: {1, 2, 3}
func (Set[T]) All ¶
All returns an iterator over the elements of the set, in no particular order. It is available when building with Go 1.23 or later.
The set must not be modified while iterating over it.
func (Set[T]) Clone ¶
Clone returns an independent copy of the set.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
a := readyset.New(1, 2)
b := a.Clone()
b.Add(3)
fmt.Println(a.Len(), b.Len())
}
Output: 2 3
func (Set[T]) Difference ¶
Difference returns a new set of the elements that are in s but not in other.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
a := readyset.New(1, 2, 3)
b := readyset.New(2, 3, 4)
fmt.Println(a.Difference(b))
}
Output: {1}
func (Set[T]) Intersection ¶
Intersection returns a new set of the elements that are in both s and other.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
a := readyset.New(1, 2, 3)
b := readyset.New(2, 3, 4)
fmt.Println(a.Intersection(b))
}
Output: {2, 3}
func (Set[T]) IsDisjoint ¶
IsDisjoint reports whether s and other have no elements in common.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
odds := readyset.New(1, 3, 5)
evens := readyset.New(2, 4, 6)
fmt.Println(odds.IsDisjoint(evens))
}
Output: true
func (Set[T]) IsSubset ¶
IsSubset reports whether every element of s is also in other. The empty set is a subset of every set, and every set is a subset of itself.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
a := readyset.New(2, 3)
b := readyset.New(1, 2, 3, 4)
fmt.Println(a.IsSubset(b), b.IsSubset(a))
}
Output: true false
func (Set[T]) IsSuperset ¶
IsSuperset reports whether every element of other is also in s.
func (Set[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It encodes the set as a JSON array of its elements, in the same stable order as Set.String, so equal sets always encode to the same bytes. An empty set encodes as [].
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/kastolars/readyset"
)
func main() {
b, _ := json.Marshal(readyset.New("b", "a"))
fmt.Println(string(b))
}
Output: ["a","b"]
func (*Set[T]) Remove ¶
func (s *Set[T]) Remove(elements ...T)
Remove deletes elements from the set. Elements not present are ignored.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
s := readyset.New(1, 2, 3)
s.Remove(1, 9) // 9 is not present; that is not an error
fmt.Println(s)
}
Output: {2, 3}
func (*Set[T]) RemoveSet ¶ added in v0.2.0
RemoveSet deletes every element of other from the set. It is the in-place counterpart of Set.Difference: afterwards s holds what s.Difference(other) would have returned, and other is not modified.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
all := readyset.New(1, 2, 3)
all.RemoveSet(readyset.New(2, 9))
fmt.Println(all)
}
Output: {1, 3}
func (Set[T]) Slice ¶
func (s Set[T]) Slice() []T
Slice returns the elements of the set as a new slice, in no particular order. The order can differ between calls.
func (Set[T]) String ¶
String returns the elements in braces, separated by commas, in a stable order: numbers numerically, strings lexically, and any other value by its printed form. It implements fmt.Stringer, so a set prints readably with fmt.Println and the %v and %s verbs.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
// Numbers print in numeric order, whatever order they were added in.
s := readyset.New(10, 2, 1)
fmt.Println(s)
}
Output: {1, 2, 10}
func (Set[T]) SymmetricDifference ¶
SymmetricDifference returns a new set of the elements that are in exactly one of s and other.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
a := readyset.New(1, 2, 3)
b := readyset.New(2, 3, 4)
fmt.Println(a.SymmetricDifference(b))
}
Output: {1, 4}
func (Set[T]) Union ¶
Union returns a new set of the elements that are in s, in other, or in both.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
a := readyset.New(1, 2, 3)
b := readyset.New(3, 4)
fmt.Println(a.Union(b))
}
Output: {1, 2, 3, 4}
func (*Set[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It decodes a JSON array into the set, replacing its previous contents; duplicate elements in the array are collapsed. A JSON null leaves the set unchanged. If decoding fails the set is also left unchanged.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/kastolars/readyset"
)
func main() {
type Config struct {
Tags readyset.Set[string] `json:"tags"`
}
var c Config
err := json.Unmarshal([]byte(`{"tags":["go","sets","go"]}`), &c)
fmt.Println(err, c.Tags.Len(), c.Tags.Contains("sets"))
}
Output: <nil> 2 true
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
order
Package order sorts values of any type into a stable, human-friendly order, so that printed and encoded sets do not depend on map iteration order.
|
Package order sorts values of any type into a stable, human-friendly order, so that printed and encoded sets do not depend on map iteration order. |
|
setlaws
Package setlaws checks the algebraic laws that every set type in this module must satisfy.
|
Package setlaws checks the algebraic laws that every set type in this module must satisfy. |
|
Package keyed provides a generic Set for elements that are identified by a key rather than by their own value.
|
Package keyed provides a generic Set for elements that are identified by a key rather than by their own value. |