From 072cc9ca134df43be680d1fa6bbaa6cc9c8a0c17 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 23 Jul 2024 19:42:55 -0700 Subject: [PATCH] Improved DecodeBinary code --- sparsevec.go | 5 +++-- vector.go | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/sparsevec.go b/sparsevec.go index f390e48..9b86e52 100644 --- a/sparsevec.go +++ b/sparsevec.go @@ -153,15 +153,16 @@ func (v *SparseVector) DecodeBinary(buf []byte) error { v.dim = int32(dim) v.indices = make([]int32, 0, nnz) v.values = make([]float32, 0, nnz) + offset := 12 for i := 0; i < nnz; i++ { - offset := 12 + 4*i v.indices = append(v.indices, int32(binary.BigEndian.Uint32(buf[offset:offset+4]))) + offset += 4 } for i := 0; i < nnz; i++ { - offset := 12 + 4*nnz + 4*i v.values = append(v.values, math.Float32frombits(binary.BigEndian.Uint32(buf[offset:offset+4]))) + offset += 4 } return nil diff --git a/vector.go b/vector.go index 44b8d9c..175b793 100644 --- a/vector.go +++ b/vector.go @@ -78,9 +78,10 @@ func (v *Vector) DecodeBinary(buf []byte) error { } v.vec = make([]float32, 0, dim) + offset := 4 for i := 0; i < dim; i++ { - offset := 4 + 4*i v.vec = append(v.vec, math.Float32frombits(binary.BigEndian.Uint32(buf[offset:offset+4]))) + offset += 4 } return nil }