blob: e14ed5ebedac36ccc4c0158a30cc8be2cf4cfda7 [file] [log] [blame]
nethercote5912c812004-02-15 15:38:08 +00001/* 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
nethercotec9f36922004-02-14 16:40:02 +00005#include <stdio.h>
nethercotea0b77912004-06-02 20:43:24 +00006#include <stdlib.h>
nethercotec9f36922004-02-14 16:40:02 +00007#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
nethercotec9f36922004-02-14 16:40:02 +000016/*
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
21floatish
22AreaBelow()
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}