22 lines
369 B
Go
22 lines
369 B
Go
package coding
|
|
|
|
import (
|
|
"bytes"
|
|
"github.com/icza/bitio"
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
func Encode(h *Heap, msg []byte) []byte {
|
|
buf := &bytes.Buffer{}
|
|
w := bitio.NewWriter(buf)
|
|
|
|
for _, b := range msg {
|
|
index := h.IncrementSymbol(b)
|
|
code := codes[index]
|
|
lo.Must0(w.WriteBits(uint64(code.value), uint8(code.bits)))
|
|
}
|
|
|
|
lo.Must0(w.Close())
|
|
return buf.Bytes()
|
|
}
|