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 "Main.h" |
| 8 | #include "Defines.h" |
| 9 | #include "Error.h" |
| 10 | #include "HpFile.h" |
| 11 | #include "Utilities.h" |
| 12 | |
| 13 | /* own stuff */ |
| 14 | #include "AreaBelow.h" |
| 15 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 16 | /* |
| 17 | * Return the area enclosed by all of the curves. The algorithm |
| 18 | * used is the same as the trapizoidal rule for integration. |
| 19 | */ |
| 20 | |
| 21 | floatish |
| 22 | AreaBelow() |
| 23 | { |
| 24 | intish i; |
| 25 | intish j; |
| 26 | intish bucket; |
| 27 | floatish value; |
| 28 | struct chunk *ch; |
| 29 | floatish area; |
| 30 | floatish trap; |
| 31 | floatish base; |
| 32 | floatish *maxima; |
| 33 | |
| 34 | maxima = (floatish *) xmalloc(nsamples * sizeof(floatish)); |
| 35 | for (i = 0; i < nsamples; i++) { |
| 36 | maxima[i] = 0.0; |
| 37 | } |
| 38 | |
| 39 | for (i = 0; i < nidents; i++) { |
| 40 | for (ch = identtable[i]->chk; ch; ch = ch->next) { |
| 41 | for (j = 0; j < ch->nd; j++) { |
| 42 | bucket = ch->d[j].bucket; |
| 43 | value = ch->d[j].value; |
| 44 | if (bucket >= nsamples) |
| 45 | Disaster("bucket out of range"); |
| 46 | maxima[ bucket ] += value; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | area = 0.0; |
| 52 | |
| 53 | for (i = 1; i < nsamples; i++) { |
| 54 | base = samplemap[i] - samplemap[i-1]; |
| 55 | if (maxima[i] > maxima[i-1]) { |
| 56 | trap = base * maxima[i-1] + ((base * (maxima[i] - maxima[i-1]))/ 2.0); |
| 57 | } else { |
| 58 | trap = base * maxima[i] + ((base * (maxima[i-1] - maxima[i]))/ 2.0); |
| 59 | } |
| 60 | |
| 61 | area += trap; |
| 62 | } |
| 63 | |
| 64 | free(maxima); |
| 65 | return area; |
| 66 | } |