github.com/vugu/vugu@v0.3.5/gen/comp-hasher.go (about)

     1  package gen
     2  
     3  import (
     4  	"encoding/binary"
     5  	"sync"
     6  
     7  	"github.com/vugu/xxhash"
     8  )
     9  
    10  var compHashCounts = make(map[string]int, 8)
    11  var compHashMU sync.Mutex
    12  
    13  // compHashCounted returns a hash value for the given string and
    14  // maintaining a count of the number of times that string was passed.
    15  // Each time a given s is provided a different hash will be returned,
    16  // but invocations from one program run to the next should return
    17  // the same series.  (This avoids unnecessary changes in generated files
    18  // when produced with the same input.)
    19  func compHashCounted(s string) uint64 {
    20  	compHashMU.Lock()
    21  	c := compHashCounts[s]
    22  	compHashCounts[s] = c + 1
    23  	compHashMU.Unlock()
    24  
    25  	h := xxhash.New()
    26  	h.WriteString(s)
    27  	var b [8]byte
    28  	binary.BigEndian.PutUint64(b[:], uint64(c))
    29  	h.Write(b[:])
    30  	return h.Sum64()
    31  
    32  }