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