pqueue

package module
v0.0.0-...-33ecc3e Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2015 License: MIT Imports: 3 Imported by: 0

README

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

This package provides a priority queue implementation and scaffold
interfaces.

Additional to original package, you can enqueue only items which haven't
been already enqueued

Installation
------------
	$ go get github.com/mileusna/gopqueue

Usage
-----
Here's trivial example of the fast queue usage:
Hope to post more examples soon

	package main

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

	// interface functions for queue items
	func (t *Task) Less(other interface{}) bool {
	    return t.priority < other.(*Task).priority
	}

	// this should return unique id from struct
	// to check weather item has already been into queue
	func (t *Task) Id() interface{} {
		return t.Name
	}
	
	func main() {
	    q := pqueue.New(0)
	    q.EnqueueUnique(&Task{"one", 10})
	    q.EnqueueUnique(&Task{"two", 2})
	    q.EnqueueUnique(&Task{"three", 5})
	    q.EnqueueUnique(&Task{"four", 7})
	    q.EnqueueUnique(&Task{"two", 5})
	    q.EnqueueUnique(&Task{"two", 6})
	    q.EnqueueUnique(&Task{"two", 7})
	    q.EnqueueUnique(&Task{"two", 8})

	    println(q.Len())	// there will be only 4 items in queue

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

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

For more information and examples check the package documentation.
	
Copyright
---------
Copyright (C) 2015 by Milos Mileusnic <milos@groowe.com>
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.

Addition to original package, this package adds method and other internals for inserting only unique items in queue

Copyright (C) 2015 by Milos Mileusnic <milos@groowe.com>

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 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) ClearHistory

func (q *Queue) ClearHistory()

Clear queue history so the elements can be EnqueueUnique again

func (*Queue) Dequeue

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

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 QueueItem) (err error)

Enqueue puts given item to the queue. Lock the queue and calls enqueue()

func (*Queue) EnqueueUnique

func (q *Queue) EnqueueUnique(item QueueItem) (added bool, err error)

Enqueue puts item in queue only if it hasn't already been in queue

func (*Queue) IdExists

func (q *Queue) IdExists(id interface{}) bool

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

IsEmpty returns true if queue is empty.

func (*Queue) ItemExists

func (q *Queue) ItemExists(item QueueItem) bool

check if item already exists in queue (or it has been into queue)

func (*Queue) Len

func (q *Queue) Len() int

Len returns number of enqueued elemnents.

func (*Queue) RemoveFromHistory

func (q *Queue) RemoveFromHistory(element interface{})

type QueueItem

type QueueItem interface {
	Less(other interface{}) bool
	Id() interface{}
}

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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