private void PerformCoding_Click(object sender, RoutedEventArgs e) { this.PerformCoding.IsEnabled = false; this.AnalyzeCharacters.IsEnabled = false; try { this.model = new ArithmeticCodingDataModel { DataToCode = this.PlainText.Text }; foreach (var item in this.ProbalityTable.Children) { if (item is StackPanel) { foreach (var t in ((StackPanel)item).Children) { if (t is TextBox txtbox) { char x = (char)int.Parse(txtbox.Name.Replace("chr_", string.Empty)); this.CheckValue(txtbox); if (Regex.IsMatch(txtbox.Text, this.regexExpression)) { var txt = txtbox.Text.Trim().Replace(" ", string.Empty); BigDouble number = BigDouble.ZERO; if (txt.Contains('.')) { number = new BigDouble(txt); } else if (txt.Contains('/')) { var spltxt = txt.Split('/'); var a = BigInteger.Parse(spltxt[0]); var b = BigInteger.Parse(spltxt[1]); number = new BigDouble(a, b); } this.model.Probabilities.Add(new Pair <char, BigDouble>(x, number)); } } } } } this.CodingStatus.Visibility = Visibility.Visible; this.worker.RunWorkerAsync(); } catch (Exception ex) { MessageBox.Show("Some values are invalid fix them. Additional: " + ex.Message, "ERROR", MessageBoxButton.OK); this.PerformCoding.IsEnabled = true; this.AnalyzeCharacters.IsEnabled = true; } }
public static ArithmeticCodingRangeModel PerformArithmeticCoding(ArithmeticCodingDataModel model) { if (!model.PerformDataValidation()) { throw new FormatException(); } var probs = model.Probabilities.OrderByDescending(x => x.Value).ToArray(); for (int i = 1, len = probs.Length; i < len; i++) { probs[i].Value += probs[i - 1].Value; } BigDouble lowerBound = BigDouble.ZERO; BigDouble upperBound = BigDouble.ONE; for (int i = 0; i < model.DataToCode.Length; i++) { var data = model.DataToCode[i]; for (int j = 0; j < probs.Length; j++) { if (probs[j].Key == data) { var range = upperBound - lowerBound; if (j == 0) { upperBound = lowerBound + (range * probs[j].Value); } else if (j == probs.Length - 1) { lowerBound = lowerBound + (range * probs[j - 1].Value); } else { upperBound = lowerBound + (range * probs[j].Value); lowerBound = lowerBound + (range * probs[j - 1].Value); } break; } } } return(new ArithmeticCodingRangeModel(lowerBound, upperBound)); }
public void BasicTest() { var model = new ArithmeticCodingDataModel { DataToCode = "Multi", Probabilities = new List <Pair <char, BigDouble> > { new Pair <char, BigDouble>('M', new BigDouble(1, 10)), new Pair <char, BigDouble>('u', new BigDouble(3, 10)), new Pair <char, BigDouble>('l', new BigDouble(3, 10)), new Pair <char, BigDouble>('t', new BigDouble(2, 10)), new Pair <char, BigDouble>('i', new BigDouble(1, 10)) } }; var result = ArithmeticCodingService.PerformArithmeticCoding(model); Assert.AreEqual("0.81602", result.LowerBound.ToString()); Assert.AreEqual("0.8162", result.UpperBound.ToString()); }