comparison Histogram.c @ 7:fa6a281bd854

Nov 14 vers -- add makeFixedBinHist & Ext version & expected value &fxd name bug
author Me
date Sun, 14 Nov 2010 11:11:44 -0800
parents a2388fae93ff
children c83c27796fad
comparison
equal deleted inserted replaced
4:bd856c869be4 5:4092e6d96a57
5 * Author: seanhalle@yahoo.com 5 * Author: seanhalle@yahoo.com
6 * 6 *
7 */ 7 */
8 #include <stdio.h> 8 #include <stdio.h>
9 #include "Histogram.h" 9 #include "Histogram.h"
10 #include "../vutilities.h"
10 11
11 12
12 /*This Histogram Abstract Data Type has a number of bins plus a range of 13 /*This Histogram Abstract Data Type has a number of bins plus a range of
13 * values that the bins span, both chosen at creation. 14 * values that the bins span, both chosen at creation.
14 * 15 *
40 for( i = 0; i < hist->numBins; i++ ) 41 for( i = 0; i < hist->numBins; i++ )
41 { 42 {
42 hist->bins[ i ] = 0; 43 hist->bins[ i ] = 0;
43 } 44 }
44 45
46 hist->name = NULL;
47 return hist;
48 }
49
50 inline void
51 makeHist_helper( Histogram *hist, int32 numBins,
52 int32 startOfRange, int32 binWidth, char *nameCopy )
53 {
54 hist->numBins = numBins;
55 hist->binWidth = binWidth;
56 hist->endOfRange = startOfRange + hist->binWidth * numBins;
57 hist->startOfRange = startOfRange;
58 hist->name = nameCopy;
59 memset( hist->bins, 0, numBins * sizeof(int32) );
60 }
61
62
63 Histogram *
64 makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth,
65 char *name )
66
67 {
68 Histogram *hist;
69
70 hist = VMS__malloc( sizeof(Histogram) );
71 hist->bins = VMS__malloc( numBins * sizeof(int32) );
72
73 makeHist_helper( hist, numBins, startOfRange, binWidth,VMS__strDup(name));
74
75 return hist;
76 }
77
78 Histogram *
79 makeFixedBinHistExt( int32 numBins, int32 startOfRange, int32 binWidth,
80 char *name )
81
82 {
83 Histogram *hist;
84
85 hist = malloc( sizeof(Histogram) );
86 hist->bins = malloc( numBins * sizeof(int32) );
87
88 makeHist_helper( hist, numBins, startOfRange, binWidth, strdup(name));
89
45 return hist; 90 return hist;
46 } 91 }
47 92
48 void inline 93 void inline
49 addToHist( int32 value, Histogram *hist ) 94 addToHist( int32 value, Histogram *hist )
63 108
64 hist->bins[ binIdx ] += 1; 109 hist->bins[ binIdx ] += 1;
65 } 110 }
66 111
67 112
113 /*Inline because use with RDTSC in innermost code so need ultra-fast
114 */
68 void inline 115 void inline
69 addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist ) 116 addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist )
70 { 117 {
71 int32 value; 118 int32 value;
72 119
76 } 123 }
77 124
78 void 125 void
79 printHist( Histogram *hist ) 126 printHist( Histogram *hist )
80 { 127 {
81 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; 128 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
129 float32 total, binPercent, expectedValue1, expectedValue2;
82 130
83 131
84 maxHeight = 0; 132 //do all except the top bin
85 for( i = 0; i < hist->numBins; i++ ) 133 maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
134 for( i = 0; i < hist->numBins -1; i++ )
86 { 135 {
87 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 136 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
137 total += hist->bins[ i ];
138 binStart = hist->startOfRange + hist->binWidth * i;
139 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
88 } 140 }
141 //copy and calc expected value minus the top bin
142 expectedValue2 = expectedValue1;
143 expectedValue2 /= total;
144
145 //now do last iteration, to add the top bin
146 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
147 total += hist->bins[ i ];
148 binStart = hist->startOfRange + hist->binWidth * i;
149 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
150
151 expectedValue1 /= total;
152
89 barValue = maxHeight / 60; //60 spaces across page for tallest bin 153 barValue = maxHeight / 60; //60 spaces across page for tallest bin
90 154
91 printf( "histogram: \n" ); 155 printf( "histogram: " );
156 if( hist->name != NULL ) printf( "%s\n", hist->name );
157 else printf( "\n" );
158 printf( "expected value: %3.2f \n", expectedValue1 );
159 printf( "expected value minus top bin: %3.2f \n", expectedValue2 );
160
92 if( barValue == 0 ) printf("error printing histogram\n"); 161 if( barValue == 0 ) printf("error printing histogram\n");
93 for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) 162 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
94 { 163 {
95 binStart = hist->startOfRange + hist->binWidth * binIdx; 164 binStart = hist->startOfRange + hist->binWidth * binIdx;
96 binEnd = binStart + hist->binWidth - 1; 165 binEnd = binStart + hist->binWidth - 1;
97 printf("bin range: %d - %d", binStart, binEnd ); 166 binPercent = 100 * hist->bins[ binIdx ] / total;
167 printf("bin range: %d - %d | %3.2f", binStart, binEnd, binPercent );
98 168
99 numBars = hist->bins[ binIdx ] / barValue; 169 numBars = hist->bins[ binIdx ] / barValue;
100 //print one bin, height of bar is num dashes across page 170 //print one bin, height of bar is num dashes across page
101 for( i = 0; i < numBars; i++ ) 171 for( i = 0; i < numBars; i++ )
102 { 172 {
108 178
109 void 179 void
110 freeHist( Histogram *hist ) 180 freeHist( Histogram *hist )
111 { 181 {
112 VMS__free( hist->bins ); 182 VMS__free( hist->bins );
183 VMS__free( hist->name );
113 VMS__free( hist ); 184 VMS__free( hist );
114 } 185 }
186 void
187 freeHistExt( Histogram *hist )
188 {
189 free( hist->bins );
190 free( hist->name );
191 free( hist );
192 }