-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmap.go
74 lines (62 loc) · 1.04 KB
/
pmap.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
package hio
type pitem struct {
k string
v int64
}
// pmap is a map of pos/offsets, keeping the order of insertion
type pmap struct {
slice []pitem
}
func newpmap() pmap {
return pmap{
slice: make([]pitem, 0),
}
}
func (p *pmap) Len() int {
return len(p.slice)
}
func (p *pmap) getidx(k string) int {
for i, item := range p.slice {
if item.k == k {
return i
}
}
return -1
}
func (p *pmap) get(k string) int64 {
i := p.getidx(k)
if i < 0 {
panic("hio: no such key [" + k + "]")
}
return p.slice[i].v
}
func (p *pmap) set(k string, v int64) {
i := p.getidx(k)
if i < 0 {
p.slice = append(p.slice,
pitem{
k: k,
v: v,
},
)
} else {
p.slice[i].v = v
}
}
func (p *pmap) has(k string) bool {
return p.getidx(k) >= 0
}
func (p *pmap) del(k string) {
i := p.getidx(k)
if i < 0 {
panic("hio: no such key [" + k + "]")
}
p.slice = append(p.slice[:i], p.slice[i+1:]...)
}
func (p *pmap) keys() []string {
keys := make([]string, 0, p.Len())
for _, v := range p.slice {
keys = append(keys, v.k)
}
return keys
}