88
99datapoints_t * datapointsCreate (int fieldCount , char * * fieldNames , int frameCapacity )
1010{
11- datapoints_t * result = (datapoints_t * ) malloc (sizeof (datapoints_t ));
11+ datapoints_t * result = (datapoints_t * ) malloc (sizeof (datapoints_t ));
1212
13- result -> fieldCount = fieldCount ;
14- result -> fieldNames = fieldNames ;
13+ result -> fieldCount = fieldCount ;
14+ result -> fieldNames = fieldNames ;
1515
16- result -> frameCount = 0 ;
17- result -> frameCapacity = frameCapacity ;
16+ result -> frameCount = 0 ;
17+ result -> frameCapacity = frameCapacity ;
1818
19- result -> frames = malloc (sizeof (* result -> frames ) * fieldCount * frameCapacity );
20- result -> frameTime = calloc (1 , sizeof (* result -> frameTime ) * frameCapacity );
21- result -> frameGap = calloc (1 , sizeof (* result -> frameGap ) * frameCapacity );
19+ result -> frames = malloc (sizeof (* result -> frames ) * fieldCount * frameCapacity );
20+ result -> frameTime = calloc (1 , sizeof (* result -> frameTime ) * frameCapacity );
21+ result -> frameGap = calloc (1 , sizeof (* result -> frameGap ) * frameCapacity );
2222
23- return result ;
23+ return result ;
2424}
2525
2626void datapointsDestroy (datapoints_t * points )
2727{
28- free (points -> frames );
29- free (points -> frameTime );
30- free (points -> frameGap );
31- free (points );
28+ free (points -> frames );
29+ free (points -> frameTime );
30+ free (points -> frameGap );
31+ free (points );
3232}
3333
3434/**
@@ -37,81 +37,81 @@ void datapointsDestroy(datapoints_t *points)
3737 */
3838void datapointsSmoothField (datapoints_t * points , int fieldIndex , int windowRadius )
3939{
40- int windowSize = windowRadius * 2 + 1 ;
41- // How many of the frames in the history actually have a valid value in them (so we can average only those)
42- int valuesInHistory = 0 ;
40+ int windowSize = windowRadius * 2 + 1 ;
41+ // How many of the frames in the history actually have a valid value in them (so we can average only those)
42+ int valuesInHistory = 0 ;
4343
44- int64_t accumulator ;
44+ int64_t accumulator ;
4545
46- if (fieldIndex < 0 || fieldIndex >= points -> fieldCount ) {
47- fprintf (stderr , "Attempt to smooth field that doesn't exist %d\n" , fieldIndex );
48- exit (-1 );
49- }
46+ if (fieldIndex < 0 || fieldIndex >= points -> fieldCount ) {
47+ fprintf (stderr , "Attempt to smooth field that doesn't exist %d\n" , fieldIndex );
48+ exit (-1 );
49+ }
5050
51- // Field values so that we know what they were originally before we overwrote them
52- int32_t * history = (int32_t * ) malloc (sizeof (* history ) * windowSize );
53- int historyHead = 0 ; //Points to the next location to insert into
54- int historyTail = 0 ; //Points to the last value in the window
51+ // Field values so that we know what they were originally before we overwrote them
52+ int32_t * history = (int32_t * ) malloc (sizeof (* history ) * windowSize );
53+ int historyHead = 0 ; //Points to the next location to insert into
54+ int historyTail = 0 ; //Points to the last value in the window
5555
56- int windowCenterIndex ;
57- int partitionLeft , partitionRight ;
58- int windowLeftIndex , windowRightIndex ;
56+ int windowCenterIndex ;
57+ int partitionLeft , partitionRight ;
58+ int windowLeftIndex , windowRightIndex ;
5959
60- for (windowCenterIndex = 0 ; windowCenterIndex < points -> frameCount ; ) {
61- partitionLeft = windowCenterIndex ;
62- //We'll refine this guess later if we find discontinuities:
63- partitionRight = points -> frameCount ;
60+ for (windowCenterIndex = 0 ; windowCenterIndex < points -> frameCount ; ) {
61+ partitionLeft = windowCenterIndex ;
62+ //We'll refine this guess later if we find discontinuities:
63+ partitionRight = points -> frameCount ;
6464
65- /*
66- * We start the right edge of the window at the beginning of the partition so that the main loop can begin by
67- * accumulating windowRadius points in the history. Those are the values we'll need before we can work out
68- * the moving average of the first value of the partition.
69- */
70- windowCenterIndex = windowCenterIndex - windowRadius ;
65+ /*
66+ * We start the right edge of the window at the beginning of the partition so that the main loop can begin by
67+ * accumulating windowRadius points in the history. Those are the values we'll need before we can work out
68+ * the moving average of the first value of the partition.
69+ */
70+ windowCenterIndex = windowCenterIndex - windowRadius ;
7171
72- windowLeftIndex = windowCenterIndex - windowRadius ;
73- windowRightIndex = windowCenterIndex + windowRadius ;
72+ windowLeftIndex = windowCenterIndex - windowRadius ;
73+ windowRightIndex = windowCenterIndex + windowRadius ;
7474
75- accumulator = 0 ;
76- valuesInHistory = 0 ;
77- historyHead = 0 ;
78- historyTail = 0 ;
75+ accumulator = 0 ;
76+ valuesInHistory = 0 ;
77+ historyHead = 0 ;
78+ historyTail = 0 ;
7979
80- //The main loop, where we march our [leftIndex...rightIndex] history window along until we exhaust this partition
81- for (; windowCenterIndex < partitionRight ; windowCenterIndex ++ , windowLeftIndex ++ , windowRightIndex ++ ) {
80+ //The main loop, where we march our [leftIndex...rightIndex] history window along until we exhaust this partition
81+ for (; windowCenterIndex < partitionRight ; windowCenterIndex ++ , windowLeftIndex ++ , windowRightIndex ++ ) {
8282
83- // Oldest value falls out of the window
84- if (windowLeftIndex - 1 >= partitionLeft ) {
85- accumulator -= history [historyTail ];
86- historyTail = (historyTail + 1 ) % windowSize ;
83+ // Oldest value falls out of the window
84+ if (windowLeftIndex - 1 >= partitionLeft ) {
85+ accumulator -= history [historyTail ];
86+ historyTail = (historyTail + 1 ) % windowSize ;
8787
88- valuesInHistory -- ;
89- }
88+ valuesInHistory -- ;
89+ }
9090
91- //New value is added to the window
92- if (windowRightIndex < partitionRight ) {
93- int32_t fieldValue = (int32_t ) points -> frames [points -> fieldCount * windowRightIndex + fieldIndex ];
91+ //New value is added to the window
92+ if (windowRightIndex < partitionRight ) {
93+ int32_t fieldValue = (int32_t ) points -> frames [points -> fieldCount * windowRightIndex + fieldIndex ];
9494
95- accumulator += fieldValue ;
95+ accumulator += fieldValue ;
9696
97- history [historyHead ] = fieldValue ;
98- historyHead = (historyHead + 1 ) % windowSize ;
97+ history [historyHead ] = fieldValue ;
98+ historyHead = (historyHead + 1 ) % windowSize ;
9999
100- valuesInHistory ++ ;
100+ valuesInHistory ++ ;
101101
102- //If there is a discontinuity after this point, adjust the right edge of the partition so we stop looking further
103- if (points -> frameGap [windowRightIndex ])
104- partitionRight = windowRightIndex + 1 ;
105- }
102+ //If there is a discontinuity after this point, adjust the right edge of the partition so we stop looking further
103+ if (points -> frameGap [windowRightIndex ])
104+ partitionRight = windowRightIndex + 1 ;
105+ }
106106
107- // Store the average of the history window into the frame in the center of the window
108- if (windowCenterIndex >= partitionLeft ) {
109- points -> frames [points -> fieldCount * windowCenterIndex + fieldIndex ] = (int32_t )(accumulator / valuesInHistory );
110- }
111- }
112- }
107+ // Store the average of the history window into the frame in the center of the window
108+ if (windowCenterIndex >= partitionLeft ) {
109+ points -> frames [points -> fieldCount * windowCenterIndex + fieldIndex ] = (int32_t )(accumulator / valuesInHistory );
110+ }
111+ }
112+ }
113113
114- free (history );
114+ free (history );
115115}
116116
117117/**
@@ -121,63 +121,63 @@ void datapointsSmoothField(datapoints_t *points, int fieldIndex, int windowRadiu
121121 */
122122int datapointsFindFrameAtTime (datapoints_t * points , int64_t time )
123123{
124- int i , lastGoodFrame = -1 ;
124+ int i , lastGoodFrame = -1 ;
125125
126- //TODO make me a binary search
127- for (i = 0 ; i < points -> frameCount ; i ++ ) {
128- if (time < points -> frameTime [i ]) {
129- return lastGoodFrame ;
130- }
131- lastGoodFrame = i ;
132- }
126+ //TODO make me a binary search
127+ for (i = 0 ; i < points -> frameCount ; i ++ ) {
128+ if (time < points -> frameTime [i ]) {
129+ return lastGoodFrame ;
130+ }
131+ lastGoodFrame = i ;
132+ }
133133
134- return lastGoodFrame ;
134+ return lastGoodFrame ;
135135}
136136
137137bool datapointsGetFrameAtIndex (datapoints_t * points , int frameIndex , int64_t * frameTime , int32_t * frame )
138138{
139- if (frameIndex < 0 || frameIndex >= points -> frameCount )
140- return false;
139+ if (frameIndex < 0 || frameIndex >= points -> frameCount )
140+ return false;
141141
142- memcpy (frame , points -> frames + frameIndex * points -> fieldCount , points -> fieldCount * sizeof (* points -> frames ));
143- * frameTime = points -> frameTime [frameIndex ];
142+ memcpy (frame , points -> frames + frameIndex * points -> fieldCount , points -> fieldCount * sizeof (* points -> frames ));
143+ * frameTime = points -> frameTime [frameIndex ];
144144
145- return true;
145+ return true;
146146}
147147
148148bool datapointsGetFieldAtIndex (datapoints_t * points , int frameIndex , int fieldIndex , int32_t * frameValue )
149149{
150- if (frameIndex < 0 || frameIndex >= points -> frameCount )
151- return false;
150+ if (frameIndex < 0 || frameIndex >= points -> frameCount )
151+ return false;
152152
153- * frameValue = points -> frames [frameIndex * points -> fieldCount + fieldIndex ];
153+ * frameValue = points -> frames [frameIndex * points -> fieldCount + fieldIndex ];
154154
155- return true;
155+ return true;
156156}
157157
158158bool datapointsSetFieldAtIndex (datapoints_t * points , int frameIndex , int fieldIndex , int32_t frameValue )
159159{
160- if (frameIndex < 0 || frameIndex >= points -> frameCount )
161- return false;
160+ if (frameIndex < 0 || frameIndex >= points -> frameCount )
161+ return false;
162162
163- points -> frames [frameIndex * points -> fieldCount + fieldIndex ] = frameValue ;
163+ points -> frames [frameIndex * points -> fieldCount + fieldIndex ] = frameValue ;
164164
165- return true;
165+ return true;
166166}
167167
168168bool datapointsGetTimeAtIndex (datapoints_t * points , int frameIndex , int64_t * frameTime )
169169{
170- if (frameIndex < 0 || frameIndex >= points -> frameCount )
171- return false;
170+ if (frameIndex < 0 || frameIndex >= points -> frameCount )
171+ return false;
172172
173- * frameTime = points -> frameTime [frameIndex ];
173+ * frameTime = points -> frameTime [frameIndex ];
174174
175- return true;
175+ return true;
176176}
177177
178178bool datapointsGetGapStartsAtIndex (datapoints_t * points , int frameIndex )
179179{
180- return frameIndex >= 0 && frameIndex < points -> frameCount && points -> frameGap [frameIndex ];
180+ return frameIndex >= 0 && frameIndex < points -> frameCount && points -> frameGap [frameIndex ];
181181}
182182
183183/**
@@ -186,22 +186,22 @@ bool datapointsGetGapStartsAtIndex(datapoints_t *points, int frameIndex)
186186 */
187187bool datapointsAddFrame (datapoints_t * points , int64_t frameTime , const int32_t * frame )
188188{
189- if (points -> frameCount >= points -> frameCapacity )
190- return false;
189+ if (points -> frameCount >= points -> frameCapacity )
190+ return false;
191191
192- points -> frameTime [points -> frameCount ] = frameTime ;
193- memcpy (points -> frames + points -> frameCount * points -> fieldCount , frame , points -> fieldCount * sizeof (* points -> frames ));
192+ points -> frameTime [points -> frameCount ] = frameTime ;
193+ memcpy (points -> frames + points -> frameCount * points -> fieldCount , frame , points -> fieldCount * sizeof (* points -> frames ));
194194
195- points -> frameCount ++ ;
195+ points -> frameCount ++ ;
196196
197- return true;
197+ return true;
198198}
199199
200200/**
201201 * Mark that a gap in the log begins after the last frame added.
202202 */
203203void datapointsAddGap (datapoints_t * points )
204204{
205- if (points -> frameCount > 0 )
206- points -> frameGap [points -> frameCount - 1 ] = 1 ;
205+ if (points -> frameCount > 0 )
206+ points -> frameGap [points -> frameCount - 1 ] = 1 ;
207207}
0 commit comments