circularqueue

package module
v0.0.0-...-a364d08 Latest Latest
Warning

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

Go to latest
Published: May 10, 2019 License: MIT Imports: 3 Imported by: 0

README

Thread-safe circular queue for Go

GoDoc

Installation

go get github.com/Mottl/circularqueue

Example

package main
  
import (
    "fmt"
    "github.com/Mottl/circularqueue"
)

func main() {
    queue := circularqueue.NewQueue(64)
    queue.Push(100)
    queue.Push(101)
    queue.Push(102)
    queue.Push(103)
    queue.PopAt(1)     // 101, nil
    queue.Pop()        // 103, nil
    fmt.Println(queue) // Queue (len=2, cap=64) [ 100, 102 ]
}

License

Use of this package is governed by MIT license that can be found in the LICENSE file.

Documentation

Overview

Package circularqueue implements a thread-safe circular queue

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

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

Cicular queue

func NewQueue

func NewQueue(capacity int) Queue

Creates new queue and allocates memory

func (*Queue) Cap

func (q *Queue) Cap() int

Cap returns the capacity (the maximum number of elements) of the queue

Example
q := NewQueue(10)

q.Push("abc")
q.Push("def")
q.Push("ghi")

fmt.Println(q.Cap())
Output:

10

func (*Queue) Len

func (q *Queue) Len() int

Returns the length (the number of elements) of the queue

Example
q := NewQueue(10)

q.Push("abc")
q.Push("def")
q.Push("ghi")

fmt.Println(q.Len())
Output:

3

func (*Queue) Pop

func (q *Queue) Pop() (interface{}, error)

Pops takes and removes the last element from the queue

Example
q := NewQueue(10)
q.Push("abc")
q.Push("def")
val1, err1 := q.Pop()
val2, err2 := q.Pop()
val3, err3 := q.Pop()
fmt.Println(val1, err1)
fmt.Println(val2, err2)
fmt.Println(val3, err3)
Output:

def <nil>
abc <nil>
<nil> Can't pop: queue is empty

func (*Queue) PopAt

func (q *Queue) PopAt(index int) (interface{}, error)

PopAt takes and removes the element at the index position from the queue

index = 0 to take the first element (the head)
index = N to take the Nth element from the head
index = -1 to take the last element (the tail)
index = -N to take the Nth element from the tail
Example
q := NewQueue(10)
q.Push("abc")
q.Push("def")
val1, err1 := q.PopAt(0)
val2, err2 := q.PopAt(0)
val3, err3 := q.PopAt(0)
fmt.Println(val1, err1)
fmt.Println(val2, err2)
fmt.Println(val3, err3)
Output:

abc <nil>
def <nil>
<nil> Can't pop: queue is empty

func (*Queue) Push

func (q *Queue) Push(element interface{}) (int, error)

Push appends the element to the end of the queue

Example
q := NewQueue(2)

vacant1, err1 := q.Push("abc")
vacant2, err2 := q.Push("def")
vacant3, err3 := q.Push("ghi")

fmt.Println(vacant1, err1)
fmt.Println(vacant2, err2)
fmt.Println(vacant3, err3)
Output:

1 <nil>
0 <nil>
0 Can't push: queue is full (capacity=2)

func (Queue) String

func (q Queue) String() string

String implements the Stringer interface

Example
q := NewQueue(10)
q.Push("abc")
q.Push("def")
q.Push("ghi")
q.PopAt(0)
fmt.Println(q)
Output:

Queue (len=2, cap=10) [ def, ghi ]

func (*Queue) Vacant

func (q *Queue) Vacant() int

Vacant returns the number of free slots in the queue

Example
q := NewQueue(10)

q.Push("abc")
q.Push("def")
q.Push("ghi")

fmt.Println(q.Vacant())
Output:

7

Jump to

Keyboard shortcuts

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