-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacked_map.go
131 lines (125 loc) · 2.36 KB
/
stacked_map.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package dscope
type _StackedMap struct {
Next *_StackedMap
Values []_Value
Height int8
}
// Load loads values with specified id
// MUST NOT modify returned slice
func (s *_StackedMap) Load(id _TypeID) ([]_Value, bool) {
var left, right, idx, l uint
var start, end int
var id2 _TypeID
for s != nil {
// do binary search
left = 0
l = uint(len(s.Values))
right = l
// find left bound
for left < right {
idx = (left + right) >> 1
id2 = s.Values[idx].typeInfo.TypeID
// need to find the first value
if id2 >= id {
right = idx
} else {
left = idx + 1
}
}
start = -1
for ; idx < l; idx++ {
id2 = s.Values[idx].typeInfo.TypeID
if id2 == id {
if start < 0 {
// found start
start = int(idx)
end = start + 1
} else {
// expand
end++
}
} else if id2 > id {
// exceed right bound
break
}
// id2 < id, skip
}
if start != -1 {
// found
return s.Values[start:end], true
}
s = s.Next
}
return nil, false
}
func (s *_StackedMap) LoadOne(id _TypeID) (ret _Value, ok bool) {
var left, right, idx uint
var id2 _TypeID
for s != nil {
left = 0
right = uint(len(s.Values))
for left < right {
idx = (left + right) >> 1
id2 = s.Values[idx].typeInfo.TypeID
if id2 > id {
right = idx
} else if id2 < id {
left = idx + 1
} else {
return s.Values[idx], true
}
}
s = s.Next
}
return
}
// Range iterates all values
// MUST NOT modify []_Value argument in callback function
func (s *_StackedMap) Range(fn func([]_Value) error) error {
keys := make(map[_TypeID]struct{})
var start, end int
for s != nil {
for j, d := range s.Values {
if _, ok := keys[d.typeInfo.TypeID]; ok {
continue
}
keys[d.typeInfo.TypeID] = struct{}{}
start = j
end = start + 1
for _, follow := range s.Values[j+1:] {
if follow.typeInfo.TypeID == d.typeInfo.TypeID {
end++
} else {
break
}
}
if err := fn(s.Values[start:end]); err != nil {
return err
}
}
s = s.Next
}
return nil
}
func (s *_StackedMap) Append(values []_Value) *_StackedMap {
if s != nil {
return &_StackedMap{
Values: values,
Next: s,
Height: s.Height + 1,
}
}
return &_StackedMap{
Values: values,
Next: s,
Height: 1,
}
}
func (s *_StackedMap) Len() int {
ret := 0
for s != nil {
ret += len(s.Values)
s = s.Next
}
return ret
}