-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Closed as duplicate of#47632
Labels
LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal
Milestone
Description
Proposal Details
container/heap provides a heap interface allowing people to create there own implementation leading to duplication of efforts.
I am proposing a generic heap implementation that uses a slice as the internal data structure that everyone can use and save the effort them the effort of implementing a new one each time they need to use a heap.
The main APIs are:
type AHeap[T any] struct {
Heap []T
LessFunc func(a, b T) bool
}
// Constructor function
func New[T any](lessFunc func(a, b T)) *AHeap[T]
// Converts a slice to a heap
// Call this function if heap is initialized with a slice that already contains elements or if heap elements where manually modified
// Time complexity is O(nlogn)
func (ah *AHeap[T]) BuildHeap()
// Returns the element at the top of the heap along with true
// Will return zeroValue of T and false if heap is empty
// Time complexity is O(1)
func (ah *AHeap[T]) Top() (T, bool)
// Removes and returns the top element of the heap along with true.
// Returns the zero value of T and false if the heap is empty.
// Time complexity is O(logn)
func (ah *AHeap[T]) Pop() (T, bool)
// Pushes T onto the heap
// Time complexity is O(logn)
func (ah *AHeap[T]) Push(value T) Metadata
Metadata
Assignees
Labels
LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal