unsafehash

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: BSD-3-Clause Imports: 8 Imported by: 1

README

unsafehash

unsafehash基于unsafe.Pointer计算comparable变量的uint64 hash值。

示例

package main

import (
	"fmt"
	"hash/crc32"

	"github.com/eachain/unsafehash"
)

type foo struct {
	a int
	b string
}

func main() {
	var hello [5]byte
	copy(hello[:], "hello")
	fmt.Println(crc32.ChecksumIEEE(hello[:]))
	fmt.Println(unsafehash.Make[string](unsafehash.CRC32)(string(hello[:])))
	fmt.Println(unsafehash.Make[[5]byte](unsafehash.CRC32)(hello))
	// Output:
	// 907060870
	// 907060870
	// 907060870

	index := unsafehash.Map[any]()
	m := make(map[uint64]any)
	m[index(123)] = 123
	m[index("hello")] = "hello"
	f := foo{a: 123, b: "abc"}
	m[index(f)] = f

	fmt.Printf("123: %v\n", m[index(123)])
	fmt.Printf("hello: %v\n", m[index("hello")])
	fmt.Printf("foo: %+v\n", m[index(f)])
	// Output:
	// 123: 123
	// hello: hello
	// foo: {a:123 b:abc}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BytesHasher

type BytesHasher interface {
	HashBytes([]byte) uint64
}

type HashBytesFunc

type HashBytesFunc func([]byte) uint64
var (
	CRC32     HashBytesFunc
	CRC64ISO  HashBytesFunc
	CRC64ECMA HashBytesFunc
	Adler32   HashBytesFunc
	FNV64     HashBytesFunc
	FNV64a    HashBytesFunc
)

func (HashBytesFunc) HashBytes

func (f HashBytesFunc) HashBytes(b []byte) uint64

type HashFunc

type HashFunc[T comparable] func(T) uint64

func Make

func Make[T comparable](hasher BytesHasher) HashFunc[T]

func Map

func Map[T comparable]() HashFunc[T]

Jump to

Keyboard shortcuts

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