priorityqueue

package module
v0.0.0-...-fe5fd33 Latest Latest
Warning

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

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

README

priority-queue

priority queue implementation in golang

Build 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 i := 0; i < pq.Len(); i++ {
        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 Heap

type Heap []*Element

Heap - array of Element pointers that will be transformed in a heap

func (Heap) Len

func (heap Heap) Len() int

func (Heap) Less

func (heap Heap) Less(i, j int) bool

func (*Heap) Pop

func (heap *Heap) Pop() interface{}

func (*Heap) Push

func (heap *Heap) Push(x interface{})

func (Heap) Swap

func (heap Heap) Swap(i, j int)

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

func (PriorityQueue) Len

func (queue PriorityQueue) Len() int

func (*PriorityQueue) Pop

func (queue *PriorityQueue) Pop() *Element

Pop - removes and returns the element with the highest 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