Skip to content

Commit b4f896b

Browse files
committed
CalculateCompressionRatio
1 parent f56e86e commit b4f896b

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

AlgorithmsLibrary/ArithmeticCodingAlgm/ArithmeticCodingAlgm.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,19 @@ public static IAlgmEncoded<string> Decode(Dictionary<char, int> frequencies, str
167167
}
168168
private static double CalculateCompressionRatio(string sourceString, string compressionString)
169169
{
170-
//byte[] BigIArray = System.Numerics.BigInteger.Parse(compressionString).ToByteArray();
171-
//string BinaryCode = string.Concat(BigIArray.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')).Reverse());
172-
//var k = Math.Round(Convert.ToDouble(((decimal)sourceString.Length * 8 / (decimal)BinaryCode.ToString().TrimStart('0').Length).ToString()), 3);
173-
return 0;
170+
var t = compressionString.Length;
171+
if (t > 100000)
172+
return 0.0;
173+
174+
//var log = Math.Ceiling(System.Numerics.BigInteger.Log(System.Numerics.BigInteger.Parse(compressionString), 2));
175+
//var bite = Math.Ceiling(log / 8);
176+
//var k1 = Math.Round(sourceString.Length / bite, 3);
177+
178+
byte[] BigIArray = System.Numerics.BigInteger.Parse(compressionString).ToByteArray();
179+
string BinaryCode = string.Concat(BigIArray.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')).Reverse());
180+
var r = BinaryCode.Length;
181+
var k = Math.Round(Convert.ToDouble(((decimal)sourceString.Length * 8 / (decimal)BinaryCode.ToString().TrimStart('0').Length).ToString()), 3);
182+
return k;
174183
//return Math.Round((double)((sourceString.Length * 8) / Convert.ToString(compressionString.Length, 2).Length), 3);
175184
}
176185
}

0 commit comments

Comments
 (0)