285# include <tclap/CmdLine.h>
293"Muerte De Antoñito El Camborio\n"
294" Federico Garcia Lorca\n"
296" Voces de muerte sonaron\n"
297"cerca del Guadalquivir.\n"
298"Voces antiguas que cercan\n"
299"voz de clavel varonil.\n"
300"Les clavo sobre las botas\n"
301"mordiscos de jabali.\n"
302"En la lucha daba saltos\n"
303"jabonados de delfin.\n"
304"Bano con sangre enemiga\n"
305"su corbata carmesi,\n"
306"pero eran cuatro punales\n"
307"y tuvo que sucumbir.\n"
308"Cuando las estrellas clavan\n"
309"rejones al agua gris,\n"
310"cuando los erales suenan\n"
311"veronicas de alheli,\n"
312"voces de muerte sonaron\n"
313"cerca del Guadalquivir.\n"
315" Antonio Torres Heredia,\n"
316"Camborio de dura crin,\n"
317"moreno de verde luna,\n"
318"voz de clavel varonil:\n"
319"quien te ha quitado la vida\n"
320"cerca del Guadalquivir?\n"
321"Mis cuatro primos Heredias\n"
322"hijos de Benameji.\n"
323"Lo que en otros no envidiaban,\n"
324"ya lo envidiaban en mi.\n"
325"Zapatos color corinto,\n"
326"medallones de marfil,\n"
327"y este cutis amasado\n"
328"con aceituna y jazmin.\n"
329"Ay Antonito el Camborio,\n"
330"digno de una Emperatriz!\n"
331"Acuerdate de la Virgen\n"
332"porque te vas a morir.\n"
333"Ay Federico Garcia,\n"
334"llama a la Guardia Civil!\n"
335"Ya mi talle se ha quebrado\n"
336"como cana de maiz.\n"
338" Tres golpes de sangre tuvo\n"
339"y se murio de perfil.\n"
340"Viva moneda que nunca\n"
341"se volvera a repetir.\n"
342"Un angel marchoso pone\n"
343"su cabeza en un cojin.\n"
344"Otros de rubor cansado,\n"
345"encendieron un candil.\n"
346"Y cuando los cuatro primos\n"
347"llegan a Benameji,\n"
348"voces de muerte cesaron\n"
349"cerca del Guadalquivir.\n";
359 return huffman_engine.
encode(str, bit_stream);
367 ofstream verso(filename, ios::out);
368 verso <<
"\\begin{verse}" << endl;
370 const char* curr = text;
371 while (*curr !=
'\0')
377 verso <<
" \\\\" << endl
382 else if (curr[1] !=
'\0')
384 verso <<
" \\\\" << endl;
394 verso <<
"\\end{verse}" << endl;
397int main(
int argc,
char *argv[])
401 TCLAP::CmdLine
cmd(
"Huffman encoding demonstration",
' ',
"1.0");
403 TCLAP::UnlabeledValueArg<string> fileArg(
"input",
404 "Input file to encode (optional, uses built-in poem if not provided)",
405 false,
"",
"filename");
408 TCLAP::ValueArg<string> outputArg(
"o",
"output",
409 "Output tree file prefix",
410 false,
"huffman",
"prefix");
413 cmd.parse(argc, argv);
415 string inputFile = fileArg.getValue();
416 string outputPrefix = outputArg.getValue();
418 cout <<
"Huffman Encoding Example" << endl;
419 cout <<
"========================" << endl << endl;
421 ofstream huf_output(outputPrefix +
".Tree", ios::out);
428 if (inputFile.empty())
431 cout <<
"Encoding built-in poem (Lorca)..." << endl;
440 ofstream
output(outputPrefix +
"_tree.h");
445 cout << endl <<
"Decoded text:" << endl;
446 cout <<
"-------------" << endl;
455 ofstream stat1(outputPrefix +
"-stat1.tex", ios::out);
456 stat1 <<
"$" << strlen(
poem) * 8 <<
"$";
460 ofstream stat2(outputPrefix +
"-stat2.tex", ios::out);
461 stat2 <<
"$" << code_len <<
"$";
465 size_t original_bits = strlen(
poem) * 8;
466 cout << endl <<
"Compression Statistics:" << endl;
467 cout <<
"-----------------------" << endl;
468 cout <<
"Original size: " << original_bits <<
" bits" << endl;
469 cout <<
"Encoded size: " << code_len <<
" bits" << endl;
470 cout <<
"Compression ratio: "
471 << (100.0 * code_len / original_bits) <<
"%" << endl;
472 cout <<
"Space saved: "
473 << (100.0 - 100.0 * code_len / original_bits) <<
"%" << endl;
481 cout <<
"Encoding file: " << inputFile << endl;
483 ifstream input1(inputFile, ios::in);
486 cerr <<
"Error: cannot open file " << inputFile << endl;
495 ifstream input2(inputFile, ios::in);
496 BitArray bit_stream(1024 * 1024 * 500);
498 code_len = encoder.
encode(input2, bit_stream);
502 ifstream input3(inputFile, ios::ate);
503 size_t original_size = input3.tellg();
506 cout << endl <<
"Compression Statistics:" << endl;
507 cout <<
"-----------------------" << endl;
508 cout <<
"Original size: " << (original_size * 8) <<
" bits" << endl;
509 cout <<
"Encoded size: " << code_len <<
" bits" << endl;
510 cout <<
"Compression ratio: "
511 << (100.0 * code_len / (original_size * 8)) <<
"%" << endl;
514 cout << endl <<
"Done. Output files:" << endl;
515 cout <<
" - " << outputPrefix <<
".Tree" << endl;
516 if (inputFile.empty())
518 cout <<
" - " << outputPrefix <<
"_tree.h" << endl;
519 cout <<
" - " << outputPrefix <<
"-verso.tex" << endl;
520 cout <<
" - " << outputPrefix <<
"-stat1.tex" << endl;
521 cout <<
" - " << outputPrefix <<
"-stat2.tex" << endl;
524 catch (TCLAP::ArgException &e)
526 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() << endl;
Huffman coding for data compression.
Contiguous array of bits.
void decode(const BitArray &bit_stream, std::ostream &output) const
Decode a bit stream.
Freq_Node *& get_freq_root()
Returns the root of the frequency tree.
void save_tree_in_array_of_chars(const std::string &array_name, std::ostream &output) const
Generate C/C++ array declarations for a Huffman tree.
size_t encode(char *input, BitArray &bit_stream)
Encode the input text.
BinNode< std::string > *& get_root()
Returns the root of the Huffman decoding tree.
void read_input(char *input, const bool &with_freqs=false)
Read a NUL-terminated character std::string, count frequencies and build the prefix tree.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
Huffman tree visualization for btreepic LaTeX package.
void huffman_to_btreepic(Freq_Node *p, const bool with_level_adjust=false)
Generate btreepic specification for a Huffman tree.
std::ostream * output_ptr
Output stream pointer for btreepic commands.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
void write_verse(const char *text, const string &filename)
Write poem in LaTeX verse format.
size_t read_and_encode(char *str, Huffman_Encoder_Engine &huffman_engine, BitArray &bit_stream)
Encode string and return encoded bit stream length.