diff FloatHist.c @ 6:a2388fae93ff

Merge addInterval version with VMS__malloc version
author SeanHalle
date Thu, 11 Nov 2010 05:45:08 -0800
parents 3d35477a5121
children 060d63cb5d34
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/FloatHist.c	Thu Nov 11 05:45:08 2010 -0800
     1.3 @@ -0,0 +1,105 @@
     1.4 +/*
     1.5 + *  Copyright 2010 OpenSourceStewardshipFoundation.org
     1.6 + *  Licensed under GNU General Public License version 2
     1.7 + *
     1.8 + * Author: seanhalle@yahoo.com
     1.9 + *
    1.10 + */
    1.11 +#include <stdio.h>
    1.12 +#include "Histogram.h"
    1.13 +
    1.14 +/*This Histogram Abstract Data Type has a number of bins, the starting
    1.15 + * value, and the width of each bin, as a float, all chosen at creation.
    1.16 + *
    1.17 + *One creates a Histogram instance using the makeFloatHistogram function, then
    1.18 + * updates it with the addToFloatHist function, and prints it out with the
    1.19 + * printFloatHist function.
    1.20 + *
    1.21 + *Note, the bin width is an integer, so the end of the range is adjusted
    1.22 + * accordingly.  Use the bin-width to calculate the bin boundaries.
    1.23 + */
    1.24 +
    1.25 +
    1.26 +FloatHist *
    1.27 +makeFloatHistogram( int32 numBins, float32 startOfRange, float32 binWidth )
    1.28 + {
    1.29 +   FloatHist *hist;
    1.30 +   int i;
    1.31 +
    1.32 +   hist = VMS__malloc( sizeof(FloatHist) );
    1.33 +   hist->bins = VMS__malloc( numBins * sizeof(int) );
    1.34 +
    1.35 +   hist->numBins      = numBins;
    1.36 +   hist->binWidth     = binWidth;
    1.37 +   hist->endOfRange   = startOfRange + hist->binWidth * numBins;
    1.38 +   hist->startOfRange = startOfRange;
    1.39 +
    1.40 +   for( i = 0; i < hist->numBins; i++ )
    1.41 +    {
    1.42 +      hist->bins[ i ] = 0;
    1.43 +    }
    1.44 +
    1.45 +   return hist;
    1.46 + }
    1.47 +
    1.48 +
    1.49 +/*All values higher than or equal to a bin's start value and less than the
    1.50 + * start value of the next higher are put into that bin.
    1.51 + */
    1.52 +void
    1.53 +addToFloatHist( float32 value, FloatHist *hist )
    1.54 + {
    1.55 +   int binIdx;
    1.56 +
    1.57 +   if( value < hist->startOfRange )
    1.58 +    { binIdx = 0;
    1.59 +    }
    1.60 +   else if( value > hist->endOfRange )
    1.61 +    { binIdx = hist->numBins - 1;
    1.62 +    }
    1.63 +   else
    1.64 +    {    //truncate so bin holds:   binStartVal =< values < nextBinStartVal
    1.65 +      binIdx = (int32)((value - hist->startOfRange) / hist->binWidth);
    1.66 +    }
    1.67 +
    1.68 +   hist->bins[ binIdx ] += 1;
    1.69 + }
    1.70 +
    1.71 +void
    1.72 +printFloatHist( FloatHist *hist )
    1.73 + {
    1.74 +   int32   binIdx, i, numBars, maxHeight;
    1.75 +   float32 barValue, binStart, binEnd;
    1.76 +
    1.77 +   maxHeight = 0;
    1.78 +   for( i = 0; i < hist->numBins; i++ )
    1.79 +    {
    1.80 +      if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
    1.81 +    }
    1.82 +   barValue = maxHeight / 40;  //40 spaces across page for tallest bin
    1.83 +
    1.84 +   printf("histogram: \n");
    1.85 +   if( barValue == 0 ) printf("error printing histogram\n");
    1.86 +   for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
    1.87 +    {
    1.88 +      binStart = hist->startOfRange + hist->binWidth * binIdx;
    1.89 +      binEnd = binStart + hist->binWidth;
    1.90 +      printf("bin range: %3f - %3f", binStart, binEnd );
    1.91 +
    1.92 +      numBars = hist->bins[ binIdx ] / barValue;
    1.93 +         //print one bin, height of bar is num dashes across page
    1.94 +      for( i = 0; i < numBars; i++ )
    1.95 +       {
    1.96 +         printf("-");
    1.97 +       }
    1.98 +      printf("\n");
    1.99 +    }
   1.100 + }
   1.101 +
   1.102 +
   1.103 +void
   1.104 +freeFloatHist( FloatHist *hist )
   1.105 + {
   1.106 +   VMS__free( hist->bins );
   1.107 +   VMS__free( hist );
   1.108 + }