package hashed

Import Path
	golang.org/x/exp/collection/hashed (on go.dev)

Dependency Relation
	imports 9 packages, and imported by 3 packages

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.) 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 : expvar.Var *Map : fmt.Stringer func NewMap[K, V](hasher maphash.Hasher[K]) *Map[K, V] func (*Map)[K, V].Clone() *Map[K, V]
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. 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. *Set : expvar.Var *Set : fmt.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])
Package-Level Functions (total 2, both are exported)
Type Parameters: K: any V: any NewMap returns a new map that uses the specified hash function and key-equivalence relation.
Type Parameters: E: any NewSet returns a new empty set that uses the specified hash function and key-equivalence relation.
Package-Level Variables (only one, which is unexported)