pqueue

package module
v0.0.0-...-7e7bc6b Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2013 License: MIT Imports: 3 Imported by: 2

README

Priority Queue in Go
====================

This package provides a priority queue implementation and scaffold
interfaces.

Installation
------------
Use the ``goinstall`` tool::

	$ goinstall github.com/nu7hatch/gopqueue

... or install it manually::

	$ git clone git://github.com/nu7hatch/gopqueue.git
	$ cd gopqueue
	$ make install

Usage
-----
Here's trivial example of the fast queue usage::

	package main

	import pqueue "github.com/nu7hatch/gopqueue"
	
	type Task struct {
	    Name     string
	    priority int
	}

	func (t *Task) Less(other interface{}) bool {
	    return t.priority < other.(*Task).priority
	}
	
	func main() {
	    q := pqueue.New(0)
	    q.Enqueue(&Task{"one", 10})
	    q.Enqueue(&Task{"two", 2})
	    q.Enqueue(&Task{"three", 5})
	    q.Enqueue(&Task{"four", 7})

	    for i := 0; i < 4; i += 1 {
	        task := q.Dequeue()
	        println(task.(*Task).Name)
	    }
	}

	// Produces:
	//
	//     two
	//     three
	//     four
	//     one

For more information and examples check the package documentation.
	
Copyright
---------
Copyright (C) 2011 by Krzysztof Kowalik <chris@nu7hat.ch>

See COPYING file for details.

Documentation

Overview

This package provides a priority queue implementation and scaffold interfaces.

Copyright (C) 2011 by Krzysztof Kowalik <chris@nu7hat.ch>

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	Less(other interface{}) bool
}

Only items implementing this interface can be enqueued on the priority queue.

type Queue

type Queue struct {
	Limit int
	// contains filtered or unexported fields
}

Queue is a threadsafe priority queue exchange. Here's a trivial example of usage:

q := pqueue.New(0)
go func() {
    for {
        task := q.Dequeue()
        println(task.(*CustomTask).Name)
    }
}()
for i := 0; i < 100; i := 1 {
    task := CustomTask{Name: "foo", priority: rand.Intn(10)}
    q.Enqueue(&task)
}

func New

func New(max int) (q *Queue)

New creates and initializes a new priority queue, taking a limit as a parameter. If 0 given, then queue will be unlimited.

func (*Queue) ChangeLimit

func (q *Queue) ChangeLimit(newLimit int)

Safely changes enqueued items limit. When limit is set to 0, then queue is unlimited.

func (*Queue) Dequeue

func (q *Queue) Dequeue() (item Interface)

Dequeue takes an item from the queue. If queue is empty then should block waiting for at least one item.

func (*Queue) Enqueue

func (q *Queue) Enqueue(item Interface) (err error)

Enqueue puts given item to the queue.

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

IsEmpty returns true if queue is empty.

func (*Queue) Len

func (q *Queue) Len() int

Len returns number of enqueued elemnents.

func (*Queue) Peek

func (q *Queue) Peek() (item Interface)

Peek returns the minimum item from the queue. If the queue is empty it returns nil.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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