hash

package
v0.0.0-...-886b66b Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

README

The deriveHash function calculates a hash of an object.

Given the following input:

package hash

import "reflect"

type Entry struct {
	Name    string
	Numbers []int
}

func hasDuplicates(es []*Entry) bool {
	m := make(map[uint64][]*Entry)
	for i, e := range es {
		h := deriveHash(e)
		for _, f := range m[h] {
			if reflect.DeepEqual(e, f) {
				return true
			}
		}
		if _, ok := m[h]; !ok {
			m[h] = []*Entry{es[i]}
		}
	}
	return false
}

goderive will generate the following code:

// Code generated by goderive DO NOT EDIT.

package hash

// deriveHash returns the hash of the object.
func deriveHash(object *Entry) uint64 {
	if object == nil {
		return 0
	}
	h := uint64(17)
	h = 31*h + deriveHash_(object.Name)
	h = 31*h + deriveHash_1(object.Numbers)
	return h
}

// deriveHash_ returns the hash of the object.
func deriveHash_(object string) uint64 {
	var h uint64
	for _, c := range object {
		h = 31*h + uint64(c)
	}
	return h
}

// deriveHash_1 returns the hash of the object.
func deriveHash_1(object []int) uint64 {
	if object == nil {
		return 0
	}
	h := uint64(17)
	for i := 0; i < len(object); i++ {
		h = 31*h + uint64(object[i])
	}
	return h
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Name    string
	Numbers []int
}

Jump to

Keyboard shortcuts

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