anyunique

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: Apache-2.0, BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package anyunique provides canonicalization of values under a caller-defined equivalence relation.

A Set holds a set of unique values of a specific type T. Calling Set.Make with two values that are equivalent according to the provided Hasher returns Handle values that are identical. Handle is a lightweight wrapper around the canonical value; use Handle.Value to obtain the underlying T.

The zero Handle represents the zero value of T. Make returns the zero Handle when called with the zero value of T: it will never try to hash the zero value.

[Set.WriteHash] writes a short representation of a canonicalized value to a maphash.Hash. It is useful when hashing structures that themselves contain canonicalized values, avoiding re-hashing the full value graph.

Values in a set are amenable to garbage collection: the set does not necesarily always grow in size.

NOTE this package assumes that T values are treated as immutable. That is, after calling Set.Make a value must not change.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handle

type Handle[T any] struct {
	// contains filtered or unexported fields
}

Handle represents a unique value of type T. If two values of type Handle[T] originating from the same Set compare equal, they are guaranteed to be equal according to the equality criteria that the set was created with.

func (Handle[T]) Value

func (u Handle[T]) Value() T

Get returns the actual value held in u. The zero value of Handle returns the zero value of T.

func (Handle[T]) WriteHash

func (u Handle[T]) WriteHash(h *maphash.Hash)

WriteHash writes a short representation of u to h. This allows callers to avoid hashing an tree of values when hashing a value that itself contains other Handle[T] items.

type Hasher

type Hasher[T any] interface {
	comparable
	Hash(*maphash.Hash, T)
	Equal(x, y T) bool
}

A Hasher defines a hash function and an equivalence relation over values of type T.

Hash must write a hash of its argument to the provided *maphash.Hash, and Equal must report whether two values are equivalent. Hash and Equal must be consistent: if Equal(x, y) is true then Hash must produce the same output for x and y.

See https://go-review.googlesource.com/c/go/+/657296/11/src/hash/maphash/hasher.go

type Set

type Set[T any, H Hasher[T]] struct {
	// contains filtered or unexported fields
}

Set holds a set of unique values of type T.

func New

func New[T any, H Hasher[T]](h H) *Set[T, H]

New returns a new set holding unique values of type T, using h to determine whether values are the same.

The equivalence relation and hash are supplied by the given Hasher.

func (*Set[T, H]) Make

func (s *Set[T, H]) Make(x T) Handle[T]

Make returns a unique value u such that u.Get() is equal to x according to the equality criteria defined by the set.

It is assumed that values will not change after passing to Make: the caller must take care to preserve immutability.

Jump to

Keyboard shortcuts

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