-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
70 lines (57 loc) · 1.06 KB
/
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
package dspc
import (
"bytes"
"cmp"
"slices"
"strconv"
)
// buffer extension with some zero allocation helpers
type betterBuffer struct {
bytes.Buffer
}
func (b *betterBuffer) WriteInt64(n int64) {
var data [20]byte
res := strconv.AppendInt(data[:0], n, 10)
b.Write(res)
}
func (b *betterBuffer) WriteInt(n int) {
b.WriteInt64(int64(n))
}
func (b *betterBuffer) WriteByteRepeated(c byte, n int) {
const bufSize = 32
var data [bufSize]byte
for i := range min(bufSize, n) {
data[i] = c
}
for {
if n < bufSize {
b.Write(data[:n])
return
}
b.Write(data[:])
n -= bufSize
}
}
func digitCount(n int64) int {
if n == 0 {
return 1
}
count := 0
if n < 0 {
count = 1 // for the minus sign
}
for n != 0 {
n /= 10
count++
}
return count
}
// takes sorted slice and returns its copy with the new value inserted at the correct position
func cloneSortedSliceAndInsert[T cmp.Ordered](s []T, v T) []T {
res := make([]T, len(s)+1)
pos, _ := slices.BinarySearch(s, v)
copy(res, s[:pos])
res[pos] = v
copy(res[pos+1:], s[pos:])
return res
}