diff DblHist.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/DblHist.c	Thu Nov 11 05:45:08 2010 -0800
     1.3 @@ -0,0 +1,107 @@
     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 +
    1.12 +#include <stdio.h>
    1.13 +#include "Histogram.h"
    1.14 +
    1.15 +
    1.16 +/*This Histogram Abstract Data Type has a number of bins, the starting
    1.17 + * value, and the width of each bin, as a float, all chosen at creation.
    1.18 + *
    1.19 + *One creates a Histogram instance using the makeFloatHistogram function, then
    1.20 + * updates it with the addToFloatHist function, and prints it out with the
    1.21 + * printFloatHist function.
    1.22 + *
    1.23 + *Note, the bin width is an integer, so the end of the range is adjusted
    1.24 + * accordingly.  Use the bin-width to calculate the bin boundaries.
    1.25 + */
    1.26 +
    1.27 +
    1.28 +DblHist *
    1.29 +makeDblHistogram( int32 numBins, float64 startOfRange, float64 binWidth )
    1.30 + {
    1.31 +   DblHist *hist;
    1.32 +   int i;
    1.33 +
    1.34 +   hist = VMS__malloc( sizeof(DblHist) );
    1.35 +   hist->bins = VMS__malloc( numBins * sizeof(int) );
    1.36 +
    1.37 +   hist->numBins      = numBins;
    1.38 +   hist->binWidth     = binWidth;
    1.39 +   hist->endOfRange   = startOfRange + hist->binWidth * numBins;
    1.40 +   hist->startOfRange = startOfRange;
    1.41 +
    1.42 +   for( i = 0; i < hist->numBins; i++ )
    1.43 +    {
    1.44 +      hist->bins[ i ] = 0;
    1.45 +    }
    1.46 +
    1.47 +   return hist;
    1.48 + }
    1.49 +
    1.50 +
    1.51 +/*All values higher than or equal to a bin's start value and less than the
    1.52 + * start value of the next higher are put into that bin.
    1.53 + */
    1.54 +void
    1.55 +addToDblHist( float64 value, DblHist *hist )
    1.56 + {
    1.57 +   int binIdx;
    1.58 +
    1.59 +   if( value < hist->startOfRange )
    1.60 +    { binIdx = 0;
    1.61 +    }
    1.62 +   else if( value > hist->endOfRange )
    1.63 +    { binIdx = hist->numBins - 1;
    1.64 +    }
    1.65 +   else
    1.66 +    {    //truncate so bin holds:   binStartVal =< values < nextBinStartVal
    1.67 +      binIdx = (int32)((value - hist->startOfRange) / hist->binWidth);
    1.68 +    }
    1.69 +
    1.70 +   hist->bins[ binIdx ] += 1;
    1.71 + }
    1.72 +
    1.73 +void
    1.74 +printDblHist( DblHist *hist )
    1.75 + {
    1.76 +   int32   binIdx, i, numBars, maxHeight;
    1.77 +   float64 barValue, binStart, binEnd;
    1.78 +
    1.79 +   maxHeight = 0;
    1.80 +   for( i = 0; i < hist->numBins; i++ )
    1.81 +    {
    1.82 +      if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
    1.83 +    }
    1.84 +   barValue = maxHeight / 40;  //40 spaces across page for tallest bin
    1.85 +
    1.86 +   printf( "histogram: \n" );
    1.87 +   if( barValue == 0 ) printf( "error printing histogram\n" );
    1.88 +   for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
    1.89 +    {
    1.90 +      binStart = hist->startOfRange + hist->binWidth * binIdx;
    1.91 +      binEnd = binStart + hist->binWidth;
    1.92 +      printf( "bin range: %.6fl - %.6fl", binStart, binEnd );
    1.93 +
    1.94 +      numBars = hist->bins[ binIdx ] / barValue;
    1.95 +         //print one bin, height of bar is num dashes across page
    1.96 +      for( i = 0; i < numBars; i++ )
    1.97 +       {
    1.98 +         printf("-");
    1.99 +       }
   1.100 +      printf("\n");
   1.101 +    }
   1.102 + }
   1.103 +
   1.104 +
   1.105 +void
   1.106 +freeDblHist( DblHist *hist )
   1.107 + {
   1.108 +   VMS__free( hist->bins );
   1.109 +   VMS__free( hist );
   1.110 + }