Example #1
2
                } else {
                    $current = $current->left;
                }
            } else {
                if ($current->left || $current->right) {
                    throw new Exception("Leaf must not have any children.");
                }
                $s .= $current->symbol;
                $current = $tree->root;
            }
        }
        if ($current->left || $current->right) {
            throw new Exception("Leaf expected at end of input.");
        }
        $s .= $current->symbol;
        return $s;
    }
}
$s = join("\n", array("4 Minute Warning - Radiohead", "", "This is just a nightmare", "Soon I'm gonna wake up", "Someone's gonna bring me 'round", "", "Running from the bombers", "Hiding in the forest", "Running through the fields", "Laying flat on the ground", "", "Just like everybody", "Stepping over hills", "Running from the underground", "", "This is your warning", "4 minute warning", "", "I don't wanna hear it", "I don't wanna know", "I just wanna run and hide", "", "This is just a nightmare", "But soon I'm gonna wake up", "Someone's gonna bring me 'round", "", "This is our warning", "4 minute warning"));
$RunHuffman = function () {
    global $s;
    $t = Huffman::buildTree($s);
    $bits = Huffman::encode($s, $t);
    $s2 = Huffman::decode($bits);
    if ($s2 != $s) {
        echo "Expected:\n{$s}\n";
        echo "Actual:\n{$s2}\n";
        throw new Exception("Incorrect result");
    }
};
$HuffmanSuite = new BenchmarkSuite('Huffman', [100000], [new Benchmark('Huffman', true, false, 100000, $RunHuffman)]);
Example #2
0
<?php

require_once 'Huffman.php';
$huffman = new Huffman();
$string = $_REQUEST['string'];
$encodedString = $huffman->encode($string);
$decodedString = $huffman->decode($encodedString);
$output = array('original' => $string, 'encoded' => $encodedString, 'decoded' => $decodedString, 'orLength' => strlen($string), 'encLength' => strlen($encodedString), 'percent' => strlen($encodedString) > 0 ? 100 - 100 * (strlen($encodedString) / strlen($string)) : 0);
echo <<<EOF
<p><strong>Код Хаффмана:</strong><br>{$output['encoded']}</p>
<p><strong>Декодированное сообщение:</strong><br>{$output['decoded']}</p>
<p><strong>Длина оригинала:</strong><br>{$output['orLength']}</p>
<p><strong>Длина кода:</strong><br>{$output['encLength']}</p>
<p><strong>Процент сжатия:</strong><br>{$output['percent']}</p>
EOF
;
Example #3
0
    move_uploaded_file($_FILES["filename"]["tmp_name"], $uploadedFilePath);
} else {
    $error = "File is not uploaded to server";
    include "../protected/view/form.php";
    exit;
}
ob_start();
$huffman = new Huffman($uploadedFilePath);
$operation = isset($_POST['operation']) ? $_POST['operation'] : Huffman::OPERATION_ENCODE;
try {
    switch ($operation) {
        case Huffman::OPERATION_ENCODE:
            $huffman->encode();
            break;
        case Huffman::OPERATION_DECODE:
            $huffman->decode();
            break;
    }
} catch (HuffmanException $e) {
    $error = $e->getMessage();
    include "../protected/view/form.php";
    exit;
} catch (Exception $e) {
    $error = "Error. Try again.";
    include "../protected/view/form.php";
    exit;
}
ob_end_clean();
header('Content-Type: application/octet-stream');
header('Content-Disposition: filename="' . basename($huffman->getOutFilePath()) . '"');
readfile($huffman->getOutFilePath());