# HG changeset patch # User Me # Date 1288501989 25200 # Node ID 06128e387cfae5de0b654bfa1a5c9a17ff753224 # Parent dbb58ebfd69018297802cc05e43d981ab36b26a1 Added DblHist and FloatHist diff -r dbb58ebfd690 -r 06128e387cfa DblHist.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DblHist.c Sat Oct 30 22:13:09 2010 -0700 @@ -0,0 +1,100 @@ +/* + * Copyright 2010 OpenSourceStewardshipFoundation.org + * Licensed under GNU General Public License version 2 + * + * Author: seanhalle@yahoo.com + * + */ + +#include "Histogram.h" +#include + + +/*This Histogram Abstract Data Type has a number of bins, the starting + * value, and the width of each bin, as a float, all chosen at creation. + * + *One creates a Histogram instance using the makeFloatHistogram function, then + * updates it with the addToFloatHist function, and prints it out with the + * printFloatHist function. + * + *Note, the bin width is an integer, so the end of the range is adjusted + * accordingly. Use the bin-width to calculate the bin boundaries. + */ + + +DblHist * +makeDblHistogram( int32 numBins, float64 startOfRange, float64 binWidth ) + { + DblHist *hist; + int i; + + hist = malloc( sizeof(DblHist) ); + hist->bins = malloc( numBins * sizeof(int) ); + + hist->numBins = numBins; + hist->binWidth = binWidth; + hist->endOfRange = startOfRange + hist->binWidth * numBins; + hist->startOfRange = startOfRange; + + for( i = 0; i < hist->numBins; i++ ) + { + hist->bins[ i ] = 0; + } + + return hist; + } + + +/*All values higher than or equal to a bin's start value and less than the + * start value of the next higher are put into that bin. + */ +void +addToDblHist( float64 value, DblHist *hist ) + { + int binIdx; + + if( value < hist->startOfRange ) + { binIdx = 0; + } + else if( value > hist->endOfRange ) + { binIdx = hist->numBins - 1; + } + else + { //truncate so bin holds: binStartVal =< values < nextBinStartVal + binIdx = (int32)((value - hist->startOfRange) / hist->binWidth); + } + + hist->bins[ binIdx ] += 1; + } + +void +printDblHist( DblHist *hist ) + { + int32 binIdx, i, numBars, maxHeight; + float64 barValue, binStart, binEnd; + + maxHeight = 0; + for( i = 0; i < hist->numBins; i++ ) + { + if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; + } + barValue = maxHeight / 40; //40 spaces across page for tallest bin + + printf( "histogram: \n" ); + 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; + printf( "bin range: %.6fl - %.6fl", binStart, binEnd ); + + numBars = hist->bins[ binIdx ] / barValue; + //print one bin, height of bar is num dashes across page + for( i = 0; i < numBars; i++ ) + { + printf("-"); + } + printf("\n"); + } + } + diff -r dbb58ebfd690 -r 06128e387cfa FloatHist.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FloatHist.c Sat Oct 30 22:13:09 2010 -0700 @@ -0,0 +1,100 @@ +/* + * Copyright 2010 OpenSourceStewardshipFoundation.org + * Licensed under GNU General Public License version 2 + * + * Author: seanhalle@yahoo.com + * + */ + +#include "Histogram.h" +#include + + +/*This Histogram Abstract Data Type has a number of bins, the starting + * value, and the width of each bin, as a float, all chosen at creation. + * + *One creates a Histogram instance using the makeFloatHistogram function, then + * updates it with the addToFloatHist function, and prints it out with the + * printFloatHist function. + * + *Note, the bin width is an integer, so the end of the range is adjusted + * accordingly. Use the bin-width to calculate the bin boundaries. + */ + + +FloatHist * +makeFloatHistogram( int32 numBins, float32 startOfRange, float32 binWidth ) + { + FloatHist *hist; + int i; + + hist = malloc( sizeof(FloatHist) ); + hist->bins = malloc( numBins * sizeof(int) ); + + hist->numBins = numBins; + hist->binWidth = binWidth; + hist->endOfRange = startOfRange + hist->binWidth * numBins; + hist->startOfRange = startOfRange; + + for( i = 0; i < hist->numBins; i++ ) + { + hist->bins[ i ] = 0; + } + + return hist; + } + + +/*All values higher than or equal to a bin's start value and less than the + * start value of the next higher are put into that bin. + */ +void +addToFloatHist( float32 value, FloatHist *hist ) + { + int binIdx; + + if( value < hist->startOfRange ) + { binIdx = 0; + } + else if( value > hist->endOfRange ) + { binIdx = hist->numBins - 1; + } + else + { //truncate so bin holds: binStartVal =< values < nextBinStartVal + binIdx = (int32)((value - hist->startOfRange) / hist->binWidth); + } + + hist->bins[ binIdx ] += 1; + } + +void +printFloatHist( FloatHist *hist ) + { + int32 binIdx, i, numBars, maxHeight; + float32 barValue, binStart, binEnd; + + maxHeight = 0; + for( i = 0; i < hist->numBins; i++ ) + { + if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; + } + barValue = maxHeight / 40; //40 spaces across page for tallest bin + + printf("histogram: \n"); + 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; + printf("bin range: %3f - %3f", binStart, binEnd ); + + numBars = hist->bins[ binIdx ] / barValue; + //print one bin, height of bar is num dashes across page + for( i = 0; i < numBars; i++ ) + { + printf("-"); + } + printf("\n"); + } + } + diff -r dbb58ebfd690 -r 06128e387cfa Histogram.h --- a/Histogram.h Sat Sep 11 04:46:29 2010 -0700 +++ b/Histogram.h Sat Oct 30 22:13:09 2010 -0700 @@ -6,6 +6,7 @@ * */ +#include "../VMS_primitive_data_types.h" #ifndef _HISTOGRAM_H #define _HISTOGRAM_H @@ -20,6 +21,26 @@ } Histogram; +typedef struct + { + float32 startOfRange; + float32 endOfRange; + int numBins; + float32 binWidth; + int *bins; + } +FloatHist; + +typedef struct + { + float64 startOfRange; + float64 endOfRange; + int numBins; + float64 binWidth; + int *bins; + } +DblHist; + Histogram * makeHistogram( int numBins, int startOfRange, int endOfRange ); @@ -29,5 +50,24 @@ void printHist( Histogram *hist ); +FloatHist * +makeFloatHistogram( int numBins, float32 startOfRange, float32 binWidth ); + +void +addToFloatHist( float32 value, FloatHist *hist ); + +void +printFloatHist( FloatHist *hist ); + + +DblHist * +makeDblHistogram( int numBins, float64 startOfRange, float64 binWidth ); + +void +addToDblHist( float64 value, DblHist *hist ); + +void +printDblHist( DblHist *hist ); + #endif /* _HISTOGRAM_H */