-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.go
102 lines (74 loc) · 1.56 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package readline
import "sync"
//acSuggestions map[string][]string
//acDescriptions map[string]map[string]string
// string
type cacheString struct {
mutex sync.Mutex
indexes []string
size int
values map[string]string
}
func (c *cacheString) Init(rl *Instance) {
c.mutex.Lock()
c.indexes = make([]string, rl.MaxCacheSize)
c.size = 0
c.values = make(map[string]string)
c.mutex.Unlock()
}
func (c *cacheString) Append(line []rune, v string) {
sLine := string(line)
c.mutex.Lock()
if c.size == len(c.indexes)-1 {
delete(c.values, c.indexes[0])
c.indexes = append(c.indexes[1:], "")
} else {
c.size++
}
c.indexes[c.size] = sLine
c.values[sLine] = v
c.mutex.Unlock()
}
func (c *cacheString) Get(line []rune) string {
sLine := string(line)
c.mutex.Lock()
v := c.values[sLine]
c.mutex.Unlock()
return v
}
// []rune
type cacheSliceRune struct {
mutex sync.Mutex
indexes []string
size int
values map[string][]rune
}
func (c *cacheSliceRune) Init(rl *Instance) {
c.mutex.Lock()
c.indexes = make([]string, rl.MaxCacheSize)
c.size = 0
c.values = make(map[string][]rune)
c.mutex.Unlock()
}
func (c *cacheSliceRune) Append(line, v []rune) {
sLine := string(line)
c.mutex.Lock()
if c.size == len(c.indexes)-1 {
delete(c.values, c.indexes[0])
c.indexes = append(c.indexes[1:], "")
} else {
c.size++
}
c.indexes[c.size] = sLine
c.values[sLine] = v
c.mutex.Unlock()
}
func (c *cacheSliceRune) Get(line []rune) []rune {
sLine := string(line)
c.mutex.Lock()
v := c.values[sLine]
c.mutex.Unlock()
return v
}
// Slice
// Map