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