Skip to content

Instantly share code, notes, and snippets.

@wrfly
Created April 2, 2019 07:26
Show Gist options
  • Save wrfly/b2a968078376fe75f981ef83527c5ffa to your computer and use it in GitHub Desktop.
Save wrfly/b2a968078376fe75f981ef83527c5ffa to your computer and use it in GitHub Desktop.
fnv.example
package main
import (
"bufio"
"hash/fnv"
"log"
"os"
)
func main() {
if len(os.Args) < 2 {
log.Fatal("give me a file name")
}
file := os.Args[1]
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
s := bufio.NewScanner(f)
distribution := make(map[uint32]int, 100)
var total int
for s.Scan() {
in := s.Bytes()
h := fnv.New32()
h.Write(in)
out := h.Sum32()
distribution[out%100]++
total++
}
var (
maxK, minK uint32
maxV, minV int
)
minV = 10021
for k, v := range distribution {
if v > maxV {
maxV = v
maxK = k
}
if v < minV {
minV = v
minK = k
}
log.Printf("%v: %v", k, v)
}
average := total / len(distribution)
log.Printf("ave=%v", average)
log.Printf("maxK=%v, maxV=%v minK=%v; minV=%v",
maxK, maxV, minK, minV)
log.Printf("[%f,%f]",
float32((maxV-average))/float32(average),
float32((minV-average))/float32(average),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment