forked from dotabuff/manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_state.go
60 lines (55 loc) · 1007 Bytes
/
field_state.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
package manta
type fieldState struct {
state []interface{}
}
func newFieldState() *fieldState {
return &fieldState{
state: make([]interface{}, 8),
}
}
func (s *fieldState) get(fp *fieldPath) interface{} {
x := s
z := 0
for i := 0; i <= fp.last; i++ {
z = fp.path[i]
if len(x.state) < z+2 {
return nil
}
if i == fp.last {
return x.state[z]
}
if _, ok := x.state[z].(*fieldState); !ok {
return nil
}
x = x.state[z].(*fieldState)
}
return nil
}
func (s *fieldState) set(fp *fieldPath, v interface{}) {
x := s
z := 0
for i := 0; i <= fp.last; i++ {
z = fp.path[i]
if y := len(x.state); y < z+2 {
z := make([]interface{}, max(z+2, y*2))
copy(z, x.state)
x.state = z
}
if i == fp.last {
if _, ok := x.state[z].(*fieldState); !ok {
x.state[z] = v
}
return
}
if _, ok := x.state[z].(*fieldState); !ok {
x.state[z] = newFieldState()
}
x = x.state[z].(*fieldState)
}
}
func max(a, b int) int {
if a > b {
return a
}
return b
}