001: /*
002: * Created on Sep 24, 2004
003: *
004: */
005: package org.jmatlab.linalg;
006:
007: import java.io.Serializable;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.Map;
011:
012: import org.jmatlab.semantic.DataType;
013: import org.jmatlab.semantic.SemanticException;
014: import org.jmatlab.semantic.Symbol;
015: import org.jmatlab.util.FormatNumber;
016:
017: /**
018: * @author MQWF
019: *
020: */
021: public class DefaultStructImpl implements IStruct, Serializable {
022:
023: private int rows = 1;
024: private int cols = 1;
025: private Map[][] maps = new Map[1][1];
026:
027: public DefaultStructImpl(Map map) {
028: maps[0][0] = map;
029: }
030:
031: public DefaultStructImpl(int n, Map map) {
032: this .maps = new HashMap[1][n];
033: for (int j = 0; j < cols; j++) {
034: this .maps[0][j] = new HashMap(13);
035: }
036: }
037:
038: public DefaultStructImpl(int m, int n, Map map) {
039: this .maps = new HashMap[1][n];
040: for (int i = 0; i < rows; i++) {
041: for (int j = 0; j < cols; j++) {
042: this .maps[0][j] = new HashMap(13);
043: }
044: }
045: }
046:
047: public void setField(String name, Symbol symbol) {
048: Map m = maps[0][0];
049: String key = name.toUpperCase();
050: if (m.containsKey(key)) {
051: m.remove(key);
052: }
053: m.put(key, symbol);
054: }
055:
056: public void setField(int n, String name, Symbol symbol) {
057: try {
058: Map m = maps[0][n - 1];
059: String key = name.toUpperCase();
060: if (m.containsKey(key)) {
061: m.remove(key);
062: }
063: m.put(key, symbol);
064: } catch (Exception e) {
065: Map[][] temp = new Map[1][n];
066: for (int j = 0; j < cols; j++) {
067: temp[0][j] = maps[0][j];
068: }
069: temp[0][n - 1] = new HashMap(13);
070: if (name != null)
071: temp[0][n - 1].put(name, symbol);
072: this .maps = temp;
073: this .cols = n;
074: }
075: }
076:
077: public void setField(int m, int n, String name, Symbol symbol) {
078: try {
079: Map mp = maps[m - 1][n - 1];
080: String key = name.toUpperCase();
081: if (mp.containsKey(key)) {
082: mp.remove(key);
083: }
084: mp.put(key, symbol);
085: } catch (Exception e) {
086: if (m > rows && n > cols) {
087: Map[][] temp = new Map[m][n];
088: for (int i = 0; i < rows; i++) {
089: for (int j = 0; j < cols; j++) {
090: temp[i][j] = maps[i][j];
091: }
092: }
093: temp[m - 1][n - 1] = new HashMap(13);
094: if (name != null)
095: temp[m - 1][n - 1].put(name, symbol);
096: this .maps = temp;
097: this .rows = m;
098: this .cols = n;
099: } else if (m > rows) {
100: Map[][] temp = new Map[m][cols];
101: for (int i = 0; i < rows; i++) {
102: for (int j = 0; j < cols; j++) {
103: temp[i][j] = maps[i][j];
104: }
105: }
106: temp[m - 1][n - 1] = new HashMap(13);
107: if (name != null)
108: temp[m - 1][n - 1].put(name, symbol);
109: this .maps = temp;
110: this .rows = m;
111: } else if (n > cols) {
112: Map[][] temp = new Map[rows][n];
113: for (int i = 0; i < rows; i++) {
114: for (int j = 0; j < cols; j++) {
115: temp[i][j] = maps[i][j];
116: }
117: }
118: temp[m - 1][n - 1] = new HashMap(13);
119: if (name != null)
120: temp[m - 1][n - 1].put(name, symbol);
121: this .maps = temp;
122: this .cols = n;
123: }
124: }
125: }
126:
127: public Symbol getField(String name) {
128: return (Symbol) maps[0][0].get(name);
129: }
130:
131: public Symbol getField(int i, String name) {
132: return (Symbol) maps[0][i - 1].get(name);
133: }
134:
135: public Symbol getField(int i, int j, String name) {
136: return (Symbol) maps[i - 1][j - 1].get(name);
137: }
138:
139: public String toString() {
140: StringBuffer buffer = new StringBuffer();
141: for (int i = 0; i < rows; i++) {
142: for (int j = 0; j < cols; j++) {
143: if (rows == 1) {
144: buffer.append("(i = " + (j + 1) + ")");
145: } else {
146: buffer.append("(i = " + (i + 1) + ", j = "
147: + (j + 1) + ")");
148: }
149: buffer.append('\n');
150: Map mp = maps[i][j];
151: if (mp == null || mp.size() == 0) {
152: buffer.append("Empty");
153: buffer.append('\n');
154: continue;
155: }
156: Iterator iter = mp.keySet().iterator();
157: while (iter.hasNext()) {
158: String key = (String) iter.next();
159: Symbol sym = (Symbol) mp.get(key);
160: buffer.append(key + " = ");
161: buffer.append('\n');
162: buffer.append(print(sym));
163: buffer.append('\n');
164: }
165: }
166: }
167: return buffer.toString();
168: }
169:
170: private String print(Symbol symbol) {
171: int type = symbol.getType();
172: String output = null;
173: if (type == DataType.MATRIX) {
174: output = symbol.getMatrix().toString();
175: } else if (type == DataType.COMPLEX) {
176: output = symbol.getComplex().toString();
177: } else if (type == DataType.DOUBLE) {
178: double d = symbol.getDouble().doubleValue();
179: if (Double.isInfinite(d)) {
180: output = "Inf";
181: } else if (Double.isInfinite(-d)) {
182: output = "-Inf";
183: } else if (Double.isNaN(d)) {
184: output = "NaN";
185: } else {
186: output = FormatNumber.format(d);
187: }
188: } else if (type == DataType.BOOLEAN) {
189: output = "" + symbol.getBoolean().booleanValue();
190: } else if (type == DataType.STRING) {
191: output = symbol.getString();
192: } else if (type == DataType.STRUCT) {
193: output = symbol.getStruct().toString();
194: } else {
195: throw new SemanticException("Unknow datatype to print.");
196: }
197: return output;
198: }
199:
200: }
|