-
Notifications
You must be signed in to change notification settings - Fork 111
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
Generic Heap #48
Merged
Merged
Generic Heap #48
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
66f639f
start heap package
patrick-ogrady 1681d51
add tests
patrick-ogrady e8fc062
remove external heap usage
patrick-ogrady 60b0e65
remove extra heap
patrick-ogrady abe4b6e
fix tests
patrick-ogrady a877c09
typo
patrick-ogrady 90cc766
re-add Push/Pop/Remove
patrick-ogrady e949a8c
fix nit
patrick-ogrady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package heap | ||
|
||
import ( | ||
"container/heap" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
// Heap[I,V] is used to track objects of [I] by [Val]. | ||
// | ||
// This data structure does not perform any synchronization and is not | ||
// safe to use concurrently without external locking. | ||
type Heap[I any, V constraints.Ordered] struct { | ||
ih *innerHeap[I, V] | ||
} | ||
|
||
// New returns an instance of Heap[I,V] | ||
func New[I any, V constraints.Ordered](items int, isMinHeap bool) *Heap[I, V] { | ||
return &Heap[I, V]{newInnerHeap[I, V](items, isMinHeap)} | ||
} | ||
|
||
// Len returns the number of items in ih. | ||
func (h *Heap[I, V]) Len() int { return h.ih.Len() } | ||
|
||
// Get returns the entry in th associated with [id], and a bool if [id] was | ||
// found in th. | ||
func (h *Heap[I, V]) Get(id ids.ID) (*Entry[I, V], bool) { | ||
return h.ih.Get(id) | ||
} | ||
|
||
// Has returns whether [id] is found in th. | ||
func (h *Heap[I, V]) Has(id ids.ID) bool { | ||
return h.ih.Has(id) | ||
} | ||
|
||
// Items returns all items in the heap in sorted order. You should not modify | ||
// the response. | ||
func (h *Heap[I, V]) Items() []*Entry[I, V] { | ||
return h.ih.items | ||
} | ||
|
||
// Push can be called by external users instead of using `containers.heap`, | ||
// which makes using this heap less error-prone. | ||
func (h *Heap[I, V]) Push(e *Entry[I, V]) { | ||
heap.Push(h.ih, e) | ||
} | ||
|
||
// Pop can be called by external users to remove an object from the heap at | ||
// a specific index instead of using `containers.heap`, | ||
// which makes using this heap less error-prone. | ||
func (h *Heap[I, V]) Pop() *Entry[I, V] { | ||
if len(h.ih.items) == 0 { | ||
return nil | ||
} | ||
return heap.Pop(h.ih).(*Entry[I, V]) | ||
} | ||
|
||
// Remove can be called by external users to remove an object from the heap at | ||
// a specific index instead of using `containers.heap`, | ||
// which makes using this heap less error-prone. | ||
func (h *Heap[I, V]) Remove(index int) *Entry[I, V] { | ||
if index >= len(h.ih.items) { | ||
return nil | ||
} | ||
return heap.Remove(h.ih, index).(*Entry[I, V]) | ||
} | ||
|
||
// First returns the first item in the heap. This is the smallest item in | ||
// a minHeap and the largest item in a maxHeap. | ||
// | ||
// If no items are in the heap, it will return nil. | ||
func (h *Heap[I, V]) First() *Entry[I, V] { | ||
if len(h.ih.items) == 0 { | ||
return nil | ||
} | ||
return h.ih.items[0] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We directly call into
ih
to avoid the overhead of another function call here