queue

package
v0.0.0-...-44b753e Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2016 License: MIT Imports: 4 Imported by: 6

Documentation

Overview

Package queue provides various thread-safe queue implementations: an unbounded queue, a bounded queue implemented as a circular queue, and a heap based priority queue.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Circular

type Circular struct {
	Queue
	Tail int
}

Circular is a bounded queue implemented as a circular queue. Even though Items, Head, and Tail are exported, in most cases, they should not be directly. Doing so may lead to outcomes less than desirable. Use the exported methods to interact with the Circular queue.

func NewCircular

func NewCircular(size int) *Circular

NewCircular returns an initialized circular queue. Even though creating the slice with an initial length is much slower than creating one without the initial length, cap only, this is done to simplify the actual queue management. Don't need to worry about appending vs adding via index and don't need to check to see if an append will cause the slice to grow.

The slice is 1 slot larger than the requested size for empty/full detection.

func (*Circular) Cap

func (c *Circular) Cap() int

Cap returns the current queue capacity:

queue cap = cap(queue) - 1

func (*Circular) Dequeue

func (c *Circular) Dequeue() (interface{}, bool)

Dequeue will remove an item from the queue and return it. If the queue is empty, a false will be returned.

func (*Circular) Enqueue

func (c *Circular) Enqueue(item interface{}) error

Enqueue will return an error if the queue is full

func (*Circular) IsEmpty

func (c *Circular) IsEmpty() bool

IsEmpty returns whether or not the queue is empty

func (*Circular) IsFull

func (c *Circular) IsFull() bool

IsFull returns whether or not the queue is full

func (*Circular) Len

func (c *Circular) Len() int

Len returns the current length of the queue (# of items in queue)

func (*Circular) Peek

func (c *Circular) Peek() (interface{}, bool)

Peek will return the next item in the queue without removing it from the queue. If the queue is empty, a false will be returned.

func (*Circular) Reset

func (c *Circular) Reset()

Reset resets a queue, zeroing out the remaining slots.

func (*Circular) Resize

func (c *Circular) Resize(size int) int

Resize resizes a queue; zeroing out the slots.

type HeapPriority

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

A HeapPriority implements heap.Interface and holds Items.

func NewHeapPriority

func NewHeapPriority(l int) *HeapPriority

NewHeapPriority returns a new priority queue with the item's cap set at l; if l > 0.

func (HeapPriority) Len

func (pq HeapPriority) Len() int

func (HeapPriority) Less

func (pq HeapPriority) Less(i, j int) bool

func (*HeapPriority) Pop

func (pq *HeapPriority) Pop() interface{}

Pop pops the next item from the priority queue.

func (*HeapPriority) Push

func (pq *HeapPriority) Push(x interface{})

Push pushes an item onto the priority queue.

func (HeapPriority) Swap

func (pq HeapPriority) Swap(i, j int)

type Item

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

An Item is something we manage in a priority queue.

type PQueue

type PQueue []*Item

PQueue represents a priority queue

func (PQueue) Len

func (pq PQueue) Len() int

func (PQueue) Less

func (pq PQueue) Less(i, j int) bool

func (*PQueue) Pop

func (pq *PQueue) Pop() interface{}

Pop pops the next itme from the priority queue.

func (*PQueue) Push

func (pq *PQueue) Push(x interface{})

Push pushes an item onto the priority queue.

func (PQueue) Swap

func (pq PQueue) Swap(i, j int)

type Queue

type Queue struct {
	sync.Mutex
	InitCap int
	Items   []interface{}
	Head    int // current item in queue
	// contains filtered or unexported fields
}

Queue represents an unbounded queue and everything needed to manage it. The preferred method for creating a new Queue is to use either NewQ() or its alias, NewQueue().

func NewQ

func NewQ(size int) *Queue

NewQ is a convenience wrapper to NewQ().

func NewQueue

func NewQueue(size int) *Queue

NewQueue returns an empty queue with an initial capacity equal to the recieved size.

func (*Queue) Cap

func (q *Queue) Cap() int

Cap returns the current size of the queue

func (*Queue) Dequeue

func (q *Queue) Dequeue() (interface{}, bool)

Dequeue removes an item from the queue. If the removal of the item empties the queue, the head and tail will be set to 0. If the queue is empty, a false will be returned, else true.

func (*Queue) Enqueue

func (q *Queue) Enqueue(item interface{}) error

Enqueue adds an item to the queue. If adding the item requires growing the queue, the queue will either be shifted, to make room at the end of the queue, or it will grow.

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

IsEmpty returns whether or not the queue is empty

func (*Queue) IsFull

func (q *Queue) IsFull() bool

IsFull returns false; this is implemented to fulfill Queuer but a dynamic queue will never be full.

func (*Queue) Len

func (q *Queue) Len() int

Len returns the current number of items in the queue

func (*Queue) Peek

func (q *Queue) Peek() (interface{}, bool)

Peek returns the next item in the queue. Post-peek, the queue remains the same.

func (*Queue) Reset

func (q *Queue) Reset()

Reset resets the queue; Head and tail point to element 0. This does not shrink the queue; for that use Resize(). Any items in the queue will be lost.

func (*Queue) Resize

func (q *Queue) Resize(size int) int

Resize resizes the queue to the received size, or, either its original capacity or to 1,25 * the number of items in the queue, whichever is larger. When a size of 0 is received, the queue will be set to either 1.25 * the number of items in the queue or its initial capacity, whichever is larger. Queues with space at the front are shifted to the front.

func (*Queue) SetShiftPercent

func (q *Queue) SetShiftPercent(i int)

SetShiftPercent sets the queue's shiftPercent: the percentage of the queue that must be empty before the remaining items will be shifted to the the beginning of the slice. This occurs when the slice is set to grow.

Valid range of values are 0-100, inclusive. Vaues < 0 are set to 0 and values > 100 are set to 100.

type Queuer

type Queuer interface {
	Enqueue(item interface{}) error
	Dequeue() (interface{}, bool)
	Peek() (interface{}, bool)
	IsEmpty() bool
	IsFull() bool
	Len() int
	Cap() int
	Reset()
	Resize(int) int
}

Queuer interface

Jump to

Keyboard shortcuts

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