generic
generic copied to clipboard
heap: clone the variadic slice in Heap.From
One should use heap.FromSlice to specifify the backing slice.
Otherwise this will modify the expanded slice.
Do you have a test case or example that demonstrates the problematic behavior?
https://go.dev/play/p/VssEFzSi2Qg
To be fair, another solution would be to just change the doc comment to mention that the variadic slice will be modified.
Also posting the snippet here as playground is having some problems with shared snippets lately:
package main
import (
"fmt"
"github.com/zyedidia/generic/heap"
)
func main() {
arr := []int{1, 2, 3, 4}
h := heap.From(func(a, b int) bool { return a < b }, arr...)
fmt.Println(arr)
h.Pop()
fmt.Println(arr) // the slice has changed
}