Skip to content

proposal: container: Generic Array Heap Implementation #73345

@chlovec

Description

@chlovec

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

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions