nethercote | 5912c81 | 2004-02-15 15:38:08 +0000 | [diff] [blame] | 1 | /* This file is part of hp2ps, a graph drawer for memory profiles. |
| 2 | Copyright (C) 2002 The University Court of the University of Glasgow. |
| 3 | This program is governed by the license contained in the file LICENSE. */ |
| 4 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
nethercote | a0b7791 | 2004-06-02 20:43:24 +0000 | [diff] [blame] | 6 | #include <stdlib.h> |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include <math.h> |
| 9 | #include "Main.h" |
| 10 | #include "Defines.h" |
| 11 | #include "Error.h" |
| 12 | #include "HpFile.h" |
| 13 | #include "Utilities.h" |
| 14 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 15 | /* own stuff */ |
| 16 | #include "Deviation.h" |
| 17 | |
| 18 | /* |
| 19 | * Reorder the identifiers in the identifier table so that the |
| 20 | * ones whose data points exhibit the mininal standard deviation |
| 21 | * come first. |
| 22 | */ |
| 23 | |
| 24 | void |
| 25 | Deviation() |
| 26 | { |
| 27 | intish i; |
| 28 | intish j; |
| 29 | floatish dev; |
| 30 | struct chunk* ch; |
| 31 | int min; |
| 32 | floatish t; |
| 33 | struct entry* e; |
| 34 | floatish *averages; |
| 35 | floatish *deviations; |
| 36 | |
| 37 | averages = (floatish*) xmalloc(nidents * sizeof(floatish)); |
| 38 | deviations = (floatish*) xmalloc(nidents * sizeof(floatish)); |
| 39 | |
| 40 | /* find averages */ |
| 41 | |
| 42 | for (i = 0; i < nidents; i++) { |
| 43 | averages[i] = 0.0; |
| 44 | } |
| 45 | |
| 46 | for (i = 0; i < nidents; i++) { |
| 47 | for (ch = identtable[i]->chk; ch; ch = ch->next) { |
| 48 | for (j = 0; j < ch->nd; j++) { |
| 49 | averages[i] += ch->d[j].value; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | for (i = 0; i < nidents; i++) { |
| 55 | averages[i] /= (floatish) nsamples; |
| 56 | } |
| 57 | |
| 58 | /* calculate standard deviation */ |
| 59 | |
| 60 | for (i = 0; i < nidents; i++) { |
| 61 | deviations[i] = 0.0; |
| 62 | } |
| 63 | |
| 64 | for (i = 0; i < nidents; i++) { |
| 65 | for (ch = identtable[i]->chk; ch; ch = ch->next) { |
| 66 | for (j = 0; j < ch->nd; j++) { |
| 67 | dev = ch->d[j].value - averages[i]; |
| 68 | deviations[i] += dev * dev; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | for (i = 0; i < nidents; i++) { |
| 74 | deviations[i] = (floatish) sqrt ((doublish) (deviations[i] / |
| 75 | (floatish) (nsamples - 1))); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* sort on basis of standard deviation */ |
| 80 | |
| 81 | for (i = 0; i < nidents-1; i++) { |
| 82 | min = i; |
| 83 | for (j = i+1; j < nidents; j++) { |
| 84 | if (deviations[ j ] < deviations[min]) { |
| 85 | min = j; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | t = deviations[min]; |
| 90 | deviations[min] = deviations[i]; |
| 91 | deviations[i] = t; |
| 92 | |
| 93 | e = identtable[min]; |
| 94 | identtable[min] = identtable[i]; |
| 95 | identtable[i] = e; |
| 96 | } |
| 97 | |
| 98 | free(averages); |
| 99 | free(deviations); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | Identorder(iflag) |
| 104 | int iflag; /* a funny three-way flag ? WDP 95/03 */ |
| 105 | { |
| 106 | int i; |
| 107 | int j; |
| 108 | int min; |
| 109 | struct entry* e; |
| 110 | |
| 111 | /* sort on basis of ident string */ |
| 112 | if (iflag > 0) { |
| 113 | /* greatest at top i.e. smallest at start */ |
| 114 | |
| 115 | for (i = 0; i < nidents-1; i++) { |
| 116 | min = i; |
| 117 | for (j = i+1; j < nidents; j++) { |
| 118 | if (strcmp(identtable[j]->name, identtable[min]->name) < 0) { |
| 119 | min = j; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | e = identtable[min]; |
| 124 | identtable[min] = identtable[i]; |
| 125 | identtable[i] = e; |
| 126 | } |
| 127 | } else { |
| 128 | /* smallest at top i.e. greatest at start */ |
| 129 | |
| 130 | for (i = 0; i < nidents-1; i++) { |
| 131 | min = i; |
| 132 | for (j = i+1; j < nidents; j++) { |
| 133 | if (strcmp(identtable[j]->name, identtable[min]->name) > 0) { |
| 134 | min = j; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | e = identtable[min]; |
| 139 | identtable[min] = identtable[i]; |
| 140 | identtable[i] = e; |
| 141 | } |
| 142 | } |
| 143 | } |