github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/syndtr/goleveldb/leveldb/util/hash.go (about)

     1  // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
     2  // All rights reserved.
     3  //
     4  // Use of this source code is governed by a BSD-style license that can be
     5  // found in the LICENSE file.
     6  
     7  package util
     8  
     9  import (
    10  	"encoding/binary"
    11  )
    12  
    13  // Hash return hash of the given data.
    14  func Hash(data []byte, seed uint32) uint32 {
    15  	// Similar to murmur hash
    16  	const (
    17  		m = uint32(0xc6a4a793)
    18  		r = uint32(24)
    19  	)
    20  	var (
    21  		h = seed ^ (uint32(len(data)) * m)
    22  		i int
    23  	)
    24  
    25  	for n := len(data) - len(data)%4; i < n; i += 4 {
    26  		h += binary.LittleEndian.Uint32(data[i:])
    27  		h *= m
    28  		h ^= (h >> 16)
    29  	}
    30  
    31  	switch len(data) - i {
    32  	default:
    33  		panic("not reached")
    34  	case 3:
    35  		h += uint32(data[i+2]) << 16
    36  		fallthrough
    37  	case 2:
    38  		h += uint32(data[i+1]) << 8
    39  		fallthrough
    40  	case 1:
    41  		h += uint32(data[i])
    42  		h *= m
    43  		h ^= (h >> r)
    44  	case 0:
    45  	}
    46  
    47  	return h
    48  }