Initial commit

This commit is contained in:
Ian Gulliver
2023-12-24 17:57:00 -05:00
parent 5b04b9130d
commit cb331a4ed5
8 changed files with 5508 additions and 0 deletions

21
encode.go Normal file
View File

@@ -0,0 +1,21 @@
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()
}