1 package org.catacomb.numeric.data;
2
3 import org.catacomb.report.E;
4
5 import java.util.ArrayList;
6
7
8 public class DataTable {
9
10 String id;
11
12 int ncol;
13
14 String[] headings;
15
16 ArrayList<double[]> rows = new ArrayList<double[]>();
17
18 int nrow;
19 double[][] columns;
20
21
22
23 public void setID(String s) {
24 id = s;
25 }
26
27 public String getID() {
28 return id;
29 }
30
31 public void setNColumn(int n) {
32 ncol = n;
33 }
34
35
36 public void setHeadings(String[] sa) {
37 if (headings != null) {
38 E.warning("overwriting headings? " + headings[0] + " with " + sa[0]);
39 }
40 headings = sa;
41 }
42
43 public String[] getHeadings() {
44 if (headings == null) {
45 headings = new String[ncol];
46 for (int i = 0; i < ncol; i++) {
47 headings[i] = "C" + i;
48 }
49 }
50 return headings;
51 }
52
53
54
55 public void addRow(double[] da) {
56 rows.add(da);
57 }
58
59
60 public void close() {
61 nrow = rows.size();
62 columns = new double[ncol][nrow];
63 for (int i = 0; i < nrow; i++) {
64 double[] row = rows.get(i);
65 for (int j = 0; j < ncol; j++) {
66 columns[j][i] = row[j];
67 }
68 }
69 }
70
71
72
73
74 public double[] getColumn(int icol) {
75 if (columns == null) {
76 close();
77 }
78 return columns[icol];
79 }
80
81
82
83
84 public int getNColumn() {
85 return ncol;
86 }
87
88 public double[][] getRows() {
89 if (columns == null) {
90 close();
91 }
92 double[][] ret = new double[nrow][ncol];
93 for (int i = 0; i < nrow; i++) {
94 for (int j = 0; j < ncol; j++) {
95 ret[i][j] = columns[j][i];
96 }
97 }
98 return ret;
99 }
100
101
102 public double[][] getColumns() {
103 if (columns == null) {
104 close();
105 }
106 return columns;
107 }
108
109
110
111 }