goque

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 2 Imported by: 0

README

Goque Instant

donation link

A fast and concurrent object queue for golang.

This module avoids shifting indexes and memory in an array. Instead it utilizes how a uint16 works, where it goes back to 0 when it passes its maximum size. This will reset the number to loop back to the front of the queue. When this happens, the queue is checked for an empty slot, and if unavailable will append to an overflow queue. The overflow queue will concurrently be added to the main queue when a new spot is available.

Notice: This is not for objects where the order is important. It should maintain a consistant order, but objects are added to the queue concurrently, which could result in an offset queue order.

Installation

go get github.com/AspieSoft/goque-instant

Usage


import (
  "github.com/AspieSoft/goque-instant"
)

func main(){
  myQueue := goque.New[int /* any */]()

  // Add objects to the queue
  myQueue.Add(1)
  myQueue.Add(2)
  myQueue.Add(3)
  myQueue.Add(4)
  myQueue.Add(5)

  // Get the next object from the queue and remove it
  myObject := myQueue.Next() // 1
  myObject = myQueue.Next() // 2
  myQueue.Next() // 3

  // Wait for the next object to be available
  myQueue.Next(true) // 4

  // Peek at the next object without removing it from the queue
  myQueue.Peek() // 5
  myQueue.Peek(true) // 5

  // Get the queue size
  myQueue.Len() // 1

  go func(){
    // Note: the Wait method will not run until the queue size reaches 0
    for myQueue.Len() != 0 {
      myQueue.Next(true)
    }
  }()

  // Wait for the queue size to reach 0
  myQueue.Wait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any]() *Queue[T]

func (*Queue[T]) Add

func (q *Queue[T]) Add(value T)

func (*Queue[T]) Len

func (q *Queue[T]) Len() uintptr

func (*Queue[T]) Next

func (q *Queue[T]) Next(wait ...bool) T

func (*Queue[T]) Peek

func (q *Queue[T]) Peek(wait ...bool) T

func (*Queue[T]) Wait added in v1.0.1

func (q *Queue[T]) Wait()

Jump to

Keyboard shortcuts

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