Skip to content

Commit

Permalink
feat: added seralization
Browse files Browse the repository at this point in the history
  • Loading branch information
AbsoluteZero000 committed Oct 5, 2024
1 parent 7e5e5b0 commit a34ae9e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
35 changes: 35 additions & 0 deletions huffman/huffman.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package huffman

import (
"bytes"
"encoding/gob"
"fmt"
)

Expand Down Expand Up @@ -50,3 +52,36 @@ func Decode(root *Node, encodedData []byte, bitLength int, verbose bool) string
}
return decodedData
}

func SerializeTree(root *Node) ([]byte, error) {
if root == nil {
return nil, nil
}

var buf bytes.Buffer
enc := gob.NewEncoder(&buf)

err := enc.Encode(root)
if err != nil {
return nil, err
}

return buf.Bytes(), nil
}

func DeserializeTree(data []byte) (*Node, error) {
if data == nil {
return nil, nil
}

buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)

var root Node
err := dec.Decode(&root)
if err != nil {
return nil, err
}

return &root, nil
}
34 changes: 34 additions & 0 deletions huffman/huffman_util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package huffman

import (
"bytes"
"container/heap"
"math"
)

type Node struct {
Expand Down Expand Up @@ -114,3 +116,35 @@ func encodeString(s string, codes map[rune]string) string {
}
return encoded
}

func bitStringToBytes(s string) []byte {
numBytes := int(math.Ceil(float64(len(s)) / 8.0))
result := make([]byte, numBytes)

for i := 0; i < len(s); i++ {
if s[i] == '1' {
byteIndex := i / 8
bitIndex := 7 - (i % 8)
result[byteIndex] |= 1 << bitIndex
}
}

return result
}

func bytesToBitString(data []byte, bitLength int) string {
var result bytes.Buffer

for i := 0; i < bitLength; i++ {
byteIndex := i / 8
bitIndex := 7 - (i % 8)

if data[byteIndex]&(1<<bitIndex) != 0 {
result.WriteByte('1')
} else {
result.WriteByte('0')
}
}

return result.String()
}
38 changes: 0 additions & 38 deletions huffman/util.go

This file was deleted.

9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"github.com/absolutezero000/encoding/huffman"
"log"
"os"
Expand All @@ -11,13 +12,13 @@ func main() {
inputFile := flag.String("i", "", "Input file name")
outputFile := flag.String("o", "", "Output file name")
verbose := flag.Bool("v", false, "Enable verbose mode")

flag.Parse()

data, err := os.ReadFile(*inputFile)
if err != nil {
log.Fatalf("Failed to read file %s: %v", *inputFile, err)
}

var encodedData []byte
var bitLength int
HuffmanTreeRoot := huffman.MakeTree(string(data))
Expand All @@ -31,4 +32,10 @@ func main() {
}
}

treeData, _ := huffman.SerializeTree(HuffmanTreeRoot)

fmt.Println(treeData)

treeRoot, _ := huffman.DeserializeTree(treeData)
_ = huffman.Decode(treeRoot, encodedData, bitLength, *verbose)
}

0 comments on commit a34ae9e

Please sign in to comment.