1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mfonte\Base62x\Compression\Huffman; |
4
|
|
|
|
5
|
|
|
use Mfonte\Base62x\Exception\CompressionException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Node in a huffman tree implementing a huffman coding. |
9
|
|
|
*/ |
10
|
|
|
class HuffmanNode |
11
|
|
|
{ |
12
|
|
|
private $weight = null; |
13
|
|
|
private $symbol = null; |
14
|
|
|
private $leftChild = null; |
15
|
|
|
private $rightChild = null; |
16
|
|
|
private $parent = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* create a new leaf node. |
20
|
|
|
*/ |
21
|
|
|
public function __construct($symbol, $weight = null) |
22
|
|
|
{ |
23
|
|
|
$this->symbol = $symbol; |
24
|
|
|
$this->weight = $weight; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* create a new node whose children are the supplied nodes. |
29
|
|
|
*/ |
30
|
|
|
public static function join(self $left, self $right) |
31
|
|
|
{ |
32
|
|
|
$newParent = new self(null, $left->getWeight() + $right->getWeight()); |
33
|
|
|
$newParent->leftChild = $left; |
34
|
|
|
$newParent->rightChild = $right; |
35
|
|
|
|
36
|
|
|
return $newParent; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* perform a depth-first search to construct a. |
41
|
|
|
*/ |
42
|
|
|
public function getCodeHash(&$hashTable, $prefix = '') |
43
|
|
|
{ |
44
|
|
|
if ($this->isLeaf()) { |
45
|
|
|
$hashTable[$this->symbol] = $prefix; |
46
|
|
|
} else { |
47
|
|
|
$this->leftChild->getCodeHash($hashTable, "{$prefix}0"); |
48
|
|
|
$this->rightChild->getCodeHash($hashTable, "{$prefix}1"); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isLeaf() |
53
|
|
|
{ |
54
|
|
|
return $this->symbol !== null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getWeight() |
58
|
|
|
{ |
59
|
|
|
return $this->weight; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getSymbol() |
63
|
|
|
{ |
64
|
|
|
return $this->symbol; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getRightChild() |
68
|
|
|
{ |
69
|
|
|
return $this->rightChild; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getLeftChild() |
73
|
|
|
{ |
74
|
|
|
return $this->leftChild; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getParent() |
78
|
|
|
{ |
79
|
|
|
return $this->parent; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* read a node encoded using __toString. |
84
|
|
|
*/ |
85
|
|
|
private static function readEncodedNode(&$str) |
86
|
|
|
{ |
87
|
|
|
$retval = null; |
88
|
|
|
$switch = $str[0]; |
89
|
|
|
$str = mb_substr($str, 1); |
90
|
|
|
switch ($switch) { |
91
|
|
|
case '0': |
92
|
|
|
$left = self::readEncodedNode($str); |
93
|
|
|
$right = self::readEncodedNode($str); |
94
|
|
|
$parent = new self(null); |
95
|
|
|
$parent->leftChild = $left; |
96
|
|
|
$parent->rightChild = $right; |
97
|
|
|
|
98
|
|
|
$retval = $parent; |
99
|
|
|
break; |
100
|
|
|
case '1': |
101
|
|
|
case '2': |
102
|
|
|
$symbol = ($switch == 1) |
103
|
|
|
? $str[0] : |
104
|
|
|
HuffmanCoding::SYMBOL_EOF; |
105
|
|
|
$str = mb_substr($str, 1); |
106
|
|
|
|
107
|
|
|
$retval = new self($symbol); |
108
|
|
|
break; |
109
|
|
|
default: |
110
|
|
|
throw new CompressionException('huffman', 'Encoding is out of sync.'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $retval; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* opposite of __toString. |
118
|
|
|
**/ |
119
|
|
|
public static function loadFromString(&$str) |
120
|
|
|
{ |
121
|
|
|
return self::readEncodedNode($str); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* create a string for compactly encoding this node (and its children). |
126
|
|
|
*/ |
127
|
|
|
public function __toString() |
128
|
|
|
{ |
129
|
|
|
if ($this->isLeaf()) { |
130
|
|
|
if ($this->symbol === HuffmanCoding::SYMBOL_EOF) { |
131
|
|
|
return '2~'; // ~ stands in BC HuffmanCoding::SYMBOL_EOF may be more than one byte |
132
|
|
|
} else { |
133
|
|
|
return '1'.$this->symbol; |
134
|
|
|
} |
135
|
|
|
} else { |
136
|
|
|
return '0'.(string) $this->leftChild.(string) $this->rightChild; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|