bits

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 4 Imported by: 3

Documentation

Overview

Package bits provides a 2D bit matrix. Logically it is similar to a 2-dimensional array of booleans, but has some significant differences:

  1. It is optimised for operating on blocks of data in the matrix, including: a. Batch copies from one matrix to another b. Batch bitwise operations (And, Not, Or, Xor)
  2. It provides some simple structures to facilitate reading along columns and rows.
  3. Unlike a [][]bool, A bits.Matrix cannot represent a jagged array.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidMatrixFormat = errors.New("invalid serialisation format for bits.Matrix")

Functions

func Copy

func Copy(source *Matrix, sourceRow, sourceCol int, dest *Matrix, destRow, destCol, height, width int) (int, int)

Copy copies a sub-range of bits from one matrix to another. All the bits in the range are overwritten in the destination matrix.

Copies from source matrix, at origin (sourceRow, sourceCol) to the dest matrix ar (destRow, destCol). Copy will panic if either the source origin or the destination origin are out of bounds. Copies a rectangle up to the size given by height and width. If the maximum-sized rectangle exceeds the bonds of either the source or destination matrix, it is trimmed. Returns the actual height and width of the rectangle copied as a result.

Copying is a read-only operation with respect to the source matrix, with the usual implications for concurrency.

func Write added in v1.2.0

func Write(writer io.Writer, matrix *Matrix) error

Types

type BitMatrixReader added in v1.2.0

type BitMatrixReader interface {
	Read(reader io.Reader) (*Matrix, error)
}

type BitMatrixReaderWriter added in v1.2.0

type BitMatrixReaderWriter interface {
	BitMatrixReader
	BitMatrixWriter
}

func DefaultReaderWriter added in v1.2.0

func DefaultReaderWriter() BitMatrixReaderWriter

type BitMatrixWriter added in v1.2.0

type BitMatrixWriter interface {
	Write(writer io.Writer, matrix *Matrix) error
}

type Cursor

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

A Cursor allows a BitMatrix to be traversed by reading across, or up and down the matrix.

The type provides a mechanism to traverse a bit matrix that is optimised for reading contiguous bits, as compared to repeatedly using Get() in a loop.

When reading from left to right, or downwards, the read is sequenced as follows: sample current bit then advance cursor position. When reading from right to left, or upwards, the read is sequenced as follows: advance cursor position then sample bit at new position. The different sequencing allows the expected behaviour that reading left then right repeatedly keeps returning the same bit.

func NewCursor

func NewCursor(m *Matrix, row, col int) *Cursor

NewCursor returns a new instance of a Cursor.

Receives the matrix to address as an argument, as well as the starting location. For rightward or downward reads, the initial cursor position should be on the first bit to be read. For leftward or upward reads, the initial cursor position should be one before the bit to be read (to its right or beneath it). Because of this, positioning the cursor at row = height, or at col = width is permitted.

func (*Cursor) Position

func (c *Cursor) Position() (row, col int)

Position returns the current position of the cursor.

func (*Cursor) ReadDown

func (c *Cursor) ReadDown() (bit, ok bool)

ReadDown samples the bit at the current position and then positions the cursor one bit further down. if ok is returned as false, the cursor cannot move further down, it's already past the bottom row.

func (*Cursor) ReadDownByte

func (c *Cursor) ReadDownByte() (b byte, bits int)

ReadDownByte constructs a byte from 8 consecutive reads downward. Returns the resulting byte and the number of bits successfully read before reading past the height of the matrix. The last bit read is always in the least significant position, regardless of the number of bits returned.

func (*Cursor) ReadLeft

func (c *Cursor) ReadLeft() (bit, ok bool)

ReadLeft positions the cursor one bit to the left and returns the value at the new position. if ok is returned as false, the cursor cannot move further left, it's already on column zero.

func (*Cursor) ReadLeftByte

func (c *Cursor) ReadLeftByte() (b byte, bits int)

ReadLeftByte constructs a byte from 8 consecutive reads leftward. Because of the leftward direction, bits in the result are sequenced in the reverse order of the natural order in the matrix. That is to say that the most significant bit of the returned byte is read from the rightmost position read by the cursor. Returns the resulting byte and the number of bits successfully read before reading past column zero. The last bit read is always in the least significant position, regardless of the number of bits returned.

func (*Cursor) ReadRight

func (c *Cursor) ReadRight() (bit, ok bool)

ReadRight samples the bit at the current position and then positions the cursor one bit to the right. if ok is returned as false, the cursor cannot move further right, it's already past the rightmost column.

func (*Cursor) ReadRightByte

func (c *Cursor) ReadRightByte() (b byte, bits int)

ReadRightByte constructs a byte from 8 consecutive reads rightward. Returns the resulting byte and the number of bits successfully read before reading past the width of the matrix. The last bit read is always in the least significant position, regardless of the number of bits returned.

func (*Cursor) ReadUp

func (c *Cursor) ReadUp() (bit, ok bool)

ReadUp positions the cursor one bit further up and returns the value at the new position. if ok is returned as false, the cursor cannot move further up, it's already on row zero.

func (*Cursor) ReadUpByte

func (c *Cursor) ReadUpByte() (b byte, bits int)

ReadUpByte constructs a byte from 8 consecutive reads upward. Returns the resulting byte and the number of bits successfully read before reading past row zero. The last bit read is always in the least significant position, regardless of the number of bits returned.

type HumanReadable added in v1.2.0

type HumanReadable struct {
	TrueRune   rune
	FalseRune  rune
	NewRowRune rune
	Terminator rune
	Padding    int
}

func (HumanReadable) Read added in v1.2.0

func (rw HumanReadable) Read(reader io.Reader) (result *Matrix, e error)

func (HumanReadable) Write added in v1.2.0

func (rw HumanReadable) Write(writer io.Writer, matrix *Matrix) error

type Matrix

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

A Matrix is a two-dimensional array of bits with fixed height and width.

Matrix is not thread-safe. Read-only operations such as Get and Clone may safely be called concurrently. Write operations such as Set and Clear should not be called concurrently with each other, or with read operations.

var ZeroMatrix *Matrix = &Matrix{}

ZeroMatrix is zero-sized matrix

func Decode

func Decode(source []uint32) *Matrix

Decode creates a new Matrix from an array of UInt32.

Provided as a complement to the Encode method, builds a Matrix from the state encoded by that method.

func FromBools added in v1.2.0

func FromBools(source [][]bool) *Matrix

FromBools builds a new Matrix from a slice of bool slices. Input must not be jagged, or the function will panic.

func NewMatrix

func NewMatrix(height, width int) *Matrix

NewMatrix creates a new Matrix with a given size

func Read added in v1.2.0

func Read(reader io.Reader) (*Matrix, error)

func (*Matrix) And

func (m *Matrix) And(other *Matrix)

And performs a bitwise and operation. The result of the operation is applied to this matrix.

func (*Matrix) Clear

func (m *Matrix) Clear()

Clear resets all the bits in the matrix back to zero.

func (*Matrix) Clone

func (m *Matrix) Clone() *Matrix

Clone creates an exact copy of the Matrix in its current state.

func (*Matrix) Encode

func (m *Matrix) Encode() []uint32

Encode returns the complete state of the matrix as an array of UInt32.

Provided as a means to serialise a matrix for persistance or transport.

func (*Matrix) Equals added in v1.2.0

func (m *Matrix) Equals(other *Matrix) bool

Equals performs a bitwise comparison of two matrices. The result is true if the two matriices are equivalent, or false otherwise.

func (*Matrix) Get

func (m *Matrix) Get(row, col int) bool

Get returns the state of a specific bit in the Matrix. Arguments specify the coordinates of the bit. Get will panic if the arguments are out of bounds.

func (*Matrix) Not

func (m *Matrix) Not()

Not performs a bitwise complement operation. The result of the operation is applied to this matrix.

func (*Matrix) Or

func (m *Matrix) Or(other *Matrix)

Or performs a bitwise or operation. The result of the operation is applied to this matrix.

func (*Matrix) Put

func (m *Matrix) Put(row, col int, value bool)

Put allocates state to a specific bit in the Matrix. Arguments specify the coordinates of the bit and the required state. Put will panic if the arguments are out of bounds.

func (*Matrix) Reset

func (m *Matrix) Reset(row, col int)

Reset allocates a value of false to a specific bit in the Matrix. Arguments specify the coordinates of the bit. Reset will panic if the arguments are out of bounds.

func (*Matrix) Set

func (m *Matrix) Set(row, col int)

Set allocates a value of true to a specific bit in the Matrix. Arguments specify the coordinates of the bit. Set will panic if the arguments are out of bounds.

func (*Matrix) Size

func (m *Matrix) Size() (height, width int)

Size returns the size of the Matrix as a two-tuple of height and width

func (*Matrix) String

func (m *Matrix) String() string

String generates a string representation of the matrix. The string generated is intended for visual inspection, and applies a style appropriate to that.

func (*Matrix) Xor

func (m *Matrix) Xor(other *Matrix)

Xor performs a bitwise xor operation. The result of the operation is applied to this matrix.

Jump to

Keyboard shortcuts

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