Skip to content

Commit b6dbcfb

Browse files
Nikolaos Karampinaskeon
Nikolaos Karampinas
authored andcommitted
Added elias gamma and delta data compression codings (#564)
* Added elias gamma and delta data compression algorithms * Added more info about the codings.
1 parent ebf96fa commit b6dbcfb

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ If you want to uninstall algorithms, it is as simple as:
116116
- [compression](algorithms/compression)
117117
- [huffman_coding](algorithms/compression/huffman_coding.py)
118118
- [rle_compression](algorithms/compression/rle_compression.py)
119+
- [elias](algorithms/compression/elias.py)
119120
- [dfs](algorithms/dfs)
120121
- [all_factors](algorithms/dfs/all_factors.py)
121122
- [count_islands](algorithms/dfs/count_islands.py)

algorithms/compression/elias.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Elias γ code or Elias gamma code is a universal code encoding positive integers.
3+
It is used most commonly when coding integers whose upper-bound cannot be determined beforehand.
4+
Elias δ code or Elias delta code is a universal code encoding the positive integers,
5+
that includes Elias γ code when calculating.
6+
Both were developed by Peter Elias.
7+
8+
"""
9+
from math import log,ceil
10+
11+
log2 = lambda x: log(x,2)
12+
13+
# Calculates the binary number
14+
def binary(x,l=1):
15+
fmt = '{0:0%db}' % l
16+
return fmt.format(x)
17+
18+
# Calculates the unary number
19+
def unary(x):
20+
return (x-1)*'1'+'0'
21+
22+
def elias_generic(lencoding, x):
23+
"""
24+
The compressed data is calculated in two parts.
25+
The first part is the unary number of 1 + ⌊log2(x)⌋.
26+
The second part is the binary number of x - 2^(⌊log2(x)⌋).
27+
For the final result we add these two parts.
28+
"""
29+
if x == 0: return '0'
30+
31+
first_part = 1 + int(log2(x))
32+
33+
a = x - 2**(int(log2(x)))
34+
35+
k = int(log2(x))
36+
37+
return lencoding(first_part) + binary(a,k)
38+
39+
def elias_gamma(x):
40+
"""
41+
For the first part we put the unary number of x.
42+
"""
43+
return elias_generic(unary, x)
44+
45+
def elias_delta(x):
46+
"""
47+
For the first part we put the elias_g of the number.
48+
"""
49+
return elias_generic(elias_gamma,x)

tests/test_compression.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from algorithms.compression.huffman_coding import HuffmanCoding
22
from algorithms.compression.rle_compression import (decode_rle, encode_rle)
3+
from algorithms.compression.elias import (elias_gamma, elias_delta)
34

45
import unittest
56

@@ -44,6 +45,25 @@ def test_decode_rle(self):
4445
self.assertEqual('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW',
4546
decode_rle('12W1B12W3B24W1B14W'))
4647

48+
class TestEliasCoding(unittest.TestCase):
49+
50+
def test_elias_gamma(self):
51+
correct_result = ['0', '00', '100', '101', '11000', '11001', '11010', '11011', '1110000', '1110001', '1110010']
52+
53+
result = []
54+
for i in range(11):
55+
result.append(elias_gamma(i))
56+
57+
self.assertEqual(correct_result, result)
58+
59+
def test_elias_delta(self):
60+
correct_result = ['0', '000', '1000', '1001', '10100', '10101', '10110', '10111', '11000000', '11000001', '11000010']
61+
62+
result = []
63+
for i in range(11):
64+
result.append(elias_delta(i))
65+
66+
self.assertEqual(correct_result, result)
4767

4868
if __name__ == "__main__":
4969
unittest.main()

0 commit comments

Comments
 (0)