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 ¶
- func Copy(source *Matrix, sourceRow, sourceCol int, dest *Matrix, ...) (int, int)
- type Cursor
- func (c *Cursor) Position() (row, col int)
- func (c *Cursor) ReadDown() (bit, ok bool)
- func (c *Cursor) ReadDownByte() (b byte, bits int)
- func (c *Cursor) ReadLeft() (bit, ok bool)
- func (c *Cursor) ReadLeftByte() (b byte, bits int)
- func (c *Cursor) ReadRight() (bit, ok bool)
- func (c *Cursor) ReadRightByte() (b byte, bits int)
- func (c *Cursor) ReadUp() (bit, ok bool)
- func (c *Cursor) ReadUpByte() (b byte, bits int)
- type Matrix
- func (m *Matrix) And(other *Matrix)
- func (m *Matrix) Clear()
- func (m *Matrix) Clone() *Matrix
- func (m *Matrix) Get(row, col int) bool
- func (m *Matrix) Not()
- func (m *Matrix) Or(other *Matrix)
- func (m *Matrix) Set(row, col int, value bool)
- func (m *Matrix) Size() (height, width int)
- func (m *Matrix) String() string
- func (m *Matrix) Xor(other *Matrix)
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
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) ReadDown ¶ added in v0.0.37
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
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
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
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
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
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
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
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.
func (*Matrix) And ¶
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) Get ¶
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 ¶
Or performs a bitwise or operation. The result of the operation is applied to this matrix.
func (*Matrix) Set ¶
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
Size returns the size of the Matrix as a two-tuple of height and width