LZContext.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Game\Services\LZCompressor;
  3. class LZContext
  4. {
  5. /**
  6. * @var array
  7. */
  8. public $dictionary = array();
  9. /**
  10. * @var array
  11. */
  12. public $dictionaryToCreate = array();
  13. /**
  14. * @var string
  15. */
  16. public $c = '';
  17. /**
  18. * @var string
  19. */
  20. public $wc = '';
  21. /**
  22. * @var string
  23. */
  24. public $w = '';
  25. /**
  26. * @var int
  27. */
  28. public $enlargeIn = 2;
  29. /**
  30. * @var int
  31. */
  32. public $dictSize = 3;
  33. /**
  34. * @var int
  35. */
  36. public $numBits = 2;
  37. /**
  38. * @var LZData
  39. */
  40. public $data;
  41. function __construct()
  42. {
  43. $this->data = new LZData;
  44. }
  45. // Helper
  46. /**
  47. * @param string $val
  48. * @return bool
  49. */
  50. public function dictionaryContains($val) {
  51. return array_key_exists($val, $this->dictionary);
  52. }
  53. /**
  54. * @param $val
  55. */
  56. public function addToDictionary($val) {
  57. $this->dictionary[$val] = $this->dictSize++;
  58. }
  59. /**
  60. * @param string $val
  61. * @return bool
  62. */
  63. public function dictionaryToCreateContains($val) {
  64. return array_key_exists($val, $this->dictionaryToCreate);
  65. }
  66. /**
  67. * decrements enlargeIn and extends numbits in case enlargeIn drops to 0
  68. */
  69. public function enlargeIn() {
  70. $this->enlargeIn--;
  71. if($this->enlargeIn==0) {
  72. $this->enlargeIn = pow(2, $this->numBits);
  73. $this->numBits++;
  74. }
  75. }
  76. }