1 /*
2 * cHistogram.cc
3 * Avida
4 *
5 * Called "histogram.cc" prior to 12/7/05.
6 * Copyright 1999-2011 Michigan State University. All rights reserved.
7 * Copyright 1993-2003 California Institute of Technology.
8 *
9 *
10 * This file is part of Avida.
11 *
12 * Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
14 *
15 * Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with Avida.
19 * If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "cHistogram.h"
24
25 #include "AvidaTools.h"
26
27 #include <cstdio>
28 #include <iostream>
29
30 using namespace std;
31 using namespace AvidaTools;
32
33
cHistogram(int in_max,int in_min)34 cHistogram::cHistogram(int in_max, int in_min)
35 {
36 max_bin = in_max;
37 min_bin = in_min;
38 entry_count = 0;
39 entry_total = 0;
40
41 int num_bins = max_bin - min_bin + 1;
42 bins = new int[num_bins];
43 for (int i = 0; i < num_bins; i++) bins[i] = 0;
44 }
45
Resize(int new_max,int new_min)46 void cHistogram::Resize(int new_max, int new_min)
47 {
48 #ifdef DEBUG
49 if (new_max < new_min) {
50 cerr << "Error: Trying to resize histogram to [" << new_min << "," << new_max << "]" << endl;
51 return;
52 }
53 #endif
54
55 // Calculate new num bins.
56 int new_num_bins = new_max - new_min + 1;
57
58 // Setup new bins, copying over information...
59 int cur_bin = 0;
60 int overlap_min = Max(min_bin, new_min);
61 int overlap_max = Min(max_bin, new_max);
62
63 int * new_bins = new int[new_num_bins];
64 for (cur_bin = new_min; cur_bin < min_bin; cur_bin++)
65 new_bins[cur_bin - new_min] = 0;
66 for (cur_bin = max_bin; cur_bin <= new_max; cur_bin++)
67 new_bins[cur_bin - new_min] = 0;
68 for (cur_bin = overlap_min; cur_bin <= overlap_max; cur_bin++)
69 new_bins[cur_bin - new_min] = bins[cur_bin - min_bin];
70
71 // Re-count bins...
72 int new_count = 0;
73 int new_total = 0;
74 for (int i = 0; i < new_num_bins; i++) {
75 new_count += new_bins[i];
76 new_total += new_bins[i] * (i + new_min);
77 }
78 entry_count = new_count;
79 entry_total = new_total;
80
81 delete [] bins;
82 bins = new_bins;
83 max_bin = new_max;
84 min_bin = new_min;
85 }
86
Print()87 void cHistogram::Print()
88 {
89 FILE * fp = fopen("test.dat", "w");
90 fprintf(fp, "Min = %d, Max = %d, Count = %d, Total = %d, Ave = %f\n",
91 min_bin, max_bin, entry_count, entry_total, GetAverage());
92 for (int i = min_bin; i <= max_bin; i++) {
93 fprintf(fp, "%d : %d\n", i, bins[i - min_bin]);
94 }
95 fflush(fp);
96 fclose(fp);
97 }
98