-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsummary.c
155 lines (132 loc) · 4.1 KB
/
summary.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <string.h>
#include "symtypes.h"
#define NUM_HISTORY 10
element_t Elements[ MAX_ELEMENTS ][ NUM_HISTORY ];
#define VMIN 0
#define VMAX 1
#define VCNT 2
#define VSUM 3
#define VTYP VMIN
// For INT/DOUBLE, 0 = max, 1 = min, 2 = count, 3 = sum
// For STRING, 0..N-1 is the unique values.
void InitializeSummary( void )
{
memset( Elements, 0, sizeof( Elements ) );
return;
}
void AddElementToSummary( unsigned int element, element_t *elemobj )
{
switch( elemobj->type )
{
case INT:
if ( Elements[ element ][ VTYP ].type == 0 )
{
// If we have no type, then initialize
Elements[ element ][ VTYP ].type = INT;
Elements[ element ][ VMAX ].u.d = INT_MIN;
Elements[ element ][ VMIN ].u.d = INT_MAX;
}
if ( Elements[ element ][ VMAX ].u.d < elemobj->u.d )
{
// Update max value.
Elements[ element ][ VMAX ].u.d = elemobj->u.d;
}
else if ( Elements[ element ][ VMIN ].u.d > elemobj->u.d )
{
// Update min value.
Elements[ element ][ VMIN ].u.d = elemobj->u.d;
}
// Update count and sum.
Elements[ element ][ VCNT ].u.d++;
Elements[ element ][ VSUM ].u.d += elemobj->u.d;
break;
case DOUBLE:
if ( Elements[ element ][ VTYP ].type == 0 )
{
// If we have no type, then initialize
Elements[ element ][ VTYP ].type = DOUBLE;
Elements[ element ][ VMAX ].u.g = DBL_MIN;
Elements[ element ][ VMIN ].u.g = DBL_MAX;
}
if ( Elements[ element ][ VMAX ].u.g < elemobj->u.g )
{
// Update max value.
Elements[ element ][ VMAX ].u.g = elemobj->u.g;
}
else if ( Elements[ element ][ VMIN ].u.g > elemobj->u.g )
{
// Update min value.
Elements[ element ][ VMIN ].u.g = elemobj->u.g;
}
// Update count and sum.
Elements[ element ][ VCNT ].u.g += 1.0;
Elements[ element ][ VSUM ].u.g += elemobj->u.g;
break;
case STRING:
for ( int i = 0 ; i < NUM_HISTORY ; i++ )
{
if ( Elements[ element ][ i ].type == STRING )
{
// Compare
if ( !strcmp( Elements[ element ][ i ].u.s, elemobj->u.s ) )
{
// Found duplicate, exit.
return;
}
}
else
{
// Found empty slot, add.
memcpy( &Elements[ element ][ i ], elemobj, sizeof( element_t ) );
Elements[ element ][ i ].type = STRING;
return;
}
}
break;
}
return;
}
void Summarize( void )
{
printf("Data Profile:\n\n");
for ( int i = 0 ; i < MAX_ELEMENTS ; i++ )
{
if ( Elements[ i ][ VTYP ].type == INT )
{
printf("Field %d: (INT)\n", i );
printf(" Min: %d\n", Elements[ i ][ VMIN ].u.d );
printf(" Max: %d\n", Elements[ i ][ VMAX ].u.d );
printf(" Avg: %g\n",
( double ) Elements[ i ][ VSUM ].u.d / ( double ) Elements[ i ][ VCNT ].u.d );
printf("\n");
}
else if ( Elements[ i ][ VTYP ].type == DOUBLE )
{
printf("Field %d: (DOUBLE)\n", i );
printf(" Min: %g\n", Elements[ i ][ VMIN ].u.g );
printf(" Max: %g\n", Elements[ i ][ VMAX ].u.g );
printf(" Avg: %g\n", Elements[ i ][ VSUM ].u.g / Elements[ i ][ VCNT ].u.g );
printf("\n");
}
else if ( Elements[ i ][ VTYP ].type == STRING )
{
int j;
printf("Field %d: (STRING)\n", i );
for ( j = 0 ; j < NUM_HISTORY ; j++ )
{
if ( Elements[ i ][ j ].type == STRING )
{
printf( " %s\n", Elements[ i ][ j ].u.s );
}
else break;
}
if ( j == NUM_HISTORY )
{
printf( " ...\n" );
}
printf("\n");
}
}
return;
}