lnwire/features: add EncodeBase32 and DecodeBase32 w/ generic helpers
This commit is contained in:
parent
2953f3532a
commit
8c2176fbf8
@ -169,12 +169,25 @@ func (fv *RawFeatureVector) Encode(w io.Writer) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return fv.encode(w, length, 8)
|
||||
}
|
||||
|
||||
// EncodeBase32 writes the feature vector in base32 representation. Every feature
|
||||
// encoded as a bit, and the bit vector is serialized using the least number of
|
||||
// bytes.
|
||||
func (fv *RawFeatureVector) EncodeBase32(w io.Writer) error {
|
||||
length := fv.SerializeSize32()
|
||||
return fv.encode(w, length, 5)
|
||||
}
|
||||
|
||||
// encode writes the feature vector
|
||||
func (fv *RawFeatureVector) encode(w io.Writer, length, width int) error {
|
||||
// Generate the data and write it.
|
||||
data := make([]byte, length)
|
||||
for feature := range fv.features {
|
||||
byteIndex := int(feature / 8)
|
||||
bitIndex := feature % 8
|
||||
data[length-byteIndex-1] |= 1 << bitIndex
|
||||
byteIndex := int(feature) / width
|
||||
bitIndex := int(feature) % width
|
||||
data[length-byteIndex-1] |= 1 << uint(bitIndex)
|
||||
}
|
||||
|
||||
_, err := w.Write(data)
|
||||
@ -193,6 +206,19 @@ func (fv *RawFeatureVector) Decode(r io.Reader) error {
|
||||
}
|
||||
length := binary.BigEndian.Uint16(l[:])
|
||||
|
||||
return fv.decode(r, int(length), 8)
|
||||
}
|
||||
|
||||
// DecodeBase32 reads the feature vector from its base32 representation. Every
|
||||
// feature encoded as a bit, and the bit vector is serialized using the least
|
||||
// number of bytes.
|
||||
func (fv *RawFeatureVector) DecodeBase32(r io.Reader, length int) error {
|
||||
return fv.decode(r, length, 5)
|
||||
}
|
||||
|
||||
// decode reads a feature vector from the next length bytes of the io.Reader,
|
||||
// assuming each byte has width feature bits encoded per byte.
|
||||
func (fv *RawFeatureVector) decode(r io.Reader, length, width int) error {
|
||||
// Read the feature vector data.
|
||||
data := make([]byte, length)
|
||||
if _, err := io.ReadFull(r, data); err != nil {
|
||||
@ -200,10 +226,10 @@ func (fv *RawFeatureVector) Decode(r io.Reader) error {
|
||||
}
|
||||
|
||||
// Set feature bits from parsed data.
|
||||
bitsNumber := len(data) * 8
|
||||
bitsNumber := len(data) * width
|
||||
for i := 0; i < bitsNumber; i++ {
|
||||
byteIndex := uint16(i / 8)
|
||||
bitIndex := uint(i % 8)
|
||||
byteIndex := int(i / width)
|
||||
bitIndex := uint(i % width)
|
||||
if (data[length-byteIndex-1]>>bitIndex)&1 == 1 {
|
||||
fv.Set(FeatureBit(i))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user