annotate DblHist.c @ 3:3d35477a5121

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