Warning: This package is currently under development and the API is subject to change. It should be considered unstable.
Fork of github.com/fufuok/utils/generic/array2d.
import "github.com/xll-gen/array2d"Package array2d contains an implementation of a 2D array.
- array2d
- Index
- type Array2D
- func New
- func NewFilled
- func FromSlice
- func FromJagged
- func (Array2D[T]) Row
- func (Array2D[T]) Col
- func (Array2D[T]) Fill
- func (Array2D[T]) Get
- func (Array2D[T]) Set
- func (Array2D[T]) Copy
- func (Array2D[T]) String
- func (Array2D[T]) Height
- func (Array2D[T]) Width
- func (Array2D[T]) ToSlices
- func (Array2D[T]) ToSlicesByCol
- func (*Array2D[T]) Rows
- func (*Rows[T]) Index
- func (*Array2D[T]) Cols
- func (*Cols[T]) Index
- func Map
- License
Array2D is a 2-dimensional array.
type Array2D[T any] struct {
// contains filtered or unexported fields
}func New[T any](height, width int, colMajor ...bool) Array2D[T]New initializes a 2-dimensional array with all zero values.
By default, it creates a row-major array.
To create a column-major array, pass true as the optional colMajor argument.
func NewFilled[T any](height, width int, value T, colMajor ...bool) Array2D[T]NewFilled initializes a 2-dimensional array with a value.
By default, it creates a row-major array.
To create a column-major array, pass true as the optional colMajor argument.
func FromSlice[T any](height, width int, slice []T, colMajor ...bool) (Array2D[T], error)FromSlice creates a 2-dimensional array from the given slice. The length of the slice must be equal to height * width.
Note: This function does not create a copy of the provided slice. Modifications to the original slice will affect the new Array2D instance.
By default, it creates a row-major array.
To create a column-major array, pass true as the optional colMajor argument.
func FromJagged[J ~[]S, S ~[]E, E any](height, width int, jagged J, colMajor ...bool) (Array2D[E], error)FromJagged creates a 2-dimensional array from a jagged slice. It returns an error if the dimensions of the jagged slice exceed the specified height or width.
By default, it creates a row-major array.
To create a column-major array, pass true as the optional colMajor argument.
func (a Array2D[T]) Row(row int) ([]T, bool)Row returns a slice for an entire row.
- For row-major arrays, this function returns a mutable slice. Changing values in this slice will affect the array.
- For column-major arrays, this function returns a new slice containing a copy of the data, so modifications to it will not affect the original array.
- It returns
falseif the row index is out of bounds.
func (a Array2D[T]) Col(col int) ([]T, bool)Col returns a slice for an entire column.
- For column-major arrays, this function returns a mutable slice. Changing values in this slice will affect the array.
- For row-major arrays, this function returns a new slice containing a copy of the data, so modifications to it will not affect the original array.
- It returns
falseif the column index is out of bounds.
func (a Array2D[T]) Fill(row1, col1, row2, col2 int, value T) errorFill will assign all values inside the region to the specified value. The coordinates are inclusive, meaning all values from [row1,col1] including [row1,col1] to [row2,col2] including [row2,col2] are set.
It returns an error if any of the coordinates are out of bounds.
func (a Array2D[T]) Get(row, col int) (T, bool)Get returns a value from the array.
It returns the zero value for T and false if the access is out-of-bounds.
func (a Array2D[T]) Set(row, col int, value T) errorSet sets a value in the array.
It returns an error on out-of-bounds access.
func (a Array2D[T]) Copy() Array2D[T]Copy returns a shallow copy of this array.
func (a Array2D[T]) String() stringString returns a string representation of this array.
func (a Array2D[T]) Height() intHeight returns the height of this array. The maximum y value is Height()-1.
func (a Array2D[T]) Width() intWidth returns the width of this array. The maximum x value is Width()-1.
func (a Array2D[T]) ToSlices() [][]TToSlices returns a slice of slices representation of the array, organized by rows.
- For row-major arrays, this is a zero-copy operation (sub-slices of the underlying array).
- For column-major arrays, this returns copies of each row (modifying the result does not affect the original array).
func (a Array2D[T]) ToSlicesByCol() [][]TToSlicesByCol returns a slice of slices representation of the array, organized by columns.
- For column-major arrays, this is a zero-copy operation (sub-slices of the underlying array).
- For row-major arrays, this returns copies of each column (modifying the result does not affect the original array).
func (a *Array2D[T]) Rows() *Rows[T]Rows returns an iterator over the rows of the array, similar to sql.Rows.
Example:
rows := arr.Rows()
rowData := make([]int, arr.Width())
for rows.Next() {
if err := rows.Scan(&rowData); err != nil {
// handle error
}
// use rowData
}func (r *Rows[T]) Index() intIndex returns the current row index. It returns -1 if Next has not been called yet.
func (a *Array2D[T]) Cols() *Cols[T]Cols returns an iterator over the columns of the array, similar to sql.Rows.
Example:
cols := arr.Cols()
colData := make([]int, arr.Height())
for cols.Next() {
if err := cols.Scan(&colData); err != nil {
// handle error
}
// use colData
}func (c *Cols[T]) Index() intIndex returns the current column index. It returns -1 if Next has not been called yet.
func Map[T any, U any](a Array2D[T], f func(v T) U) Array2D[U]Map creates a new Array2D by applying a function to each element of the input array.
The new array will have the same dimensions and memory layout (row/column-major) as the original.
The mapping function f is applied to each element of type T to produce an element of type U.
Example:
arr := array2d.NewFilled[int](2, 3, 1)
mapped := array2d.Map(arr, func(v int) string {
return fmt.Sprintf("val:%d", v)
})
fmt.Println(mapped)This project is licensed under the MIT License - see the LICENSE file for details.
Copyright (c) 2025 xll-gen
Copyright (c) 2021-2024 fufuok