forked from ortuman/nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.go
More file actions
39 lines (34 loc) · 737 Bytes
/
slice.go
File metadata and controls
39 lines (34 loc) · 737 Bytes
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
// SPDX-License-Identifier: Apache-2.0
package nuke
const growThreshold = 256
// SliceAppend appends elements to a slice of type T using a provided Arena
// for memory allocation if needed.
func SliceAppend[T any](a Arena, s []T, data ...T) []T {
if a == nil {
return append(s, data...)
}
s = growSlice(a, s, len(data))
s = append(s, data...)
return s
}
func growSlice[T any](a Arena, s []T, dataLen int) []T {
newLen := len(s) + dataLen
newCap := cap(s)
if newCap > 0 {
for newLen > newCap {
if newCap < growThreshold {
newCap *= 2
} else {
newCap += newCap / 4
}
}
} else {
newCap = dataLen
}
if newCap == cap(s) {
return s
}
s2 := MakeSlice[T](a, len(s), newCap)
copy(s2, s)
return s2
}