2016-05-08 12:33:17 +02:00
|
|
|
// Copyright 2016 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BaseTileHash struct {
|
|
|
|
// XXX: Maybe use some kind of LRU cache instead?
|
2016-05-08 17:22:04 +02:00
|
|
|
hashes map[struct{ x, y int }][]byte
|
2016-05-08 12:33:17 +02:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseTileHash() *BaseTileHash {
|
2016-05-08 17:22:04 +02:00
|
|
|
return &BaseTileHash{hashes: map[struct{ x, y int }][]byte{}}
|
2016-05-08 12:33:17 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 17:33:51 +02:00
|
|
|
func (bth *BaseTileHash) Update(x, y int, hash []byte) bool {
|
2016-05-08 17:22:04 +02:00
|
|
|
key := struct{ x, y int }{x, y}
|
2016-05-08 12:33:17 +02:00
|
|
|
bth.Lock()
|
|
|
|
defer bth.Unlock()
|
|
|
|
if old, found := bth.hashes[key]; found {
|
2016-05-08 17:22:04 +02:00
|
|
|
if !bytes.Equal(old, hash) {
|
2016-05-08 12:33:17 +02:00
|
|
|
bth.hashes[key] = hash
|
2016-05-08 17:22:04 +02:00
|
|
|
return true
|
2016-05-08 12:33:17 +02:00
|
|
|
}
|
2016-05-08 17:22:04 +02:00
|
|
|
return false
|
2016-05-08 12:33:17 +02:00
|
|
|
}
|
|
|
|
bth.hashes[key] = hash
|
|
|
|
return true
|
|
|
|
}
|