ghash_sdbm.go 834 B

123456789101112131415161718192021222324252627
  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/gogf/gf.
  6. package ghash
  7. // SDBM implements the classic SDBM hash algorithm for 32 bits.
  8. func SDBM(str []byte) uint32 {
  9. var hash uint32
  10. for i := 0; i < len(str); i++ {
  11. // equivalent to: hash = 65599*hash + uint32(str[i]);
  12. hash = uint32(str[i]) + (hash << 6) + (hash << 16) - hash
  13. }
  14. return hash
  15. }
  16. // SDBM64 implements the classic SDBM hash algorithm for 64 bits.
  17. func SDBM64(str []byte) uint64 {
  18. var hash uint64
  19. for i := 0; i < len(str); i++ {
  20. // equivalent to: hash = 65599*hash + uint32(str[i])
  21. hash = uint64(str[i]) + (hash << 6) + (hash << 16) - hash
  22. }
  23. return hash
  24. }