multipage_hp2ps
[ghc.git] / utils / hp2ps / TopTwenty.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "Defines.h"
5 #include "Error.h"
6 #include "HpFile.h"
7 #include "Utilities.h"
8
9 /* own stuff */
10 #include "TopTwenty.h"
11
12 /*
13 * We only have room in the key for a maximum of 20 identifiers.
14 * We therefore choose to keep the top 20 bands --- these will
15 * be the most important ones, since this pass is performed after
16 * the threshold and standard deviation passes. If there are more
17 * than 20 bands, the excess are gathered together as an "OTHER" ]
18 * band which appears as band 20.
19 */
20
21 void
22 TopTwenty()
23 {
24 intish i;
25 intish j;
26 intish compact;
27 intish bucket;
28 floatish value;
29 struct entry* en;
30 struct chunk* ch;
31 floatish *other;
32
33 i = nidents;
34 if (i <= TWENTY) return; /* nothing to do! */
35
36 other = (floatish*) xmalloc(nsamples * sizeof(floatish));
37 /* build a list of samples for "OTHER" */
38
39 compact = (i - TWENTY) + 1;
40
41 for (i = 0; i < nsamples; i++) {
42 other[ i ] = 0.0;
43 }
44
45 for (i = 0; i < compact && i < nidents; i++) {
46 for (ch = identtable[i]->chk; ch; ch = ch->next) {
47 for (j = 0; j < ch->nd; j++) {
48 bucket = ch->d[j].bucket;
49 value = ch->d[j].value;
50 if (bucket >= nsamples)
51 Disaster("bucket out of range");
52 other[ bucket ] += value;
53 }
54 }
55 }
56
57 en = MakeEntry("OTHER");
58 en->next = 0;
59
60 for (i = 0; i < nsamples; i++) {
61 StoreSample(en, i, other[i]);
62 }
63
64 /* slide samples down */
65 for (i = compact; i < nidents; i++) {
66 identtable[i-compact+1] = identtable[i];
67 }
68
69 nidents = TWENTY;
70 identtable[0] = en;
71 free(other);
72 }