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