<commit_before>#include <cmath>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;

#include "Generator_polar_SC_sys.hpp"

Generator_polar_SC_sys
::Generator_polar_SC_sys(const int& K,
                         const int& N,
                         const float& snr,
                         const mipp::vector<int>& frozen_bits,
                         const std::vector<Pattern_SC_interface*> &patterns,
                         const Pattern_SC_interface &pattern_rate0,
                         const Pattern_SC_interface &pattern_rate1,
                         ostream &dec_stream,
                         ostream &short_dec_stream,
                         ostream &graph_stream,
                         ostream &short_graph_stream)
: K(K),
  N(N),
  m((int)log2(N)),
  snr(snr),
  frozen_bits(frozen_bits),
  patterns(patterns),
  pattern_rate0(pattern_rate0),
  pattern_rate1(pattern_rate1),
  parser(N, frozen_bits, patterns, pattern_rate0, pattern_rate1),
  dec_stream(dec_stream),
  short_dec_stream(short_dec_stream),
  graph_stream(graph_stream),
  short_graph_stream(short_graph_stream),
  tab("\t"),
  inlining_level(3),
  stats(m +1),
  n_nodes_before_compression(0),
  n_nodes_after_compression(0)
{
	for (unsigned i = 0; i < stats.size(); i++)
		stats[i].resize(patterns.size());
}

Generator_polar_SC_sys
::~Generator_polar_SC_sys()
{
}

void Generator_polar_SC_sys
::generate()
{
	n_nodes_before_compression = 0;
	n_nodes_after_compression = 0;
	for (unsigned i = 0; i < stats.size(); i++)
		std::fill(stats[i].begin(), stats[i].end(), 0);

	string class_name = "Decoder_polar_SC_fast_sys_N"   + to_string(N) +
	                                             "_K"   + to_string(K) +
	                                           "_SNR" + to_string((int)(snr*10));

	string pragma_name = "DECODER_POLAR_SC_FAST_SYS_N"   + to_string(N) +
	                                              "_K"   + to_string(K) +
	                                            "_SNR" + to_string((int)(snr*10)) + "_HPP_";

	const auto n_lines = (int)ceil((float)frozen_bits.size() / 32.f);

	string fbits_name = "Frozen_bits_" + to_string(N) +
	                               "_" + to_string(K) +
	                               "_" + to_string((int)(snr*10));

	stringstream fbits;
	auto i = 0;
	for (auto j = 0; j < n_lines; j++)
	{
		auto k = 0;
		while (k < 32 && i < (int)frozen_bits.size())
		{
			fbits << frozen_bits[i];
			if (i < (int)frozen_bits.size() -1) fbits << ", ";
			i++; k++;
		}
		if (i < (int)frozen_bits.size()) fbits << endl;
	}

	// decoder generation
	stringstream dec_common1, dec_common2, dec_common3, dec, short_dec1, short_dec2;
	dec_common1 << "#ifndef " << pragma_name                                                                  << endl;
	dec_common1 << "#define " << pragma_name                                                                  << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << "#include <cassert>"                                                                       << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << "#include \"../Decoder_polar_SC_fast_sys.hpp\""                                            << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << "static const char " << fbits_name << "[" << N << "] = {"                                  << endl;
	dec_common1 << fbits.str() << "};"                                                                        << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << "template <typename B, typename R, class API_polar>"                                       << endl;
	dec_common1 << "class " << class_name << " : public Decoder_polar_SC_fast_sys<B, R, API_polar>"           << endl;
	dec_common1 << "{"                                                                                        << endl;
	dec_common1 << "public:"                                                                                  << endl;
	dec_common1 << tab << class_name << "(const int& K, const int& N, const mipp::vector<B>& frozen_bits, "
	                   << "const int n_frames = 1)"                                                           << endl;
	dec_common1 << tab << ": Decoder_polar_SC_fast_sys<B, R, API_polar>(K, N, frozen_bits, n_frames)"         << endl;
	dec_common1 << tab << "{"                                                                                 << endl;
	dec_common1 << tab << tab << "assert(N == " << N << ");"                                                  << endl;
	dec_common1 << tab << tab << "assert(K == " << K << ");"                                                  << endl;
	dec_common1 << tab << tab                                                                                 << endl;
	dec_common1 << tab << tab << "auto i = 0;"                                                                << endl;
	dec_common1 << tab << tab << "while (i < " << N << " && " << fbits_name << "[i] == frozen_bits[i]) i++;"  << endl;
	dec_common1 << tab << tab << "assert(i == " << N << ");"                                                  << endl;
	dec_common1 << tab << "}"                                                                                 << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << tab << "virtual ~" << class_name << "()"                                                   << endl;
	dec_common1 << tab << "{"                                                                                 << endl;
	dec_common1 << tab << "}"                                                                                 << endl;
	dec_common1                                                                                               << endl;
	dec_common2 << tab << "void decode()"                                                                     << endl;
	dec_common2 << tab << "{"                                                                                 << endl;
	dec_common2 << tab << tab << "auto &l = this->l;"                                                         << endl;
	dec_common2 << tab << tab << "auto &s = this->s;"                                                         << endl;
	dec_common2                                                                                               << endl;
	this->recursive_generate_decoder(parser.get_polar_tree()->get_root(), dec);
	dec_common3 << tab << "}"                                                                                 << endl;
	dec_common3 << "};" << ""                                                                                 << endl;
	dec_common3 << "#endif"                                                                                   << endl;

	dec_stream << dec_common1.str();
	dec_stream << dec_common2.str();
	dec_stream << dec.str();
	dec_stream << dec_common3.str();

	// graph generation
	stringstream graph, short_graph;
	this->recursive_generate_graph(parser.get_polar_tree()->get_root(), graph);
	this->subtree_occurences_cpy = this->subtree_occurences;
	this->recursive_generate_short_graph(parser.get_polar_tree()->get_root(), short_graph);
	stringstream graph_common1;
	graph_common1 << "digraph " << class_name << "{" << endl;
	graph_common1 << tab << "subgraph cluster_0 {" << endl;
	graph_common1 << tab << tab << "label=\"Legend\";" << endl;
	graph_common1 << tab << tab << "rankdir=LR;" << endl;
	graph_common1 << tab << tab << "rank=same;" << endl;
	for (unsigned p = 0; p < patterns.size(); p++)
	{
		graph_common1 << tab << tab;
		graph_common1 << "\"" << patterns[p]->name() << "\"["
		                      << "style=filled, "
		                      << "fillcolor=\"" << patterns[p]->fill_color() << "\","
		                      << "fontcolor=\"" << patterns[p]->font_color() << "\""
		                      << "];" << endl;
	}
	for (unsigned p = 0; p < patterns.size() -1; p++)
	{
		graph_common1 << tab << tab;
		graph_common1 << "\"" << patterns[p]->name() << "\" -> " << "\"" << patterns[p +1]->name() << "\";" << endl;
	}

	float compression_rate = (float)n_nodes_before_compression / (float)n_nodes_after_compression;
	graph_common1 << tab << "}" << endl;
	graph_common1 << tab << "subgraph cluster_1 {" << endl;
	graph_common1 << tab << tab
	              << "label=\"Decoder SC systematic - "
	              << "N="                                        << N                     << ", "
	              << "K="                                        << K                     << ", "
	              << "R="                << std::setprecision(3) << ((float)K / (float)N) << ", "
	              << "SNR="              << std::setprecision(2) << snr                   << " - "
	              << "Compression rate=" << std::setprecision(3) << compression_rate      << "\";" << endl;

	stringstream graph_common2;
	graph_common2 << tab << "}" << endl;
	graph_common2 << "}" << endl;

	graph_stream << graph_common1.str();
	graph_stream << graph.str();
	graph_stream << graph_common2.str();<commit_msg>Fix a potential 0 division.<commit_after>#include <cmath>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
using namespace std;

#include "Generator_polar_SC_sys.hpp"

Generator_polar_SC_sys
::Generator_polar_SC_sys(const int& K,
                         const int& N,
                         const float& snr,
                         const mipp::vector<int>& frozen_bits,
                         const std::vector<Pattern_SC_interface*> &patterns,
                         const Pattern_SC_interface &pattern_rate0,
                         const Pattern_SC_interface &pattern_rate1,
                         ostream &dec_stream,
                         ostream &short_dec_stream,
                         ostream &graph_stream,
                         ostream &short_graph_stream)
: K(K),
  N(N),
  m((int)log2(N)),
  snr(snr),
  frozen_bits(frozen_bits),
  patterns(patterns),
  pattern_rate0(pattern_rate0),
  pattern_rate1(pattern_rate1),
  parser(N, frozen_bits, patterns, pattern_rate0, pattern_rate1),
  dec_stream(dec_stream),
  short_dec_stream(short_dec_stream),
  graph_stream(graph_stream),
  short_graph_stream(short_graph_stream),
  tab("\t"),
  inlining_level(3),
  stats(m +1),
  n_nodes_before_compression(0),
  n_nodes_after_compression(0)
{
	for (unsigned i = 0; i < stats.size(); i++)
		stats[i].resize(patterns.size());
}

Generator_polar_SC_sys
::~Generator_polar_SC_sys()
{
}

void Generator_polar_SC_sys
::generate()
{
	n_nodes_before_compression = 0;
	n_nodes_after_compression = 0;
	for (unsigned i = 0; i < stats.size(); i++)
		std::fill(stats[i].begin(), stats[i].end(), 0);

	string class_name = "Decoder_polar_SC_fast_sys_N"   + to_string(N) +
	                                             "_K"   + to_string(K) +
	                                           "_SNR" + to_string((int)(snr*10));

	string pragma_name = "DECODER_POLAR_SC_FAST_SYS_N"   + to_string(N) +
	                                              "_K"   + to_string(K) +
	                                            "_SNR" + to_string((int)(snr*10)) + "_HPP_";

	const auto n_lines = (int)ceil((float)frozen_bits.size() / 32.f);

	string fbits_name = "Frozen_bits_" + to_string(N) +
	                               "_" + to_string(K) +
	                               "_" + to_string((int)(snr*10));

	stringstream fbits;
	auto i = 0;
	for (auto j = 0; j < n_lines; j++)
	{
		auto k = 0;
		while (k < 32 && i < (int)frozen_bits.size())
		{
			fbits << frozen_bits[i];
			if (i < (int)frozen_bits.size() -1) fbits << ", ";
			i++; k++;
		}
		if (i < (int)frozen_bits.size()) fbits << endl;
	}

	// decoder generation
	stringstream dec_common1, dec_common2, dec_common3, dec, short_dec1, short_dec2;
	dec_common1 << "#ifndef " << pragma_name                                                                  << endl;
	dec_common1 << "#define " << pragma_name                                                                  << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << "#include <cassert>"                                                                       << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << "#include \"../Decoder_polar_SC_fast_sys.hpp\""                                            << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << "static const char " << fbits_name << "[" << N << "] = {"                                  << endl;
	dec_common1 << fbits.str() << "};"                                                                        << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << "template <typename B, typename R, class API_polar>"                                       << endl;
	dec_common1 << "class " << class_name << " : public Decoder_polar_SC_fast_sys<B, R, API_polar>"           << endl;
	dec_common1 << "{"                                                                                        << endl;
	dec_common1 << "public:"                                                                                  << endl;
	dec_common1 << tab << class_name << "(const int& K, const int& N, const mipp::vector<B>& frozen_bits, "
	                   << "const int n_frames = 1)"                                                           << endl;
	dec_common1 << tab << ": Decoder_polar_SC_fast_sys<B, R, API_polar>(K, N, frozen_bits, n_frames)"         << endl;
	dec_common1 << tab << "{"                                                                                 << endl;
	dec_common1 << tab << tab << "assert(N == " << N << ");"                                                  << endl;
	dec_common1 << tab << tab << "assert(K == " << K << ");"                                                  << endl;
	dec_common1 << tab << tab                                                                                 << endl;
	dec_common1 << tab << tab << "auto i = 0;"                                                                << endl;
	dec_common1 << tab << tab << "while (i < " << N << " && " << fbits_name << "[i] == frozen_bits[i]) i++;"  << endl;
	dec_common1 << tab << tab << "assert(i == " << N << ");"                                                  << endl;
	dec_common1 << tab << "}"                                                                                 << endl;
	dec_common1                                                                                               << endl;
	dec_common1 << tab << "virtual ~" << class_name << "()"                                                   << endl;
	dec_common1 << tab << "{"                                                                                 << endl;
	dec_common1 << tab << "}"                                                                                 << endl;
	dec_common1                                                                                               << endl;
	dec_common2 << tab << "void decode()"                                                                     << endl;
	dec_common2 << tab << "{"                                                                                 << endl;
	dec_common2 << tab << tab << "auto &l = this->l;"                                                         << endl;
	dec_common2 << tab << tab << "auto &s = this->s;"                                                         << endl;
	dec_common2                                                                                               << endl;
	this->recursive_generate_decoder(parser.get_polar_tree()->get_root(), dec);
	dec_common3 << tab << "}"                                                                                 << endl;
	dec_common3 << "};" << ""                                                                                 << endl;
	dec_common3 << "#endif"                                                                                   << endl;

	dec_stream << dec_common1.str();
	dec_stream << dec_common2.str();
	dec_stream << dec.str();
	dec_stream << dec_common3.str();

	// graph generation
	stringstream graph, short_graph;
	this->recursive_generate_graph(parser.get_polar_tree()->get_root(), graph);
	this->subtree_occurences_cpy = this->subtree_occurences;
	this->recursive_generate_short_graph(parser.get_polar_tree()->get_root(), short_graph);
	stringstream graph_common1;
	graph_common1 << "digraph " << class_name << "{" << endl;
	graph_common1 << tab << "subgraph cluster_0 {" << endl;
	graph_common1 << tab << tab << "label=\"Legend\";" << endl;
	graph_common1 << tab << tab << "rankdir=LR;" << endl;
	graph_common1 << tab << tab << "rank=same;" << endl;
	for (unsigned p = 0; p < patterns.size(); p++)
	{
		graph_common1 << tab << tab;
		graph_common1 << "\"" << patterns[p]->name() << "\"["
		                      << "style=filled, "
		                      << "fillcolor=\"" << patterns[p]->fill_color() << "\","
		                      << "fontcolor=\"" << patterns[p]->font_color() << "\""
		                      << "];" << endl;
	}
	for (unsigned p = 0; p < patterns.size() -1; p++)
	{
		graph_common1 << tab << tab;
		graph_common1 << "\"" << patterns[p]->name() << "\" -> " << "\"" << patterns[p +1]->name() << "\";" << endl;
	}

	assert(n_nodes_after_compression != 0);
	float compression_rate = (float)n_nodes_before_compression / (float)n_nodes_after_compression;
	graph_common1 << tab << "}" << endl;
	graph_common1 << tab << "subgraph cluster_1 {" << endl;
	graph_common1 << tab << tab
	              << "label=\"Decoder SC systematic - "
	              << "N="                                        << N                     << ", "
	              << "K="                                        << K                     << ", "
	              << "R="                << std::setprecision(3) << ((float)K / (float)N) << ", "
	              << "SNR="              << std::setprecision(2) << snr                   << " - "
	              << "Compression rate=" << std::setprecision(3) << compression_rate      << "\";" << endl;

	stringstream graph_common2;
	graph_common2 << tab << "}" << endl;
	graph_common2 << "}" << endl;

	graph_stream << graph_common1.str();
	graph_stream << graph.str();
	graph_stream << graph_common2.str();
{'fraction_non_alphanumeric': 0.13541174618170368, 'fraction_numerical': 0.011231827008869995, 'mean_word_length': 1.1741214057507987, 'pattern_counts': {'":': 2, '<': 855, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 250, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1363992', 'n_tokens_mistral': 5024, 'n_tokens_neox': 4777, 'n_words': 951}