generic icon indicating copy to clipboard operation
generic copied to clipboard

heap: clone the variadic slice in Heap.From

Open gnojus opened this issue 1 year ago • 3 comments

One should use heap.FromSlice to specifify the backing slice.

Otherwise this will modify the expanded slice.

gnojus avatar Jan 07 '24 14:01 gnojus

Do you have a test case or example that demonstrates the problematic behavior?

zyedidia avatar Jan 08 '24 05:01 zyedidia

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.

gnojus avatar Jan 09 '24 11:01 gnojus

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
}

gnojus avatar Jan 16 '24 17:01 gnojus