| 125 | |
| 126 | template <typename TSource> |
| 127 | void ReadEncoding(TSource & src) |
| 128 | { |
| 129 | DeleteHuffmanTree(m_root); |
| 130 | m_root = new Node(0 /* symbol */, 0 /* freq */, false /* isLeaf */); |
| 131 | |
| 132 | m_encoderTable.clear(); |
| 133 | m_decoderTable.clear(); |
| 134 | |
| 135 | size_t sz = static_cast<size_t>(ReadVarUint<uint32_t, TSource>(src)); |
| 136 | for (size_t i = 0; i < sz; ++i) |
| 137 | { |
| 138 | uint32_t bits = ReadVarUint<uint32_t, TSource>(src); |
| 139 | uint32_t len = ReadVarUint<uint32_t, TSource>(src); |
| 140 | uint32_t symbol = ReadVarUint<uint32_t, TSource>(src); |
| 141 | Code code(bits, len); |
| 142 | |
| 143 | m_encoderTable[symbol] = code; |
| 144 | m_decoderTable[code] = symbol; |
| 145 | |
| 146 | Node * cur = m_root; |
| 147 | for (size_t j = 0; j < len; ++j) |
| 148 | { |
| 149 | if (((bits >> j) & 1) == 0) |
| 150 | { |
| 151 | if (!cur->l) |
| 152 | cur->l = new Node(0 /* symbol */, 0 /* freq */, false /* isLeaf */); |
| 153 | cur = cur->l; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | if (!cur->r) |
| 158 | cur->r = new Node(0 /* symbol */, 0 /* freq */, false /* isLeaf */); |
| 159 | cur = cur->r; |
| 160 | } |
| 161 | cur->depth = j + 1; |
| 162 | } |
| 163 | cur->isLeaf = true; |
| 164 | cur->symbol = symbol; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | bool Encode(uint32_t symbol, Code & code) const; |
| 169 | bool Decode(Code const & code, uint32_t & symbol) const; |