Involved Source Files Package hashed defines hash-based containers such as [Map].set.go
Code Examples
package main
import (
"fmt"
"hash/maphash"
"math/big"
"golang.org/x/exp/collection/hashed"
)
// bigIntHasher is a maphash.Hasher for [*big.Int] values
// that compares them mathematically, not by pointer.
type bigIntHasher struct{}
func (bigIntHasher) Hash(h *maphash.Hash, b *big.Int) {
maphash.WriteComparable(h, b.Sign())
for _, word := range b.Bits() {
maphash.WriteComparable(h, word)
}
}
func (bigIntHasher) Equal(x, y *big.Int) bool {
return x.Cmp(y) == 0
}
func main() {
m := hashed.NewMap[*big.Int, string](bigIntHasher{})
// Build a map.
i100 := big.NewInt(100)
i200 := big.NewInt(200)
m.Set(i100, "one hundred")
m.Set(i200, "two hundred")
fmt.Println("Map:", m)
// Look up an entry using a distinct but
// numerically equivalent big.Int.
i100b := big.NewInt(100)
fmt.Println("Same pointer:", i100 == i100b)
fmt.Println("Get(i100b):", m.Get(i100b))
}
package main
import (
"fmt"
"hash/maphash"
"math/big"
"golang.org/x/exp/collection/hashed"
)
// bigIntHasher is a maphash.Hasher for [*big.Int] values
// that compares them mathematically, not by pointer.
type bigIntHasher struct{}
func (bigIntHasher) Hash(h *maphash.Hash, b *big.Int) {
maphash.WriteComparable(h, b.Sign())
for _, word := range b.Bits() {
maphash.WriteComparable(h, word)
}
}
func (bigIntHasher) Equal(x, y *big.Int) bool {
return x.Cmp(y) == 0
}
func main() {
s := hashed.NewSet[*big.Int](bigIntHasher{})
// Build a set.
i100 := big.NewInt(100)
i200 := big.NewInt(200)
s.Insert(i100)
s.Insert(i200)
fmt.Println("Set:", s)
// Look up an entry using a distinct but
// numerically equivalent big.Int.
i100b := big.NewInt(100)
fmt.Println("Same pointer:", i100 == i100b)
fmt.Println("Contains(i100b):", s.Contains(i100b))
}
Package-Level Type Names (total 5, in which 2 are exported)
/* sort exporteds by: | */
Type Parameters:
K: any
V: any Map[K, V] is a hash-table based mapping from keys K to values V,
using the hash function and key-equivalence relation specified
at construction.
A Map must be created with [NewMap].
Neither a nil pointer nor the zero value are valid maps.
Map values must not be copied; use [Map.Clone] instead.
Do not use Map with a comparable key type K and its usual ==
equivalence relation; instead, use Go's built-in map type.
(If you need set operations such as Union and Intersection,
use the helpers in the collection/set package.)hashermaphash.Hasher[K] // number of map entriesseedmaphash.Seed // maps hash to entries of that hash All returns an iterator over the key/value entries of the map in
unspecified order.
It is safe to modify the map during iteration. As with the built-in
map, if a key is removed during iteration, its entry will not be
yielded by the iterator. If a key is added during iteration, it may
or may not be yielded by the iterator. The iterator yields the
value most recently associated with a given key. Clear removes all entries from the map. Clone returns a new non-nil map with the same entries as m. Contains reports whether the map contains an entry with the given key. Delete removes the entry with the given key, if present.
It returns the previous value for key if present, or the zero value if not.
The boolean (deleted) indicates whether the operation deleted a map entry. DeleteFunc removes each map entry for which del returns true.
It reports whether the map changed. Get returns the value corresponding the given key,
or the zero value if the entry is not present. Keys returns an iterator over the keys of the map in random order.
It is safe to modify the map during iteration, with the same
semantics as the [Map.All] iterator. Len returns the number of map entries. Lookup reports whether the map contains the specified key, and returns
the corresponding value if found, or the zero value if not. Set updates the map entry for key to value.
It returns the previous value for key if present, or the zero value if not.
The boolean (present) indicates whether the operation updated an existing entry
for that key. String returns a string representation of the map's entries
in an unspecified but deterministic order.
Keys and values are printed as if by [fmt.Sprint]. Values returns an iterator over the values of the map in random order.
It is safe to modify the map during iteration, with the same
semantics as the [Map.All] iterator.(*Map[K, V]) cloneFrom(src *Map[K, V]) hash returns the hash of the specified key.(*Map[K, V]) init(hasher maphash.Hasher[K]) setAll calls Set(k, v) for each key/value pair in the sequence.
It reports whether the number of map entries increased.
*Map : expvar.Var
*Map : fmt.Stringer
*Map : context.stringer
*Map : runtime.stringer
func NewMap[K, V](hasher maphash.Hasher[K]) *Map[K, V]
func (*Map)[K, V].Clone() *Map[K, V]
func (*Map)[K, V].cloneFrom(src *Map[K, V])
func go.pact.im/x/plumb/internal/solve.sortTypesByProducer(ts []types.Type, supply *Map[types.Type, *solve.Instance])
Type Parameters:
E: any Set[E] is a hash-table based set of elements of type E,
using the hash function and key-equivalence relation specified
at construction.
A Set must be created with [NewSet].
Neither a nil pointer nor the zero value are valid sets.
Set values must not be copied; use [Set.Clone] instead.
Do not use Set with a comparable element type E and its usual ==
equivalence relation; instead, use Go's built-in map type.
(If you need set operations such as Union and Intersection,
use the helpers in the [golang.org/x/exp/collection/set] package.)
Set should not be used with elements that are (or contain)
floating-point numbers whose value may be NaN, as it may take
shortcuts based on algebraic identities that assume e == e for any
element e. For example, Intersects(s, s) returns true for any
non-empty set, without inspecting the elements.
For all binary operations on sets, like the set algebra methods
[Set.Union], [Set.Intersection] and so on, the two sets must
use the same key-equivalence relation.mMap[E, unit] All returns an iterator over the elements of the set in unspecified order.
It is safe to modify the set during iteration. As with the built-in
set, if an element is removed during iteration, it will not be
yielded by the iterator. If an element is added during iteration,
it may or may not be yielded by the iterator. Clear removes all entries from the set. Clone returns a new set with the same elements as s. Contains reports whether the set contains the specified element. Delete removes the given element, if present.
It reports whether the element was present. DeleteFunc deletes each element e in the set for which del returns true.
It reports whether the set changed. Difference returns a new set containing elements that are in s but not in other. DifferenceWith removes any elements from s that are in other. Equal reports whether s and other contain the same elements. Insert adds the specified element to the set
(if no equal element is present),
and reports whether the set grew. Intersection returns a new set containing elements that are in both s and other. IntersectionWith removes any elements from s that are not in other. Intersects reports whether s and other have any elements in common. Len returns the number of set elements. String returns a string representation of the set's elements
in an unspecified but deterministic order.
Elements are printed as if by [fmt.Sprint]. SymmetricDifference returns a new set containing elements that are in either s or other, but not both. SymmetricDifferenceWith sets s to the symmetric difference of s and other. Union returns a new set containing all elements of s and other. UnionWith adds all elements of other to s. containsAll reports whether set s contains all element of the sequence. diff adds to s the elements of from that are not in exclude. insertAll calls Insert for each element in the sequence.
It reports whether the size changed.
*Set : expvar.Var
*Set : fmt.Stringer
*Set : context.stringer
*Set : runtime.stringer
func NewSet[E](hasher maphash.Hasher[E]) *Set[E]
func (*Set)[E].Clone() *Set[E]
func (*Set)[E].Difference(other *Set[E]) *Set[E]
func (*Set)[E].Intersection(other *Set[E]) *Set[E]
func (*Set)[E].SymmetricDifference(other *Set[E]) *Set[E]
func (*Set)[E].Union(other *Set[E]) *Set[E]
func (*Set)[E].Difference(other *Set[E]) *Set[E]
func (*Set)[E].DifferenceWith(other *Set[E])
func (*Set)[E].Equal(other *Set[E]) bool
func (*Set)[E].Intersection(other *Set[E]) *Set[E]
func (*Set)[E].IntersectionWith(other *Set[E])
func (*Set)[E].Intersects(other *Set[E]) bool
func (*Set)[E].SymmetricDifference(other *Set[E]) *Set[E]
func (*Set)[E].SymmetricDifferenceWith(other *Set[E])
func (*Set)[E].Union(other *Set[E]) *Set[E]
func (*Set)[E].UnionWith(other *Set[E])
func (*Set)[E].diff(from, exclude *Set[E])
func go.pact.im/x/plumb/internal/gotypes.tupleContains(tup *types.Tuple, seen *Set[types.Type], pred func(types.Type) bool) bool
func go.pact.im/x/plumb/internal/gotypes.typeContains(t types.Type, seen *Set[types.Type], pred func(types.Type) bool) bool
noCopy may be added to structs which must not be copied
after the first use.
See https://golang.org/issues/8005#issuecomment-190753527
for details.
Note that it must not be embedded, due to the Lock and Unlock methods. Lock is a no-op used by -copylocks checker from `go vet`.(*noCopy) Unlock()
*noCopy : sync.Locker