annotate FloatHist.c @ 4:83a412f2ef98

Added stdio.h and copy-paste error on FreeDblHist to FreeFloatHist
author Me
date Tue, 02 Nov 2010 16:47:21 -0700
parents 3d35477a5121
children 060d63cb5d34
rev   line source
Me@2 1 /*
Me@2 2 * Copyright 2010 OpenSourceStewardshipFoundation.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@4 8 #include <stdio.h>
Me@2 9 #include "Histogram.h"
Me@2 10
Me@2 11 /*This Histogram Abstract Data Type has a number of bins, the starting
Me@2 12 * value, and the width of each bin, as a float, all chosen at creation.
Me@2 13 *
Me@2 14 *One creates a Histogram instance using the makeFloatHistogram function, then
Me@2 15 * updates it with the addToFloatHist function, and prints it out with the
Me@2 16 * printFloatHist function.
Me@2 17 *
Me@2 18 *Note, the bin width is an integer, so the end of the range is adjusted
Me@2 19 * accordingly. Use the bin-width to calculate the bin boundaries.
Me@2 20 */
Me@2 21
Me@2 22
Me@2 23 FloatHist *
Me@2 24 makeFloatHistogram( int32 numBins, float32 startOfRange, float32 binWidth )
Me@2 25 {
Me@2 26 FloatHist *hist;
Me@2 27 int i;
Me@2 28
Me@3 29 hist = VMS__malloc( sizeof(FloatHist) );
Me@3 30 hist->bins = VMS__malloc( numBins * sizeof(int) );
Me@2 31
Me@2 32 hist->numBins = numBins;
Me@2 33 hist->binWidth = binWidth;
Me@2 34 hist->endOfRange = startOfRange + hist->binWidth * numBins;
Me@2 35 hist->startOfRange = startOfRange;
Me@2 36
Me@2 37 for( i = 0; i < hist->numBins; i++ )
Me@2 38 {
Me@2 39 hist->bins[ i ] = 0;
Me@2 40 }
Me@2 41
Me@2 42 return hist;
Me@2 43 }
Me@2 44
Me@2 45
Me@2 46 /*All values higher than or equal to a bin's start value and less than the
Me@2 47 * start value of the next higher are put into that bin.
Me@2 48 */
Me@2 49 void
Me@2 50 addToFloatHist( float32 value, FloatHist *hist )
Me@2 51 {
Me@2 52 int binIdx;
Me@2 53
Me@2 54 if( value < hist->startOfRange )
Me@2 55 { binIdx = 0;
Me@2 56 }
Me@2 57 else if( value > hist->endOfRange )
Me@2 58 { binIdx = hist->numBins - 1;
Me@2 59 }
Me@2 60 else
Me@2 61 { //truncate so bin holds: binStartVal =< values < nextBinStartVal
Me@2 62 binIdx = (int32)((value - hist->startOfRange) / hist->binWidth);
Me@2 63 }
Me@2 64
Me@2 65 hist->bins[ binIdx ] += 1;
Me@2 66 }
Me@2 67
Me@2 68 void
Me@2 69 printFloatHist( FloatHist *hist )
Me@2 70 {
Me@2 71 int32 binIdx, i, numBars, maxHeight;
Me@2 72 float32 barValue, binStart, binEnd;
Me@2 73
Me@2 74 maxHeight = 0;
Me@2 75 for( i = 0; i < hist->numBins; i++ )
Me@2 76 {
Me@2 77 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
Me@2 78 }
Me@2 79 barValue = maxHeight / 40; //40 spaces across page for tallest bin
Me@2 80
Me@2 81 printf("histogram: \n");
Me@2 82 if( barValue == 0 ) printf("error printing histogram\n");
Me@2 83 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
Me@2 84 {
Me@2 85 binStart = hist->startOfRange + hist->binWidth * binIdx;
Me@2 86 binEnd = binStart + hist->binWidth;
Me@2 87 printf("bin range: %3f - %3f", binStart, binEnd );
Me@2 88
Me@2 89 numBars = hist->bins[ binIdx ] / barValue;
Me@2 90 //print one bin, height of bar is num dashes across page
Me@2 91 for( i = 0; i < numBars; i++ )
Me@2 92 {
Me@2 93 printf("-");
Me@2 94 }
Me@2 95 printf("\n");
Me@2 96 }
Me@2 97 }
Me@2 98
Me@3 99
Me@3 100 void
Me@4 101 freeFloatHist( FloatHist *hist )
Me@3 102 {
Me@3 103 VMS__free( hist->bins );
Me@3 104 VMS__free( hist );
Me@3 105 }