-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
71 lines (60 loc) · 807 Bytes
/
util.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
package k2tree
import (
"fmt"
)
func max(i, j int) int {
if i > j {
return i
}
return j
}
func min(i, j int) int {
if i < j {
return i
}
return j
}
func abs(i int) int {
if i < 0 {
return -i
}
return i
}
func intPow(a, b int) int {
var result = 1
for 0 != b {
if 0 != (b & 1) {
result *= a
}
b >>= 1
a *= a
}
return result
}
func assert(test bool, errstr string) {
if !test {
panic(errstr)
}
}
type twosHistogram struct {
buckets [65]int
}
func (th *twosHistogram) Add(n int) {
if n == 0 || n == 1 {
th.buckets[0] += 1
return
}
count := 0
for n > 0 {
n = n >> 1
count += 1
}
th.buckets[count] += 1
}
func (th twosHistogram) String() string {
out := "\n"
for i, x := range th.buckets {
out += fmt.Sprintf("%d: %d\n", 1<<i, x)
}
return out
}