wah getting fucked up on endian business
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
namespace fxl.codes.kisekae.data.Archives.Algorithms;
|
||||
|
||||
public class HuffmanDynamic : IAlgorithm
|
||||
{
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
namespace fxl.codes.kisekae.data.Archives.Algorithms;
|
||||
|
||||
public class HuffmanNode
|
||||
{
|
||||
public HuffmanNode(char value)
|
||||
{
|
||||
}
|
||||
|
||||
public HuffmanNode(HuffmanNode left, HuffmanNode right)
|
||||
{
|
||||
Left = left;
|
||||
Left.Side = HuffmanNodeSide.Left;
|
||||
Left.Parent = this;
|
||||
|
||||
Right = right;
|
||||
Right.Side = HuffmanNodeSide.Right;
|
||||
Right.Parent = this;
|
||||
|
||||
Weight = left.Weight + right.Weight;
|
||||
}
|
||||
|
||||
public char? Value { get; set; }
|
||||
public int Weight { get; set; } = 1;
|
||||
|
||||
public HuffmanNode? Parent { get; set; }
|
||||
public HuffmanNode? Left { get; set; }
|
||||
public HuffmanNode? Right { get; set; }
|
||||
public HuffmanNodeSide? Side { get; set; }
|
||||
}
|
||||
|
||||
public enum HuffmanNodeSide
|
||||
{
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
namespace fxl.codes.kisekae.data.Archives.Algorithms;
|
||||
|
||||
public class HuffmanStatic : IAlgorithm
|
||||
{
|
||||
}
|
||||
@@ -2,4 +2,6 @@ namespace fxl.codes.kisekae.data.Archives.Algorithms;
|
||||
|
||||
public interface IAlgorithm
|
||||
{
|
||||
byte[] Encode(ReadOnlySpan<byte> stream);
|
||||
byte[] Decode(ReadOnlySpan<byte> stream);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System.Text;
|
||||
|
||||
namespace fxl.codes.kisekae.data.Archives.Algorithms;
|
||||
|
||||
internal class LZW : IAlgorithm
|
||||
{
|
||||
private readonly int Max;
|
||||
|
||||
internal LZW(int max = short.MaxValue)
|
||||
{
|
||||
Max = max;
|
||||
}
|
||||
|
||||
public byte[] Decode(ReadOnlySpan<byte> stream)
|
||||
{
|
||||
uint next = byte.MaxValue + 1;
|
||||
var dictionary = new Dictionary<uint, string>();
|
||||
for (uint i = 0; i <= byte.MaxValue; i++) dictionary.Add(i, $"{(char)i}");
|
||||
|
||||
var buffer = "";
|
||||
var decoded = new List<string>();
|
||||
for (var i = 0; i < stream.Length; i += 4)
|
||||
{
|
||||
var code = BitConverter.ToUInt32(stream[i..(i + 4)]);
|
||||
if (!dictionary.ContainsKey(code)) dictionary[code] = buffer + buffer[0];
|
||||
|
||||
decoded.Add(dictionary[code]);
|
||||
if (buffer.Length > 0 && next <= Max) dictionary[next++] = buffer + dictionary[code][0];
|
||||
|
||||
buffer = dictionary[code];
|
||||
}
|
||||
|
||||
return decoded.SelectMany(Encoding.UTF8.GetBytes).ToArray();
|
||||
}
|
||||
|
||||
public byte[] Encode(ReadOnlySpan<byte> stream)
|
||||
{
|
||||
var next = byte.MaxValue + 1;
|
||||
var dictionary = new Dictionary<string, int>();
|
||||
for (var i = 0; i <= byte.MaxValue; i++) dictionary.Add($"{(char)i}", i);
|
||||
|
||||
var buffer = "";
|
||||
var encoded = new List<int>();
|
||||
foreach (var part in stream)
|
||||
{
|
||||
var charValue = (char)part;
|
||||
buffer += charValue;
|
||||
if (dictionary.ContainsKey(buffer)) continue;
|
||||
if (next <= Max) dictionary.Add(buffer, next++);
|
||||
encoded.Add(dictionary[$"{buffer[..^1]}"]);
|
||||
buffer = $"{charValue}";
|
||||
}
|
||||
|
||||
encoded.Add(dictionary[buffer]);
|
||||
return encoded.SelectMany(BitConverter.GetBytes).ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user