annotate DblHist.c @ 36:6bdcb337576b

adding netbeans project directories to repository
author Sean Halle <seanhalle@yahoo.com>
date Fri, 14 Feb 2014 07:47:13 -0800
parents 8166ea441cb5
children
rev   line source
Me@2 1 /*
seanhalle@31 2 * Copyright 2010 OpenSourceResearchInstitute.org
Me@2 3 * Licensed under GNU General Public License version 2
Me@2 4 *
Me@2 5 * Author: seanhalle@yahoo.com
Me@2 6 *
Me@2 7 */
Me@2 8
Me@4 9 #include <stdio.h>
seanhalle@35 10 #include <PR__include/prmalloc.h>
seanhalle@35 11 #include <PR__include/prhistogram.h>
Me@2 12
Me@2 13
Me@2 14 /*This Histogram Abstract Data Type has a number of bins, the starting
Me@2 15 * value, and the width of each bin, as a float, all chosen at creation.
Me@2 16 *
Me@2 17 *One creates a Histogram instance using the makeFloatHistogram function, then
Me@2 18 * updates it with the addToFloatHist function, and prints it out with the
Me@2 19 * printFloatHist function.
Me@2 20 *
Me@2 21 *Note, the bin width is an integer, so the end of the range is adjusted
Me@2 22 * accordingly. Use the bin-width to calculate the bin boundaries.
Me@2 23 */
Me@2 24
Me@2 25
Me@2 26 DblHist *
Me@2 27 makeDblHistogram( int32 numBins, float64 startOfRange, float64 binWidth )
Me@2 28 {
Me@2 29 DblHist *hist;
Me@2 30 int i;
Me@2 31
seanhalle@34 32 hist = PR__malloc( sizeof(DblHist) );
seanhalle@34 33 hist->bins = PR__malloc( numBins * sizeof(int) );
Me@2 34
Me@2 35 hist->numBins = numBins;
Me@2 36 hist->binWidth = binWidth;
Me@2 37 hist->endOfRange = startOfRange + hist->binWidth * numBins;
Me@2 38 hist->startOfRange = startOfRange;
Me@2 39
Me@2 40 for( i = 0; i < hist->numBins; i++ )
Me@2 41 {
Me@2 42 hist->bins[ i ] = 0;
Me@2 43 }
Me@2 44
Me@2 45 return hist;
Me@2 46 }
Me@2 47
Me@2 48
Me@2 49 /*All values higher than or equal to a bin's start value and less than the
Me@2 50 * start value of the next higher are put into that bin.
Me@2 51 */
Me@2 52 void
Me@2 53 addToDblHist( float64 value, DblHist *hist )
Me@2 54 {
Me@2 55 int binIdx;
Me@2 56
Me@2 57 if( value < hist->startOfRange )
Me@2 58 { binIdx = 0;
Me@2 59 }
Me@2 60 else if( value > hist->endOfRange )
Me@2 61 { binIdx = hist->numBins - 1;
Me@2 62 }
Me@2 63 else
Me@2 64 { //truncate so bin holds: binStartVal =< values < nextBinStartVal
Me@2 65 binIdx = (int32)((value - hist->startOfRange) / hist->binWidth);
Me@2 66 }
Me@2 67
Me@2 68 hist->bins[ binIdx ] += 1;
Me@2 69 }
Me@2 70
Me@2 71 void
Me@2 72 printDblHist( DblHist *hist )
Me@2 73 {
Me@2 74 int32 binIdx, i, numBars, maxHeight;
Me@2 75 float64 barValue, binStart, binEnd;
Me@2 76
Me@2 77 maxHeight = 0;
Me@2 78 for( i = 0; i < hist->numBins; i++ )
Me@2 79 {
Me@2 80 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
Me@2 81 }
Me@2 82 barValue = maxHeight / 40; //40 spaces across page for tallest bin
Me@2 83
Me@2 84 printf( "histogram: \n" );
Me@2 85 if( barValue == 0 ) printf( "error printing histogram\n" );
Me@2 86 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
Me@2 87 {
Me@2 88 binStart = hist->startOfRange + hist->binWidth * binIdx;
Me@2 89 binEnd = binStart + hist->binWidth;
Me@2 90 printf( "bin range: %.6fl - %.6fl", binStart, binEnd );
Me@2 91
Me@2 92 numBars = hist->bins[ binIdx ] / barValue;
Me@2 93 //print one bin, height of bar is num dashes across page
Me@2 94 for( i = 0; i < numBars; i++ )
Me@2 95 {
Me@2 96 printf("-");
Me@2 97 }
Me@2 98 printf("\n");
Me@2 99 }
Me@2 100 }
Me@2 101
Me@3 102
Me@3 103 void
Me@3 104 freeDblHist( DblHist *hist )
Me@3 105 {
seanhalle@34 106 PR__free( hist->bins );
seanhalle@34 107 PR__free( hist );
Me@3 108 }