queue

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2020 License: BSD-3-Clause Imports: 2 Imported by: 0

README

queue

Simple to use FIFO data structures.

Usage

Here's a working example of how to use the Queue.

package main

import (
    "fmt"

    "github.com/btnmasher/queue"
)

// Declare
var unbounded queue.Queue
var bounded queue.Queue
var clearme queue.Queue

// Instantiate
func init() {
    unbounded = queue.NewUnbounded()
    bounded = queue.NewBounded(3)
    clearme = queue.NewUnbounded()
}

// Use
func main() {
    words := []string{"This", "was", "a", "triumph"}

    for _, word := range words {
        unbounded.Add(word)
        clearme.Add(word)

        if err := bounded.Add(word); err != nil {
            // Queue maximum was reached.
            fmt.Println(err)
        }
    }

    clearme.Clear() //Demonstrating clearing queues

    // Demonstrating getting queue length.
    fmt.Printf("Bounded Length: %v\n", bounded.Len())
    fmt.Printf("Unbounded Length: %v\n", unbounded.Len())
    fmt.Printf("Cleared Length: %v\n", clearme.Len())

    for {
        out1 := unbounded.Take()
        out2 := bounded.Take()

        if out1 != nil { // Take() returns nil if nothing is enqueued
            fmt.Printf("Unbounded: %v\n", out1.(string))
            // Using type coercion, nodes are of type interface{}
            // (though not necessary with fmt.Println, just
            // done as a demonstration.)
        }

        fmt.Printf("Bounded: %v\n", out2)

        fmt.Printf("Cleared: %v\n", clearme.Take())
    }

Output:

Unable to add value, queue is full.
Bounded Length: 3
Unbounded Length: 4
Cleared Length: 0
Unbounded: This
Bounded: This
Cleared: <nil>
Unbounded: was
Bounded: was
Cleared: <nil>
Unbounded: a
Bounded: a
Cleared: <nil>
Unbounded: triumph
Bounded: <nil>
Cleared: <nil>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoundedQueue

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

BoundedQueue is a queue that has a specified size.

func NewBounded

func NewBounded(size int) *BoundedQueue

New returns a new BoundedQueue with the specified buffer size. Size is forced to be greater than zero, will default to 1.

func (*BoundedQueue) Add

func (q *BoundedQueue) Add(value interface{}) error

Push adds a node to the queue. Concurrency safe.

func (*BoundedQueue) Clear

func (q *BoundedQueue) Clear()

Clear empties the queue and leaves it at a freshly initialized state. Not concurrency safe.

func (*BoundedQueue) Len

func (q *BoundedQueue) Len() int

Len returns the length of the items in the queue. Concurrency safe.

func (*BoundedQueue) Take

func (q *BoundedQueue) Take() interface{}

Take removes and returns a node from the queue in first to last order. Returns nil if there is nothing in the queue. Concurrency safe.

type Queue

type Queue interface {
	Add(value interface{}) error
	Take() interface{}
	Clear()
	Len() int
}

Queue is a basic FIFO queue

type UnboundedQueue

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

Unbounded queue is a queue that can continuously be added to.

func NewUnbounded

func NewUnbounded() *UnboundedQueue

New returns a new UnboundedQueue with the specified buffer size, must be greater than 1

func (*UnboundedQueue) Add

func (q *UnboundedQueue) Add(value interface{}) error

Push adds a node to the queue. Not concurrency safe.

func (*UnboundedQueue) Clear

func (q *UnboundedQueue) Clear()

Clear empties the queue and leaves it at a freshly initialized state. Not concurrency safe.

func (*UnboundedQueue) Len

func (q *UnboundedQueue) Len() int

Len returns the length of the items in the queue. Not concurrency safe.

func (*UnboundedQueue) Take

func (q *UnboundedQueue) Take() interface{}

Take removes and returns a node from the queue in first to last order. Returns nil if there is nothing in the queue. Not concurrency safe.

Jump to

Keyboard shortcuts

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