ringbuffer

package module
v0.0.0-...-6737e83 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2017 License: MIT Imports: 1 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

RingBuffer

simple ring (circular) buffer implementation

Software License Travis Codecov branch Go Report Card Go Doc

Installation

go get -u github.com/hirose31/ringbuffer

Usage

package main

import (
	"fmt"

	"github.com/hirose31/ringbuffer"
)

func main() {
	rb := ringbuffer.NewRingBuffer(3)

	// push & shift
	rb.Push("foo")
	rb.Push("bar")

	val, err := rb.Shift()
	if err != nil {
		panic(err)
	}
	fmt.Printf("got: %s\n", val.(string))
	// => got: foo

	// clear and fetch all elements
	rb.Push("baz")
	vals, err := rb.Clear()
	if err != nil {
		panic(err)
	}
	for i, v := range vals {
		fmt.Printf("[%d]%s\n", i, v)
	}
	// => [0]bar
	// => [1]baz

	// circular
	rb.Push("one")
	rb.Push("two")
	rb.Push("three")
	rb.Push("four")
	vals, err = rb.Clear()
	if err != nil {
		panic(err)
	}
	for i, v := range vals {
		fmt.Printf("[%d]%s\n", i, v)
	}
	// "one" is overwritten with "four"
	// => [0]two
	// => [1]three
	// => [2]four
}

Documentation

Overview

Package ringbuffer is simple ring (circular) buffer implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RingBuffer

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

RingBuffer type represents ring buffer.

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

NewRingBuffer returns a new RingBuffer.

func (*RingBuffer) Clear

func (rb *RingBuffer) Clear() ([]interface{}, error)

Clear removes all elements and returns those elements.

func (*RingBuffer) Fetch

func (rb *RingBuffer) Fetch() ([]interface{}, error)

Fetch returns all elements

func (*RingBuffer) Push

func (rb *RingBuffer) Push(val interface{}) error

Push pushes the element val to the tail of the ring buffer.

func (*RingBuffer) Shift

func (rb *RingBuffer) Shift() (interface{}, error)

Shift removes an element from head of the ring buffer and returns it.

Jump to

Keyboard shortcuts

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