[GO] implementation of c / c ++> RingBuffer (N margins)

Operation check


C++ Builder XE4

In graph drawing, old data does not fit in the drawing area and is unnecessary, so I wanted a process to delete the old data.

It's easy if you use TList etc., but I implemented the same process myself so that it can be used in C project.

As a process

--keepnum: Minimum number of data you want to keep --margin: Number of data to allow margin

When the number of data reaches (keepnum + margin), the margin is removed. By doing this, the intention is that the process of removing data can be executed at certain intervals without executing it every time data is added.

Processing body


int TimeGraph_KeepLatestNsets(int keepnum, int margin, TG_graphData_t *graphDataPtr, int *dataCount)
{
	if (*dataCount < (keepnum + margin) ) {
		return *dataCount;
	}

	for(int idx = 0; idx < keepnum; idx++) {
		TG_graphData_t *src = &graphDataPtr[idx + margin];
		TG_graphData_t *dst = &graphDataPtr[idx];
		TimeGraph_SetEachData(dst, src->sampleDt, src->val, src->errStatus);
		// set 0 to moved one
		TimeGraph_SetEachData(src, src->sampleDt, 0.0, src->errStatus);
	}
	*dataCount = keepnum;

	return *dataCount;
}

Test function


void Test_TimeGraph_KeepLatestNsets()
{
	TG_graphData_t data[10];
	int dataCnt = 0;

	const int keepnum= 5;
	const int margin = 3;

	for(int idx = 0; idx < (keepnum + margin); idx++) {
		if (idx < keepnum) {
			TimeGraph_SetEachData(&data[idx], Now(), 3.141592, TG_DATA_OK); 
		} else {
			TimeGraph_SetEachData(&data[idx], Now(), 2.7182, TG_DATA_OK); 		
		}
		dataCnt++;
	}

	int preCnt = dataCnt;
	int nowCnt = TimeGraph_KeepLatestNsets(keepnum, margin, &data[0], &dataCnt);
	if (preCnt != nowCnt) {
		// after removal of old data
		int nop551 = 1;
		(void) nop551; //Avoidance of warning "The value assigned to xxx is not used"
	} else {
		int nop554 = 1;
		(void) nop554; //Avoidance of warning "The value assigned to xxx is not used"
	}
}

TG_graphData_t is a structure for data. It uses a member variable called TDateTime, but when using it in C, it will be changed to the built-in data type as appropriate.

TimeGraph_SetEachData(dst, src->sampleDt, src->val, src->errStatus); Sets the data after sampleDt in the structure of the dst pointer. This is done to prevent forgetting to assign some member variables.

You might want to rethink the variable names a bit more.

Recommended Posts

implementation of c / c ++> RingBuffer (N margins)
C> Comma-delimited string itemization> Implementation of extractCsvRow ()
Implementation of Fibonacci sequence
Quantum computer implementation of quantum walk 2
Implementation of TF-IDF using gensim
Implementation of MathJax on Sphinx
Progress of 5/6 ~ C arrangement etc. ~
Explanation and implementation of SocialFoceModel
Implementation of game theory-Prisoner's dilemma-
ARC067 C --Factors of Factorial
Implementation of independent component analysis
Quantum computer implementation of quantum walk 3
Python implementation of particle filters
Declaration of C global variables
Implementation of quicksort in Python
Quantum computer implementation of quantum walk 1
Deep reinforcement learning 2 Implementation of reinforcement learning
Implementation of Scale-space for SIFT
C ++ implementation of protobuf makes Chainer's Caffe model load 10 times faster
Geometry> Clockwise angle calculation of two vectors> Link: python implementation / C implementation