Skip to content

proposal: container: Generic Array Heap Implementation #73345

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

Closed
chlovec opened this issue Apr 12, 2025 · 4 comments
Closed

proposal: container: Generic Array Heap Implementation #73345

chlovec opened this issue Apr 12, 2025 · 4 comments
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Milestone

Comments

@chlovec
Copy link

chlovec commented Apr 12, 2025

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) 
@gopherbot gopherbot added this to the Proposal milestone Apr 12, 2025
@thepudds
Copy link
Contributor

Does this overlap with #47632?

@chlovec
Copy link
Author

chlovec commented Apr 12, 2025

Does this overlap with #47632?

There may be some overlap but my proposal seems to be cleaner and clearer.

@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Apr 12, 2025
@seankhliao
Copy link
Member

please discuss this in #47632, there is a lot of overlap with what it calls the simpler API that doesn't cover sufficient existing use cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
None yet
Development

No branches or pull requests

5 participants