Files
lattigo/utils/structs/map.go
Christian Mouchet 28d498ffc7 improved Vector, Matrix and Map types
Vector and Matrix now use []T and [][]T as underlying types, which makes them more versatile and easy to cast to from slices.
Map still uses map[K]V as map values cannot be addressed and this is anoying to use.
I also got rid of the Codec[T] type as it can be replaced with a 3-liner (i.e., equivalent to calling the Codex and checking the error).
There is also a small change of the OperendQ.Encode method that now uses OperandQ.WriteTo instead of OperandQ.Encode (since it is a bit fasterfor some reason...). This is an experiment and more work on the serialization is needed.
2023-06-07 20:18:13 +02:00

227 lines
5.4 KiB
Go

package structs
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"github.com/tuneinsight/lattigo/v4/utils"
"github.com/tuneinsight/lattigo/v4/utils/buffer"
"golang.org/x/exp/constraints"
)
// Map is a struct storing a map of any value indexed by unsigned integers.
// The size of the map is limited to 2^32.
type Map[K constraints.Integer, T any] map[K]*T
// CopyNew creates a copy of the oject.
func (m Map[K, T]) CopyNew() *Map[K, T] {
if c, isCopiable := any(new(T)).(CopyNewer[T]); !isCopiable {
panic(fmt.Errorf("vector component of type %T does not comply to %T", new(T), c))
}
var mcpy = make(Map[K, T])
for key, val := range m {
mcpy[key] = any(&val).(CopyNewer[T]).CopyNew()
}
return &mcpy
}
// WriteTo writes the object on an io.Writer.
// To ensure optimal efficiency and minimal allocations, the user is encouraged
// to provide a struct implementing the interface buffer.Writer, which defines
// a subset of the method of the bufio.Writer.
// If w is not compliant to the buffer.Writer interface, it will be wrapped in
// a new bufio.Writer.
// For additional information, see lattigo/utils/buffer/writer.go.
func (m *Map[K, T]) WriteTo(w io.Writer) (n int64, err error) {
if w, isWritable := any(new(T)).(io.WriterTo); !isWritable {
return 0, fmt.Errorf("vector component of type %T does not comply to %T", new(T), w)
}
switch w := w.(type) {
case buffer.Writer:
var inc1 int
if inc1, err = buffer.WriteUint32(w, uint32(len(*m))); err != nil {
return n + int64(inc1), err
}
n += int64(inc1)
for _, key := range utils.GetSortedKeys(*m) {
if inc1, err = buffer.WriteUint64(w, uint64(key)); err != nil {
return n + int64(inc1), err
}
n += int64(inc1)
var inc2 int64
val := (*m)[key]
if inc2, err = any(val).(io.WriterTo).WriteTo(w); err != nil {
return n + inc2, err
}
n += inc2
}
return
default:
return m.WriteTo(bufio.NewWriter(w))
}
}
// ReadFrom reads on the object from an io.Writer.
// To ensure optimal efficiency and minimal allocations, the user is encouraged
// to provide a struct implementing the interface buffer.Reader, which defines
// a subset of the method of the bufio.Reader.
// If r is not compliant to the buffer.Reader interface, it will be wrapped in
// a new bufio.Reader.
// For additional information, see lattigo/utils/buffer/reader.go.
func (m *Map[K, T]) ReadFrom(r io.Reader) (n int64, err error) {
if r, isReadable := any(new(T)).(io.ReaderFrom); !isReadable {
return 0, fmt.Errorf("vector component of type %T does not comply to %T", new(T), r)
}
switch r := r.(type) {
case buffer.Reader:
var inc1 int
var size uint32
if inc1, err = buffer.ReadUint32(r, &size); err != nil {
return n + int64(inc1), err
}
n += int64(inc1)
if (*m) == nil {
*m = make(Map[K, T], size)
}
for i := 0; i < int(size); i++ {
var key uint64
if inc1, err = buffer.ReadUint64(r, &key); err != nil {
return n + int64(inc1), err
}
n += int64(inc1)
var val *T = new(T)
var inc2 int64
if inc2, err = any(val).(io.ReaderFrom).ReadFrom(r); err != nil {
return n + inc2, err
}
(*m)[K(key)] = val
n += inc2
}
return
default:
return m.ReadFrom(bufio.NewReader(r))
}
}
// BinarySize returns the size in bytes of the object
// when encoded using Encode.
func (m Map[K, T]) BinarySize() (size int) {
if s, isSizable := any(new(T)).(BinarySizer); !isSizable {
panic(fmt.Errorf("vector component of type %T does not comply to %T", new(T), s))
}
size = 4 // #Ct
for _, v := range m {
size += 8
size += any(v).(BinarySizer).BinarySize()
}
return
}
// Encode encodes the object into a binary form on a preallocated slice of bytes
// and returns the number of bytes written.
func (m *Map[K, T]) Encode(p []byte) (n int, err error) {
if e, isEncodable := any(new(T)).(Encoder); !isEncodable {
panic(fmt.Errorf("vector component of type %T does not comply to %T", new(T), e))
}
if len(p) < m.BinarySize() {
return n, fmt.Errorf("cannot Encode: len(p)=%d < %d", len(p), m.BinarySize())
}
binary.LittleEndian.PutUint32(p, uint32(len(*m)))
n += 4
for _, key := range utils.GetSortedKeys(*m) {
binary.LittleEndian.PutUint64(p[n:], uint64(key))
n += 8
var inc int
val := (*m)[key]
if inc, err = any(val).(Encoder).Encode(p[n:]); err != nil {
return n + inc, err
}
n += inc
}
return
}
// Decode decodes a slice of bytes generated by Encode
// on the object and returns the number of bytes read.
func (m *Map[K, T]) Decode(p []byte) (n int, err error) {
if d, isDecodable := any(new(T)).(Decoder); !isDecodable {
panic(fmt.Errorf("vector component of type %T does not comply to %T", new(T), d))
}
size := int(binary.LittleEndian.Uint32(p[n:]))
n += 4
if (*m) == nil {
*m = make(Map[K, T], size)
}
for i := 0; i < size; i++ {
idx := K(binary.LittleEndian.Uint64(p[n:]))
n += 8
var inc int
var val *T = new(T)
if inc, err = any(val).(Decoder).Decode(p[n:]); err != nil {
return n + inc, err
}
(*m)[idx] = val
n += inc
}
return
}
// MarshalBinary encodes the object into a binary form on a newly allocated slice of bytes.
func (m *Map[K, T]) MarshalBinary() (p []byte, err error) {
buf := bytes.NewBuffer([]byte{})
_, err = m.WriteTo(buf)
return buf.Bytes(), err
}
// UnmarshalBinary decodes a slice of bytes generated by
// MarshalBinary or WriteTo on the object.
func (m *Map[K, T]) UnmarshalBinary(p []byte) (err error) {
_, err = m.ReadFrom(bytes.NewBuffer(p))
return
}