-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
165 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,112 @@ | ||
using D2SLib.IO; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace D2SLib.Model.Huffman | ||
namespace D2SLib.Model.Huffman; | ||
|
||
//hardcoded.... | ||
public class HuffmanTree | ||
{ | ||
//hardcoded.... | ||
public class HuffmanTree | ||
{ | ||
public Node Root { get; set; } | ||
public Node? Root { get; set; } | ||
|
||
public static Dictionary<char, string> TABLE = new Dictionary<char, string> | ||
{ | ||
{'0', "11111011"}, | ||
{' ', "10"}, | ||
{'1', "1111100"}, | ||
{'2', "001100"}, | ||
{'3', "1101101"}, | ||
{'4', "11111010"}, | ||
{'5', "00010110"}, | ||
{'6', "1101111"}, | ||
{'7', "01111"}, | ||
{'8', "000100"}, | ||
{'9', "01110"}, | ||
{'a', "11110"}, | ||
{'b', "0101"}, | ||
{'c', "01000"}, | ||
{'d', "110001"}, | ||
{'e', "110000"}, | ||
{'f', "010011"}, | ||
{'g', "11010"}, | ||
{'h', "00011"}, | ||
{'i', "1111110"}, | ||
{'j', "000101110"}, | ||
{'k', "010010"}, | ||
{'l', "11101"}, | ||
{'m', "01101"}, | ||
{'n', "001101"}, | ||
{'o', "1111111"}, | ||
{'p', "11001"}, | ||
{'q', "11011001"}, | ||
{'r', "11100"}, | ||
{'s', "0010"}, | ||
{'t', "01100"}, | ||
{'u', "00001"}, | ||
{'v', "1101110"}, | ||
{'w', "00000"}, | ||
{'x', "00111"}, | ||
{'y', "0001010"}, | ||
{'z', "11011000"} | ||
}; | ||
public static readonly IReadOnlyDictionary<char, string> TABLE = new Dictionary<char, string>(38) | ||
{ | ||
{'0', "11111011"}, | ||
{' ', "10"}, | ||
{'1', "1111100"}, | ||
{'2', "001100"}, | ||
{'3', "1101101"}, | ||
{'4', "11111010"}, | ||
{'5', "00010110"}, | ||
{'6', "1101111"}, | ||
{'7', "01111"}, | ||
{'8', "000100"}, | ||
{'9', "01110"}, | ||
{'a', "11110"}, | ||
{'b', "0101"}, | ||
{'c', "01000"}, | ||
{'d', "110001"}, | ||
{'e', "110000"}, | ||
{'f', "010011"}, | ||
{'g', "11010"}, | ||
{'h', "00011"}, | ||
{'i', "1111110"}, | ||
{'j', "000101110"}, | ||
{'k', "010010"}, | ||
{'l', "11101"}, | ||
{'m', "01101"}, | ||
{'n', "001101"}, | ||
{'o', "1111111"}, | ||
{'p', "11001"}, | ||
{'q', "11011001"}, | ||
{'r', "11100"}, | ||
{'s', "0010"}, | ||
{'t', "01100"}, | ||
{'u', "00001"}, | ||
{'v', "1101110"}, | ||
{'w', "00000"}, | ||
{'x', "00111"}, | ||
{'y', "0001010"}, | ||
{'z', "11011000"} | ||
}; | ||
|
||
//todo find a way to build this like d2? | ||
public void Build(List<string> items) | ||
//todo find a way to build this like d2? | ||
public void Build(List<string> items) | ||
{ | ||
Root = new Node(); | ||
foreach (var entry in TABLE) | ||
{ | ||
Root = new Node(); | ||
foreach(KeyValuePair<char, string> entry in TABLE) | ||
var current = Root; | ||
foreach (char bit in entry.Value) | ||
{ | ||
var Current = Root; | ||
foreach(char bit in entry.Value) | ||
if (bit == '1') | ||
{ | ||
if(bit == '1') | ||
if (current.Right == null) | ||
{ | ||
if(Current.Right == null) | ||
{ | ||
Current.Right = new Node(); | ||
} | ||
Current = Current.Right; | ||
} else if(bit == '0') | ||
current.Right = new Node(); | ||
} | ||
current = current.Right; | ||
} | ||
else if (bit == '0') | ||
{ | ||
if (current.Left == null) | ||
{ | ||
if (Current.Left == null) | ||
{ | ||
Current.Left = new Node(); | ||
} | ||
Current = Current.Left; | ||
current.Left = new Node(); | ||
} | ||
current = current.Left; | ||
} | ||
Current.Symbol = entry.Key; | ||
} | ||
current.Symbol = entry.Key; | ||
} | ||
} | ||
|
||
public BitArray EncodeChar(char source) | ||
{ | ||
List<bool> encodedSymbol = this.Root.Traverse(source, new List<bool>()); | ||
return new BitArray(encodedSymbol.ToArray()); | ||
} | ||
public BitArray EncodeChar(char source) | ||
{ | ||
var encodedSymbol = Root?.Traverse(source, new List<bool>()); | ||
if (encodedSymbol is null) | ||
throw new InvalidOperationException("Could not encode with an empty tree."); | ||
return new BitArray(encodedSymbol.ToArray()); | ||
} | ||
|
||
public char DecodeChar(IBitReader reader) | ||
public char DecodeChar(IBitReader reader) | ||
{ | ||
var current = Root; | ||
while (!(current?.IsLeaf() ?? true)) | ||
{ | ||
Node current = this.Root; | ||
while(!current.IsLeaf()) | ||
if (reader.ReadBit()) | ||
{ | ||
if (reader.ReadBit()) | ||
if (current.Right is not null) | ||
{ | ||
if (current.Right != null) | ||
{ | ||
current = current.Right; | ||
} | ||
current = current.Right; | ||
} | ||
else | ||
} | ||
else | ||
{ | ||
if (current.Left is not null) | ||
{ | ||
if (current.Left != null) | ||
{ | ||
current = current.Left; | ||
} | ||
current = current.Left; | ||
} | ||
} | ||
return current.Symbol; | ||
} | ||
return current?.Symbol ?? '\0'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
namespace D2SLib.Model.Huffman; | ||
|
||
namespace D2SLib.Model.Huffman | ||
public class Node | ||
{ | ||
public class Node | ||
{ | ||
public char Symbol { get; set; } | ||
public int Frequency { get; set; } | ||
public Node Right { get; set; } | ||
public Node Left { get; set; } | ||
public char Symbol { get; set; } | ||
public int Frequency { get; set; } | ||
public Node? Right { get; set; } | ||
public Node? Left { get; set; } | ||
|
||
public List<bool> Traverse(char symbol, List<bool> data) | ||
public List<bool>? Traverse(char symbol, List<bool> data) | ||
{ | ||
// Leaf | ||
if (Right == null && Left == null) | ||
{ | ||
// Leaf | ||
if (Right == null && Left == null) | ||
if (symbol.Equals(Symbol)) | ||
{ | ||
if (symbol.Equals(this.Symbol)) | ||
{ | ||
return data; | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
return data; | ||
} | ||
else | ||
{ | ||
List<bool> left = null; | ||
List<bool> right = null; | ||
|
||
if (Left != null) | ||
{ | ||
List<bool> leftPath = new List<bool>(); | ||
leftPath.AddRange(data); | ||
leftPath.Add(false); | ||
|
||
left = Left.Traverse(symbol, leftPath); | ||
} | ||
return null; | ||
} | ||
} | ||
else | ||
{ | ||
List<bool>? left = null; | ||
List<bool>? right = null; | ||
|
||
if (Right != null) | ||
{ | ||
List<bool> rightPath = new List<bool>(); | ||
rightPath.AddRange(data); | ||
rightPath.Add(true); | ||
right = Right.Traverse(symbol, rightPath); | ||
} | ||
if (Left is not null) | ||
{ | ||
var leftPath = new List<bool>(data) { false }; | ||
left = Left.Traverse(symbol, leftPath); | ||
} | ||
|
||
if (left != null) | ||
{ | ||
return left; | ||
} | ||
else | ||
{ | ||
return right; | ||
} | ||
if (Right is not null) | ||
{ | ||
var rightPath = new List<bool>(data) { true }; | ||
right = Right.Traverse(symbol, rightPath); | ||
} | ||
} | ||
|
||
public bool IsLeaf() | ||
{ | ||
return (Left == null && Right == null); | ||
if (left != null) | ||
{ | ||
return left; | ||
} | ||
else | ||
{ | ||
return right; | ||
} | ||
} | ||
} | ||
|
||
public bool IsLeaf() => Left is null && Right is null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.