bits

package
v0.0.43 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package bits provides the means to store and manipulate two-dimensional arrays of bits. 2D bit arrays can be provisioned in Go using [][]bool, however this package provides more efficient storage and manipulation as well as convenient read/write mechanisms. Types and functions defined in the bits package are fundamental to the modelling and manipulation of dot-matrix displays.

Index

Constants

This section is empty.

Variables

This section is empty.

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.

Types

type Cursor added in v0.0.37

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 added in v0.0.37

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 added in v0.0.37

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

Position returns the current position of the cursor.

func (*Cursor) ReadDown added in v0.0.37

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 added in v0.0.37

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 added in v0.0.37

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 added in v0.0.37

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 added in v0.0.37

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 added in v0.0.37

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 added in v0.0.37

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 added in v0.0.37

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 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 NewMatrix

func NewMatrix(height, width int) *Matrix

NewMatrix creates a new Matrix with a given size

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) 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 added in v0.0.37

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) Set

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

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

func (*Matrix) Size added in v0.0.30

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