Skip to content

invisiblefunnel/flatqueue-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flatqueue-go Tests

A Go 1.18+ port of the flatqueue priority queue library using generics.

Peek, PeekValue, and Pop will panic if called on an empty queue. You must check Len accordingly.

package main

import "github.com/invisiblefunnel/flatqueue-go/v2"

type Item struct {
    Name  string
    Value int
}

func main() {
    items := []Item{
        {"X", 5},
        {"Y", 2},
        {"Z", 3},
    }

    var q flatqueue.FlatQueue[Item, int]

    for _, item := range items {
        q.Push(item, item.Value)
    }

    var (
        item  Item
        value int
    )

    item = q.Peek()       // top item
    value = q.PeekValue() // top item value
    item = q.Pop()        // remove and return the top item

    // loop while the queue is not empty
    for q.Len() > 0 {
        q.Pop()
    }
}