| rev |
line source |
|
Me@2
|
1 /*
|
|
Me@2
|
2 * Copyright 2010 OpenSourceStewardshipFoundation.org
|
|
Me@2
|
3 * Licensed under GNU General Public License version 2
|
|
Me@2
|
4 *
|
|
Me@2
|
5 * Author: seanhalle@yahoo.com
|
|
Me@2
|
6 *
|
|
Me@2
|
7 */
|
|
Me@2
|
8
|
|
Me@4
|
9 #include <stdio.h>
|
|
Me@2
|
10 #include "Histogram.h"
|
|
Me@2
|
11
|
|
Me@2
|
12
|
|
Me@2
|
13 /*This Histogram Abstract Data Type has a number of bins, the starting
|
|
Me@2
|
14 * value, and the width of each bin, as a float, all chosen at creation.
|
|
Me@2
|
15 *
|
|
Me@2
|
16 *One creates a Histogram instance using the makeFloatHistogram function, then
|
|
Me@2
|
17 * updates it with the addToFloatHist function, and prints it out with the
|
|
Me@2
|
18 * printFloatHist function.
|
|
Me@2
|
19 *
|
|
Me@2
|
20 *Note, the bin width is an integer, so the end of the range is adjusted
|
|
Me@2
|
21 * accordingly. Use the bin-width to calculate the bin boundaries.
|
|
Me@2
|
22 */
|
|
Me@2
|
23
|
|
Me@2
|
24
|
|
Me@2
|
25 DblHist *
|
|
Me@2
|
26 makeDblHistogram( int32 numBins, float64 startOfRange, float64 binWidth )
|
|
Me@2
|
27 {
|
|
Me@2
|
28 DblHist *hist;
|
|
Me@2
|
29 int i;
|
|
Me@2
|
30
|
|
Me@16
|
31 hist = VMS_int__malloc( sizeof(DblHist) );
|
|
Me@16
|
32 hist->bins = VMS_int__malloc( numBins * sizeof(int) );
|
|
Me@2
|
33
|
|
Me@2
|
34 hist->numBins = numBins;
|
|
Me@2
|
35 hist->binWidth = binWidth;
|
|
Me@2
|
36 hist->endOfRange = startOfRange + hist->binWidth * numBins;
|
|
Me@2
|
37 hist->startOfRange = startOfRange;
|
|
Me@2
|
38
|
|
Me@2
|
39 for( i = 0; i < hist->numBins; i++ )
|
|
Me@2
|
40 {
|
|
Me@2
|
41 hist->bins[ i ] = 0;
|
|
Me@2
|
42 }
|
|
Me@2
|
43
|
|
Me@2
|
44 return hist;
|
|
Me@2
|
45 }
|
|
Me@2
|
46
|
|
Me@2
|
47
|
|
Me@2
|
48 /*All values higher than or equal to a bin's start value and less than the
|
|
Me@2
|
49 * start value of the next higher are put into that bin.
|
|
Me@2
|
50 */
|
|
Me@2
|
51 void
|
|
Me@2
|
52 addToDblHist( float64 value, DblHist *hist )
|
|
Me@2
|
53 {
|
|
Me@2
|
54 int binIdx;
|
|
Me@2
|
55
|
|
Me@2
|
56 if( value < hist->startOfRange )
|
|
Me@2
|
57 { binIdx = 0;
|
|
Me@2
|
58 }
|
|
Me@2
|
59 else if( value > hist->endOfRange )
|
|
Me@2
|
60 { binIdx = hist->numBins - 1;
|
|
Me@2
|
61 }
|
|
Me@2
|
62 else
|
|
Me@2
|
63 { //truncate so bin holds: binStartVal =< values < nextBinStartVal
|
|
Me@2
|
64 binIdx = (int32)((value - hist->startOfRange) / hist->binWidth);
|
|
Me@2
|
65 }
|
|
Me@2
|
66
|
|
Me@2
|
67 hist->bins[ binIdx ] += 1;
|
|
Me@2
|
68 }
|
|
Me@2
|
69
|
|
Me@2
|
70 void
|
|
Me@2
|
71 printDblHist( DblHist *hist )
|
|
Me@2
|
72 {
|
|
Me@2
|
73 int32 binIdx, i, numBars, maxHeight;
|
|
Me@2
|
74 float64 barValue, binStart, binEnd;
|
|
Me@2
|
75
|
|
Me@2
|
76 maxHeight = 0;
|
|
Me@2
|
77 for( i = 0; i < hist->numBins; i++ )
|
|
Me@2
|
78 {
|
|
Me@2
|
79 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
|
|
Me@2
|
80 }
|
|
Me@2
|
81 barValue = maxHeight / 40; //40 spaces across page for tallest bin
|
|
Me@2
|
82
|
|
Me@2
|
83 printf( "histogram: \n" );
|
|
Me@2
|
84 if( barValue == 0 ) printf( "error printing histogram\n" );
|
|
Me@2
|
85 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
|
|
Me@2
|
86 {
|
|
Me@2
|
87 binStart = hist->startOfRange + hist->binWidth * binIdx;
|
|
Me@2
|
88 binEnd = binStart + hist->binWidth;
|
|
Me@2
|
89 printf( "bin range: %.6fl - %.6fl", binStart, binEnd );
|
|
Me@2
|
90
|
|
Me@2
|
91 numBars = hist->bins[ binIdx ] / barValue;
|
|
Me@2
|
92 //print one bin, height of bar is num dashes across page
|
|
Me@2
|
93 for( i = 0; i < numBars; i++ )
|
|
Me@2
|
94 {
|
|
Me@2
|
95 printf("-");
|
|
Me@2
|
96 }
|
|
Me@2
|
97 printf("\n");
|
|
Me@2
|
98 }
|
|
Me@2
|
99 }
|
|
Me@2
|
100
|
|
Me@3
|
101
|
|
Me@3
|
102 void
|
|
Me@3
|
103 freeDblHist( DblHist *hist )
|
|
Me@3
|
104 {
|
|
Me@16
|
105 VMS_int__free( hist->bins );
|
|
Me@16
|
106 VMS_int__free( hist );
|
|
Me@3
|
107 }
|