<commit_before>#include <numeric>
#include <iostream>
#include <sstream>

#include "Tools/Exception/exception.hpp"
#include "Tools/Math/matrix.h"

#include "Encoder_LDPC.hpp"

using namespace aff3ct;
using namespace aff3ct::module;

template <typename B>
Encoder_LDPC<B>
::Encoder_LDPC(const int K, const int N, const int n_frames)
: Encoder<B>(K, N, n_frames)
{
	const std::string name = "Encoder_LDPC";
	this->set_name(name);
}

template <typename B>
Encoder_LDPC<B>
::Encoder_LDPC(const int K, const int N, const tools::Sparse_matrix &G, const int n_frames)
: Encoder<B>(K, N, n_frames), G(G)
{
	const std::string name = "Encoder_LDPC";
	this->set_name(name);

	check_G_dimensions();
}

template <typename B>
Encoder_LDPC<B>
::Encoder_LDPC(const int K, const int N, const tools::Sparse_matrix &G, const tools::Sparse_matrix &H, const int n_frames)
: Encoder<B>(K, N, n_frames), G(G), H(H)
{
	const std::string name = "Encoder_LDPC";
	this->set_name(name);

	check_G_dimensions();
	check_H_dimensions();
}

template <typename B>
void Encoder_LDPC<B>
::check_G_dimensions()
{
	try
	{
		this->_check_G_dimensions();
	}
	catch(tools::invalid_argument&)
	{
		this->G.self_transpose();
		this->_check_G_dimensions();
	}
}

template <typename B>
void Encoder_LDPC<B>
::check_H_dimensions()
{
	try
	{
		this->_check_H_dimensions();
	}
	catch(tools::invalid_argument&)
	{
		this->H.self_transpose();
		this->_check_H_dimensions();
	}
}

template <typename B>
void Encoder_LDPC<B>
::_check_G_dimensions()
{
	if (this->K != (int)this->G.get_n_cols())
	{
		std::stringstream message;
		message << "The built G matrix has a dimension 'K' different than the given one ('K' = " << this->K
		        << ", 'G.get_n_cols()' = " << this->G.get_n_cols() << ").";
		throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
	}

	if (this->N != (int)this->G.get_n_rows())
	{
		std::stringstream message;
		message << "The built G matrix has a dimension 'N' different than the given one ('N' = " << this->N
		        << ", 'G.get_n_rows()' = " << this->G.get_n_rows() << ").";
		throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
	}
}

template <typename B>
void Encoder_LDPC<B>
::_check_H_dimensions()
{
	if (this->N != (int)this->H.get_n_rows())
	{
		std::stringstream message;
		message << "The built H matrix has a dimension 'N' different than the given one ('N' = " << this->N
		        << ", 'H.get_n_rows()' = " << this->H.get_n_rows() << ").";
		throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
	}
}

template <typename B>
void Encoder_LDPC<B>
::_encode(const B *U_K, B *X_N, const int frame_id)
{
	for (unsigned i = 0; i < this->N; i++)
	{
		X_N[i] = 0;
		auto& links = this->G.get_cols_from_row(i);
		for (unsigned j = 0; j < links.size(); j++)
			X_N[i] += U_K[ links[j] ];
		X_N[i] &= (B)1; // modulo 2
	}
}

template <typename B>
const std::vector<uint32_t>& Encoder_LDPC<B>
::get_info_bits_pos() const
{
	throw tools::unimplemented_error(__FILE__, __LINE__, __func__);
}

template <typename B>
bool Encoder_LDPC<B>
::is_sys() const
{
	throw tools::unimplemented_error(__FILE__, __LINE__, __func__);
}

template <typename B><commit_msg>Remove compilation warning comparison signed/unsigned<commit_after>#include <numeric>
#include <iostream>
#include <sstream>

#include "Tools/Exception/exception.hpp"
#include "Tools/Math/matrix.h"

#include "Encoder_LDPC.hpp"

using namespace aff3ct;
using namespace aff3ct::module;

template <typename B>
Encoder_LDPC<B>
::Encoder_LDPC(const int K, const int N, const int n_frames)
: Encoder<B>(K, N, n_frames)
{
	const std::string name = "Encoder_LDPC";
	this->set_name(name);
}

template <typename B>
Encoder_LDPC<B>
::Encoder_LDPC(const int K, const int N, const tools::Sparse_matrix &G, const int n_frames)
: Encoder<B>(K, N, n_frames), G(G)
{
	const std::string name = "Encoder_LDPC";
	this->set_name(name);

	check_G_dimensions();
}

template <typename B>
Encoder_LDPC<B>
::Encoder_LDPC(const int K, const int N, const tools::Sparse_matrix &G, const tools::Sparse_matrix &H, const int n_frames)
: Encoder<B>(K, N, n_frames), G(G), H(H)
{
	const std::string name = "Encoder_LDPC";
	this->set_name(name);

	check_G_dimensions();
	check_H_dimensions();
}

template <typename B>
void Encoder_LDPC<B>
::check_G_dimensions()
{
	try
	{
		this->_check_G_dimensions();
	}
	catch(tools::invalid_argument&)
	{
		this->G.self_transpose();
		this->_check_G_dimensions();
	}
}

template <typename B>
void Encoder_LDPC<B>
::check_H_dimensions()
{
	try
	{
		this->_check_H_dimensions();
	}
	catch(tools::invalid_argument&)
	{
		this->H.self_transpose();
		this->_check_H_dimensions();
	}
}

template <typename B>
void Encoder_LDPC<B>
::_check_G_dimensions()
{
	if (this->K != (int)this->G.get_n_cols())
	{
		std::stringstream message;
		message << "The built G matrix has a dimension 'K' different than the given one ('K' = " << this->K
		        << ", 'G.get_n_cols()' = " << this->G.get_n_cols() << ").";
		throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
	}

	if (this->N != (int)this->G.get_n_rows())
	{
		std::stringstream message;
		message << "The built G matrix has a dimension 'N' different than the given one ('N' = " << this->N
		        << ", 'G.get_n_rows()' = " << this->G.get_n_rows() << ").";
		throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
	}
}

template <typename B>
void Encoder_LDPC<B>
::_check_H_dimensions()
{
	if (this->N != (int)this->H.get_n_rows())
	{
		std::stringstream message;
		message << "The built H matrix has a dimension 'N' different than the given one ('N' = " << this->N
		        << ", 'H.get_n_rows()' = " << this->H.get_n_rows() << ").";
		throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
	}
}

template <typename B>
void Encoder_LDPC<B>
::_encode(const B *U_K, B *X_N, const int frame_id)
{
	for (auto i = 0; i < this->N; i++)
	{
		X_N[i] = 0;
		auto& links = this->G.get_cols_from_row(i);
		for (unsigned j = 0; j < links.size(); j++)
			X_N[i] += U_K[ links[j] ];
		X_N[i] &= (B)1; // modulo 2
	}
}

template <typename B>
const std::vector<uint32_t>& Encoder_LDPC<B>
::get_info_bits_pos() const
{
	throw tools::unimplemented_error(__FILE__, __LINE__, __func__);
}

template <typename B>
bool Encoder_LDPC<B>
::is_sys() const
{
	throw tools::unimplemented_error(__FILE__, __LINE__, __func__);
}

template <typename B>
{'fraction_non_alphanumeric': 0.17263387297633873, 'fraction_numerical': 0.002801992528019925, 'mean_word_length': 4.916206261510129, 'pattern_counts': {'":': 0, '<': 123, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6527568', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2489, 'n_words': 561}