changeset 6:a2388fae93ff

Merge addInterval version with VMS__malloc version
author SeanHalle
date Thu, 11 Nov 2010 05:45:08 -0800
parents 13b8591dd045 83a412f2ef98
children fa6a281bd854
files Histogram.c Histogram.h
diffstat 4 files changed, 265 insertions(+), 5 deletions(-) [+]
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 + }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/FloatHist.c	Thu Nov 11 05:45:08 2010 -0800
     2.3 @@ -0,0 +1,105 @@
     2.4 +/*
     2.5 + *  Copyright 2010 OpenSourceStewardshipFoundation.org
     2.6 + *  Licensed under GNU General Public License version 2
     2.7 + *
     2.8 + * Author: seanhalle@yahoo.com
     2.9 + *
    2.10 + */
    2.11 +#include <stdio.h>
    2.12 +#include "Histogram.h"
    2.13 +
    2.14 +/*This Histogram Abstract Data Type has a number of bins, the starting
    2.15 + * value, and the width of each bin, as a float, all chosen at creation.
    2.16 + *
    2.17 + *One creates a Histogram instance using the makeFloatHistogram function, then
    2.18 + * updates it with the addToFloatHist function, and prints it out with the
    2.19 + * printFloatHist function.
    2.20 + *
    2.21 + *Note, the bin width is an integer, so the end of the range is adjusted
    2.22 + * accordingly.  Use the bin-width to calculate the bin boundaries.
    2.23 + */
    2.24 +
    2.25 +
    2.26 +FloatHist *
    2.27 +makeFloatHistogram( int32 numBins, float32 startOfRange, float32 binWidth )
    2.28 + {
    2.29 +   FloatHist *hist;
    2.30 +   int i;
    2.31 +
    2.32 +   hist = VMS__malloc( sizeof(FloatHist) );
    2.33 +   hist->bins = VMS__malloc( numBins * sizeof(int) );
    2.34 +
    2.35 +   hist->numBins      = numBins;
    2.36 +   hist->binWidth     = binWidth;
    2.37 +   hist->endOfRange   = startOfRange + hist->binWidth * numBins;
    2.38 +   hist->startOfRange = startOfRange;
    2.39 +
    2.40 +   for( i = 0; i < hist->numBins; i++ )
    2.41 +    {
    2.42 +      hist->bins[ i ] = 0;
    2.43 +    }
    2.44 +
    2.45 +   return hist;
    2.46 + }
    2.47 +
    2.48 +
    2.49 +/*All values higher than or equal to a bin's start value and less than the
    2.50 + * start value of the next higher are put into that bin.
    2.51 + */
    2.52 +void
    2.53 +addToFloatHist( float32 value, FloatHist *hist )
    2.54 + {
    2.55 +   int binIdx;
    2.56 +
    2.57 +   if( value < hist->startOfRange )
    2.58 +    { binIdx = 0;
    2.59 +    }
    2.60 +   else if( value > hist->endOfRange )
    2.61 +    { binIdx = hist->numBins - 1;
    2.62 +    }
    2.63 +   else
    2.64 +    {    //truncate so bin holds:   binStartVal =< values < nextBinStartVal
    2.65 +      binIdx = (int32)((value - hist->startOfRange) / hist->binWidth);
    2.66 +    }
    2.67 +
    2.68 +   hist->bins[ binIdx ] += 1;
    2.69 + }
    2.70 +
    2.71 +void
    2.72 +printFloatHist( FloatHist *hist )
    2.73 + {
    2.74 +   int32   binIdx, i, numBars, maxHeight;
    2.75 +   float32 barValue, binStart, binEnd;
    2.76 +
    2.77 +   maxHeight = 0;
    2.78 +   for( i = 0; i < hist->numBins; i++ )
    2.79 +    {
    2.80 +      if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
    2.81 +    }
    2.82 +   barValue = maxHeight / 40;  //40 spaces across page for tallest bin
    2.83 +
    2.84 +   printf("histogram: \n");
    2.85 +   if( barValue == 0 ) printf("error printing histogram\n");
    2.86 +   for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
    2.87 +    {
    2.88 +      binStart = hist->startOfRange + hist->binWidth * binIdx;
    2.89 +      binEnd = binStart + hist->binWidth;
    2.90 +      printf("bin range: %3f - %3f", binStart, binEnd );
    2.91 +
    2.92 +      numBars = hist->bins[ binIdx ] / barValue;
    2.93 +         //print one bin, height of bar is num dashes across page
    2.94 +      for( i = 0; i < numBars; i++ )
    2.95 +       {
    2.96 +         printf("-");
    2.97 +       }
    2.98 +      printf("\n");
    2.99 +    }
   2.100 + }
   2.101 +
   2.102 +
   2.103 +void
   2.104 +freeFloatHist( FloatHist *hist )
   2.105 + {
   2.106 +   VMS__free( hist->bins );
   2.107 +   VMS__free( hist );
   2.108 + }
     3.1 --- a/Histogram.c	Thu Nov 11 05:37:07 2010 -0800
     3.2 +++ b/Histogram.c	Thu Nov 11 05:45:08 2010 -0800
     3.3 @@ -5,9 +5,8 @@
     3.4   * Author: seanhalle@yahoo.com
     3.5   *
     3.6   */
     3.7 -
     3.8 +#include <stdio.h>
     3.9  #include "Histogram.h"
    3.10 -#include <malloc.h>
    3.11  
    3.12  
    3.13  /*This Histogram Abstract Data Type has a number of bins plus a range of
    3.14 @@ -24,12 +23,14 @@
    3.15  
    3.16  Histogram *
    3.17  makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange )
    3.18 +
    3.19   {
    3.20     Histogram *hist;
    3.21     int32 i;
    3.22  
    3.23 -   hist = malloc( sizeof(Histogram) );
    3.24 -   hist->bins = malloc( numBins * sizeof(int32) );
    3.25 +
    3.26 +   hist = VMS__malloc( sizeof(Histogram) );
    3.27 +   hist->bins = VMS__malloc( numBins * sizeof(int32) );
    3.28  
    3.29     hist->numBins      = numBins;
    3.30     hist->binWidth     = (endOfRange - startOfRange) / numBins;
    3.31 @@ -79,6 +80,7 @@
    3.32   {
    3.33     int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
    3.34  
    3.35 +
    3.36     maxHeight = 0;
    3.37     for( i = 0; i < hist->numBins; i++ )
    3.38      {
    3.39 @@ -86,7 +88,7 @@
    3.40      }
    3.41     barValue = maxHeight / 60;  //60 spaces across page for tallest bin
    3.42  
    3.43 -   printf("histogram: \n");
    3.44 +   printf( "histogram: \n" );
    3.45     if( barValue == 0 ) printf("error printing histogram\n");
    3.46     for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
    3.47      {
    3.48 @@ -104,3 +106,9 @@
    3.49      }
    3.50   }
    3.51  
    3.52 +void
    3.53 +freeHist( Histogram *hist )
    3.54 + {
    3.55 +   VMS__free( hist->bins );
    3.56 +   VMS__free( hist );
    3.57 + }
     4.1 --- a/Histogram.h	Thu Nov 11 05:37:07 2010 -0800
     4.2 +++ b/Histogram.h	Thu Nov 11 05:45:08 2010 -0800
     4.3 @@ -6,6 +6,7 @@
     4.4   * 
     4.5   */
     4.6  
     4.7 +#include "../VMS_primitive_data_types.h"
     4.8  
     4.9  #ifndef _HISTOGRAM_H
    4.10  #define	_HISTOGRAM_H
    4.11 @@ -20,6 +21,26 @@
    4.12   }
    4.13  Histogram;
    4.14  
    4.15 +typedef struct
    4.16 + {
    4.17 +   float32  startOfRange;
    4.18 +   float32  endOfRange;
    4.19 +   int      numBins;
    4.20 +   float32  binWidth;
    4.21 +   int     *bins;
    4.22 + }
    4.23 +FloatHist;
    4.24 +
    4.25 +typedef struct
    4.26 + {
    4.27 +   float64 startOfRange;
    4.28 +   float64  endOfRange;
    4.29 +   int      numBins;
    4.30 +   float64  binWidth;
    4.31 +   int     *bins;
    4.32 + }
    4.33 +DblHist;
    4.34 +
    4.35  Histogram *
    4.36  makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange );
    4.37  
    4.38 @@ -32,5 +53,24 @@
    4.39  void
    4.40  printHist( Histogram *hist );
    4.41  
    4.42 +FloatHist *
    4.43 +makeFloatHistogram( int numBins, float32 startOfRange, float32 binWidth );
    4.44 +
    4.45 +void
    4.46 +addToFloatHist( float32 value, FloatHist *hist );
    4.47 +
    4.48 +void
    4.49 +printFloatHist( FloatHist *hist );
    4.50 +
    4.51 +
    4.52 +DblHist *
    4.53 +makeDblHistogram( int numBins, float64 startOfRange, float64 binWidth );
    4.54 +
    4.55 +void
    4.56 +addToDblHist( float64 value, DblHist *hist );
    4.57 +
    4.58 +void
    4.59 +printDblHist( DblHist *hist );
    4.60 +
    4.61  #endif	/* _HISTOGRAM_H */
    4.62