Skip to content

Commit

Permalink
Improved DecodeBinary code
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jul 24, 2024
1 parent b6de02a commit 072cc9c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 3 additions & 2 deletions sparsevec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 072cc9c

Please sign in to comment.