Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heap: clone the variadic slice in Heap.From #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gnojus
Copy link

@gnojus gnojus commented Jan 7, 2024

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

Otherwise this will modify the expanded slice.

One should use heap.FromSlice to specifify the backing slice.
@zyedidia
Copy link
Owner

zyedidia commented Jan 8, 2024

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

@gnojus
Copy link
Author

gnojus commented Jan 9, 2024

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
Copy link
Author

gnojus commented Jan 16, 2024

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants