hashutil

package
v0.0.0-...-bd0c551 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package hashutil provides generic hashing utilities for hash computation and equality comparison of various types.

This package defines the Hasher interface, allowing custom hash and equality comparison logic for any type. It also provides default hasher implementations for comparable types, as well as specialized hashers for slice and map types.

Features:

  • Supports hash computation and equality comparison for any type
  • Provides default implementation for comparable types
  • Supports hash computation for slice and map types
  • Compatible with the standard library maphash package

Usage examples:

// Create a string hash map using the default hasher
stringHasher := hashutil.Default[string]{}
m := hashmap.NewHashMap[string, int](stringHasher)

// Create a slice hasher
intHasher := hashutil.Default[int]{}
sliceHasher := hashutil.NewSliceHasher[int](intHasher)
sliceMap := hashmap.NewHashMap[[]int, string](sliceHasher)

// Create a map hasher
valueHasher := hashutil.Default[string]{}
mapHasher := hashutil.NewMapHasher[map[string]string](valueHasher)
mapSet := hashset.NewHashSet[map[string]string](mapHasher)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Default

type Default[E comparable] struct {
	// contains filtered or unexported fields
}

DefaultHasher is a default hasher implementation for comparable types It uses Go's standard maphash package for hash computation

func (Default[E]) Equal

func (Default[E]) Equal(x, y E) bool

Equal compares two comparable values for equality

Parameters:

  • x, y: The two values to compare

Returns:

  • true if x and y are equal, false otherwise

Time Complexity: O(1)

func (Default[E]) Hash

func (Default[E]) Hash(h *maphash.Hash, v E)

Hash computes hash for comparable values using maphash.Hash

Parameters:

  • h: maphash.Hash instance for computing hash values
  • v: The value to hash

Time Complexity: O(1)

type Hasher

type Hasher[E any] interface {
	// Equal compares two values for equality
	//
	// Parameters:
	//   - x, y: The two values to compare
	//
	// Returns:
	//   - true if x and y are equal, false otherwise
	Equal(x, y E) bool
	// Hash computes a hash value for the given value using maphash.Hash
	//
	// Parameters:
	//   - h: maphash.Hash instance for computing hash values
	//   - v: The value to hash
	Hash(h *maphash.Hash, v E)
}

Hasher defines a generic interface for comparing and hashing values of any type

type MapHasher

type MapHasher[E ~map[K]V, K comparable, V any, H Hasher[V]] struct {
	// contains filtered or unexported fields
}

MapHasher is a hasher implementation for map types It uses the key and value types' Hasher to compute the hash for the entire map

func NewMapHasher

func NewMapHasher[E ~map[K]V, K comparable, V any, H Hasher[V]](hasher H) MapHasher[E, K, V, H]

NewMapHasher creates a new map hasher instance

Parameters:

  • hasher: Hasher for computing hash values of map values

Returns:

  • Newly created map hasher instance

Time Complexity: O(1)

func (MapHasher[E, K, V, H]) Equal

func (mh MapHasher[E, K, V, H]) Equal(x, y E) bool

Equal compares two maps for equality

Parameters:

  • x, y: The two maps to compare

Returns:

  • true if x and y are equal, false otherwise

Time Complexity: O(n), where n is the number of keys in the maps

func (MapHasher[E, K, V, H]) Hash

func (mh MapHasher[E, K, V, H]) Hash(h *maphash.Hash, v E)

Hash computes hash for a map using maphash.Hash

Parameters:

  • h: maphash.Hash instance for computing hash values
  • v: The map to hash

Time Complexity: O(n), where n is the number of keys in the map

type SliceHasher

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

SliceHasher is a hasher implementation for slice types It uses the element type's Hasher to compute the hash for the entire slice

func NewSliceHasher

func NewSliceHasher[E ~[]T, T any, H Hasher[T]](hasher H) SliceHasher[E, T, H]

NewSliceHasher creates a new slice hasher instance

Parameters:

  • hasher: Hasher for computing hash values of slice elements

Returns:

  • Newly created slice hasher instance

Time Complexity: O(1)

func (SliceHasher[E, T, H]) Equal

func (sh SliceHasher[E, T, H]) Equal(x, y E) bool

Equal compares two slices for equality

Parameters:

  • x, y: The two slices to compare

Returns:

  • true if x and y are equal, false otherwise

Time Complexity: O(n), where n is the length of the slices

func (SliceHasher[E, T, H]) Hash

func (sh SliceHasher[E, T, H]) Hash(h *maphash.Hash, v E)

Hash computes hash for a slice using maphash.Hash

Parameters:

  • h: maphash.Hash instance for computing hash values
  • v: The slice to hash

Time Complexity: O(n), where n is the length of the slice

Jump to

Keyboard shortcuts

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