Why are Golang heaps so complicated
dolthub.com
dolthub.com
Especially for Pop() it’s completely gratuitous. It’s fairly common that you want to peek at the min-element prior to popping. For example, a work queue might only want to pop jobs if their scheduling time exceeds the current wall time. In that case you already need to inspect x[0] by hand. And if you do that, you simply don’t use Pop()’s return value.
I would rather see that the ‘any’ arguments/return values are removed entirely, and that the caller is responsible for reading/writing them from/to the array directly. It’s sufficient for container/heap to just implement the heapify algorithms. Its interface could just be identical to sort.Interface.
Even though some people hate it, I think it’s beautiful that sort.Sort() is implemented in such a way that it’s oblivious of the underlying data structure and element type, I wish container/heap adhered to the same principle.
For a heap, something like this seems fine? https://pkg.go.dev/github.com/fufuok/utils/generic/heap#sect...
(Hi, Ed!)
Custom types would require a custom interface.
The main difference is that is that the comparison function is determined from the type of the heap element both for built-in and user-defined types.
const (
Break BreakOrContinue = iota
Continue BreakOrContinue = iota
)
This is not how `iota` works...This is a nice example of the bizarre negativity you tend to get when posting any project on an Internet forum. I don’t expect anyone to be even remotely impressed by my implementation of a completely standard binary heap. But it’s amusing that the one comment that this has attracted is a piece of snark that appears not even to be technically correct.
NameError: name 'heappush' is not defined
Lies, damn lies