proposal: container/heap: efficient in-order Iter function #73659
Labels
LibraryProposal
Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool
Proposal
Milestone
Proposal Details
The recommended way to iterate over a heap in order is to copy it and pop it until it's empty. This may not be possible for some
heap.Interface
implementations or may be very costly (the data copying) depending on the elements in the heap. I'm proposing the following func be added to thecontainer/heap
module. It operates inO(n log n)
, only allocates half of the size of the heap using only ints, and only relies onLen()
andLess(i,j)
on theheap.Interface
.Recommended usage:
A more concrete implementation using the
myHeap
type inheap_test.go
.Implementation:
First time filing a ticket and I can provide an MR with unit tests, I don't even know if these sort of suggestions are supported. Just straight up giving code. If this having an in-order implementation and other supported types not having it feels odd I can make a more comprehensive MR with whatever data structures make this feel like a more complete contribution. Otherwise I'll just keep this code to myself and anyone else interested can use it as well. I know I couldn't find any examples of code like this online and it saddened me.
The text was updated successfully, but these errors were encountered: