-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
49 lines (44 loc) · 1.23 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package texture
import "fmt"
// Cache provides a simple caching layer for a field which can be used when the expense of recalcuating a
// source is expensive or for when multiple queries are likely to be made e.g. morphological and convolution
// operations.
type Cache struct {
Name string
Src Field
Resolution float64
Limit int
oneovrres float64
cache map[string]float64
}
// NewCache creates a new Cache with the specified resolution and limit. Once the limit is reached, the cache
// will be reset. The resolution determines the accuracy of the x,y mapping to previous requests.
func NewCache(src Field, resolution float64, limit int) *Cache {
return &Cache{"Cache", src, resolution, limit, 1 / resolution, make(map[string]float64)}
}
// Eval2 implements the Field interface.
func (c *Cache) Eval2(x, y float64) float64 {
ind := c.cacheInd(x, y)
res, ok := c.cache[ind]
if !ok {
res = c.Src.Eval2(x, y)
if len(c.cache) > c.Limit {
c.cache = make(map[string]float64)
}
c.cache[ind] = res
}
return res
}
func (c *Cache) cacheInd(x, y float64) string {
x *= c.oneovrres
if x < 0 {
x -= 1
}
ix := int(x)
y *= c.oneovrres
if y < 0 {
y -= 1
}
iy := int(y)
return fmt.Sprintf("%d.%d", ix, iy)
}