forked from Heru-Luin/PHP-Huffman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
48 lines (41 loc) · 1.82 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<style>body {font-family: 'courier new';}</style>
<?php
require __DIR__ . '/vendor/autoload.php';
function printExampleResult($str, $encodedStr, $decodedStr)
{
$strLen = strlen($str);
$encodedStrLen = strlen($encodedStr);
echo 'Original string : '.$str.'<br />'.
'Encoded string : '.$encodedStr.'<br />'.
'Decoded string : '.$decodedStr.'<br />'.
'Original length : '.$strLen.'<br />'.
'Encoded length : '.$encodedStrLen.'<br />'.
'Percentage gain : '.(100 * $encodedStrLen / $strLen).'%<br /><br />';
}
// Classic use
$huffman = new App\Huffman();
$exampleString = '0123456789';
$encodedString = $huffman->encode($exampleString);
$decodedString = $huffman->decode($encodedString);
printExampleResult($exampleString, $encodedString, $decodedString);
// Generating a dictionnary
$huffman = new App\Huffman();
$huffman->generateDictionary('0123456789abcdef');
$dictionary = $huffman->getDictionary();
$exampleString = '4009814546017120030654ab480184190804298b01980908bb098981f989182804082040498249d840298e42984984290842984298042d980d49824928e402984f0984298429849082498498a98429802c498b42098';
$encodedString = $huffman->encode($exampleString);
$decodedString = $huffman->decode($encodedString);
printExampleResult($exampleString, $encodedString, $decodedString);
// Using a dictionnary
$huffman = new App\Huffman();
$huffman->setDictionary($dictionary);
$exampleString = md5(rand());
$encodedString = $huffman->encode($exampleString);
$decodedString = $huffman->decode($encodedString);
printExampleResult($exampleString, $encodedString, $decodedString);
// Classic use
$huffman = new App\Huffman();
$exampleString = sha1(rand());
$encodedString = $huffman->encode($exampleString);
$decodedString = $huffman->decode($encodedString);
printExampleResult($exampleString, $encodedString, $decodedString);