Files
lattigo/utils/structs/matrix.go
2023-10-05 09:18:08 +02:00

167 lines
4.3 KiB
Go

package structs
import (
"bufio"
"fmt"
"io"
"github.com/tuneinsight/lattigo/v4/utils/buffer"
)
// Matrix is a struct storing a vector of Vector.
type Matrix[T any] [][]T
func (m Matrix[T]) CopyNew() *Matrix[T] {
if c, isCopiable := any(new(T)).(CopyNewer[T]); !isCopiable {
panic(fmt.Errorf("matrix component of type %T does not comply to %T", new(T), c))
}
mcpy := Matrix[T](make([][]T, len(m)))
for i := range m {
mcpy[i] = make([]T, len(m[i]))
for j := range m[i] {
mcpy[i][j] = *any(&m[i][j]).(CopyNewer[T]).CopyNew()
}
}
return &mcpy
}
// BinarySize returns the serialized size of the object in bytes.
func (m Matrix[T]) BinarySize() (size int) {
if _, isSizable := any(new(T)).(BinarySizer); !isSizable {
panic(fmt.Errorf("matrix component of type %T does not comply to %T", new(T), new(BinarySizer)))
}
size += 8
for _, v := range m {
/* #nosec G601 -- Implicit memory aliasing in for loop acknowledged */
size += (*Vector[T])(&v).BinarySize()
}
return
}
// WriteTo writes the object on an io.Writer. It implements the io.WriterTo
// interface, and will write exactly object.BinarySize() bytes on w.
//
// Unless w implements the buffer.Writer interface (see lattigo/utils/buffer/writer.go),
// it will be wrapped into a bufio.Writer. Since this requires allocations, it
// is preferable to pass a buffer.Writer directly:
//
// - When writing multiple times to a io.Writer, it is preferable to first wrap the
// io.Writer in a pre-allocated bufio.Writer.
// - When writing to a pre-allocated var b []byte, it is preferable to pass
// buffer.NewBuffer(b) as w (see lattigo/utils/buffer/buffer.go).
func (m Matrix[T]) WriteTo(w io.Writer) (n int64, err error) {
if _, isWritable := any(new(T)).(io.WriterTo); !isWritable {
return 0, fmt.Errorf("vector component of type %T does not comply to %T", new(T), new(io.WriterTo))
}
switch w := w.(type) {
case buffer.Writer:
var inc int64
if inc, err = buffer.WriteAsUint64[int](w, len(m)); err != nil {
return inc, err
}
n += inc
for _, v := range m {
vec := Vector[T](v)
if inc, err = vec.WriteTo(w); err != nil {
return n + inc, err
}
n += inc
}
return n, w.Flush()
default:
return m.WriteTo(bufio.NewWriter(w))
}
}
// ReadFrom reads on the object from an io.Writer. It implements the
// io.ReaderFrom interface.
//
// Unless r implements the buffer.Reader interface (see see lattigo/utils/buffer/reader.go),
// it will be wrapped into a bufio.Reader. Since this requires allocation, it
// is preferable to pass a buffer.Reader directly:
//
// - When reading multiple values from a io.Reader, it is preferable to first
// first wrap io.Reader in a pre-allocated bufio.Reader.
// - When reading from a var b []byte, it is preferable to pass a buffer.NewBuffer(b)
// as w (see lattigo/utils/buffer/buffer.go).
func (m *Matrix[T]) ReadFrom(r io.Reader) (n int64, err error) {
if _, isReadable := any(new(T)).(io.ReaderFrom); !isReadable {
return 0, fmt.Errorf("vector component of type %T does not comply to %T", new(T), new(io.ReaderFrom))
}
switch r := r.(type) {
case buffer.Reader:
var size int
var inc int64
if n, err = buffer.ReadAsUint64[int](r, &size); err != nil {
return int64(n), fmt.Errorf("cannot read matrix size: %w", err)
}
if cap(*m) < size {
*m = make([][]T, size)
}
*m = (*m)[:size]
for i := range *m {
if inc, err = (*Vector[T])(&(*m)[i]).ReadFrom(r); err != nil {
return n + inc, err
}
n += inc
}
return n, nil
default:
return m.ReadFrom(bufio.NewReader(r))
}
}
// MarshalBinary encodes the object into a binary form on a newly allocated slice of bytes.
func (m Matrix[T]) MarshalBinary() (p []byte, err error) {
buf := buffer.NewBufferSize(m.BinarySize())
_, err = m.WriteTo(buf)
return buf.Bytes(), err
}
// UnmarshalBinary decodes a slice of bytes generated by
// MarshalBinary or WriteTo on the object.
func (m *Matrix[T]) UnmarshalBinary(p []byte) (err error) {
_, err = m.ReadFrom(buffer.NewBuffer(p))
return
}
func (m Matrix[T]) Equal(other Matrix[T]) bool {
if _, isEquatable := any(new(T)).(Equatable[T]); !isEquatable {
panic(fmt.Errorf("matrix component of type %T does not comply to %T", new(T), new(Equatable[T])))
}
isEqual := true
for i := range m {
for j := range m[i] {
isEqual = isEqual && any(&m[i][j]).(Equatable[T]).Equal(&other[i][j])
}
}
return isEqual
}