Simplified tile hash.

This commit is contained in:
Sascha L. Teichmann 2016-05-08 17:22:04 +02:00
parent 0900bd16ce
commit 792aae99d7

View File

@ -10,32 +10,27 @@ import (
"sync" "sync"
) )
type btPos struct {
x int
y int
}
type BaseTileHash struct { type BaseTileHash struct {
// XXX: Maybe use some kind of LRU cache instead? // XXX: Maybe use some kind of LRU cache instead?
hashes map[btPos][]byte hashes map[struct{ x, y int }][]byte
sync.Mutex sync.Mutex
} }
func NewBaseTileHash() *BaseTileHash { func NewBaseTileHash() *BaseTileHash {
return &BaseTileHash{hashes: map[btPos][]byte{}} return &BaseTileHash{hashes: map[struct{ x, y int }][]byte{}}
} }
func (bth *BaseTileHash) Update(x, y int, img image.Image) bool { func (bth *BaseTileHash) Update(x, y int, img image.Image) bool {
hash := SHA1Image(img) hash := SHA1Image(img)
key := btPos{x, y} key := struct{ x, y int }{x, y}
bth.Lock() bth.Lock()
defer bth.Unlock() defer bth.Unlock()
if old, found := bth.hashes[key]; found { if old, found := bth.hashes[key]; found {
equals := bytes.Equal(old, hash) if !bytes.Equal(old, hash) {
if !equals {
bth.hashes[key] = hash bth.hashes[key] = hash
return true
} }
return !equals return false
} }
bth.hashes[key] = hash bth.hashes[key] = hash
return true return true