# HG changeset patch # User Me # Date 1289761904 28800 # Node ID fa6a281bd8548ba17e39c0a25b90d9c22ca9dd7d # Parent a2388fae93ff0cf88d335b1e3b55c7200cc84a32 Nov 14 vers -- add makeFixedBinHist & Ext version & expected value &fxd name bug diff -r a2388fae93ff -r fa6a281bd854 Histogram.c --- a/Histogram.c Thu Nov 11 05:45:08 2010 -0800 +++ b/Histogram.c Sun Nov 14 11:11:44 2010 -0800 @@ -7,6 +7,7 @@ */ #include #include "Histogram.h" +#include "../vutilities.h" /*This Histogram Abstract Data Type has a number of bins plus a range of @@ -42,6 +43,50 @@ hist->bins[ i ] = 0; } + hist->name = NULL; + return hist; + } + +inline void +makeHist_helper( Histogram *hist, int32 numBins, + int32 startOfRange, int32 binWidth, char *nameCopy ) + { + hist->numBins = numBins; + hist->binWidth = binWidth; + hist->endOfRange = startOfRange + hist->binWidth * numBins; + hist->startOfRange = startOfRange; + hist->name = nameCopy; + memset( hist->bins, 0, numBins * sizeof(int32) ); + } + + +Histogram * +makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth, + char *name ) + + { + Histogram *hist; + + hist = VMS__malloc( sizeof(Histogram) ); + hist->bins = VMS__malloc( numBins * sizeof(int32) ); + + makeHist_helper( hist, numBins, startOfRange, binWidth,VMS__strDup(name)); + + return hist; + } + +Histogram * +makeFixedBinHistExt( int32 numBins, int32 startOfRange, int32 binWidth, + char *name ) + + { + Histogram *hist; + + hist = malloc( sizeof(Histogram) ); + hist->bins = malloc( numBins * sizeof(int32) ); + + makeHist_helper( hist, numBins, startOfRange, binWidth, strdup(name)); + return hist; } @@ -65,6 +110,8 @@ } +/*Inline because use with RDTSC in innermost code so need ultra-fast + */ void inline addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist ) { @@ -78,23 +125,46 @@ void printHist( Histogram *hist ) { - int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; + int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; + float32 total, binPercent, expectedValue1, expectedValue2; - maxHeight = 0; - for( i = 0; i < hist->numBins; i++ ) + //do all except the top bin + maxHeight = 0; total = 0.0; expectedValue1 = 0.0; + for( i = 0; i < hist->numBins -1; i++ ) { if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; + total += hist->bins[ i ]; + binStart = hist->startOfRange + hist->binWidth * i; + expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0); } + //copy and calc expected value minus the top bin + expectedValue2 = expectedValue1; + expectedValue2 /= total; + + //now do last iteration, to add the top bin + if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; + total += hist->bins[ i ]; + binStart = hist->startOfRange + hist->binWidth * i; + expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0); + + expectedValue1 /= total; + barValue = maxHeight / 60; //60 spaces across page for tallest bin - printf( "histogram: \n" ); + printf( "histogram: " ); + if( hist->name != NULL ) printf( "%s\n", hist->name ); + else printf( "\n" ); + printf( "expected value: %3.2f \n", expectedValue1 ); + printf( "expected value minus top bin: %3.2f \n", expectedValue2 ); + if( barValue == 0 ) printf("error printing histogram\n"); for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) { binStart = hist->startOfRange + hist->binWidth * binIdx; binEnd = binStart + hist->binWidth - 1; - printf("bin range: %d - %d", binStart, binEnd ); + binPercent = 100 * hist->bins[ binIdx ] / total; + printf("bin range: %d - %d | %3.2f", binStart, binEnd, binPercent ); numBars = hist->bins[ binIdx ] / barValue; //print one bin, height of bar is num dashes across page @@ -110,5 +180,13 @@ freeHist( Histogram *hist ) { VMS__free( hist->bins ); + VMS__free( hist->name ); VMS__free( hist ); } +void +freeHistExt( Histogram *hist ) + { + free( hist->bins ); + free( hist->name ); + free( hist ); + } diff -r a2388fae93ff -r fa6a281bd854 Histogram.h --- a/Histogram.h Thu Nov 11 05:45:08 2010 -0800 +++ b/Histogram.h Sun Nov 14 11:11:44 2010 -0800 @@ -13,11 +13,12 @@ typedef struct { - int startOfRange; - int endOfRange; - int numBins; - int binWidth; - int *bins; + char *name; + int32 startOfRange; + int32 endOfRange; + int32 numBins; + int32 binWidth; + int32 *bins; } Histogram; @@ -25,9 +26,9 @@ { float32 startOfRange; float32 endOfRange; - int numBins; + int32 numBins; float32 binWidth; - int *bins; + int32 *bins; } FloatHist; @@ -35,15 +36,23 @@ { float64 startOfRange; float64 endOfRange; - int numBins; + int32 numBins; float64 binWidth; - int *bins; + int32 *bins; } DblHist; Histogram * makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange ); +Histogram * +makeFixedBinHist( int32 numBins, int32 startOfRange, int32 binWidth, + char *name ); + +Histogram * +makeFixedBinHistExt( int32 numBins, int32 startOfRange, int32 binWidth, + char *name ); + void inline addToHist( int32 value, Histogram *hist );