#include "StdAfx.h"
#include "GdataTable.h"
//***************************************************************************
GdataTable::GdataTable(void)
{
}
//***************************************************************************
GdataTable::~GdataTable(void)
{
	delAll();
}
//***************************************************************************
int* GdataTable::getI(int row, int col)
{
	if(col>=colm.n || col<0)
		return NULL;
	if(colm[col].type !=Tint)
		return NULL;
	return &(*(Cgarray<int>*)buf[col])[row];
}
//***************************************************************************
__int64* GdataTable::getI64(int row, int col)
{
	if(col>=colm.n || col<0)
		return NULL;
	if(colm[col].type !=Tint64)
		return NULL;
	return &(*(Cgarray<__int64>*)buf[col])[row];
}
//***************************************************************************
double* GdataTable::getD(int row, int col)
{
	if(col>=colm.n || col<0)
		return NULL;
	if(colm[col].type !=Tdouble)
		return NULL;
	return &(*(Cgarray<double>*)buf[col])[row];
}
//***************************************************************************
bool* GdataTable::getBool(int row, int col)
{
	if(col>=colm.n || col<0)
		return NULL;
	if(colm[col].type !=Tbool)
		return NULL;
	return &(*(Cgarray<bool>*)buf[col])[row];
}
//***************************************************************************
char* GdataTable::getS(int row, int col)
{
	if(col>=colm.n || col<0)
		return NULL;
	if(colm[col].type !=Tstring)
		return NULL;
	return &(*(Cgarray<char>*)buf[col])[row*colm[col].nb];
}
//***************************************************************************
void* GdataTable::getBin(int row, int col)
{
	if(col>=colm.n || col<0)
		return NULL;
	if(colm[col].type !=Tbin)
		return NULL;
	return (void*)&(*(Cgarray<char>*)buf[col])[row*colm[col].nb];
}
//***************************************************************************
void* GdataTable::get(int row, int col)
{
	if(col>=colm.n || col<0)
		return NULL;
	switch(colm[col].type)
	{
	case(Tbool):
		return(void*)&(*(Cgarray<bool>*)buf[col])[row];
	case(Tint):
		return(void*)&(*(Cgarray<int>*)buf[col])[row];
	case(Tdouble):
		return(void*)&(*(Cgarray<double>*)buf[col])[row];
	case(Tint64):
		return(void*)&(*(Cgarray<__int64>*)buf[col])[row];
	case(Tstring):
	case(Tbin):
		return(void*)&(*(Cgarray<char>*)buf[col])[row*colm[col].nb];
	default:
		break;
	}
	return NULL;
}
//***************************************************************************
bool GdataTable::addColm(char* name, int type, int size)
{
	colmName.add(name);
	GdataTableColum inf;
	void* c = NULL;
	inf.flags=0;
	inf.nb=size;
	inf.type=type;
	int nn =nRow();
	switch(type)
	{
	case(Tbool):
		c = new Cgarray<bool>();
		if(!inf.nb)
			inf.nb = sizeof(bool);
		if(nn>0)
		{
			if(!((*(Cgarray<bool>*)c)+=nn))
				return false;
			((Cgarray<bool>*)c)->n = nn;
		}
		break;
	case(Tint):
		c = new Cgarray<int>();
		inf.nb = sizeof(long);
		if(nn>0)
		{
			if(!((*(Cgarray<int>*)c)+=nn))
				return false;
			((Cgarray<int>*)c)->n = nn;
		}
		break;
	case(Tdouble):
		c = new Cgarray<double>();
		inf.nb = sizeof(double);
		if(nn>0)
		{
			if(!((*(Cgarray<double>*)c)+=nn))
				return false;
			((Cgarray<double>*)c)->n = nn;
		}
		break;
	case(Tint64):
		c = new Cgarray<__int64>();
		inf.nb = sizeof(__int64);
		if(nn>0)
		{
			if(!((*(Cgarray<__int64>*)c)+=nn))
				return false;
			((Cgarray<__int64>*)c)->n = nn;
		}
		break;
	case(Tstring):
	case(Tbin):
		c = new Cgarray<char>();
		if(nn>0)
		{
			if(!((*(Cgarray<char>*)c)+=(nn*inf.nb)))
				return false;
			((Cgarray<char>*)c)->n = (nn*inf.nb);
		}
		break;
	default:
		return false;
	}
	if(!c)
		return false;
	if(!(buf+c))
		return false;
	if(!(colm+inf))
		return false;
	return true;
}
//***************************************************************************
bool GdataTable::InsertColm(int icol, char* name, int type, int size)
{
	return false;
	/*if(icol <colmName.n_i)
		colmName.a*/
	colmName.add(name);
	GdataTableColum inf;
	void* c = NULL;
	inf.flags = 0;
	inf.nb = size;
	inf.type = type;
	int nn = nRow();
	switch (type)
	{
	case(Tbool):
		c = new Cgarray<bool>();
		if (!inf.nb)
			inf.nb = sizeof(bool);
		if (nn > 0)
		{
			if (!((*(Cgarray<bool>*)c) += nn))
				return false;
			((Cgarray<bool>*)c)->n = nn;
		}
		break;
	case(Tint):
		c = new Cgarray<int>();
		inf.nb = sizeof(long);
		if (nn > 0)
		{
			if (!((*(Cgarray<int>*)c) += nn))
				return false;
			((Cgarray<int>*)c)->n = nn;
		}
		break;
	case(Tdouble):
		c = new Cgarray<double>();
		inf.nb = sizeof(double);
		if (nn > 0)
		{
			if (!((*(Cgarray<double>*)c) += nn))
				return false;
			((Cgarray<double>*)c)->n = nn;
		}
		break;
	case(Tint64):
		c = new Cgarray<__int64>();
		inf.nb = sizeof(__int64);
		if (nn > 0)
		{
			if (!((*(Cgarray<__int64>*)c) += nn))
				return false;
			((Cgarray<__int64>*)c)->n = nn;
		}
		break;
	case(Tstring):
	case(Tbin):
		c = new Cgarray<char>();
		if (nn > 0)
		{
			if (!((*(Cgarray<char>*)c) += (nn*inf.nb)))
				return false;
			((Cgarray<char>*)c)->n = (nn*inf.nb);
		}
		break;
	default:
		return false;
	}
	if (!c)
		return false;
	if (!(buf + c))
		return false;
	if (!(colm + inf))
		return false;
	return true;
}

//***************************************************************************
void GdataTable::delAll()
{
	for(int i =0; i<buf.n; i++)
	{
		switch(colm[i].type)
		{
		case(Tbool):
			delete (Cgarray<bool>*)buf[i];
			break;
		case(Tint):
			delete (Cgarray<int>*)buf[i];
			break;
		case(Tdouble):
			delete (Cgarray<double>*)buf[i];
			break;
		case(Tint64):
			delete (Cgarray<__int64>*)buf[i];
			break;
		case(Tstring):
			delete (Cgarray<char>*)buf[i];
			break;
		case(Tbin):
			delete (Cgarray<char>*)buf[i];
			break;
		default:
			break;
		}
	}
	colm.n =0;
	buf.n =0;
	colmName.n_i =0;
}
//***************************************************************************
void GdataTable::delAllRow()
{
	for(int i =0; i<buf.n; i++)
	{
		switch(colm[i].type)
		{
		case(Tbool):
			((Cgarray<bool>*)buf[i])->n=0;
			break;
		case(Tint):
			((Cgarray<int>*)buf[i])->n=0;
			break;
		case(Tdouble):
			((Cgarray<double>*)buf[i])->n=0;
			break;
		case(Tint64):
			((Cgarray<__int64>*)buf[i])->n=0;
			break;
		case(Tstring):
			((Cgarray<char>*)buf[i])->n=0;
			break;
		case(Tbin):
			((Cgarray<char>*)buf[i])->n=0;
			break;
		default:
			break;
		}
	}
}
//***************************************************************************
void GdataTable::removeColm(int i)
{
	switch(colm[i].type)
	{
	case(Tbool):
		delete (Cgarray<bool>*)buf[i];
		break;
	case(Tint):
		delete (Cgarray<int>*)buf[i];
		break;
	case(Tdouble):
		delete (Cgarray<double>*)buf[i];
		break;
	case(Tint64):
		delete (Cgarray<__int64>*)buf[i];
		break;
	case(Tstring):
		delete (Cgarray<char>*)buf[i];
		break;
	case(Tbin):
		delete (Cgarray<char>*)buf[i];
		break;
	default:
		break;
	}
	colm.remove(i);
	buf.remove(i);
	colmName.borra(i);
	
}
//***************************************************************************
int GdataTable::nRow()
{
	int i =0;
	if(colm.n<=0)
		return 0;
	switch(colm[i].type)
	{
	case(Tbool):
		return((Cgarray<bool>*)buf[i])->n;
	case(Tint):
		return((Cgarray<int>*)buf[i])->n;
	case(Tdouble):
		return((Cgarray<double>*)buf[i])->n;
	case(Tint64):
		return((Cgarray<__int64>*)buf[i])->n;
	case(Tstring):
		return((Cgarray<char>*)buf[i])->n/ colm[i].nb;
	case(Tbin):
		return((Cgarray<char>*)buf[i])->n / colm[i].nb;
	default:
		break;
	}
	return -1;
}
//***************************************************************************
int GdataTable::getType(int icol)
{
	if(icol<0 || icol>= colm.n)
		return Tndef;
	return colm[icol].type;
}
//***************************************************************************
int GdataTable::getInd(char* colName)
{
	for(int i =0; i<colm.n; i++)
	{
		if(!strcmp(colName,colmName.get(i)))
			return i;
	}
	return -1;
}
//***************************************************************************
bool GdataTable::addRow(int nrow)
{
	for(int i =0; i<buf.n; i++)
	{
		switch(colm[i].type)
		{
		case(Tbool):
			if(!((*(Cgarray<bool>*)buf[i])+=nrow))
				return false;
			(*(Cgarray<bool>*)buf[i]).n+=nrow;
			break;
		case(Tint):
			if(!((*(Cgarray<int>*)buf[i])+=nrow))
				return false;
			(*(Cgarray<int>*)buf[i]).n+=nrow;
			break;
		case(Tdouble):
			if(!((*(Cgarray<double>*)buf[i])+=nrow))
				return false;
			(*(Cgarray<double>*)buf[i]).n+=nrow;
			break;
		case(Tint64):
			if(!((*(Cgarray<__int64>*)buf[i])+=nrow))
				return false;
			(*(Cgarray<__int64>*)buf[i]).n+=nrow;
			break;
		case(Tstring):
			if(!((*(Cgarray<char>*)buf[i])+=(nrow*colm[i].nb)))
				return false;
			(*(Cgarray<char>*)buf[i]).n+=(nrow*colm[i].nb);
			break;
		case(Tbin):
			if(!((*(Cgarray<char>*)buf[i])+=(nrow*colm[i].nb)))
				return false;
			(*(Cgarray<char>*)buf[i]).n+=(nrow*colm[i].nb);
			break;
		default:
			return false;

		}
	}
	return true;
}
//***************************************************************************
void GdataTable::removeRow(int i)
{
	for(int i =0; i<buf.n; i++)
	{
		switch(colm[i].type)
		{
		case(Tbool):
			(*(Cgarray<bool>*)buf[i]).remove(i);
			break;
		case(Tint):
			(*(Cgarray<int>*)buf[i]).remove(i);
			break;
		case(Tdouble):
			(*(Cgarray<double>*)buf[i]).remove(i);
			break;
		case(Tint64):
			(*(Cgarray<__int64>*)buf[i]).remove(i);
				
			break;
		case(Tstring):
			(*(Cgarray<char>*)buf[i]).remove(i, colm[i].nb);
			break;
		case(Tbin):
			(*(Cgarray<char>*)buf[i]).remove(i, colm[i].nb);
			break;
		default:
			break;
		}
	}
}
//***************************************************************************
int GdataTable::nColm()
{
	return colm.n;
}
//***************************************************************************
int GdataTable::getSize( int icol )
{
	if(icol<0 || icol>colm.n)
		return -1;
	return colm[icol].nb;
}
//***************************************************************************
char* GdataTable::getName( int icol )
{
	if(icol<0 || icol>colm.n)
		return 0;
	return colmName.get(icol);
}
//***************************************************************************
bool GdataTable::rename(int icol, char *nomb)
{
	if (icol<0 || icol>colm.n)
		return 0;
	return colmName.replace(icol,nomb);
}
//***************************************************************************
bool GdataTable::addMemRow( int nrow )
{
	for(int i =0; i<buf.n; i++)
	{
		switch(colm[i].type)
		{
		case(Tbool):
			if(!((*(Cgarray<bool>*)buf[i])+=nrow))
				return false;
			break;
		case(Tint):
			if(!((*(Cgarray<int>*)buf[i])+=nrow))
				return false;
			break;
		case(Tdouble):
			if(!((*(Cgarray<double>*)buf[i])+=nrow))
				return false;
			break;
		case(Tint64):
			if(!((*(Cgarray<__int64>*)buf[i])+=nrow))
				return false;
			break;
		case(Tstring):
			if(!((*(Cgarray<char>*)buf[i])+=(nrow*colm[i].nb)))
				return false;
			break;
		case(Tbin):
			if(!((*(Cgarray<char>*)buf[i])+=(nrow*colm[i].nb)))
				return false;
			break;
		default:
			return false;

		}
	}
	return true;
}
//***************************************************************************
int GdataTable::getSizeASCII( int icol )
{
	if(!colm[icol].nb)
	{
		switch(colm[icol].type)
		{
		case(Tbool):
			return 1;
		case(Tint):
			return 4;
		case(Tdouble):
			return 8;
		case(Tint64):
			return 8;
		case(Tbin):
		case(Tstring):
			return colm[icol].nb;
		default:
			return -1;
		}
	}
	else
		return colm[icol].nb;
}
//***************************************************************************