gotb

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2018 License: MIT Imports: 1 Imported by: 1

README

gotb

Implements a few tail buffer abstract data types.

Description

Tail buffers are useful when a program needs to track the N final elements added to a list, but not necessarily track previous elements.

// tail copies the final num lines from io.Reader to io.Writer.
func tail(num int, r io.Reader, w io.Writer) error {
	cb, err := gotb.NewStrings(num)
	if err != nil {
		return err
	}

	scanner := bufio.NewScanner(r)

	for scanner.Scan() {
		_, _ = cb.QueueDequeue(scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		return err
	}

	for _, line := range cb.Drain() {
		if _, err = fmt.Fprintln(w, line); err != nil {
			return err
		}
	}

	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewInterfaces

func NewInterfaces(n int) (interface{}, error)

NewInterfaces returns a newly initialized buffer for N interface{} items, where 0 <= N.

Types

type Interfaces

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

Interfaces is a data structure for storing the previous N string items, where 0 <= N <= arbitrary limit, and the arbitrary limit is proportional to how much memory ought to be allocated to the data structure.

func (*Interfaces) Drain

func (tb *Interfaces) Drain() []interface{}

Drain returns all items from the structure. This implimentation is not designed to handle invocation of any other methods after calling Drain.

func (*Interfaces) QueueDequeue

func (tb *Interfaces) QueueDequeue(newItem interface{}) (interface{}, bool)

QueueDequeue stores the newly provided item in the queue and returns the Nth previous item from the queue, along with a second return value of true. If exactly N or fewer than N items have thus far been stored in the buffer, a nil value will be returned along with a second return value of false.

type Strings

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

Strings is a data structure for storing the previous N string items, where 0 <= N <= arbitrary limit, and the arbitrary limit is proportional to how much memory ought to be allocated to the data structure.

func NewStrings

func NewStrings(n int) (*Strings, error)

NewStrings returns a newly initialized buffer for N string items, where 0 <= N.

func (*Strings) Drain

func (tb *Strings) Drain() []string

Drain returns all items from the structure. This implimentation is not designed to handle invocation of any other methods after calling Drain.

func (*Strings) QueueDequeue

func (tb *Strings) QueueDequeue(newItem string) (string, bool)

QueueDequeue stores the newly provided item in the queue and returns the Nth previous item from the queue, along with a second return value of true. If exactly N or fewer than N items have thus far been stored in the buffer, an empty string will be returned along with a second return value of false.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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