github.com/wangyougui/gf/v2@v2.6.5/encoding/ghash/ghash_sdbm.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package ghash
     8  
     9  // SDBM implements the classic SDBM hash algorithm for 32 bits.
    10  func SDBM(str []byte) uint32 {
    11  	var hash uint32
    12  	for i := 0; i < len(str); i++ {
    13  		// equivalent to: hash = 65599*hash + uint32(str[i]);
    14  		hash = uint32(str[i]) + (hash << 6) + (hash << 16) - hash
    15  	}
    16  	return hash
    17  }
    18  
    19  // SDBM64 implements the classic SDBM hash algorithm for 64 bits.
    20  func SDBM64(str []byte) uint64 {
    21  	var hash uint64
    22  	for i := 0; i < len(str); i++ {
    23  		// equivalent to: hash = 65599*hash + uint32(str[i])
    24  		hash = uint64(str[i]) + (hash << 6) + (hash << 16) - hash
    25  	}
    26  	return hash
    27  }