gorgonia.org/tensor@v0.9.24/sparse_io.go (about)

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