VarMap.java
Go to the documentation of this file.00001 package com.graphbuilder.math;
00002
00012 public class VarMap {
00013
00014 private boolean caseSensitive = true;
00015 private String[] name = new String[2];
00016 private double[] value = new double[2];
00017 private int numVars = 0;
00018
00019 public VarMap() {
00020 this(true);
00021 }
00022
00023 public VarMap(boolean caseSensitive) {
00024 this.caseSensitive = caseSensitive;
00025 }
00026
00032 public double getValue(String varName) {
00033 for (int i = 0; i < numVars; i++)
00034 if (caseSensitive && name[i].equals(varName) || !caseSensitive && name[i].equalsIgnoreCase(varName))
00035 return value[i];
00036
00037 throw new RuntimeException("variable value has not been set: " + varName);
00038 }
00039
00045 public void setValue(String varName, double val) {
00046 if (varName == null)
00047 throw new IllegalArgumentException("varName cannot be null");
00048
00049 for (int i = 0; i < numVars; i++) {
00050 if (caseSensitive && name[i].equals(varName) || !caseSensitive && name[i].equalsIgnoreCase(varName)) {
00051 value[i] = val;
00052 return;
00053 }
00054 }
00055
00056 if (numVars == name.length) {
00057 String[] tmp1 = new String[2 * numVars];
00058 double[] tmp2 = new double[tmp1.length];
00059
00060 for (int i = 0; i < numVars; i++) {
00061 tmp1[i] = name[i];
00062 tmp2[i] = value[i];
00063 }
00064
00065 name = tmp1;
00066 value = tmp2;
00067 }
00068
00069 name[numVars] = varName;
00070 value[numVars] = val;
00071 numVars++;
00072 }
00073
00077 public boolean isCaseSensitive() {
00078 return caseSensitive;
00079 }
00080
00084 public String[] getVariableNames() {
00085 String[] arr = new String[numVars];
00086
00087 for (int i = 0; i < arr.length; i++)
00088 arr[i] = name[i];
00089
00090 return arr;
00091 }
00092
00097 public double[] getValues() {
00098 double[] arr = new double[numVars];
00099
00100 for (int i = 0; i < arr.length; i++)
00101 arr[i] = value[i];
00102
00103 return arr;
00104 }
00105
00109 public void remove(String varName) {
00110 for (int i = 0; i < numVars; i++) {
00111 if (caseSensitive && name[i].equals(varName) || !caseSensitive && name[i].equalsIgnoreCase(varName)) {
00112 for (int j = i + 1; j < numVars; j++) {
00113 name[j - 1] = name[j];
00114 value[j - 1] = value[j];
00115 }
00116 numVars--;
00117 name[numVars] = null;
00118 value[numVars] = 0;
00119 break;
00120 }
00121 }
00122 }
00123 }