1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
package tensor
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"github.com/pkg/errors"
)
func (t *CS) GobEncode() (p []byte, err error) {
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
if err = encoder.Encode(t.s); err != nil {
return
}
if err = encoder.Encode(t.o); err != nil {
return
}
if err = encoder.Encode(t.indices); err != nil {
return
}
if err = encoder.Encode(t.indptr); err != nil {
return
}
data := t.Data()
if err = encoder.Encode(&data); err != nil {
return
}
return buf.Bytes(), nil
}
func (t *CS) GobDecode(p []byte) (err error) {
buf := bytes.NewBuffer(p)
decoder := gob.NewDecoder(buf)
var shape Shape
if err = decoder.Decode(&shape); err != nil {
return
}
t.s = shape
var o DataOrder
if err = decoder.Decode(&o); err != nil {
return
}
var indices []int
if err = decoder.Decode(&indices); err != nil {
return
}
t.indices = indices
var indptr []int
if err = decoder.Decode(&indptr); err != nil {
return
}
t.indptr = indptr
var data interface{}
if err = decoder.Decode(&data); err != nil {
return
}
t.array = arrayFromSlice(data)
return nil
}
func (t *CS) WriteNpy(w io.Writer) error { return errors.Errorf("Cannot write to Npy") }
func (t *CS) ReadNpy(r io.Reader) error { return errors.Errorf("Cannot read from npy") }
func (t *CS) Format(s fmt.State, c rune) {}
func (t *CS) String() string { return "CS" }
|