Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Histogram
changeset 2:06128e387cfa
Added DblHist and FloatHist
| author | Me |
|---|---|
| date | Sat, 30 Oct 2010 22:13:09 -0700 |
| parents | dbb58ebfd690 |
| children | 3d35477a5121 |
| files | DblHist.c FloatHist.c Histogram.h |
| diffstat | 3 files changed, 240 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/DblHist.c Sat Oct 30 22:13:09 2010 -0700 1.3 @@ -0,0 +1,100 @@ 1.4 +/* 1.5 + * Copyright 2010 OpenSourceStewardshipFoundation.org 1.6 + * Licensed under GNU General Public License version 2 1.7 + * 1.8 + * Author: seanhalle@yahoo.com 1.9 + * 1.10 + */ 1.11 + 1.12 +#include "Histogram.h" 1.13 +#include <malloc.h> 1.14 + 1.15 + 1.16 +/*This Histogram Abstract Data Type has a number of bins, the starting 1.17 + * value, and the width of each bin, as a float, all chosen at creation. 1.18 + * 1.19 + *One creates a Histogram instance using the makeFloatHistogram function, then 1.20 + * updates it with the addToFloatHist function, and prints it out with the 1.21 + * printFloatHist function. 1.22 + * 1.23 + *Note, the bin width is an integer, so the end of the range is adjusted 1.24 + * accordingly. Use the bin-width to calculate the bin boundaries. 1.25 + */ 1.26 + 1.27 + 1.28 +DblHist * 1.29 +makeDblHistogram( int32 numBins, float64 startOfRange, float64 binWidth ) 1.30 + { 1.31 + DblHist *hist; 1.32 + int i; 1.33 + 1.34 + hist = malloc( sizeof(DblHist) ); 1.35 + hist->bins = malloc( numBins * sizeof(int) ); 1.36 + 1.37 + hist->numBins = numBins; 1.38 + hist->binWidth = binWidth; 1.39 + hist->endOfRange = startOfRange + hist->binWidth * numBins; 1.40 + hist->startOfRange = startOfRange; 1.41 + 1.42 + for( i = 0; i < hist->numBins; i++ ) 1.43 + { 1.44 + hist->bins[ i ] = 0; 1.45 + } 1.46 + 1.47 + return hist; 1.48 + } 1.49 + 1.50 + 1.51 +/*All values higher than or equal to a bin's start value and less than the 1.52 + * start value of the next higher are put into that bin. 1.53 + */ 1.54 +void 1.55 +addToDblHist( float64 value, DblHist *hist ) 1.56 + { 1.57 + int binIdx; 1.58 + 1.59 + if( value < hist->startOfRange ) 1.60 + { binIdx = 0; 1.61 + } 1.62 + else if( value > hist->endOfRange ) 1.63 + { binIdx = hist->numBins - 1; 1.64 + } 1.65 + else 1.66 + { //truncate so bin holds: binStartVal =< values < nextBinStartVal 1.67 + binIdx = (int32)((value - hist->startOfRange) / hist->binWidth); 1.68 + } 1.69 + 1.70 + hist->bins[ binIdx ] += 1; 1.71 + } 1.72 + 1.73 +void 1.74 +printDblHist( DblHist *hist ) 1.75 + { 1.76 + int32 binIdx, i, numBars, maxHeight; 1.77 + float64 barValue, binStart, binEnd; 1.78 + 1.79 + maxHeight = 0; 1.80 + for( i = 0; i < hist->numBins; i++ ) 1.81 + { 1.82 + if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 1.83 + } 1.84 + barValue = maxHeight / 40; //40 spaces across page for tallest bin 1.85 + 1.86 + printf( "histogram: \n" ); 1.87 + if( barValue == 0 ) printf( "error printing histogram\n" ); 1.88 + for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) 1.89 + { 1.90 + binStart = hist->startOfRange + hist->binWidth * binIdx; 1.91 + binEnd = binStart + hist->binWidth; 1.92 + printf( "bin range: %.6fl - %.6fl", binStart, binEnd ); 1.93 + 1.94 + numBars = hist->bins[ binIdx ] / barValue; 1.95 + //print one bin, height of bar is num dashes across page 1.96 + for( i = 0; i < numBars; i++ ) 1.97 + { 1.98 + printf("-"); 1.99 + } 1.100 + printf("\n"); 1.101 + } 1.102 + } 1.103 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/FloatHist.c Sat Oct 30 22:13:09 2010 -0700 2.3 @@ -0,0 +1,100 @@ 2.4 +/* 2.5 + * Copyright 2010 OpenSourceStewardshipFoundation.org 2.6 + * Licensed under GNU General Public License version 2 2.7 + * 2.8 + * Author: seanhalle@yahoo.com 2.9 + * 2.10 + */ 2.11 + 2.12 +#include "Histogram.h" 2.13 +#include <malloc.h> 2.14 + 2.15 + 2.16 +/*This Histogram Abstract Data Type has a number of bins, the starting 2.17 + * value, and the width of each bin, as a float, all chosen at creation. 2.18 + * 2.19 + *One creates a Histogram instance using the makeFloatHistogram function, then 2.20 + * updates it with the addToFloatHist function, and prints it out with the 2.21 + * printFloatHist function. 2.22 + * 2.23 + *Note, the bin width is an integer, so the end of the range is adjusted 2.24 + * accordingly. Use the bin-width to calculate the bin boundaries. 2.25 + */ 2.26 + 2.27 + 2.28 +FloatHist * 2.29 +makeFloatHistogram( int32 numBins, float32 startOfRange, float32 binWidth ) 2.30 + { 2.31 + FloatHist *hist; 2.32 + int i; 2.33 + 2.34 + hist = malloc( sizeof(FloatHist) ); 2.35 + hist->bins = malloc( numBins * sizeof(int) ); 2.36 + 2.37 + hist->numBins = numBins; 2.38 + hist->binWidth = binWidth; 2.39 + hist->endOfRange = startOfRange + hist->binWidth * numBins; 2.40 + hist->startOfRange = startOfRange; 2.41 + 2.42 + for( i = 0; i < hist->numBins; i++ ) 2.43 + { 2.44 + hist->bins[ i ] = 0; 2.45 + } 2.46 + 2.47 + return hist; 2.48 + } 2.49 + 2.50 + 2.51 +/*All values higher than or equal to a bin's start value and less than the 2.52 + * start value of the next higher are put into that bin. 2.53 + */ 2.54 +void 2.55 +addToFloatHist( float32 value, FloatHist *hist ) 2.56 + { 2.57 + int binIdx; 2.58 + 2.59 + if( value < hist->startOfRange ) 2.60 + { binIdx = 0; 2.61 + } 2.62 + else if( value > hist->endOfRange ) 2.63 + { binIdx = hist->numBins - 1; 2.64 + } 2.65 + else 2.66 + { //truncate so bin holds: binStartVal =< values < nextBinStartVal 2.67 + binIdx = (int32)((value - hist->startOfRange) / hist->binWidth); 2.68 + } 2.69 + 2.70 + hist->bins[ binIdx ] += 1; 2.71 + } 2.72 + 2.73 +void 2.74 +printFloatHist( FloatHist *hist ) 2.75 + { 2.76 + int32 binIdx, i, numBars, maxHeight; 2.77 + float32 barValue, binStart, binEnd; 2.78 + 2.79 + maxHeight = 0; 2.80 + for( i = 0; i < hist->numBins; i++ ) 2.81 + { 2.82 + if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 2.83 + } 2.84 + barValue = maxHeight / 40; //40 spaces across page for tallest bin 2.85 + 2.86 + printf("histogram: \n"); 2.87 + if( barValue == 0 ) printf("error printing histogram\n"); 2.88 + for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) 2.89 + { 2.90 + binStart = hist->startOfRange + hist->binWidth * binIdx; 2.91 + binEnd = binStart + hist->binWidth; 2.92 + printf("bin range: %3f - %3f", binStart, binEnd ); 2.93 + 2.94 + numBars = hist->bins[ binIdx ] / barValue; 2.95 + //print one bin, height of bar is num dashes across page 2.96 + for( i = 0; i < numBars; i++ ) 2.97 + { 2.98 + printf("-"); 2.99 + } 2.100 + printf("\n"); 2.101 + } 2.102 + } 2.103 +
3.1 --- a/Histogram.h Sat Sep 11 04:46:29 2010 -0700 3.2 +++ b/Histogram.h Sat Oct 30 22:13:09 2010 -0700 3.3 @@ -6,6 +6,7 @@ 3.4 * 3.5 */ 3.6 3.7 +#include "../VMS_primitive_data_types.h" 3.8 3.9 #ifndef _HISTOGRAM_H 3.10 #define _HISTOGRAM_H 3.11 @@ -20,6 +21,26 @@ 3.12 } 3.13 Histogram; 3.14 3.15 +typedef struct 3.16 + { 3.17 + float32 startOfRange; 3.18 + float32 endOfRange; 3.19 + int numBins; 3.20 + float32 binWidth; 3.21 + int *bins; 3.22 + } 3.23 +FloatHist; 3.24 + 3.25 +typedef struct 3.26 + { 3.27 + float64 startOfRange; 3.28 + float64 endOfRange; 3.29 + int numBins; 3.30 + float64 binWidth; 3.31 + int *bins; 3.32 + } 3.33 +DblHist; 3.34 + 3.35 Histogram * 3.36 makeHistogram( int numBins, int startOfRange, int endOfRange ); 3.37 3.38 @@ -29,5 +50,24 @@ 3.39 void 3.40 printHist( Histogram *hist ); 3.41 3.42 +FloatHist * 3.43 +makeFloatHistogram( int numBins, float32 startOfRange, float32 binWidth ); 3.44 + 3.45 +void 3.46 +addToFloatHist( float32 value, FloatHist *hist ); 3.47 + 3.48 +void 3.49 +printFloatHist( FloatHist *hist ); 3.50 + 3.51 + 3.52 +DblHist * 3.53 +makeDblHistogram( int numBins, float64 startOfRange, float64 binWidth ); 3.54 + 3.55 +void 3.56 +addToDblHist( float64 value, DblHist *hist ); 3.57 + 3.58 +void 3.59 +printDblHist( DblHist *hist ); 3.60 + 3.61 #endif /* _HISTOGRAM_H */ 3.62
