package graphimport ()// priorityQueue implements a minimum priority queue using a minimum binary heap// that prioritizes smaller values over larger values.type priorityQueue[ comparable] struct { items *minHeap[] cache map[]*priorityItem[]}// priorityItem is an item on the binary heap consisting of a priority value and// an actual payload value.type priorityItem[ comparable] struct { value priority float64 index int}func newPriorityQueue[ comparable]() *priorityQueue[] {return &priorityQueue[]{items: &minHeap[]{},cache: map[]*priorityItem[]{}, }}// Len returns the total number of items in the priority queue.func ( *priorityQueue[]) () int {return .items.Len()}// Push pushes a new item with the given priority into the queue. This operation// may cause a re-balance of the heap and thus scales with O(log n).func ( *priorityQueue[]) ( , float64) {if , := .cache[]; {return } := &priorityItem[]{value: ,priority: ,index: 0, }heap.Push(.items, ) .cache[] = }// Pop returns and removes the item with the lowest priority. This operation may// cause a re-balance of the heap and thus scales with O(log n).func ( *priorityQueue[]) () (, error) {iflen(*.items) == 0 {varreturn , errors.New("priority queue is empty") } := heap.Pop(.items).(*priorityItem[])delete(.cache, .value)return .value, nil}// UpdatePriority updates the priority of a given item and sets it to the given// priority. If the item doesn't exist, nothing happens. This operation may// cause a re-balance of the heap and this scales with O(log n).func ( *priorityQueue[]) ( , float64) { , := .cache[]if ! {return } .priority = heap.Fix(.items, .index)}// minHeap is a minimum binary heap that implements heap.Interface.type minHeap[ comparable] []*priorityItem[]func ( *minHeap[]) () int {returnlen(*)}func ( *minHeap[]) (, int) bool {return (*)[].priority < (*)[].priority}func ( *minHeap[]) (, int) { (*)[], (*)[] = (*)[], (*)[] (*)[].index = (*)[].index = }func ( *minHeap[]) ( interface{}) { := .(*priorityItem[]) .index = len(*) * = append(*, )}func ( *minHeap[]) () interface{} { := * := [len()-1] * = [:len()-1]return}type stack[ any] interface { push() pop() (, error) top() (, error) isEmpty() bool// forEach iterate the stack from bottom to top forEach(func())}func newStack[ any]() stack[] {return &stackImpl[]{elements: make([], 0), }}type stackImpl[ any] struct { elements []}func ( *stackImpl[]) ( ) { .elements = append(.elements, )}func ( *stackImpl[]) () (, error) { , := .top()if != nil {varreturn , } .elements = .elements[:len(.elements)-1]return , nil}func ( *stackImpl[]) () (, error) {if .isEmpty() {varreturn , errors.New("no element in stack") }return .elements[len(.elements)-1], nil}func ( *stackImpl[]) () bool {returnlen(.elements) == 0}func ( *stackImpl[]) ( func()) {for , := range .elements { () }}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.