view 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
line source
1 /*
2 * Copyright 2010 OpenSourceStewardshipFoundation.org
3 * Licensed under GNU General Public License version 2
4 *
5 * Author: seanhalle@yahoo.com
6 *
7 */
8 #include <stdio.h>
9 #include "Histogram.h"
10 #include "../vutilities.h"
13 /*This Histogram Abstract Data Type has a number of bins plus a range of
14 * values that the bins span, both chosen at creation.
15 *
16 *One creates a Histogram instance using the makeHistogram function, then
17 * updates it with the addToHist function, and prints it out with the
18 * printHist function.
19 *
20 *Note, the bin width is an integer, so the end of the range is adjusted
21 * accordingly. Use the bin-width to calculate the bin boundaries.
22 */
25 Histogram *
26 makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange )
28 {
29 Histogram *hist;
30 int32 i;
33 hist = VMS__malloc( sizeof(Histogram) );
34 hist->bins = VMS__malloc( numBins * sizeof(int32) );
36 hist->numBins = numBins;
37 hist->binWidth = (endOfRange - startOfRange) / numBins;
38 hist->endOfRange = startOfRange + hist->binWidth * numBins;
39 hist->startOfRange = startOfRange;
41 for( i = 0; i < hist->numBins; i++ )
42 {
43 hist->bins[ i ] = 0;
44 }
46 hist->name = NULL;
47 return hist;
48 }
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 }
63 Histogram *
64 makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth,
65 char *name )
67 {
68 Histogram *hist;
70 hist = VMS__malloc( sizeof(Histogram) );
71 hist->bins = VMS__malloc( numBins * sizeof(int32) );
73 makeHist_helper( hist, numBins, startOfRange, binWidth,VMS__strDup(name));
75 return hist;
76 }
78 Histogram *
79 makeFixedBinHistExt( int32 numBins, int32 startOfRange, int32 binWidth,
80 char *name )
82 {
83 Histogram *hist;
85 hist = malloc( sizeof(Histogram) );
86 hist->bins = malloc( numBins * sizeof(int32) );
88 makeHist_helper( hist, numBins, startOfRange, binWidth, strdup(name));
90 return hist;
91 }
93 void inline
94 addToHist( int32 value, Histogram *hist )
95 {
96 int32 binIdx;
98 if( value < hist->startOfRange )
99 { binIdx = 0;
100 }
101 else if( value > hist->endOfRange )
102 { binIdx = hist->numBins - 1;
103 }
104 else
105 {
106 binIdx = (value - hist->startOfRange) / hist->binWidth;
107 }
109 hist->bins[ binIdx ] += 1;
110 }
113 /*Inline because use with RDTSC in innermost code so need ultra-fast
114 */
115 void inline
116 addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist )
117 {
118 int32 value;
120 value = endIntvl - startIntvl;
121 if( value < 0 || value > 10000000 ) return; //sanity check
122 addToHist( value, hist );
123 }
125 void
126 printHist( Histogram *hist )
127 {
128 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
129 float32 total, binPercent, expectedValue1, expectedValue2;
132 //do all except the top bin
133 maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
134 for( i = 0; i < hist->numBins -1; i++ )
135 {
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);
140 }
141 //copy and calc expected value minus the top bin
142 expectedValue2 = expectedValue1;
143 expectedValue2 /= total;
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);
151 expectedValue1 /= total;
153 barValue = maxHeight / 60; //60 spaces across page for tallest bin
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 );
161 if( barValue == 0 ) printf("error printing histogram\n");
162 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
163 {
164 binStart = hist->startOfRange + hist->binWidth * binIdx;
165 binEnd = binStart + hist->binWidth - 1;
166 binPercent = 100 * hist->bins[ binIdx ] / total;
167 printf("bin range: %d - %d | %3.2f", binStart, binEnd, binPercent );
169 numBars = hist->bins[ binIdx ] / barValue;
170 //print one bin, height of bar is num dashes across page
171 for( i = 0; i < numBars; i++ )
172 {
173 printf("-");
174 }
175 printf("\n");
176 }
177 }
179 void
180 freeHist( Histogram *hist )
181 {
182 VMS__free( hist->bins );
183 VMS__free( hist->name );
184 VMS__free( hist );
185 }
186 void
187 freeHistExt( Histogram *hist )
188 {
189 free( hist->bins );
190 free( hist->name );
191 free( hist );
192 }