Arithmetic coding/As a generalized change of radix: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 17:
{{trans|Python}}
 
<syntaxhighlight lang="11l">F cumulative_freq(freq)
[Int = Int] cf
V total = 0
Line 112:
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 249:
=={{header|D}}==
{{trans|Go}}
<syntaxhighlight lang=D"d">import std.array;
import std.bigint;
import std.stdio;
Line 408:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 581:
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class ArithmeticCoding {
private static class Triple<A, B, C> {
A a
Line 727:
Implementation:
 
<syntaxhighlight lang=J"j">NB. generate a frequency dictionary from a reference string
aekDict=:verb define
d=. ~.y NB. dictionary lists unique characters
Line 782:
Example use:
 
<syntaxhighlight lang=J"j"> aek 'DABDDB'
Dictionary:
A 1r6
Line 839:
=={{header|Java}}==
{{trans|Kotlin}}
<syntaxhighlight lang=Java"java">import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
Line 985:
=={{header|JavaScript}}==
{{trans|C#}}
<syntaxhighlight lang=JavaScript"javascript">
function CumulativeFreq(freq) {
let total = 0;
Line 1,081:
=={{header|Julia}}==
Taken from the wikipedia example.
<syntaxhighlight lang="julia">function charfreq(s)
d = Dict()
for c in s
Line 1,172:
=={{header|Kotlin}}==
{{trans|Go}}
<syntaxhighlight lang="scala">// version 1.2.10
 
import java.math.BigInteger
Line 1,311:
{{trans|Kotlin}}
{{libheader|bignum}}
<syntaxhighlight lang=Nim"nim">import strformat, sugar, tables
import bignum
 
Line 1,394:
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">use Math::BigInt (try => 'GMP');
 
sub cumulative_freq {
Line 1,516:
{{trans|Kotlin}}
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,632:
=={{header|Python}}==
{{works with|Python|3.1+}}
<syntaxhighlight lang="python">from collections import Counter
 
def cumulative_freq(freq):
Line 1,740:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang=perl6"raku" line>sub cumulative_freq(%freq) {
my %cf;
my $total = 0;
Line 1,860:
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def cumulative_freq(freq)
cf = {}
total = 0
Line 1,979:
=={{header|Scala}}==
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/EUNJ0zp/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/DVl840oDS2mFvFQ560fxJAScastie (remote JVM)].
<syntaxhighlight lang=Scala"scala">object ArithmeticCoding extends App {
val (radix, strings) = (10, Seq("DABDDB", "DABDDBBDDBA", "ABRACADABRA", "TOBEORNOTTOBEORTOBEORNOT"))
val fmt = "%-25s=> %19s * %d^%s"
Line 2,057:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func cumulative_freq(freq) {
var cf = Hash()
var total = 0
Line 2,173:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Numerics
Imports System.Text
Imports Freq = System.Collections.Generic.Dictionary(Of Char, Long)
Line 2,310:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/big" for BigInt
import "/fmt" for Fmt
 
Line 2,441:
=={{header|zkl}}==
Uses libGMP (GNU MP Bignum Library)
<syntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"); // libGMP
 
fcn cumulativeFreq(freqHash){
Line 2,474:
return(enc,powr,freqHash);
}</syntaxhighlight>
<syntaxhighlight lang="zkl">fcn arithmethicDecoding(enc, radix, powr, freqHash){
enc*=radix.pow(powr);
base:=freqHash.values.sum(0);
Line 2,498:
decoded.text // Return the decoded output
}</syntaxhighlight>
<syntaxhighlight lang="zkl">radix:=10;
testStrings:=T(
"DABDDB",
10,333

edits