/* *************************************************************

        Final Project                                 MMU 1996

        ######################################################
        #                                                    #
        #               A Modular Neural Network             #   
        #                                                    #
        #              A Class for Recoding Data             #   
        #                                                    #
        ######################################################

        Albrecht Schmidt                              17.09.96

        FILE: CEncode.C                            Version 1.0

   ************************************************************* */

#include <iostream.h>
#include <string.h>
#include <math.h>


#include "CEncode.h"

// #define DEBUG

#define PRG_NAME "\nCEncode"


// constructor 
CEncode::CEncode()
{
	strcpy(bin2[0],"0");
	strcpy(bin2[1],"1");

	strcpy(bin4[0],"0 0");
	strcpy(bin4[1],"0 1");
	strcpy(bin4[2],"1 0");
	strcpy(bin4[3],"1 1");

	strcpy(bin8[0],"0 0 0");
	strcpy(bin8[1],"0 0 1");
	strcpy(bin8[2],"0 1 0");
	strcpy(bin8[3],"0 1 1");
	strcpy(bin8[4],"1 0 0");
	strcpy(bin8[5],"1 0 1");
	strcpy(bin8[6],"1 1 0");
	strcpy(bin8[7],"1 1 1");

	strcpy(bin16[0] ,"0 0 0 0");
	strcpy(bin16[1] ,"0 0 0 1");
	strcpy(bin16[2] ,"0 0 1 0");
	strcpy(bin16[3] ,"0 0 1 1");
	strcpy(bin16[4] ,"0 1 0 0");
	strcpy(bin16[5] ,"0 1 0 1");
	strcpy(bin16[6] ,"0 1 1 0");
	strcpy(bin16[7] ,"0 1 1 1");
	strcpy(bin16[8] ,"1 0 0 0");
	strcpy(bin16[9] ,"1 0 0 1");
	strcpy(bin16[10],"1 0 1 0");
	strcpy(bin16[11],"1 0 1 1");
	strcpy(bin16[12],"1 1 0 0");
	strcpy(bin16[13],"1 1 0 1");
	strcpy(bin16[14],"1 1 1 0");
	strcpy(bin16[15],"1 1 1 1");

	strcpy(bit2[0],"0 1");
	strcpy(bit2[1],"1 0");

	strcpy(bit4[0],"0 0 0 1");
	strcpy(bit4[1],"0 0 1 0");
	strcpy(bit4[2],"0 1 0 0");
	strcpy(bit4[3],"1 0 0 0");

	strcpy(bit5[0],"0 0 0 0 1");
	strcpy(bit5[1],"0 0 0 1 0");
	strcpy(bit5[2],"0 0 1 0 0");
	strcpy(bit5[3],"0 1 0 0 0");
	strcpy(bit5[4],"1 0 0 0 0");

	strcpy(bit8[0],"0 0 0 0 0 0 0 1");
	strcpy(bit8[1],"0 0 0 0 0 0 1 0");
	strcpy(bit8[2],"0 0 0 0 0 1 0 0");
	strcpy(bit8[3],"0 0 0 0 1 0 0 0");
	strcpy(bit8[4],"0 0 0 1 0 0 0 0");
	strcpy(bit8[5],"0 0 1 0 0 0 0 0");
	strcpy(bit8[6],"0 1 0 0 0 0 0 0");
	strcpy(bit8[7],"1 0 0 0 0 0 0 0");

	strcpy(bit12[0] ,"0 0 0 0 0 0 0 0 0 0 0 1");
	strcpy(bit12[1] ,"0 0 0 0 0 0 0 0 0 0 1 0");
	strcpy(bit12[2] ,"0 0 0 0 0 0 0 0 0 1 0 0");
	strcpy(bit12[3] ,"0 0 0 0 0 0 0 0 1 0 0 0");
	strcpy(bit12[4] ,"0 0 0 0 0 0 0 1 0 0 0 0");
	strcpy(bit12[5] ,"0 0 0 0 0 0 1 0 0 0 0 0");
	strcpy(bit12[6] ,"0 0 0 0 0 1 0 0 0 0 0 0");
	strcpy(bit12[7] ,"0 0 0 0 1 0 0 0 0 0 0 0");
	strcpy(bit12[8] ,"0 0 0 1 0 0 0 0 0 0 0 0");
	strcpy(bit12[9] ,"0 0 1 0 0 0 0 0 0 0 0 0");
	strcpy(bit12[10],"0 1 0 0 0 0 0 0 0 0 0 0");
	strcpy(bit12[11],"1 0 0 0 0 0 0 0 0 0 0 0");

}

// private function for casting double to int 
int CEncode::Round(double value)
{
	#if DEBUG
		cout << PRG_NAME << "::Round(...)" << "\n";
	#endif
	return (int) floor(value + 0.5);
}

// removes the space in a string 
char * CEncode::RmSpace(char * inp)
{
	#if DEBUG
		cout << PRG_NAME << "::Round(...)" << "\n";
	#endif
	char * retVal;
	int i, j=0;
	retVal = new char[strlen(inp)];
	for(i=0; i< (int)strlen(inp);i++)
		if(inp[i] != ' ')
			retVal[j++]=inp[i];
	retVal[j]='\0';
	return retVal;
}

// discrete digit
double CEncode::Discret10(double value, double min = 0, double max=10)
{
        #if DEBUG
                cout << PRG_NAME << "::Discret10(...)" << "\n";
        #endif
	if (value < min ) value = min;
	if (value > max ) value = max;
	return ((double)Round( ((value - min) / (max - min) ) * 10)) /10;
}

// binary 
char * CEncode::BinEncode2( double value, double min = 0, double max = 1)
{
	#if DEBUG
		cout << PRG_NAME << "::BinEncode2(...)" << "\n";
	#endif
	const int min_index=0,
		  max_index=1;
	int index;
	index = Round(((value-min)/(max-min)) * max_index);
	if (index <= min_index) index = min_index;
	if (index >= max_index) index = max_index;
	return bin2[index];
}

char * CEncode::BinEncode4( double value, double min = 0, double max =3)
{
	#if DEBUG
		cout << PRG_NAME << "::BinEncode4(...)" << "\n";
	#endif
	const int min_index=0,
		  max_index=3;
	int index;
	index = Round(((value-min)/(max-min)) * max_index);
	if (index <= min_index) index = min_index;
	if (index >= max_index) index = max_index;
	return bin4[index];
}

char * CEncode::BinEncode8( double value, double min = 0, double max =7)
{
	#if DEBUG
		cout << PRG_NAME << "::BinEncode8(...)" << "\n";
	#endif
	const int min_index=0,
		  max_index=7;
	int index;
	index = Round(((value-min)/(max-min)) * max_index);
	if (index <= min_index) index = min_index;
	if (index >= max_index) index = max_index;
	return bin8[index];
}

char * CEncode::BinEncode16(double value, double min = 0, double max =15)
{
	#if DEBUG
		cout << PRG_NAME << "::BinEncode16(...)" << "\n";
	#endif
	const int min_index=0,
		  max_index=15;
	int index;
	index = Round(((value-min)/(max-min)) * max_index);
	if (index <= min_index) index = min_index;
	if (index >= max_index) index = max_index;
	return bin16[index];
}

// bit vector
char * CEncode::BitEncode2( double value, double min = 0, double max =1)
{
	#if DEBUG
		cout << PRG_NAME << "::BitEncode2(...)" << "\n";
	#endif
	const int min_index=0,
		  max_index=1;
	int index;
	index = Round(((value-min)/(max-min)) * max_index);
	if (index <= min_index) index = min_index;
	if (index >= max_index) index = max_index;
	return bit2[index];
}

char * CEncode::BitEncode4( double value, double min = 0, double max =3)
{
	#if DEBUG
		cout << PRG_NAME << "::BitEncode4(...)" << "\n";
	#endif
	const int min_index=0,
		  max_index=3;
	int index;
	index = Round(((value-min)/(max-min)) * max_index);
	if (index <= min_index) index = min_index;
	if (index >= max_index) index = max_index;
	return bit4[index];
}

char * CEncode::BitEncode5( double value, double min = 0, double max =4)
{
        #if DEBUG
                cout << PRG_NAME << "::BitEncode5(...)" << "\n";
        #endif
        const int min_index=0,
                  max_index=4;
        int index;  
        index = Round(((value-min)/(max-min)) * max_index);
        if (index <= min_index) index = min_index;
        if (index >= max_index) index = max_index;
        return bit5[index];
}

char * CEncode::BitEncode8( double value, double min = 0, double max =7)
{
	#if DEBUG
		cout << PRG_NAME << "::BitEncode8(...)" << "\n";
	#endif
	const int min_index=0,
		  max_index=7;
	int index;
	index = Round(((value-min)/(max-min)) * max_index);
	if (index <= min_index) index = min_index;
	if (index >= max_index) index = max_index;
	return bit8[index];
}

char * CEncode::BitEncode12(double value, double min = 0, double max =11)
{
	#if DEBUG
		cout << PRG_NAME << "::BitEncode12(...)" << "\n";
	#endif
	const int min_index=0,
		  max_index=11;
	int index;
	index = Round(((value-min)/(max-min)) * max_index);
	if (index <= min_index) index = min_index;
	if (index >= max_index) index = max_index;
	return bit12[index];
}
