Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Levenshtein coding

From Wikipedia, the free encyclopedia
Universal coding

Levenshtein coding is auniversal code encoding the non-negative integers developed byVladimir Levenshtein.[1][2]

Encoding

[edit]

The code ofzero is "0"; to code apositive number:

  1. Initialize the step count variableC to 1.
  2. Write thebinary representation of the number without the leading "1" to the beginning of the code.
  3. LetM be the number of bits written in step 2.
  4. IfM is not 0, incrementC, repeat from step 2 with M as the new number.
  5. WriteC "1" bits and a "0" to the beginning of the code.

The code begins:

NumberEncodingImplied probability
001/2
1101/4
2110 01/16
3110 11/16
41110 0 001/128
51110 0 011/128
61110 0 101/128
71110 0 111/128
81110 1 0001/256
91110 1 0011/256
101110 1 0101/256
111110 1 0111/256
121110 1 1001/256
131110 1 1011/256
141110 1 1101/256
151110 1 1111/256
1611110 0 00 00001/4096
1711110 0 00 00011/4096

To decode a Levenshtein-coded integer:

  1. Count the number of "1" bits until a "0" is encountered.
  2. If the count is zero, the value is zero, otherwise
  3. Discard the "1" bits just counted and the first "0" encountered
  4. Start with a variableN, set it to a value of 1 and repeatcount minus 1 times:
  5. ReadN bits (and remove them from the encoded integer), prepend "1", assign the resulting value toN

The Levenshtein code of a positive integer is always one bit longer than theElias omega code of that integer. However, there is a Levenshtein code for zero, whereas Elias omega coding would require the numbers to be shifted so that a zero is represented by the code for one instead.

Example code

[edit]

Encoding

[edit]
voidlevenshteinEncode(char*source,char*dest){IntReaderintreader(source);BitWriterbitwriter(dest);while(intreader.hasLeft()){intnum=intreader.getInt();if(num==0)bitwriter.outputBit(0);else{intc=0;BitStackbits;do{intm=0;for(inttemp=num;temp>1;temp>>=1)// calculate floor(log2(num))++m;for(inti=0;i<m;++i)bits.pushBit((num>>i)&1);num=m;++c;}while(num>0);for(inti=0;i<c;++i)bitwriter.outputBit(1);bitwriter.outputBit(0);while(bits.length()>0)bitwriter.outputBit(bits.popBit());}}}

Decoding

[edit]
voidlevenshteinDecode(char*source,char*dest){BitReaderbitreader(source);IntWriterintwriter(dest);while(bitreader.hasLeft()){intn=0;while(bitreader.inputBit())// potentially dangerous with malformed files.++n;intnum;if(n==0)num=0;else{num=1;for(inti=0;i<n-1;++i){intval=1;for(intj=0;j<num;++j)val=(val<<1)|bitreader.inputBit();num=val;}}intwriter.putInt(num);// write out the value}bitreader.close();intwriter.close();}

See also

[edit]

References

[edit]
  1. ^"1968 paper by V. I. Levenshtein (in Russian)"(PDF).
  2. ^David Salomon (2007).Variable-length codes for data compression. Springer. p. 80.ISBN 978-1-84628-958-3.
Lossless
type
Entropy
Dictionary
Other
Hybrid
Lossy
type
Transform
Predictive
Audio
Concepts
Codec
parts
Image
Concepts
Methods
Video
Concepts
Codec
parts
Theory
Community
People
Retrieved from "https://en.wikipedia.org/w/index.php?title=Levenshtein_coding&oldid=1261421333"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp