queue

package
v0.0.0-...-41349f7 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2015 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

The priority queue is almost a spitting image of the logic used for a regular queue. In order to keep the logic fast, this code is repeated instead of using casts to cast to interface{} back and forth. If Go had inheritance and generics, this problem would be easier to solve.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteInParallel

func ExecuteInParallel(q *Queue, fn func(interface{}))

ExecuteInParallel will (in parallel) call the provided function with each item in the queue until the queue is exhausted. When the queue is exhausted execution is complete and all goroutines will be killed. This means that the queue will be disposed so cannot be used again.

Types

type DisposedError

type DisposedError struct{}

func (DisposedError) Error

func (de DisposedError) Error() string

type Item

type Item interface {
	// Compare returns a bool that can be used to determine
	// ordering in the priority queue.  Assuming the queue
	// is in ascending order, this should return > logic.
	// Return 1 to indicate this object is greater than the
	// the other logic, 0 to indicate equality, and -1 to indicate
	// less than other.
	Compare(other Item) int
}

Item is an item that can be added to the priority queue.

type PriorityQueue

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

PriorityQueue is similar to queue except that it takes items that implement the Item interface and adds them to the queue in priority order.

func NewPriorityQueue

func NewPriorityQueue(hint int) *PriorityQueue

NewPriorityQueue is the constructor for a priority queue.

func (*PriorityQueue) Dispose

func (pq *PriorityQueue) Dispose()

Dispose will prevent any further reads/writes to this queue and frees available resources.

func (*PriorityQueue) Disposed

func (pq *PriorityQueue) Disposed() bool

Disposed returns a bool indicating if this queue has been disposed.

func (*PriorityQueue) Empty

func (pq *PriorityQueue) Empty() bool

Empty returns a bool indicating if there are any items left in the queue.

func (*PriorityQueue) Get

func (pq *PriorityQueue) Get(number int) ([]Item, error)

Get retrieves items from the queue. If the queue is empty, this call blocks until the next item is added to the queue. This will attempt to retrieve number of items.

func (*PriorityQueue) Len

func (pq *PriorityQueue) Len() int

Len returns a number indicating how many items are in the queue.

func (*PriorityQueue) Peek

func (pq *PriorityQueue) Peek() Item

Peek will look at the next item without removing it from the queue.

func (*PriorityQueue) Put

func (pq *PriorityQueue) Put(items ...Item) error

Put adds items to the queue.

type Queue

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

Queue is the struct responsible for tracking the state of the queue.

func New

func New(hint int64) *Queue

New is a constructor for a new threadsafe queue.

func (*Queue) Dispose

func (q *Queue) Dispose()

Dispose will dispose of this queue. Any subsequent calls to Get or Put will return an error.

func (*Queue) Disposed

func (q *Queue) Disposed() bool

Disposed returns a bool indicating if this queue has had disposed called on it.

func (*Queue) Empty

func (q *Queue) Empty() bool

Empty returns a bool indicating if this bool is empty.

func (*Queue) Get

func (q *Queue) Get(number int64) ([]interface{}, error)

Get will add an item to the queue. If there are some items in the queue, get will return a number UP TO the number passed in as a parameter. If no items are in the queue, this method will pause until items are added to the queue.

func (*Queue) Len

func (q *Queue) Len() int64

Len returns the number of items in this queue.

func (*Queue) Put

func (q *Queue) Put(items ...interface{}) error

Put will add the specified items to the queue.

func (*Queue) TakeUntil

func (q *Queue) TakeUntil(checker func(item interface{}) bool) ([]interface{}, error)

TakeUntil takes a function and returns a list of items that match the checker until the checker returns false. This does not wait if there are no items in the queue.

Jump to

Keyboard shortcuts

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