1 package org.catacomb.numeric.data;
2
3 import java.lang.reflect.Field;
4
5 import org.catacomb.interlish.content.BasicTouchTime;
6 import org.catacomb.report.E;
7
8
9 import java.util.ArrayList;
10
11 public class DDSlice extends StackMultipleSlice implements NumVectorSet {
12
13
14 String[] elementNames;
15 double[][] data;
16
17 int npcache;
18
19
20
21
22 public DDSlice(BlockStack bs, String fnm, Field f, String u, String t, String[] sa) {
23 super(bs, fnm, f, u, t);
24 elementNames = sa;
25 npcache = 0;
26 data = new double[10][];
27 }
28
29
30 public BasicTouchTime getTouchTime() {
31 return blockStack.getChangeTime();
32 }
33
34
35 public String[] getNames() {
36 return elementNames;
37 }
38
39
40 public double[] getData() {
41 E.warning("shouldn't be calling ...");
42 return null;
43 }
44
45 public double[][] getDData() {
46 if (upToDate()) {
47
48
49 } else {
50 int np = blockStack.getSize();
51 if (np > data.length) {
52 double[][] dn = new double[np + np/2 + 10][];
53 for (int i = 0; i < npcache; i++) {
54 dn[i] = data[i];
55 }
56 data = dn;
57 }
58
59 try {
60
61 for (int i = npcache; i < np; i++) {
62 data[i] = (double[])(field.get(blockStack.getBlock(i)));
63 }
64 npcache = np;
65
66 } catch (Exception ex) {
67 E.error("exception reading slice from block stack " + this + " " + ex);
68 }
69
70 }
71
72 cacheTime.now();
73
74 return data;
75 }
76
77
78 void clearCache() {
79 npcache = 0;
80 }
81
82
83 public ArrayList<NumVector> getVectors() {
84 ArrayList<NumVector> ret = new ArrayList<NumVector>();
85 double[][] dd = getDData();
86 if (npcache > 0) {
87 if (dd[0] == null) {
88 E.error("no data in slcice? " + getName() + " " + getLabel());
89
90 } else {
91 int nvec = dd[0].length;
92 for (int i = 0; i < nvec; i++) {
93
94 String snm = null;
95 if (elementNames != null && elementNames.length > i) {
96 snm = elementNames[i];
97 }
98 if (snm == null) {
99 snm = getName() + " " + i;
100 }
101
102 ret.add(new DDSubSlice(this, i, snm));
103 }
104 }
105 }
106 return ret;
107 }
108
109
110 public ArrayList<NumVector> getByIndex(int[] ia) {
111 ArrayList<NumVector> ret = new ArrayList<NumVector>();
112 ArrayList<NumVector> av = getVectors();
113 if (ia == null) {
114 ret.addAll(av);
115
116 } else {
117 for (int i : ia) {
118 ret.add(av.get(i));
119 }
120 }
121 return ret;
122 }
123
124
125
126 }