本文整理汇总了Golang中github.com/dchest/siphash.Hash函数的典型用法代码示例。如果您正苦于以下问题:Golang Hash函数的具体用法?Golang Hash怎么用?Golang Hash使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Hash函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: makeId
func makeId(dv driver.Value) uint64 {
switch vt := dv.(type) {
case int:
return uint64(vt)
case int64:
return uint64(vt)
case []byte:
return siphash.Hash(0, 1, vt)
// iv, err := strconv.ParseUint(string(vt), 10, 64)
// if err != nil {
// u.Warnf("could not create id: %v for %v", err, dv)
// }
// return iv
case string:
return siphash.Hash(0, 1, []byte(vt))
// iv, err := strconv.ParseUint(vt, 10, 64)
// if err != nil {
// u.Warnf("could not create id: %v for %v", err, dv)
// }
// return iv
case *Key:
//u.Infof("got %#v", vt)
return vt.Id
case datasource.KeyCol:
//u.Infof("got %#v", vt)
return makeId(vt.Val)
default:
//u.LogTracef(u.WARN, "wat")
u.Warnf("not implemented conversion: %T", dv)
}
return 0
}
示例2: New
// New Replica Factor 1 with random
func New(randomized bool) *RF1R {
res := RF1R{}
if randomized {
res.hasher1 = func(b []byte) uint64 { return siphash.Hash(0, 0, b) }
res.hasher2 = func(b []byte) uint64 { return siphash.Hash(2, 3, b) }
} else {
res.hasher1 = func(b []byte) uint64 { return siphash.Hash(0, 0, b) }
res.hasher2 = func(b []byte) uint64 { return siphash.Hash(0, 0, b) }
}
return &res
}
示例3: BenchmarkSipHashFast
func BenchmarkSipHashFast(b *testing.B) {
var key0, key1 uint64 = 0, 1
b.StartTimer()
for i := 0; i < b.N; i++ {
siphash.Hash(key0, key1, []byte("Hello World"))
}
}
示例4: Add
func (t *T) Add(key string, val interface{}) {
newitem := slruItem{0, key, val, siphash.Hash(0, 0, stringToSlice(key))}
oitem, evicted := t.lru.add(newitem)
if !evicted {
return
}
// estimate count of what will be evicted from slru
victim := t.slru.victim()
if victim == nil {
t.slru.add(oitem)
return
}
if !t.bouncer.allow(oitem.keyh) {
return
}
vcount := t.c.estimate(victim.keyh)
ocount := t.c.estimate(oitem.keyh)
if ocount < vcount {
return
}
t.slru.add(oitem)
}
示例5: New
// New Replica Factor 1 with random
func New() *RF2 {
res := RF2{}
res.hasher = func(b []byte) uint64 { return siphash.Hash(0, 0, b) }
return &res
}
示例6: main
func main() {
verbose := flag.Bool("v", false, "verbose")
flag.Parse()
scanner := bufio.NewScanner(os.Stdin)
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
for scanner.Scan() {
fname := scanner.Text()
b, err := ioutil.ReadFile(fname)
if err != nil {
log.Println(err)
continue
}
sip := siphash.Hash(0, 0, b)
data, err := lz4.Encode(nil, b)
if err != nil {
log.Println("error packing: ", err)
continue
}
const headerSize = 4 + 8 + 2 + 4 // + len(fname)
h := struct {
PacketSize uint32
SipHash uint64
Fname []byte `binpack:"lenprefix=uint16"`
UncompressedSize uint32
}{
PacketSize: uint32(headerSize + len(fname) + len(data)),
SipHash: sip,
Fname: []byte(fname),
UncompressedSize: uint32(len(b)),
}
if *verbose {
log.Printf("%s: %d -> %d\n", fname, len(b), len(data))
}
err = binpack.Write(w, binary.LittleEndian, h)
if err != nil {
log.Printf("error writing header %+v\n", err)
break
}
_, err = w.Write(data)
if err != nil {
log.Printf("error writing %+v\n", err)
break
}
}
}
示例7: SendCmpctBlk
func (c *OneConnection) SendCmpctBlk(hash *btc.Uint256) {
crec := GetchBlockForBIP152(hash)
if crec == nil {
fmt.Println(c.ConnID, "cmpctblock not sent for", hash.String())
return
}
k0 := binary.LittleEndian.Uint64(crec.BIP152[8:16])
k1 := binary.LittleEndian.Uint64(crec.BIP152[16:24])
msg := new(bytes.Buffer)
msg.Write(crec.Data[:80])
msg.Write(crec.BIP152[:8])
btc.WriteVlen(msg, uint64(len(crec.Block.Txs)-1)) // all except coinbase
for i := 1; i < len(crec.Block.Txs); i++ {
var lsb [8]byte
var hasz *btc.Uint256
if c.Node.SendCmpctVer == 2 {
hasz = crec.Block.Txs[i].WTxID()
} else {
hasz = crec.Block.Txs[i].Hash
}
binary.LittleEndian.PutUint64(lsb[:], siphash.Hash(k0, k1, hasz.Hash[:]))
msg.Write(lsb[:6])
}
msg.Write([]byte{1}) // one preffiled tx
msg.Write([]byte{0}) // coinbase - index 0
if c.Node.SendCmpctVer == 2 {
msg.Write(crec.Block.Txs[0].Raw) // coinbase - index 0
} else {
crec.Block.Txs[0].WriteSerialized(msg) // coinbase - index 0
}
c.SendRawMsg("cmpctblock", msg.Bytes())
}
示例8: Hash
// Hash returns a simhash value for the document returned by the scanner
func Hash(scanner FeatureScanner) uint64 {
var signs [64]int64
for scanner.Scan() {
b := scanner.Bytes()
h := siphash.Hash(0, 0, b)
for i := 0; i < 64; i++ {
negate := int(h) & 1
// if negate is 1, we will negate '-1', below
r := (-1 ^ -negate) + negate
signs[i] += int64(r)
h >>= 1
}
}
var shash uint64
// TODO: can probably be done with SSE?
for i := 63; i >= 0; i-- {
shash <<= 1
shash |= uint64(signs[i]>>63) & 1
}
return shash
}
示例9: Get
func (t *T) Get(key string) (interface{}, bool) {
t.w++
if t.w == t.samples {
t.c.reset()
t.bouncer.reset()
t.w = 0
}
val, ok := t.data[key]
if !ok {
keyh := siphash.Hash(0, 0, stringToSlice(key))
t.c.add(keyh)
return nil, false
}
item := val.Value.(*slruItem)
t.c.add(item.keyh)
v := item.value
if item.listid == 0 {
t.lru.get(val)
} else {
t.slru.get(val)
}
return v, true
}
示例10: main
func main() {
f := flag.String("f", "/dev/stdin", "input file")
n := flag.Int("n", 100e6, "cardinality estimate of set")
fprate := flag.Float64("fp", 0.00000001, "false positive rate")
flag.Parse()
file, err := os.Open(*f)
if err != nil {
log.Fatal(err)
}
var lines int
b := bloomf.New(*n, *fprate, func(b []byte) uint64 { return siphash.Hash(0, 0, b) })
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines++
if !b.Lookup(scanner.Bytes()) {
os.Stdout.Write(scanner.Bytes())
os.Stdout.Write([]byte("\n"))
}
b.Insert(scanner.Bytes())
if lines%(1<<20) == 0 {
log.Println(lines)
}
}
}
示例11: Set
func (d *Dict) Set(key []byte, value interface{}) {
p := siphash.Hash(d.k0, d.k1, key) % dictSize
node := d.data[p]
if node == nil {
d.data[p] = &dictNode{
k: key,
v: value,
next: nil,
}
d.size += 1
return
}
for {
if bytes.Equal(node.k, key) {
node.v = value
return
}
if node.next != nil {
node = node.next
continue
}
node.next = &dictNode{
k: key,
v: value,
next: nil,
}
d.size += 1
return
}
}
示例12: Delete
func (d *Dict) Delete(key []byte) {
p := siphash.Hash(d.k0, d.k1, key) % dictSize
node := d.data[p]
if node == nil {
return
}
if bytes.Equal(node.k, key) {
d.data[p] = nil
d.size -= 1
return
}
next := node.next
for {
if next == nil {
return
}
if bytes.Equal(next.k, key) {
node.next = next.next
d.size -= 1
return
}
next = node.next
}
panic("unreachable code")
}
示例13: main
func main() {
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
u := siphash.Hash(0, 0, scan.Bytes())
fmt.Printf("%016x\n", u)
}
}
示例14: hashOne
func hashOne(k0, k1 uint64, r io.Reader, name string) {
buf, err := ioutil.ReadAll(r)
if err != nil {
log.Println(name, err)
return
}
h := siphash.Hash(k0, k1, buf)
fmt.Printf("%016x %s\n", h, name)
}
示例15: sipuintptr
func sipuintptr(s []uintptr) uint64 {
b := make([]byte, len(s)*8)
for i, v := range s {
binary.LittleEndian.PutUint64(b[i*8:], uint64(v))
}
return siphash.Hash(0, 0, b)
}