00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package org.gecode;
00025
00026 import java.util.ArrayList;
00027 import java.lang.reflect.Constructor;
00028 import java.lang.reflect.InvocationTargetException;
00029
00058 public class VarArray<Var extends GecodeVar> extends ArrayList<Var> {
00061 public VarArray() {
00062 super();
00063 }
00064
00067 public VarArray(int size) {
00068 super(size);
00069 }
00070
00073 @SuppressWarnings("unchecked")
00074 public <C extends Iterable<Var>> VarArray(JavaSpace newHome,
00075 boolean share, C va) {
00076 super();
00077 for (Var v: va) {
00078 add((Var)v.copy(newHome, share));
00079 }
00080 }
00081
00084 public <C extends Iterable<? extends Var>> VarArray(C va) {
00085 super();
00086 for (Var v: va) add(v);
00087 }
00088
00091 public <V2 extends Var> VarArray(V2... args) {
00092 super(args.length);
00093 for (V2 v: args) add(v);
00094 }
00095
00099 public VarArray(JavaSpace home, int size, Class<? extends Var> type) {
00100 super(size);
00101 try {
00102 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class);
00103
00104 for (int i = 0; i < size; ++i) {
00105 add(ctor.newInstance(home));
00106 }
00107 } catch(NoSuchMethodException ex) {
00108 throw new FatalException(ex);
00109 } catch(InstantiationException ex) {
00110 throw new FatalException(ex);
00111 } catch(IllegalAccessException ex) {
00112 throw new FatalException(ex);
00113 } catch(InvocationTargetException ex) {
00114 throw new FatalException(ex);
00115 }
00116 }
00117
00121 public VarArray(JavaSpace home, int size, Class<? extends Var> type, Object arg) {
00122 super(size);
00123 try {
00124 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class, arg.getClass());
00125
00126 for (int i = 0; i < size; ++i) {
00127 add(ctor.newInstance(home, arg));
00128 }
00129 } catch(NoSuchMethodException ex) {
00130 throw new FatalException(ex);
00131 } catch(InstantiationException ex) {
00132 throw new FatalException(ex);
00133 } catch(IllegalAccessException ex) {
00134 throw new FatalException(ex);
00135 } catch(InvocationTargetException ex) {
00136 throw new FatalException(ex);
00137 }
00138 }
00139
00143 public VarArray(JavaSpace home, int size, Class<? extends Var> type,
00144 Object arg1, Object arg2) {
00145 super(size);
00146 try {
00147 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class,
00148 arg1.getClass(),
00149 arg2.getClass());
00150
00151 for (int i = 0; i < size; ++i) {
00152 add(ctor.newInstance(home, arg1, arg2));
00153 }
00154 } catch(NoSuchMethodException ex) {
00155 throw new FatalException(ex);
00156 } catch(InstantiationException ex) {
00157 throw new FatalException(ex);
00158 } catch(IllegalAccessException ex) {
00159 throw new FatalException(ex);
00160 } catch(InvocationTargetException ex) {
00161 throw new FatalException(ex);
00162 }
00163 }
00164
00168 public VarArray(JavaSpace home, int size, Class<? extends Var> type,
00169 Object arg1, Object arg2, Object arg3) {
00170 super(size);
00171 try {
00172 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class,
00173 arg1.getClass(),
00174 arg2.getClass(),
00175 arg3.getClass());
00176
00177 for (int i = 0; i < size; ++i) {
00178 add(ctor.newInstance(home, arg1, arg2, arg3));
00179 }
00180 } catch(NoSuchMethodException ex) {
00181 throw new FatalException(ex);
00182 } catch(InstantiationException ex) {
00183 throw new FatalException(ex);
00184 } catch(IllegalAccessException ex) {
00185 throw new FatalException(ex);
00186 } catch(InvocationTargetException ex) {
00187 throw new FatalException(ex);
00188 }
00189 }
00190
00194 public <A> VarArray(JavaSpace home, Class<? extends Var> type, A[] as) {
00195 super(as.length);
00196 if (as.length == 0) return;
00197 try {
00198 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class, as[0].getClass());
00199 for (A a: as) {
00200 add(ctor.newInstance(home, a));
00201 }
00202 } catch(NoSuchMethodException ex) {
00203 throw new FatalException(ex);
00204 } catch(InstantiationException ex) {
00205 throw new FatalException(ex);
00206 } catch(IllegalAccessException ex) {
00207 throw new FatalException(ex);
00208 } catch(InvocationTargetException ex) {
00209 throw new FatalException(ex);
00210 }
00211 }
00212
00216 public <A,B> VarArray(JavaSpace home, Class<? extends Var> type, A[] as, B[] bs) {
00217 super(as.length);
00218 if (as.length != bs.length)
00219 throw new IllegalArgumentException("Arraylengths do not match: " +
00220 as.length + " != " + bs.length);
00221 if (as.length == 0) return;
00222 try {
00223 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class,
00224 as[0].getClass(),
00225 bs[0].getClass());
00226 for (int i = 0; i < as.length; ++i) {
00227 add(ctor.newInstance(home, as[i], bs[i]));
00228 }
00229 } catch(NoSuchMethodException ex) {
00230 throw new FatalException(ex);
00231 } catch(InstantiationException ex) {
00232 throw new FatalException(ex);
00233 } catch(IllegalAccessException ex) {
00234 throw new FatalException(ex);
00235 } catch(InvocationTargetException ex) {
00236 throw new FatalException(ex);
00237 }
00238 }
00239
00242 public boolean assigned() {
00243 for (Var v : this)
00244 if (!v.assigned())
00245 return false;
00246 return true;
00247 }
00248
00251 public void swap(int a, int b) {
00252 Var tmp = get(a);
00253 set(a, get(b));
00254 set(b, tmp);
00255 }
00256
00257 public String toString() {
00258 String ret = "[";
00259 int n = size();
00260 for (int i=0; i < n; i++) {
00261 GecodeVar v = get(i);
00262 String name = v.getName();
00263 if (!name.equals(""))
00264 ret += name+"=";
00265 ret += v.toString();
00266 if (i<n-1)
00267 ret += ",";
00268 }
00269 return ret+"]";
00270 }
00271 }