Me@2: /* Me@2: * Copyright 2010 OpenSourceStewardshipFoundation.org Me@2: * Licensed under GNU General Public License version 2 Me@2: * Me@2: * Author: seanhalle@yahoo.com Me@2: * Me@2: */ Me@4: #include Me@2: #include "Histogram.h" Me@2: Me@2: /*This Histogram Abstract Data Type has a number of bins, the starting Me@2: * value, and the width of each bin, as a float, all chosen at creation. Me@2: * Me@2: *One creates a Histogram instance using the makeFloatHistogram function, then Me@2: * updates it with the addToFloatHist function, and prints it out with the Me@2: * printFloatHist function. Me@2: * Me@2: *Note, the bin width is an integer, so the end of the range is adjusted Me@2: * accordingly. Use the bin-width to calculate the bin boundaries. Me@2: */ Me@2: Me@2: Me@2: FloatHist * Me@2: makeFloatHistogram( int32 numBins, float32 startOfRange, float32 binWidth ) Me@2: { Me@2: FloatHist *hist; Me@2: int i; Me@2: Me@3: hist = VMS__malloc( sizeof(FloatHist) ); Me@3: hist->bins = VMS__malloc( numBins * sizeof(int) ); Me@2: Me@2: hist->numBins = numBins; Me@2: hist->binWidth = binWidth; Me@2: hist->endOfRange = startOfRange + hist->binWidth * numBins; Me@2: hist->startOfRange = startOfRange; Me@2: Me@2: for( i = 0; i < hist->numBins; i++ ) Me@2: { Me@2: hist->bins[ i ] = 0; Me@2: } Me@2: Me@2: return hist; Me@2: } Me@2: Me@2: Me@2: /*All values higher than or equal to a bin's start value and less than the Me@2: * start value of the next higher are put into that bin. Me@2: */ Me@2: void Me@2: addToFloatHist( float32 value, FloatHist *hist ) Me@2: { Me@2: int binIdx; Me@2: Me@2: if( value < hist->startOfRange ) Me@2: { binIdx = 0; Me@2: } Me@2: else if( value > hist->endOfRange ) Me@2: { binIdx = hist->numBins - 1; Me@2: } Me@2: else Me@2: { //truncate so bin holds: binStartVal =< values < nextBinStartVal Me@2: binIdx = (int32)((value - hist->startOfRange) / hist->binWidth); Me@2: } Me@2: Me@2: hist->bins[ binIdx ] += 1; Me@2: } Me@2: Me@2: void Me@2: printFloatHist( FloatHist *hist ) Me@2: { Me@2: int32 binIdx, i, numBars, maxHeight; Me@2: float32 barValue, binStart, binEnd; Me@2: Me@2: maxHeight = 0; Me@2: for( i = 0; i < hist->numBins; i++ ) Me@2: { Me@2: if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; Me@2: } Me@2: barValue = maxHeight / 40; //40 spaces across page for tallest bin Me@2: Me@2: printf("histogram: \n"); Me@2: if( barValue == 0 ) printf("error printing histogram\n"); Me@2: for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) Me@2: { Me@2: binStart = hist->startOfRange + hist->binWidth * binIdx; Me@2: binEnd = binStart + hist->binWidth; Me@2: printf("bin range: %3f - %3f", binStart, binEnd ); Me@2: Me@2: numBars = hist->bins[ binIdx ] / barValue; Me@2: //print one bin, height of bar is num dashes across page Me@2: for( i = 0; i < numBars; i++ ) Me@2: { Me@2: printf("-"); Me@2: } Me@2: printf("\n"); Me@2: } Me@2: } Me@2: Me@3: Me@3: void Me@4: freeFloatHist( FloatHist *hist ) Me@3: { Me@3: VMS__free( hist->bins ); Me@3: VMS__free( hist ); Me@3: }