106 constexpr int maxNodesNeeded= 256 + 255;
107 Node nodePool[maxNodesNeeded];
112 struct cmpHN {
bool operator()(Node* l, Node* r) {
return (l->frequency > r->frequency); } };
121 for (std::size_t i = 0; i < 256 ; ++i)
122 if( (
symbols + i)->frequency > 0 )
123 underlyingNodeVector.
push_back(
new (nodePool + npNext++) Node(
symbols + i) );
126ALIB_DBG( dbgAllValuesAreSame= (sortedNodes.size() == 1); )
128 while (sortedNodes.size() > 1)
131 Node* left = sortedNodes.top(); sortedNodes.pop();
132 Node* right= sortedNodes.top(); sortedNodes.pop();
136 sortedNodes.push(
new ( nodePool + npNext++ ) Node(left, right) );
139 tree= sortedNodes.top();
140 ALIB_ASSERT_ERROR( npNext <= maxNodesNeeded ,
"BITBUFFER/AC/HFMN",
"This can never happen" )
153 stack[0] = Stack{ tree, 0 };
156TEMP_PT(
Log_Warning(
"------ Huffman Encoding Table ----------") )
161 auto* node= stack[depth].node;
170 bw.
Write<9>( 1 |
static_cast<unsigned int>(node->getSymbol() -
symbols) << 1 );
173 node->getSymbol()->wordLength= depth;
174 for(
int i= 0 ; i <= wordNo ; ++i )
175 node->getSymbol()->words[i]= words[i];
179 Lox_Warning(
"HM I: {:3}: {:<15} (len={!ATAB:2}, freq={:>5})",
182 node->symbol->wordLength,
183 node->symbol->frequency ) )
186 words[wordNo]&= ~(1 << ( bitNo ) );
195 if( stack[depth].walked == 0)
197 stack[depth].walked++;
198 stack[depth+1]= Stack{ node->getLeft() , 0};
204 if( stack[depth].walked == 1)
206 stack[depth].walked++;
207 words[wordNo] |= (1 << ( bitNo ) );
208 stack[depth+1]= Stack{ node->getRight() , 0};
214 words[wordNo]&= ~(1 << ( bitNo ) );
217TEMP_PT(
Log_Warning(
"------End of Huffman Encoding Table ----------") )
223TEMP_PT(
Log_Warning(
"------ Huffman Decoding Table ----------") )
233 stack[depth= 0]= Stack{ &
tree, 0 };
236 auto* node= stack[depth].node;
240 node->symbol= uint8_t(
br.
Read<8>());
247TEMP_PT( dbgWord.DeleteEnd(1); )
252 if( stack[depth].read == 0)
256TEMP_PT( dbgWord <<
'0'; )
257 stack[depth+1]= Stack{ node->left , 0 };
263 if( stack[depth].read == 1)
266TEMP_PT( dbgWord <<
'1'; )
268 stack[depth+1]= Stack{ node->right , 0 };
273TEMP_PT( dbgWord.DeleteEnd(1); )
279TEMP_PT(
Log_Warning(
"------End of Huffman Decoding Table ----------") )