PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Robin   Variable Length Coding   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: Variable Length Coding
Compress data using variable length encoding
Author: By
Last change:
Date: 18 years ago
Size: 1,046 bytes
 

Contents

Class file image Download
<?php

require './vlc.class.php';

/* Example usage */

// create a sorted array with 1,000 random values
$values = array();
for(
$i=0; $i<1000; $i++ ) {
   
$values[] = rand(1,999999);
}
sort($values);

print_r( $values );

$vlcw = new VLCWriter();

// store the first value as is
$base = array_shift( $values );
$vlcw->zipInt( $base );

// for all other values, store the delta
while( $value = array_shift( $values ) ) {
   
$delta = $value - $base;
   
$vlcw->zipInt( $delta );
   
$base = $value;
}

// get the binary output
$data = $vlcw->toString();

// normal length is 1000*4 bytes (1000 32-bit integers)
$encoded_length = strlen( $data );
$gain = ( 1000 * 4 ) - $encoded_length;

print
"Original length: 4000 bytes\n";
print
"Encoded length: " . $encoded_length . " bytes\n";

// Decode the binary data

$vlcr = new VLCReader( $data );

$values = array();
$base = 0;

while(
$delta = $vlcr->unzipInt() ) {
   
$value = $base + $delta;
   
$values[] = $value;
   
$base = $value;
}

print_r( $values );

?>