fifo

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: MIT Imports: 2 Imported by: 0

README

fifo

Go Report Card Go Reference codecov

This package provides thread safe unique use case FIFO structures.

Ring

Ring provides a fixed-length FIFO linked-list style ring structure. Push() adds new elements to the ring, and Pop() removes the oldest element from the ring. When the ring reaches the specified max lengh, a Push() operation will first remove the oldest element before adding the new element, maintaining the max length. Do() accepts a function, and iterates forward performing that function on each element in the ring.

Movement

Moving around the ring is achieved by calling Next(), Prev(), or Move(n) where n is an integer. Positive integers Move forward, negative integers Move backwards around the ring. All movement methods return the destination value.

Values

Calling Value() on any ring element provides the value of the current cursor position within the ring. Calling SetValue() on any ring element replaces the value at the current cursor position.

Example
package main

import (
  "fmt"
  "bytes"
  
  "fifo"
)

func printRing(r *fifo.Ring) {
  var buf bytes.Buffer
  fmt.Fprint(&buf, "{ ")
  r.Do(func(p interface{}) {
		fmt.Fprintf(&buf, "%d ", p.(int))
	})
    fmt.Fprintf(&buf, "} Len: %d, Avail: %d", r.Len(), r.Avail())
  fmt.Println(buf.String())
}

func main() {
  max := 5
  r := fifo.NewRing(max)
  printRing(r)
  for i := 0; i < (max * 2); i++ {
    r.Push(i)
    printRing(r)
  }
  _ = r.Pop()
  printRing(r)
}

Results in:

{ } Len: 0, Avail: 5
{ 0 } Len: 1, Avail: 4
{ 0 1 } Len: 2, Avail: 3
{ 0 1 2 } Len: 3, Avail: 2
{ 0 1 2 3 } Len: 4, Avail: 1
{ 0 1 2 3 4 } Len: 5, Avail: 0
{ 1 2 3 4 5 } Len: 5, Avail: 0
{ 2 3 4 5 6 } Len: 5, Avail: 0
{ 3 4 5 6 7 } Len: 5, Avail: 0
{ 4 5 6 7 8 } Len: 5, Avail: 0
{ 5 6 7 8 9 } Len: 5, Avail: 0
{ 6 7 8 9 } Len: 4, Avail: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ring

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

Ring provides a thread safe length limited ring buffer that operates in a FIFO manner

func NewRing

func NewRing(max int) *Ring

New creates a ring with max n elements this does not actually create the elements

func (*Ring) Avail

func (r *Ring) Avail() int

Avail returns the current space available in the ring

func (*Ring) Do

func (r *Ring) Do(f func(interface{}))

Do calls function f on each element in the ring iterating forward from the current position

func (*Ring) Len

func (r *Ring) Len() int

Len returns the current length of the ring

func (*Ring) Move

func (r *Ring) Move(n int) interface{}

Move moves the currnet position n % r.Len() elements backwards (n < 0) or forward (n > 0) in the ring and returns the destination value. r must not be empty

func (*Ring) Next

func (r *Ring) Next() interface{}

Next moves the ring one element forward, and returns the destination value. r must not be empty

func (*Ring) Pop

func (r *Ring) Pop() interface{}

Pop removes the oldest element from the ring;

func (*Ring) Prev

func (r *Ring) Prev() interface{}

Prev rmoves the ring element backward, and returns the destination value. r must not be empty

func (*Ring) Push

func (r *Ring) Push(val interface{})

Push adds elements to the ring; if the ring is full it replaces the oldest element

func (*Ring) SetValue

func (r *Ring) SetValue(val interface{})

SetValue changes the value of the current ring element

func (*Ring) Value

func (r *Ring) Value() interface{}

Value returns the value of the current ring element

Jump to

Keyboard shortcuts

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