annotate Histogram.c @ 5:13b8591dd045

added addInterval -- is old version, have to merge with VMS__malloc version
author SeanHalle
date Thu, 11 Nov 2010 05:37:07 -0800
parents 6e864a0cb520
children a2388fae93ff
rev   line source
Me@0 1 /*
Me@0 2 * Copyright 2010 OpenSourceStewardshipFoundation.org
Me@0 3 * Licensed under GNU General Public License version 2
Me@0 4 *
Me@0 5 * Author: seanhalle@yahoo.com
Me@0 6 *
Me@0 7 */
Me@0 8
Me@0 9 #include "Histogram.h"
Me@0 10 #include <malloc.h>
Me@0 11
Me@0 12
Me@0 13 /*This Histogram Abstract Data Type has a number of bins plus a range of
Me@0 14 * values that the bins span, both chosen at creation.
Me@0 15 *
Me@0 16 *One creates a Histogram instance using the makeHistogram function, then
Me@0 17 * updates it with the addToHist function, and prints it out with the
Me@0 18 * printHist function.
Me@0 19 *
Me@0 20 *Note, the bin width is an integer, so the end of the range is adjusted
Me@0 21 * accordingly. Use the bin-width to calculate the bin boundaries.
Me@0 22 */
Me@0 23
Me@0 24
Me@0 25 Histogram *
SeanHalle@5 26 makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange )
Me@0 27 {
Me@0 28 Histogram *hist;
SeanHalle@5 29 int32 i;
Me@0 30
Me@0 31 hist = malloc( sizeof(Histogram) );
SeanHalle@5 32 hist->bins = malloc( numBins * sizeof(int32) );
Me@0 33
Me@0 34 hist->numBins = numBins;
Me@0 35 hist->binWidth = (endOfRange - startOfRange) / numBins;
Me@0 36 hist->endOfRange = startOfRange + hist->binWidth * numBins;
Me@0 37 hist->startOfRange = startOfRange;
Me@0 38
Me@0 39 for( i = 0; i < hist->numBins; i++ )
Me@0 40 {
Me@0 41 hist->bins[ i ] = 0;
Me@0 42 }
Me@0 43
Me@0 44 return hist;
Me@0 45 }
Me@0 46
SeanHalle@5 47 void inline
SeanHalle@5 48 addToHist( int32 value, Histogram *hist )
Me@0 49 {
SeanHalle@5 50 int32 binIdx;
Me@0 51
Me@0 52 if( value < hist->startOfRange )
Me@0 53 { binIdx = 0;
Me@0 54 }
Me@0 55 else if( value > hist->endOfRange )
Me@0 56 { binIdx = hist->numBins - 1;
Me@0 57 }
Me@0 58 else
Me@0 59 {
Me@0 60 binIdx = (value - hist->startOfRange) / hist->binWidth;
Me@0 61 }
Me@0 62
Me@0 63 hist->bins[ binIdx ] += 1;
Me@0 64 }
Me@0 65
SeanHalle@5 66
SeanHalle@5 67 void inline
SeanHalle@5 68 addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist )
SeanHalle@5 69 {
SeanHalle@5 70 int32 value;
SeanHalle@5 71
SeanHalle@5 72 value = endIntvl - startIntvl;
SeanHalle@5 73 if( value < 0 || value > 10000000 ) return; //sanity check
SeanHalle@5 74 addToHist( value, hist );
SeanHalle@5 75 }
SeanHalle@5 76
Me@0 77 void
Me@0 78 printHist( Histogram *hist )
Me@0 79 {
SeanHalle@5 80 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
Me@0 81
Me@0 82 maxHeight = 0;
Me@0 83 for( i = 0; i < hist->numBins; i++ )
Me@0 84 {
Me@0 85 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
Me@0 86 }
Me@0 87 barValue = maxHeight / 60; //60 spaces across page for tallest bin
Me@0 88
Me@0 89 printf("histogram: \n");
Me@0 90 if( barValue == 0 ) printf("error printing histogram\n");
Me@0 91 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
Me@0 92 {
Me@0 93 binStart = hist->startOfRange + hist->binWidth * binIdx;
Me@0 94 binEnd = binStart + hist->binWidth - 1;
Me@0 95 printf("bin range: %d - %d", binStart, binEnd );
Me@0 96
Me@0 97 numBars = hist->bins[ binIdx ] / barValue;
Me@0 98 //print one bin, height of bar is num dashes across page
Me@0 99 for( i = 0; i < numBars; i++ )
Me@0 100 {
Me@0 101 printf("-");
Me@0 102 }
Me@0 103 printf("\n");
Me@0 104 }
Me@0 105 }
Me@0 106