buffer

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2021 License: BSD-2-Clause Imports: 9 Imported by: 0

README

buffer

Ring buffers in Go.

Documentation

Overview

Package buffer implements ring buffers of bytes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer implements a ring buffer. The ring buffer has space for 2**N bytes for user-specified N.

func New

func New(numBits byte) *Buffer

New is a convenience function that allocates a new Buffer and calls Init on it.

func (Buffer) Bytes added in v1.2.0

func (buffer Buffer) Bytes() []byte

Bytes allocates and returns a copy of the Buffer's contents.

func (Buffer) Cap

func (buffer Buffer) Cap() uint

Cap returns the maximum byte capacity of the Buffer.

func (*Buffer) Clear

func (buffer *Buffer) Clear()

Clear erases the contents of the Buffer.

func (*Buffer) CommitBulkRead

func (buffer *Buffer) CommitBulkRead(length uint)

CommitBulkRead completes the bulk read begun by the previous call to PrepareBulkRead. The argument must be between 0 and the length of the slice returned by PrepareBulkRead.

func (*Buffer) CommitBulkWrite

func (buffer *Buffer) CommitBulkWrite(length uint)

CommitBulkWrite completes the bulk write begun by the previous call to PrepareBulkWrite. The argument must be between 0 and the length of the slice returned by PrepareBulkWrite.

func (Buffer) DebugString added in v1.1.0

func (buffer Buffer) DebugString() string

DebugString returns a detailed dump of the Buffer's internal state.

func (Buffer) GoString added in v1.1.0

func (buffer Buffer) GoString() string

GoString returns a brief dump of the Buffer's internal state.

func (*Buffer) Init

func (buffer *Buffer) Init(numBits byte)

Init initializes the Buffer. The Buffer will hold a maximum of 2**N bits, where N is the argument provided. The argument must be a number between 0 and 31 inclusive.

func (Buffer) IsEmpty

func (buffer Buffer) IsEmpty() bool

IsEmpty returns true iff the Buffer contains no bytes.

func (Buffer) IsFull

func (buffer Buffer) IsFull() bool

IsFull returns true iff the Buffer contains the maximum number of bytes.

func (Buffer) Len

func (buffer Buffer) Len() uint

Len returns the number of bytes currently in the Buffer.

func (Buffer) NumBits added in v1.1.0

func (buffer Buffer) NumBits() byte

NumBits returns the number of bits used to initialize this Buffer.

func (*Buffer) PrepareBulkRead

func (buffer *Buffer) PrepareBulkRead(length uint) []byte

PrepareBulkRead obtains a slice from which the caller can read bytes. The bytes do not leave the buffer's contents until CommitBulkRead is called. If CommitBulkRead is not subsequently called, the read acts as a "peek" operation.

The returned slice may contain fewer bytes than requested; it will return a zero-length slice iff the buffer is empty. The caller must check its length before using it. A short but non-empty return slice does *not* indicate an empty buffer.

The returned slice is only valid until the next call to any mutating method on this Buffer; mutating methods are those which take a pointer receiver.

func (*Buffer) PrepareBulkWrite

func (buffer *Buffer) PrepareBulkWrite(length uint) []byte

PrepareBulkWrite obtains a slice into which the caller can write bytes. The bytes do not become a part of the buffer's contents until CommitBulkWrite is called. If CommitBulkWrite is not subsequently called, the write is considered abandoned.

The returned slice may contain fewer bytes than requested; it will return a nil slice iff the buffer is full. The caller must check the slice's length before using it. A short but non-empty return slice does *not* indicate a full buffer.

The returned slice is only valid until the next call to any mutating method on this Buffer; mutating methods are those which take a pointer receiver.

func (*Buffer) Read

func (buffer *Buffer) Read(p []byte) (int, error)

Read reads a slice of bytes from the Buffer. If the buffer is empty, ErrEmpty is returned.

func (*Buffer) ReadByte

func (buffer *Buffer) ReadByte() (byte, error)

ReadByte reads a single byte from the Buffer. If the buffer is empty, ErrEmpty is returned.

func (*Buffer) ReadFrom

func (buffer *Buffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom attempts to fill this Buffer by reading from the provided Reader. May return any error returned by the Reader, including io.EOF. If a nil error is returned, then the buffer is now full.

func (Buffer) Slices added in v1.2.0

func (buffer Buffer) Slices() [][]byte

Slices returns zero or more []byte slices which provide a view of the Buffer's contents. The slices are ordered from oldest to newest, the slices are only valid until the next mutating method call, and the contents of the slices should not be modified.

func (Buffer) String added in v1.1.0

func (buffer Buffer) String() string

String returns a plain-text description of the buffer.

func (*Buffer) Write

func (buffer *Buffer) Write(p []byte) (int, error)

Write writes a slice of bytes to the Buffer. If the Buffer is full, as many bytes as possible are written to the Buffer and ErrFull is returned.

func (*Buffer) WriteByte

func (buffer *Buffer) WriteByte(ch byte) error

WriteByte writes a single byte to the Buffer. If the Buffer is full, ErrFull is returned.

func (*Buffer) WriteTo

func (buffer *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo attempts to drain this Buffer by writing to the provided Writer. May return any error returned by the Writer. If a nil error is returned, then the Buffer is now empty.

type Error

type Error byte

Error is the type for the error constants returned by this package.

const (
	// ErrEmpty is returned when reading from an empty Buffer.
	ErrEmpty Error = iota

	// ErrFull is returned when writing to a full Buffer.
	ErrFull

	// ErrBadDistance is returned when Window.LookupByte is called with a
	// distance that isn't contained within the Window.
	ErrBadDistance
)

func (Error) Error

func (err Error) Error() string

Error returns the error message for this error.

func (Error) GoString

func (err Error) GoString() string

GoString returns the name of the Go constant.

type Hybrid added in v1.2.0

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

Hybrid implements a combination Window/Buffer that uses the Window to remember bytes that were recently removed from the Buffer, and that hashes all data that enters the Window so that LZ77-style prefix matching can be made efficient.

func NewHybrid added in v1.2.0

func NewHybrid(o HybridOptions) *Hybrid

NewHybrid is a convenience function that allocates a Hybrid and calls Init on it.

func (*Hybrid) Advance added in v1.2.0

func (hybrid *Hybrid) Advance() (buf []byte, bestDistance uint, bestLength uint, bestFound bool)

Advance moves a slice of bytes from the Hybrid's Buffer to its Window. The nature of the slice depends on the Hybrid's prefix match settings, the contents of the Hybrid's Window, and the contents of the Hybrid's Buffer.

func (Hybrid) Buffer added in v1.2.0

func (hybrid Hybrid) Buffer() Buffer

Buffer returns a copy of the Hybrid's Buffer.

func (Hybrid) BufferNumBits added in v1.2.0

func (hybrid Hybrid) BufferNumBits() byte

BufferNumBits returns the size of the Buffer in bits.

func (*Hybrid) Clear added in v1.2.0

func (hybrid *Hybrid) Clear()

Clear clears all data in the entire Hybrid.

func (Hybrid) DebugString added in v1.2.0

func (hybrid Hybrid) DebugString() string

DebugString returns a detailed dump of the Hybrid's internal state.

func (Hybrid) GoString added in v1.2.0

func (hybrid Hybrid) GoString() string

GoString returns a brief dump of the Hybrid's internal state.

func (Hybrid) HashNumBits added in v1.2.0

func (hybrid Hybrid) HashNumBits() byte

HashNumBits returns the size of the hash function output in bits.

func (*Hybrid) Init added in v1.2.0

func (hybrid *Hybrid) Init(o HybridOptions)

Init initializes a Hybrid.

func (Hybrid) IsEmpty added in v1.2.0

func (hybrid Hybrid) IsEmpty() bool

IsEmpty returns true iff the Hybrid's Buffer is empty.

func (Hybrid) IsFull added in v1.2.0

func (hybrid Hybrid) IsFull() bool

IsFull returns true iff the Hybrid's Buffer is full.

func (*Hybrid) SetWindow added in v1.2.0

func (hybrid *Hybrid) SetWindow(p []byte)

SetWindow replaces the Hybrid's Window with the given data.

func (Hybrid) String added in v1.2.0

func (hybrid Hybrid) String() string

String returns a plain-text description of the Hybrid.

func (Hybrid) Window added in v1.2.0

func (hybrid Hybrid) Window() Window

Window returns a copy of the Hybrid's Window.

func (*Hybrid) WindowClear added in v1.2.0

func (hybrid *Hybrid) WindowClear()

WindowClear clears just the data in the Hybrid's Window.

func (Hybrid) WindowNumBits added in v1.2.0

func (hybrid Hybrid) WindowNumBits() byte

WindowNumBits returns the size of the Window in bits.

func (*Hybrid) Write added in v1.2.0

func (hybrid *Hybrid) Write(p []byte) (int, error)

Write writes a slice of bytes to the Hybrid's Buffer.

func (*Hybrid) WriteByte added in v1.2.0

func (hybrid *Hybrid) WriteByte(ch byte) error

WriteByte writes a single byte to the Hybrid's Buffer.

type HybridOptions added in v1.2.0

type HybridOptions struct {
	WindowNumBits  byte
	BufferNumBits  byte
	HashNumBits    byte
	MaxMatchLength uint16
}

HybridOptions holds options for initializing an instance of Hybrid.

type Window

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

Window implements a sliding window backed by a ring buffer. The ring buffer has space for 2**N bytes for user-specified N.

func NewWindow

func NewWindow(numBits byte) *Window

NewWindow is a convenience function that allocates a Window and calls Init on it.

func (Window) Bytes added in v1.2.0

func (window Window) Bytes() []byte

Bytes allocates and returns a copy of the Window's contents.

func (Window) Cap

func (window Window) Cap() uint

Cap returns the maximum byte capacity of the Window.

func (*Window) Clear

func (window *Window) Clear()

Clear erases the contents of the Window.

func (Window) DebugString added in v1.1.0

func (window Window) DebugString() string

DebugString returns a detailed dump of the Window's internal state.

func (Window) FindLongestPrefix

func (window Window) FindLongestPrefix(p []byte) (distance uint, length uint, ok bool)

FindLongestPrefix searches the Window for the longest prefix of the given byte slice that exists within the Window's history.

This method could use some additional optimization.

func (Window) GoString added in v1.1.0

func (window Window) GoString() string

GoString returns a brief dump of the Window's internal state.

func (Window) Hash

func (window Window) Hash(hashes ...hash.Hash)

Hash non-destructively writes the contents of the Window into the provided Hash object(s).

func (Window) Hash32

func (window Window) Hash32(fn func() hash.Hash32) uint32

Hash32 is a convenience method that constructs a Hash32, calls Window.Hash with it, and calls Sum32 on it.

func (*Window) Init

func (window *Window) Init(numBits byte)

Init initializes the Window. The Window will hold a maximum of 2**N bits, where N is the argument provided. The argument must be a number between 0 and 31 inclusive.

func (Window) IsEmpty

func (window Window) IsEmpty() bool

IsEmpty returns true iff the Window contains no bytes.

func (Window) IsFull

func (window Window) IsFull() bool

IsFull returns true iff the Window contains the maximum number of bytes.

func (Window) Len

func (window Window) Len() uint

Len returns the number of bytes currently in the Window.

func (Window) LookupByte

func (window Window) LookupByte(distance uint) (byte, error)

LookupByte returns a byte which was written previously. The argument is the offset into the window, with 1 representing the most recently written byte and Window.Cap() representing the oldest byte still within the Window.

func (Window) NumBits added in v1.1.0

func (window Window) NumBits() byte

NumBits returns the number of bits used to initialize this Window.

func (Window) Slices added in v1.2.0

func (window Window) Slices() [][]byte

Slices returns zero or more []byte slices which provide a view of the Window's contents. The slices are ordered from oldest to newest, the slices are only valid until the next mutating method call, and the contents of the slices should not be modified.

func (Window) String added in v1.1.0

func (window Window) String() string

String returns a plain-text description of the Window.

func (*Window) Write

func (window *Window) Write(p []byte) (int, error)

Write writes a slice of bytes to the Window. If the Window is full or if the slice exceeds the capacity of the Window, the oldest bytes in the inferred stream are dropped until the slice fits.

func (*Window) WriteByte

func (window *Window) WriteByte(ch byte) error

WriteByte writes a single byte to the Window. If the Window is full, the oldest byte in the inferred stream is dropped.

Jump to

Keyboard shortcuts

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