mirror of
https://github.com/tuneinsight/lattigo.git
synced 2025-09-13 03:27:14 +00:00
removed the Encode and Decode interface
This commit is contained in:
@@ -2,7 +2,6 @@ package structs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
@@ -137,8 +136,7 @@ func (m *Map[K, T]) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// BinarySize returns the size in bytes of the object
|
||||
// when encoded using Encode.
|
||||
// BinarySize returns the serialized size of the object in bytes.
|
||||
func (m Map[K, T]) BinarySize() (size int) {
|
||||
|
||||
if s, isSizable := any(new(T)).(BinarySizer); !isSizable {
|
||||
@@ -168,66 +166,3 @@ func (m *Map[K, T]) UnmarshalBinary(p []byte) (err error) {
|
||||
_, err = m.ReadFrom(buffer.NewBuffer(p))
|
||||
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 = new(T)
|
||||
if inc, err = any(val).(Decoder).Decode(p[n:]); err != nil {
|
||||
return n + inc, err
|
||||
}
|
||||
(*m)[idx] = val
|
||||
n += inc
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user