priorityqueue

package module
v1.0.2-0...-5c6611c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2020 License: Unlicense Imports: 1 Imported by: 0

README

priority-queue

priority queue implementation in golang

PkgGoDev Build Status Coverage Status

Description

A generic priority queue implementation over a max heap.

Usage

package main

import (
    "fmt"
    "github.com/dannoane/priority-queue"
)

func main() {
    pq := priorityqueue.NewPriorityQueue()
    pq.Push(&priorityqueue.Element{Value: 1, Priority: 200})
    pq.Push(&priorityqueue.Element{Value: 2, Priority: 100})
    pq.Push(&priorityqueue.Element{Value: 3, Priority: 300})

    for !pq.IsEmpty() {
        fmt.Println(pq.Pop().Value)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {
	Value    interface{}
	Priority int
	// contains filtered or unexported fields
}

Element - struct used to represent an item in the prioriry queue

type PriorityQueue

type PriorityQueue struct {
	// contains filtered or unexported fields
}

PriorityQueue - wrapper over the heap implementation

func NewPriorityQueue

func NewPriorityQueue() PriorityQueue

NewPriorityQueue - creates a new priority queue and initializes the heap

func (*PriorityQueue) At

func (queue *PriorityQueue) At(index int) *Element

At - get the element at the specified index from the heap

func (PriorityQueue) IsEmpty

func (queue PriorityQueue) IsEmpty() bool

IsEmpty - returns true if the heap has no elements, false otherwise

func (PriorityQueue) Len

func (queue PriorityQueue) Len() int

Len - return the number of elements in the heap

func (*PriorityQueue) Peek

func (queue *PriorityQueue) Peek() *Element

Peek - returns the element with the highest priority from the heap

func (*PriorityQueue) Pop

func (queue *PriorityQueue) Pop() *Element

Pop - removes and returns the element with the highest priority from the heap

func (*PriorityQueue) PopLowest

func (queue *PriorityQueue) PopLowest() *Element

PopLowest - removes and returns the element with the lowest priority from the heap

func (*PriorityQueue) Push

func (queue *PriorityQueue) Push(element *Element)

Push - add a new element in the right position in the heap according to its priority

func (*PriorityQueue) Remove

func (queue *PriorityQueue) Remove(index int) *Element

Remove - removes and returns the element at position index from the heap

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL