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.
There are two kinds of set. 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 sets of records identified by a key, such as users by email or rows by ID, including records that cannot be compared with ==, use github.com/kastolars/readyset/keyed. Two elements there are the same element when their keys are equal.
Union, difference and intersection also have in-place forms: Set.AddSet, Set.RemoveSet and Set.RetainSet update a set instead of returning a new one. Set.Filter, Set.RemoveFunc and Set.Pop select, delete and drain elements, and Sorted and Collect convert to ordered slices and from iterators.
A Set is not safe for concurrent use. SyncSet, created with NewSync, is the same set behind a lock, for goroutines that share it.
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]) ContainsAll(elements ...T) bool
- func (s Set[T]) ContainsAny(elements ...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]) Filter(keep func(T) bool) Set[T]
- 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]) IsProperSubset(other Set[T]) bool
- func (s Set[T]) IsProperSuperset(other Set[T]) 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]) Pop() (T, bool)
- func (s *Set[T]) Remove(elements ...T)
- func (s *Set[T]) RemoveFunc(remove func(T) bool) int
- func (s *Set[T]) RemoveSet(other Set[T])
- func (s *Set[T]) RetainSet(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
- type SyncSet
- func (s *SyncSet[T]) Add(elements ...T)
- func (s *SyncSet[T]) AddSet(other *SyncSet[T])
- func (s *SyncSet[T]) All() iter.Seq[T]
- func (s *SyncSet[T]) Clear()
- func (s *SyncSet[T]) Clone() *SyncSet[T]
- func (s *SyncSet[T]) Contains(element T) bool
- func (s *SyncSet[T]) ContainsAll(elements ...T) bool
- func (s *SyncSet[T]) ContainsAny(elements ...T) bool
- func (s *SyncSet[T]) Difference(other *SyncSet[T]) *SyncSet[T]
- func (s *SyncSet[T]) Do(fn func(*Set[T]))
- func (s *SyncSet[T]) Equal(other *SyncSet[T]) bool
- func (s *SyncSet[T]) Filter(keep func(T) bool) *SyncSet[T]
- func (s *SyncSet[T]) Intersection(other *SyncSet[T]) *SyncSet[T]
- func (s *SyncSet[T]) IsDisjoint(other *SyncSet[T]) bool
- func (s *SyncSet[T]) IsEmpty() bool
- func (s *SyncSet[T]) IsProperSubset(other *SyncSet[T]) bool
- func (s *SyncSet[T]) IsProperSuperset(other *SyncSet[T]) bool
- func (s *SyncSet[T]) IsSubset(other *SyncSet[T]) bool
- func (s *SyncSet[T]) IsSuperset(other *SyncSet[T]) bool
- func (s *SyncSet[T]) Len() int
- func (s *SyncSet[T]) MarshalJSON() ([]byte, error)
- func (s *SyncSet[T]) Pop() (T, bool)
- func (s *SyncSet[T]) Remove(elements ...T)
- func (s *SyncSet[T]) RemoveFunc(remove func(T) bool) int
- func (s *SyncSet[T]) RemoveSet(other *SyncSet[T])
- func (s *SyncSet[T]) RetainSet(other *SyncSet[T])
- func (s *SyncSet[T]) Slice() []T
- func (s *SyncSet[T]) Snapshot() Set[T]
- func (s *SyncSet[T]) String() string
- func (s *SyncSet[T]) SymmetricDifference(other *SyncSet[T]) *SyncSet[T]
- func (s *SyncSet[T]) Union(other *SyncSet[T]) *SyncSet[T]
- func (s *SyncSet[T]) UnmarshalJSON(data []byte) error
Examples ¶
- Collect
- New
- NewSync
- Set.Add
- Set.AddSet
- Set.Clone
- Set.ContainsAll
- Set.ContainsAny
- Set.Difference
- Set.Filter
- Set.Intersection
- Set.IsDisjoint
- Set.IsProperSubset
- Set.IsSubset
- Set.MarshalJSON
- Set.Pop
- Set.Remove
- Set.RemoveFunc
- Set.RemoveSet
- Set.RetainSet
- Set.String
- Set.SymmetricDifference
- Set.Union
- Set.UnmarshalJSON
- Sorted
- SyncSet.Do
- SyncSet.Snapshot
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.
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.
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 Set that holds or has held elements copies a reference to the same underlying storage; use Set.Clone for an independent copy.
An element must be equal to itself to be in a set, so Set.Add ignores a NaN, or any value containing one: a set could never find or remove it again. If T is an interface type, adding or looking up an element whose dynamic type cannot be compared, such as a slice or a map, panics, as it does for a Go map key.
A Set is not safe for concurrent use; use a SyncSet to share one between goroutines.
func Collect ¶ added in v0.3.0
func Collect[T comparable](seq iter.Seq[T]) Set[T]
Collect returns a new set of the elements yielded by seq. As with New, an element that is not equal to itself, such as a NaN, is ignored.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/kastolars/readyset"
)
func main() {
words := []string{"b", "a", "b", "c"}
unique := readyset.Collect(slices.Values(words))
fmt.Println(unique)
}
Output: {a, b, c}
func New ¶
func New[T comparable](elements ...T) Set[T]
New returns a Set containing elements. Duplicates are ignored, and so is any element that is not equal to itself, such as a NaN.
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, and so is any element that is not equal to itself, such as a NaN.
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.
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]) ContainsAll ¶ added in v0.3.0
ContainsAll reports whether every one of elements is in the set. It is true when elements is empty.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
s := readyset.New(1, 2, 3)
fmt.Println(s.ContainsAll(1, 2), s.ContainsAll(1, 9))
}
Output: true false
func (Set[T]) ContainsAny ¶ added in v0.3.0
ContainsAny reports whether at least one of elements is in the set. It is false when elements is empty.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
s := readyset.New(1, 2, 3)
fmt.Println(s.ContainsAny(9, 3), s.ContainsAny(8, 9))
}
Output: true false
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]) Filter ¶ added in v0.3.0
Filter returns a new set of the elements for which keep returns true.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
s := readyset.New(1, 2, 3, 4, 5, 6)
fmt.Println(s.Filter(func(n int) bool { return n > 4 }))
}
Output: {5, 6}
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]) IsProperSubset ¶ added in v0.3.0
IsProperSubset reports whether s is a subset of other and the two are not equal, that is, other has at least one element that s lacks.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
a, b := readyset.New(1, 2), readyset.New(1, 2, 3)
fmt.Println(a.IsSubset(a), a.IsProperSubset(a), a.IsProperSubset(b))
}
Output: true false true
func (Set[T]) IsProperSuperset ¶ added in v0.3.0
IsProperSuperset reports whether s is a superset of other and the two are not equal, that is, s has at least one element that other lacks.
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 a set encodes the same way whatever its map iteration order. 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]) Pop ¶ added in v0.3.0
Pop removes and returns an arbitrary element of the set. The boolean is false if the set was empty. Which element is returned is unspecified and can differ between calls, so use it to drain a worklist, not to pick a particular element.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
// Pop drains a worklist: process each item once, in no particular order.
todo := readyset.New("a", "b", "c")
n := 0
for {
_, ok := todo.Pop()
if !ok {
break
}
n++
}
fmt.Println(n, todo.IsEmpty())
}
Output: 3 true
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]) RemoveFunc ¶ added in v0.3.0
RemoveFunc deletes every element for which remove returns true and reports how many it deleted. remove must not modify the set.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
s := readyset.New(1, 2, 3, 4, 5, 6)
removed := s.RemoveFunc(func(n int) bool { return n%2 == 0 })
fmt.Println(removed, s)
}
Output: 3 {1, 3, 5}
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]) RetainSet ¶ added in v0.3.0
RetainSet deletes every element that is not also in other. It is the in-place counterpart of Set.Intersection: afterwards s holds what s.Intersection(other) would have returned, and other is not modified.
Example ¶
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
granted := readyset.New("read", "write", "admin")
granted.RetainSet(readyset.New("read", "write", "delete"))
fmt.Println(granted)
}
Output: {read, write}
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, including when T is an interface type and the array holds an element that cannot be in a set, such as a nested array.
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
type SyncSet ¶ added in v0.4.0
type SyncSet[T comparable] struct { // contains filtered or unexported fields }
SyncSet is a Set that is safe for concurrent use by multiple goroutines.
Use it when goroutines share a set and you want the locking done for you. A plain Set is faster and has no restrictions, so prefer it when only one goroutine touches the set at a time, or when you already hold a lock of your own around it.
The zero value is an empty set that is ready to use. A SyncSet contains a lock, so it must be used through a pointer and must not be copied after first use; go vet reports copies. Use NewSync to create one with elements.
Every method is atomic by itself, but a sequence of calls is not. This is a race, because another goroutine can add the element between the two calls:
if !s.Contains(x) { // wrong
s.Add(x)
}
Use SyncSet.Do to run several steps under one lock.
Operations that take another SyncSet lock both sets, always in the same order, so two goroutines running a.Union(b) and b.Union(a) cannot deadlock, and a.Union(a) is fine. Functions you pass in (to SyncSet.Filter, SyncSet.RemoveFunc and SyncSet.Do) run while the lock is held, so they must not call methods on any SyncSet, not only the same one. A callback that locks a different set does so outside that fixed order, so two callbacks that each use the other's set can deadlock. To combine two sets use a two-set method such as SyncSet.RemoveSet or SyncSet.Intersection, or take a SyncSet.Snapshot of the other set before the call.
The lock protects the set, not what its elements point to. Elements are copied by value into snapshots, clones and slices, and anything an element holds a reference to, such as a pointer, is shared and not locked.
func NewSync ¶ added in v0.4.0
func NewSync[T comparable](elements ...T) *SyncSet[T]
NewSync returns a SyncSet containing elements. Duplicates are ignored.
Example ¶
A SyncSet can be shared between goroutines without any locking of your own.
package main
import (
"fmt"
"sync"
"github.com/kastolars/readyset"
)
func main() {
seen := readyset.NewSync[string]()
var wg sync.WaitGroup
for _, word := range []string{"b", "a", "b", "c", "a"} {
wg.Add(1)
go func() {
defer wg.Done()
seen.Add(word)
}()
}
wg.Wait()
fmt.Println(seen.Len(), seen)
}
Output: 3 {a, b, c}
func (*SyncSet[T]) Add ¶ added in v0.4.0
func (s *SyncSet[T]) Add(elements ...T)
Add inserts elements into the set. Elements already present are ignored.
func (*SyncSet[T]) AddSet ¶ added in v0.4.0
AddSet inserts every element of other into the set. It is the in-place counterpart of SyncSet.Union, and other is not modified. See Set.AddSet.
func (*SyncSet[T]) All ¶ added in v0.4.0
All returns an iterator over the elements of the set, in no particular order. The elements are copied when iteration starts, so the loop body may freely change this set (or any other goroutine may) without affecting or deadlocking the loop, and the loop does not see those changes.
func (*SyncSet[T]) Clear ¶ added in v0.4.0
func (s *SyncSet[T]) Clear()
Clear removes every element from the set.
func (*SyncSet[T]) ContainsAll ¶ added in v0.4.0
ContainsAll reports whether every one of elements is in the set. It is true when elements is empty.
func (*SyncSet[T]) ContainsAny ¶ added in v0.4.0
ContainsAny reports whether at least one of elements is in the set. It is false when elements is empty.
func (*SyncSet[T]) Difference ¶ added in v0.4.0
Difference returns a new set of the elements that are in s but not in other.
func (*SyncSet[T]) Do ¶ added in v0.4.0
Do calls fn with the underlying Set while holding the set's write lock, and returns when fn returns. Use it for a sequence of steps that must not be interleaved with other goroutines, such as adding an element only if it is absent:
var added bool
s.Do(func(set *readyset.Set[string]) {
if !set.Contains(name) {
set.Add(name)
added = true
}
})
fn must not call methods on s, which would deadlock, or on any other SyncSet, which can deadlock too (see SyncSet), and must not keep the *Set after it returns.
Example ¶
Contains followed by Add is a race, because another goroutine can add the name in between. Do runs both steps under one lock.
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
names := readyset.NewSync("ann")
claim := func(name string) bool {
claimed := false
names.Do(func(set *readyset.Set[string]) {
if !set.Contains(name) {
set.Add(name)
claimed = true
}
})
return claimed
}
fmt.Println(claim("ann"), claim("bob"))
}
Output: false true
func (*SyncSet[T]) Equal ¶ added in v0.4.0
Equal reports whether s and other contain exactly the same elements.
func (*SyncSet[T]) Filter ¶ added in v0.4.0
Filter returns a new set of the elements for which keep returns true. keep runs while the set is locked, so it must not call methods on any SyncSet.
func (*SyncSet[T]) Intersection ¶ added in v0.4.0
Intersection returns a new set of the elements that are in both s and other.
func (*SyncSet[T]) IsDisjoint ¶ added in v0.4.0
IsDisjoint reports whether s and other have no elements in common.
func (*SyncSet[T]) IsProperSubset ¶ added in v0.4.0
IsProperSubset reports whether s is a subset of other and the two are not equal.
func (*SyncSet[T]) IsProperSuperset ¶ added in v0.4.0
IsProperSuperset reports whether s is a superset of other and the two are not equal.
func (*SyncSet[T]) IsSubset ¶ added in v0.4.0
IsSubset reports whether every element of s is also in other.
func (*SyncSet[T]) IsSuperset ¶ added in v0.4.0
IsSuperset reports whether every element of other is also in s.
func (*SyncSet[T]) MarshalJSON ¶ added in v0.4.0
MarshalJSON encodes the set as a JSON array in a stable order. See Set.MarshalJSON.
func (*SyncSet[T]) Pop ¶ added in v0.4.0
Pop removes and returns an arbitrary element of the set. The boolean is false if the set was empty. See Set.Pop.
func (*SyncSet[T]) Remove ¶ added in v0.4.0
func (s *SyncSet[T]) Remove(elements ...T)
Remove deletes elements from the set. Elements not present are ignored.
func (*SyncSet[T]) RemoveFunc ¶ added in v0.4.0
RemoveFunc deletes every element for which remove returns true and reports how many it deleted. remove runs while the set is locked, so it must not call methods on any SyncSet.
func (*SyncSet[T]) RemoveSet ¶ added in v0.4.0
RemoveSet deletes every element of other from the set. It is the in-place counterpart of SyncSet.Difference, and other is not modified.
func (*SyncSet[T]) RetainSet ¶ added in v0.4.0
RetainSet deletes every element that is not also in other. It is the in-place counterpart of SyncSet.Intersection, and other is not modified.
func (*SyncSet[T]) Slice ¶ added in v0.4.0
func (s *SyncSet[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 (*SyncSet[T]) Snapshot ¶ added in v0.4.0
Snapshot returns the elements as a plain, independent Set, for use with Sorted or in code that does not need locking. Later changes to either set do not affect the other. The elements themselves are copied by value, so anything they refer to is still shared.
Example ¶
Snapshot copies a SyncSet into a plain Set, for functions like Sorted.
package main
import (
"fmt"
"github.com/kastolars/readyset"
)
func main() {
s := readyset.NewSync(30, 10, 20)
fmt.Println(readyset.Sorted(s.Snapshot()))
}
Output: [10 20 30]
func (*SyncSet[T]) String ¶ added in v0.4.0
String returns the elements in braces, in a stable order. See Set.String.
func (*SyncSet[T]) SymmetricDifference ¶ added in v0.4.0
SymmetricDifference returns a new set of the elements that are in exactly one of s and other.
func (*SyncSet[T]) Union ¶ added in v0.4.0
Union returns a new set of the elements that are in s, in other, or in both.
func (*SyncSet[T]) UnmarshalJSON ¶ added in v0.4.0
UnmarshalJSON decodes a JSON array into the set, replacing its previous contents. See Set.UnmarshalJSON.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
jsonenc
Package jsonenc encodes the JSON arrays that sets marshal to.
|
Package jsonenc encodes the JSON arrays that sets marshal to. |
|
lockorder
Package lockorder locks two read-write mutexes together without risking a deadlock.
|
Package lockorder locks two read-write mutexes together without risking a deadlock. |
|
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. |