2014-08-21 00:09:40 -05:00
|
|
|
package hashcode
|
|
|
|
|
2015-02-11 13:40:49 -06:00
|
|
|
import (
|
|
|
|
"hash/crc32"
|
|
|
|
)
|
2014-08-21 00:09:40 -05:00
|
|
|
|
|
|
|
// String hashes a string to a unique hashcode.
|
2015-02-11 12:59:21 -06:00
|
|
|
//
|
|
|
|
// crc32 returns a uint32, but for our use we need
|
|
|
|
// and non negative integer. Here we cast to an integer
|
|
|
|
// and invert it if the result is negative.
|
2014-08-21 00:09:40 -05:00
|
|
|
func String(s string) int {
|
2015-02-11 12:59:21 -06:00
|
|
|
v := int(crc32.ChecksumIEEE([]byte(s)))
|
2016-07-01 16:40:07 -05:00
|
|
|
if v >= 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
if -v >= 0 {
|
2015-02-11 12:59:21 -06:00
|
|
|
return -v
|
|
|
|
}
|
2016-07-01 16:40:07 -05:00
|
|
|
// v == MinInt
|
|
|
|
return 0
|
2014-08-21 00:09:40 -05:00
|
|
|
}
|