本文整理汇总了Golang中hash/fnv.New64a函数的典型用法代码示例。如果您正苦于以下问题:Golang New64a函数的具体用法?Golang New64a怎么用?Golang New64a使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New64a函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: hash
func hash(v interface{}) int {
switch v := v.(type) {
case []interface{}:
var h int
for _, e := range v {
h += hash(e)
}
return h
case map[string]interface{}:
var h int
for k, v := range v {
h += hash(k) + hash(v)
}
return h
case string:
h := fnv.New64a()
h.Write([]byte(v))
return int(h.Sum64())
case bool:
if v {
return 1
}
return 0
case nil:
return 0
case json.Number:
h := fnv.New64a()
h.Write([]byte(v))
return int(h.Sum64())
}
panic(fmt.Sprintf("illegal argument: %v (%T)", v, v))
}
示例2: putGetParallel
func putGetParallel(power int, factor float64) {
var h Hashmap
h = NewChainedHash(power, fnv.New64a())
n := uint64(math.Pow(2, float64(power)))
var wg sync.WaitGroup
wg.Add(2)
go func() {
for i := uint64(0); i < uint64((float64(n) * factor)); i++ {
h.Put(strconv.FormatUint(i, 10), strconv.FormatUint(i, 10))
}
wg.Done()
}()
go func() {
for cycle := 0; cycle < 5; cycle++ {
for i := uint64(0); i < n; i++ {
h.Get(strconv.FormatUint(i, 10))
}
}
wg.Done()
}()
wg.Wait()
}
示例3: hashKey
func (c *myCipher) hashKey(key string) {
k := []byte(key)
hash := fnv.New64a()
hash.Write(k)
rand.Seed(int64(hash.Sum64()))
c.cipherTextIdx = rand.Perm(c.d.col)
}
示例4: getKey
func (n *node) getKey() string {
t := []byte(strconv.FormatInt(time.Now().UnixNano(), 10))
h := fnv.New64a()
h.Write(t)
s := strconv.FormatUint(h.Sum64(), 10)
return s
}
示例5: Lookup
func (tab *Table) Lookup(proto protocols.Protocol, hostID string, port uint16) (Rule, bool) {
var sum = fnv.New64a()
var buf [2]byte
sum.Reset()
buf[0] = byte(proto)
sum.Write(buf[:1])
io.WriteString(sum, hostID)
binary.BigEndian.PutUint16(buf[:], port)
sum.Write(buf[:])
id := sum.Sum64()
nEntries := len(tab.entries)
idx := sort.Search(nEntries, func(idx int) bool {
return tab.entries[idx].id >= id
})
for _, entry := range tab.entries[idx:] {
if entry.id != id {
break
}
if entry.Protocol == proto && entry.SrcHostID == hostID && entry.SrcPort == port {
return entry.Rule, true
}
}
return Rule{}, false
}
示例6: buildTable
func buildTable(rules []Rule) *Table {
var entries = make([]tableEntry, len(rules))
var sum = fnv.New64a()
for i, rule := range rules {
var e tableEntry
var buf [2]byte
sum.Reset()
buf[0] = byte(rule.Protocol)
sum.Write(buf[:1])
io.WriteString(sum, rule.SrcHostID)
binary.BigEndian.PutUint16(buf[:], rule.SrcPort)
sum.Write(buf[:])
id := sum.Sum64()
e.id = id
e.Rule = rule
entries[i] = e
}
sort.Sort(sortedByID(entries))
return &Table{entries: entries}
}
示例7: philosopher
// Goroutine for philosopher actions. An instance is run for each
// philosopher. Instances run concurrently.
func philosopher(phName string,
dominantHand, otherHand chan fork, done chan bool) {
fmt.Println(phName, "seated")
// each philosopher goroutine has a random number generator,
// seeded with a hash of the philosopher's name.
h := fnv.New64a()
h.Write([]byte(phName))
rg := rand.New(rand.NewSource(int64(h.Sum64())))
// utility function to sleep for a randomized nominal time
rSleep := func(t time.Duration) {
time.Sleep(t/2 + time.Duration(rg.Int63n(int64(t))))
}
for h := hunger; h > 0; h-- {
fmt.Println(phName, "hungry")
<-dominantHand // pick up forks
<-otherHand
fmt.Println(phName, "eating")
rSleep(eat)
dominantHand <- 'f' // put down forks
otherHand <- 'f'
fmt.Println(phName, "thinking")
rSleep(think)
}
fmt.Println(phName, "satisfied")
done <- true
fmt.Println(phName, "left the table")
}
示例8: Hash
func Hash(image image.Image) uint64 {
hash := fnv.New64a()
rect := image.Bounds()
size := (rect.Max.X - rect.Min.X) * (rect.Max.Y - rect.Min.Y) * 16
data := make([]byte, size)
pos := 0
for y := rect.Min.Y; y < rect.Max.Y; y++ {
for x := rect.Min.X; x < rect.Max.X; x++ {
color := image.At(x, y)
r, g, b, a := color.RGBA()
convertUint32ToBytes(r, data[pos:pos+4])
pos += 4
convertUint32ToBytes(g, data[pos:pos+4])
pos += 4
convertUint32ToBytes(b, data[pos:pos+4])
pos += 4
convertUint32ToBytes(a, data[pos:pos+4])
}
}
hash.Write(data)
return hash.Sum64()
}
示例9: partitionForKey
func (r ring) partitionForKey(key string) uint {
hasher := fnv.New64a()
hasher.Write([]byte(key))
keyHash := hasher.Sum64()
return uint(jump.Hash(keyHash, len(r.members)))
}
示例10: sliceStringID64
func sliceStringID64(sign []string) int64 {
h := fnv.New64a()
for _, s := range sign {
h.Write([]byte(s))
}
return int64Bytes(h.Sum(nil))
}
示例11: fnv64a
func (e *Engine) fnv64a() error {
data, err := computeHash(fnv.New64a(), e.stack.Pop())
if err == nil {
e.stack.Push(data)
}
return err
}
示例12: getResources
func getResources(w http.ResponseWriter, r *http.Request) *appError {
//omitted code to get top, filter et cetera from the Request
c := appengine.NewContext(r)
h := fnv.New64a()
h.Write([]byte("rap_query" + top + filter + orderby + skip))
cacheKey := h.Sum64()
cv, err := memcache.Get(c, fmt.Sprint(cacheKey))
if err == nil {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(cv.Value)
return nil
}
//omitting code that get resources from DataStore
//cache this result with it's query as the key
memcache.Set(c, &memcache.Item{
Key: fmt.Sprint(cacheKey),
Value: result,
})
//return the json version
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(result)
return nil
}
示例13: TodayUnique
func (s *Store) TodayUnique(siteId int64, uid int64) (cnt int64, e error) {
var (
hl *hyperloglog.HyperLogLogPlus
)
hl, e = s.getHyperLoglog(siteId)
if e != nil {
return
}
h := fnv.New64a()
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(uid))
io.WriteString(h, hex.EncodeToString(b))
hl.Add(h)
// encode and store hyperloglog
var bts []byte
bts, e = hl.GobEncode()
if e != nil {
return
}
e = s.ldb.Put(itob32(siteId), bts, nil)
if e != nil {
return
}
cnt = int64(hl.Count())
return
}
示例14: hash
// hash returns an identifying hash for the target.
func (t *Target) hash() uint64 {
h := fnv.New64a()
h.Write([]byte(t.labels.Fingerprint().String()))
h.Write([]byte(t.URL().String()))
return h.Sum64()
}
示例15: init
func init() {
fnv1 := fnv.New64a()
fnv2 := fnv.New64a()
h1 = func(b []byte) uint64 {
fnv1.Reset()
fnv1.Write([]byte{0xd5, 0x9d, 0x34, 0x24, 0xf4, 0x15, 0xa4, 0xfe})
fnv1.Write(b)
return fnv1.Sum64()
}
h2 = func(b []byte) uint64 {
fnv2.Reset()
fnv2.Write([]byte{0x06, 0x43, 0x0c, 0xd9, 0xde, 0x74, 0xbf, 0xf1})
fnv2.Write(b)
return fnv2.Sum64()
}
}