www.pudn.com > B-spline.zip > intFIFO.cpp, change:2012-03-17,size:6032b


#include"intFIFO.h" 
#include<malloc.h> 
 
#define RECENT_SENSOR_NUM 10 
 
intFIFO::intFIFO(int Size) 
{ 
	int i; 
	intFIFOADT *pt; 
	FIFOSize=Size; 
	FIFOBuf=new intFIFOADT[Size]; 
	FIFOBuf[0].value=0; 
	FIFOBuf[0].next=FIFOBuf+1; 
	for(i=1,pt=FIFOBuf;i<Size-1;i++) 
	{ 
		FIFOBuf[i].value=0; 
		FIFOBuf[i].next=FIFOBuf+i+1; 
		FIFOBuf[i].prev=FIFOBuf+i-1; 
	} 
	FIFOBuf[i].prev=FIFOBuf+i-1; 
	FIFOBuf[i].next=FIFOBuf; 
	FIFOBuf[i].value=0; 
	FIFOBuf[0].prev=FIFOBuf+i; 
	wrpt=FIFOBuf; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","w"); 
		fprintf(log,"initialize\n"); 
		fclose(log); 
	} 
#endif 
} 
/* 
intFIFO::intFIFO() 
{ 
	int i; 
	intFIFOADT *pt; 
	FIFOSize=_INTFIFO_FIFOSIZE; 
	for(i=0,pt=FIFOBuf;i<FIFOSize-1;i++) 
	{ 
		FIFOBuf[i].value=0; 
		FIFOBuf[i].next=FIFOBuf+i+1; 
	} 
	FIFOBuf[i].next=FIFOBuf; 
	FIFOBuf[i].value=0; 
	wrpt=FIFOBuf; 
} 
*/ 
 
void intFIFO::clrFIFO() 
{ 
	intFIFOADT *pt; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---clrfifo\n"); 
		fclose(log); 
	} 
#endif 
	wrpt=FIFOBuf; 
	pt=FIFOBuf; 
	do 
	{ 
		pt->value=0; 
		pt=pt->next; 
	}while(pt!=FIFOBuf); 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---clrfifo\n"); 
		fclose(log); 
	} 
#endif 
} 
 
int intFIFO::pushFIFO(int newValue) 
{ 
	int tmp; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---pushFIFO\n"); 
		fclose(log); 
	} 
#endif 
	tmp=wrpt->value; 
	wrpt->value=newValue; 
	wrpt=wrpt->next; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---pushFIFO\n"); 
		fclose(log); 
	} 
#endif 
	return tmp; 
} 
long intFIFO::getTotal() 
{ 
	int i; 
	long total; 
	intFIFOADT *pt; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---getTotal\n"); 
		fclose(log); 
	} 
#endif 
	for(i=0,total=0,pt=FIFOBuf;i<FIFOSize;i++,pt=pt->next) 
	{ 
		total+=pt->value; 
	} 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---getTotal\n"); 
		fclose(log); 
	} 
#endif 
	return total; 
} 
 
intFIFO::~intFIFO() 
{ 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"class release\n"); 
		fclose(log); 
	} 
#endif 
	delete [] FIFOBuf; 
} 
int intFIFO::SaveToFile(char *filename) 
{ 
	int i; 
	intFIFOADT *p; 
	FILE *f=NULL; 
	fopen_s(&f,filename,"w"); 
	if(f==NULL) 
		return -1; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---SaveToFile\n"); 
		fclose(log); 
	} 
#endif 
	for(p=wrpt,i=0;i<FIFOSize;p=p->next,i++) 
	{ 
		fwrite(&(p->value),sizeof(int),1,f); 
	} 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---SaveToFile\n"); 
		fclose(log); 
	} 
#endif 
	fclose(f); 
	return 0; 
} 
int intFIFO::LoadFromFile(char *filename) 
{ 
	int i; 
	intFIFOADT *p; 
	FILE *f; 
	fopen_s(&f,filename,"r"); 
	if(!f) 
	{ 
		return -1; 
	} 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---LoadFromFile\n"); 
		fclose(log); 
	} 
#endif 
	wrpt=FIFOBuf; 
	for(p=wrpt,i=0;i<FIFOSize;p=p->next,i++) 
	{ 
		fread(&(p->value),sizeof(int),1,f); 
	} 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---LoadFromFile\n"); 
		fclose(log); 
	} 
#endif 
	fclose(f); 
	return 0; 
} 
int intFIFO::ReadFIFOElement(int index) 
{ 
	intFIFOADT *p; 
	int tmp; 
	int i=0; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---ReadFIFOElement\n"); 
		fclose(log); 
	} 
#endif 
	for(p=wrpt;i<index;i++,p=p->next) ; 
	tmp=p->value; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---ReadFIFOElement\n"); 
		fclose(log); 
	} 
#endif 
	return tmp; 
} 
int intFIFO::ValueExist(int value) 
{ 
	int i; 
	intFIFOADT *p; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---ValueExist\n"); 
		fclose(log); 
	} 
#endif 
	for(i=0,p=wrpt;i<FIFOSize;i++,p=p->next) 
	{ 
		if(p->value==value) 
			break; 
	} 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---ValueExist\n"); 
		fclose(log); 
	} 
#endif 
	if(i<FIFOSize) 
		return i; 
	else 
		return -1; 
} 
int intFIFO::WriteFIFOElement(int index,int value) 
{ 
	int OriginalValue; 
	intFIFOADT *p; 
	int i; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---WriteFIFOElement\n"); 
		fclose(log); 
	} 
#endif 
	for(i=0,p=wrpt;i<index;i++,p=p->next) ; 
	OriginalValue=p->value; 
	p->value=value; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---WriteFIFOElement\n"); 
		fclose(log); 
	} 
#endif 
	return OriginalValue; 
} 
long intFIFO::SquareDifference() 
{ 
	int i; 
	intFIFOADT *p; 
	long average,sqaverage; 
	average=0; 
	sqaverage=0; 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"enter lock---SquareDifference\n"); 
		fclose(log); 
	} 
#endif 
	for(i=0,p=wrpt;i<FIFOSize;i++,p=p->next) 
	{ 
		average+=p->value; 
		sqaverage+=(p->value)*(p->value); 
	} 
#ifdef INTFIFO_LOG 
	if(FIFOSize==RECENT_SENSOR_NUM) 
	{ 
		fopen_s(&log,"intfifo.log","a"); 
		fprintf(log,"leave lock---SquareDifference\n"); 
		fclose(log); 
	} 
#endif 
	return (sqaverage*FIFOSize-average*average)/FIFOSize; 
} 
int intFIFO::Size() 
{ 
	return FIFOSize; 
}