-
Notifications
You must be signed in to change notification settings - Fork 0
/
splay_test.go
266 lines (231 loc) · 6.41 KB
/
splay_test.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package splay_test
import (
gojson "encoding/json"
"fmt"
"io"
"math"
"os"
splay "splay-test/splayTree"
"testing"
)
type editTrace struct {
Edits [][]interface{} `json:"edits"`
FinalText string `json:"finalText"`
}
// func TestSplayTree(t *testing.T) {
// readEditingTraceFromFile reads trace from editing-trace.json.
func readEditingTraceFromFile(b *testing.B) (*editTrace, error) {
var trace editTrace
file, err := os.Open("./editing-trace.json")
if err != nil {
return nil, err
}
defer func() {
if err = file.Close(); err != nil {
b.Fatal(err)
}
}()
byteValue, err := io.ReadAll(file)
if err != nil {
return nil, err
}
if err = gojson.Unmarshal(byteValue, &trace); err != nil {
return nil, err
}
return &trace, err
}
type stringValue struct {
content string
removed bool
}
func newSplayNode(content string) *splay.Node[*stringValue] {
return splay.NewNode(&stringValue{
content: content,
})
}
func (v *stringValue) Len() int {
if v.removed {
return 0
}
return len(v.content)
}
func (v *stringValue) String() string {
return v.content
}
type splayTrees interface {
Insert(*splay.Node[*stringValue]) *splay.Node[*stringValue]
Find(int) (*splay.Node[*stringValue], int, error)
Delete(*splay.Node[*stringValue])
Height() int
RotateCount() int
}
func Execute(tree splayTrees, editingTrace *editTrace) (int, int, int) {
maxHeight := 0
maxRotate := 0
prevRotate := 0
for _, edit := range editingTrace.Edits {
cursor := int(edit[0].(float64))
mode := int(edit[1].(float64))
if mode == 0 {
strValue, ok := edit[2].(string)
if ok {
tree.Insert(newSplayNode(strValue))
}
} else {
node, _, err := tree.Find(cursor)
if err != nil {
tree.Delete(node)
}
}
if tree.RotateCount()-prevRotate > maxRotate {
maxRotate = tree.RotateCount() - prevRotate
}
if maxHeight < tree.Height() {
maxHeight = tree.Height()
}
prevRotate = tree.RotateCount()
}
sumRotate := tree.RotateCount()
res := fmt.Sprintf("\nresult(%d,%d,%d)", maxHeight, maxRotate, sumRotate)
fmt.Println(res)
return maxHeight, maxRotate, tree.RotateCount()
}
func BenchmarkBasicSplayTree(b *testing.B) {
b.StopTimer()
editingTrace, err := readEditingTraceFromFile(b)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
tree := splay.NewBasicSplayTree[*stringValue](nil)
Execute(tree, editingTrace)
b.StopTimer()
}
func BenchmarkRandom(b *testing.B) {
b.StopTimer()
editingTrace, err := readEditingTraceFromFile(b)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
functions := []func(i int) int{
func(i int) int { return int(math.Sqrt(float64(i))) },
func(i int) int { return int(math.Log2(float64(i))) },
func(i int) int {
x := int(math.Log2(float64(i)))
return x * x
},
func(i int) int {
k := 3
return k * i / (int(math.Log2(float64(i))) + 1)
},
}
functionNames := []string{"sqrt", "log", "log^2", "k(n/ln{n})"}
splayCounts := []int{1, 2, 3}
ratios := []int{10, 20, 30, 40, 50}
bounds := []int{10000, 50000, 100000}
b.Run("random splay by counting", func(b *testing.B) {
for fc := 0; fc < 4; fc++ {
for spCnt := 0; spCnt < 3; spCnt++ {
b.Run(fmt.Sprintf("function: %s, splayCount: %d", functionNames[fc], splayCounts[spCnt]), func(b *testing.B) {
tree := splay.NewRandomByCountSplayTree[*stringValue](nil, functions[fc], splayCounts[spCnt])
Execute(tree, editingTrace)
})
}
}
})
b.Run("random splay by k-height", func(b *testing.B) {
for r := 0; r < 5; r++ {
for spCnt := 0; spCnt < 3; spCnt++ {
b.Run(fmt.Sprintf("ratio: %d, splayCount: %d", ratios[r], splayCounts[spCnt]), func(b *testing.B) {
tree := splay.NewRandomKSplayTree[*stringValue](nil, ratios[r], splayCounts[spCnt])
Execute(tree, editingTrace)
})
}
}
})
b.Run("random splay by bounded height", func(b *testing.B) {
for bd := 0; bd < 3; bd++ {
for spCnt := 0; spCnt < 3; spCnt++ {
b.Run(fmt.Sprintf("bound: %d, splayCount: %d", bounds[bd], splayCounts[spCnt]), func(b *testing.B) {
tree := splay.NewRandomBoundSplayTree[*stringValue](nil, bounds[bd], splayCounts[spCnt])
Execute(tree, editingTrace)
})
}
}
})
b.StopTimer()
}
func BenchmarkMaxHeightSplay(b *testing.B) {
b.StopTimer()
editingTrace, err := readEditingTraceFromFile(b)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
functions := []func(i int) int{
func(i int) int { return int(math.Sqrt(float64(i))) },
func(i int) int { return int(math.Log2(float64(i))) },
func(i int) int {
x := int(math.Log2(float64(i)))
return x * x
},
func(i int) int {
k := 3
return k * i / (int(math.Log2(float64(i))) + 1)
},
}
functionNames := []string{"sqrt", "log", "log^2", "k(n/ln{n})"}
splayCounts := []int{1, 2, 3}
ratios := []int{10, 20, 30, 40, 50}
bounds := []int{10000, 50000, 100000}
b.Run("max height splay by counting", func(b *testing.B) {
for fc := 0; fc < 4; fc++ {
for spCnt := 0; spCnt < 3; spCnt++ {
b.Run(fmt.Sprintf("function: %s, splayCount: %d", functionNames[fc], splayCounts[spCnt]), func(b *testing.B) {
tree := splay.NewMaxHeightByCountSplayTree[*stringValue](nil, functions[fc], splayCounts[spCnt])
Execute(tree, editingTrace)
})
}
}
})
b.Run("max height splay by k-height", func(b *testing.B) {
for r := 0; r < 5; r++ {
for spCnt := 0; spCnt < 3; spCnt++ {
b.Run(fmt.Sprintf("ratio: %d, splayCount: %d", ratios[r], splayCounts[spCnt]), func(b *testing.B) {
tree := splay.NewMaxHeightKSplayTree[*stringValue](nil, ratios[r], splayCounts[spCnt])
Execute(tree, editingTrace)
})
}
}
})
b.Run("max height splay by bounded height", func(b *testing.B) {
for bd := 0; bd < 3; bd++ {
for spCnt := 0; spCnt < 3; spCnt++ {
b.Run(fmt.Sprintf("bound: %d, splayCount: %d", bounds[bd], splayCounts[spCnt]), func(b *testing.B) {
tree := splay.NewMaxHeightBoundSplayTree[*stringValue](nil, bounds[bd], splayCounts[spCnt])
Execute(tree, editingTrace)
})
}
}
})
b.StopTimer()
}
func BenchmarkSTLB(b *testing.B) {
b.StopTimer()
editingTrace, err := readEditingTraceFromFile(b)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
thresholds := []int{1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000}
b.Run("max height splay by counting", func(b *testing.B) {
for i := 0; i < 10; i++ {
b.Run(fmt.Sprintf("ration: %d", thresholds[i]), func(b *testing.B) {
tree := splay.NewSTLB[*stringValue](nil, thresholds[i])
Execute(tree, editingTrace)
})
}
})
b.StopTimer()
}