Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Histogram
changeset 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 |
| files | Histogram.c Histogram.h |
| diffstat | 2 files changed, 101 insertions(+), 14 deletions(-) [+] |
line diff
1.1 --- a/Histogram.c Thu Nov 11 05:45:08 2010 -0800 1.2 +++ b/Histogram.c Sun Nov 14 11:11:44 2010 -0800 1.3 @@ -7,6 +7,7 @@ 1.4 */ 1.5 #include <stdio.h> 1.6 #include "Histogram.h" 1.7 +#include "../vutilities.h" 1.8 1.9 1.10 /*This Histogram Abstract Data Type has a number of bins plus a range of 1.11 @@ -42,6 +43,50 @@ 1.12 hist->bins[ i ] = 0; 1.13 } 1.14 1.15 + hist->name = NULL; 1.16 + return hist; 1.17 + } 1.18 + 1.19 +inline void 1.20 +makeHist_helper( Histogram *hist, int32 numBins, 1.21 + int32 startOfRange, int32 binWidth, char *nameCopy ) 1.22 + { 1.23 + hist->numBins = numBins; 1.24 + hist->binWidth = binWidth; 1.25 + hist->endOfRange = startOfRange + hist->binWidth * numBins; 1.26 + hist->startOfRange = startOfRange; 1.27 + hist->name = nameCopy; 1.28 + memset( hist->bins, 0, numBins * sizeof(int32) ); 1.29 + } 1.30 + 1.31 + 1.32 +Histogram * 1.33 +makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth, 1.34 + char *name ) 1.35 + 1.36 + { 1.37 + Histogram *hist; 1.38 + 1.39 + hist = VMS__malloc( sizeof(Histogram) ); 1.40 + hist->bins = VMS__malloc( numBins * sizeof(int32) ); 1.41 + 1.42 + makeHist_helper( hist, numBins, startOfRange, binWidth,VMS__strDup(name)); 1.43 + 1.44 + return hist; 1.45 + } 1.46 + 1.47 +Histogram * 1.48 +makeFixedBinHistExt( int32 numBins, int32 startOfRange, int32 binWidth, 1.49 + char *name ) 1.50 + 1.51 + { 1.52 + Histogram *hist; 1.53 + 1.54 + hist = malloc( sizeof(Histogram) ); 1.55 + hist->bins = malloc( numBins * sizeof(int32) ); 1.56 + 1.57 + makeHist_helper( hist, numBins, startOfRange, binWidth, strdup(name)); 1.58 + 1.59 return hist; 1.60 } 1.61 1.62 @@ -65,6 +110,8 @@ 1.63 } 1.64 1.65 1.66 +/*Inline because use with RDTSC in innermost code so need ultra-fast 1.67 + */ 1.68 void inline 1.69 addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist ) 1.70 { 1.71 @@ -78,23 +125,46 @@ 1.72 void 1.73 printHist( Histogram *hist ) 1.74 { 1.75 - int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; 1.76 + int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; 1.77 + float32 total, binPercent, expectedValue1, expectedValue2; 1.78 1.79 1.80 - maxHeight = 0; 1.81 - for( i = 0; i < hist->numBins; i++ ) 1.82 + //do all except the top bin 1.83 + maxHeight = 0; total = 0.0; expectedValue1 = 0.0; 1.84 + for( i = 0; i < hist->numBins -1; i++ ) 1.85 { 1.86 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 1.87 + total += hist->bins[ i ]; 1.88 + binStart = hist->startOfRange + hist->binWidth * i; 1.89 + expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0); 1.90 } 1.91 + //copy and calc expected value minus the top bin 1.92 + expectedValue2 = expectedValue1; 1.93 + expectedValue2 /= total; 1.94 + 1.95 + //now do last iteration, to add the top bin 1.96 + if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 1.97 + total += hist->bins[ i ]; 1.98 + binStart = hist->startOfRange + hist->binWidth * i; 1.99 + expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0); 1.100 + 1.101 + expectedValue1 /= total; 1.102 + 1.103 barValue = maxHeight / 60; //60 spaces across page for tallest bin 1.104 1.105 - printf( "histogram: \n" ); 1.106 + printf( "histogram: " ); 1.107 + if( hist->name != NULL ) printf( "%s\n", hist->name ); 1.108 + else printf( "\n" ); 1.109 + printf( "expected value: %3.2f \n", expectedValue1 ); 1.110 + printf( "expected value minus top bin: %3.2f \n", expectedValue2 ); 1.111 + 1.112 if( barValue == 0 ) printf("error printing histogram\n"); 1.113 for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) 1.114 { 1.115 binStart = hist->startOfRange + hist->binWidth * binIdx; 1.116 binEnd = binStart + hist->binWidth - 1; 1.117 - printf("bin range: %d - %d", binStart, binEnd ); 1.118 + binPercent = 100 * hist->bins[ binIdx ] / total; 1.119 + printf("bin range: %d - %d | %3.2f", binStart, binEnd, binPercent ); 1.120 1.121 numBars = hist->bins[ binIdx ] / barValue; 1.122 //print one bin, height of bar is num dashes across page 1.123 @@ -110,5 +180,13 @@ 1.124 freeHist( Histogram *hist ) 1.125 { 1.126 VMS__free( hist->bins ); 1.127 + VMS__free( hist->name ); 1.128 VMS__free( hist ); 1.129 } 1.130 +void 1.131 +freeHistExt( Histogram *hist ) 1.132 + { 1.133 + free( hist->bins ); 1.134 + free( hist->name ); 1.135 + free( hist ); 1.136 + }
2.1 --- a/Histogram.h Thu Nov 11 05:45:08 2010 -0800 2.2 +++ b/Histogram.h Sun Nov 14 11:11:44 2010 -0800 2.3 @@ -13,11 +13,12 @@ 2.4 2.5 typedef struct 2.6 { 2.7 - int startOfRange; 2.8 - int endOfRange; 2.9 - int numBins; 2.10 - int binWidth; 2.11 - int *bins; 2.12 + char *name; 2.13 + int32 startOfRange; 2.14 + int32 endOfRange; 2.15 + int32 numBins; 2.16 + int32 binWidth; 2.17 + int32 *bins; 2.18 } 2.19 Histogram; 2.20 2.21 @@ -25,9 +26,9 @@ 2.22 { 2.23 float32 startOfRange; 2.24 float32 endOfRange; 2.25 - int numBins; 2.26 + int32 numBins; 2.27 float32 binWidth; 2.28 - int *bins; 2.29 + int32 *bins; 2.30 } 2.31 FloatHist; 2.32 2.33 @@ -35,15 +36,23 @@ 2.34 { 2.35 float64 startOfRange; 2.36 float64 endOfRange; 2.37 - int numBins; 2.38 + int32 numBins; 2.39 float64 binWidth; 2.40 - int *bins; 2.41 + int32 *bins; 2.42 } 2.43 DblHist; 2.44 2.45 Histogram * 2.46 makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange ); 2.47 2.48 +Histogram * 2.49 +makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth, 2.50 + char *name ); 2.51 + 2.52 +Histogram * 2.53 +makeFixedBinHistExt( int32 numBins, int32 startOfRange, int32 binWidth, 2.54 + char *name ); 2.55 + 2.56 void inline 2.57 addToHist( int32 value, Histogram *hist ); 2.58
