mirror of
https://github.com/icecoder/ICEcoder.git
synced 2026-03-09 18:16:48 +01:00
LS String PHP lib added
To compress and decompress strings with LZ compression
This commit is contained in:
91
LZCompressor/LZContext.php
Normal file
91
LZCompressor/LZContext.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace LZCompressor;
|
||||
|
||||
class LZContext
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $dictionary = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $dictionaryToCreate = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $c = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $wc = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $w = '';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $enlargeIn = 2;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $dictSize = 3;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $numBits = 2;
|
||||
|
||||
/**
|
||||
* @var LZData
|
||||
*/
|
||||
public $data;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->data = new LZData;
|
||||
}
|
||||
|
||||
// Helper
|
||||
|
||||
/**
|
||||
* @param string $val
|
||||
* @return bool
|
||||
*/
|
||||
public function dictionaryContains($val) {
|
||||
return array_key_exists($val, $this->dictionary);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $val
|
||||
*/
|
||||
public function addToDictionary($val) {
|
||||
$this->dictionary[$val] = $this->dictSize++;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $val
|
||||
* @return bool
|
||||
*/
|
||||
public function dictionaryToCreateContains($val) {
|
||||
return array_key_exists($val, $this->dictionaryToCreate);
|
||||
}
|
||||
|
||||
/**
|
||||
* decrements enlargeIn and extends numbits in case enlargeIn drops to 0
|
||||
*/
|
||||
public function enlargeIn() {
|
||||
$this->enlargeIn--;
|
||||
if($this->enlargeIn==0) {
|
||||
$this->enlargeIn = pow(2, $this->numBits);
|
||||
$this->numBits++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user