[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / utils / hp2ps / TraceElement.c
1 #include <stdio.h>
2 #include "Main.h"
3 #include "Defines.h"
4 #include "HpFile.h"
5 #include "Error.h"
6 #include "Utilities.h"
7
8 /* own stuff */
9 #include "TraceElement.h"
10
11 /*
12  *      Compute the total volume for each identifier, and the grand 
13  *      total of these totals. The identifiers whose totals when 
14  *      added together amount to less that a threshold percentage 
15  *      (default 1%) of the grand total are considered to be ``trace
16  *      elements'' and they are thrown away.    
17  */
18
19 extern void free();
20
21 extern floatish thresholdpercent;
22
23 void TraceElement()
24 {
25     intish i;
26     intish j;
27     struct chunk* ch;
28     floatish grandtotal;
29     intish   min;
30     floatish t;
31     floatish p;
32     struct entry* e;
33     intish *totals; 
34
35     totals = (intish *) xmalloc(nidents * sizeof(intish));
36
37     /* find totals */
38
39     for (i = 0; i < nidents; i++) {
40         totals[ i ] = 0;
41     }
42  
43     for (i = 0; i < nidents; i++) {
44         for (ch = identtable[i]->chk; ch; ch = ch->next) {
45             for (j = 0; j < ch->nd; j++) {
46                 totals[ i ] += ch->d[j].value; 
47             }
48         }
49     }    
50
51     /* sort on the basis of total */
52
53     for (i = 0; i < nidents-1; i++) {
54         min = i;
55         for (j = i+1; j < nidents; j++) {
56             if (totals[ j ] < totals[ min ]) {
57                 min = j;
58             }
59         }    
60
61         t = totals[ min ];
62         totals[ min ] = totals[ i ];
63         totals[ i ] = t;
64
65         e = identtable[ min ];
66         identtable[ min ] = identtable[ i ];
67         identtable[ i ] = e;
68     }
69
70
71     /* find the grand total (NB: can get *BIG*!) */
72
73     grandtotal = 0.0;
74
75     for (i = 0; i < nidents; i++) {
76         grandtotal += (floatish) totals[ i ];
77     }
78
79     t = 0.0;    /* cumulative percentage */
80    
81     for (i = 0; i < nidents; i++) {
82         p = (100.0 * (floatish) totals[i]) / grandtotal;
83         t = t + p; 
84         if (t >= THRESHOLD_PERCENT) {
85             break;
86         }
87     }
88
89     /* identifiers from 0 to i-1 should be removed */
90     for (j = 0; i < nidents; i++, j++) {
91         identtable[j] = identtable[i]; 
92     }
93
94     nidents = j;
95
96     free(totals);
97 }