VarMatrix.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.gecode;
00026
00027 import java.util.ArrayList;
00028 import java.lang.reflect.Constructor;
00029 import java.lang.reflect.InvocationTargetException;
00030
00044 public class VarMatrix<Var extends GecodeVar> extends VarArray<Var> {
00047 private int height, width;
00048
00049 private int getPos(int r, int c) {
00050 return r*width + c;
00051 }
00052
00053
00054
00057 public VarMatrix() {
00058 super();
00059
00060 }
00061
00064 public VarMatrix(int h, int w) {
00065 super(h * w);
00066 height = h; width = w;
00067 }
00068
00071 @SuppressWarnings("unchecked")
00072 public VarMatrix(JavaSpace newHome,
00073 boolean share, VarMatrix<Var> va) {
00074 super(va.size());
00075 height = va.height; width = va.width;
00076 for (Var v: va) {
00077 add((Var)v.copy(newHome, share));
00078 }
00079 }
00080
00083 public <C extends Iterable<? extends Var>> VarMatrix(int h, int w, C va) {
00084 super(h*w);
00085 height = h; width = w;
00086 int cnt = 0;
00087 for (Var v: va) {
00088 if (++cnt > h*w) throw new ArgumentSizeMismatchException();
00089 add(v);
00090 }
00091 }
00092
00095 public <V2 extends Var> VarMatrix(int h, int w, V2... args) {
00096 super(args.length);
00097 height = h; width = w;
00098 if (args.length != h*w) throw new ArgumentSizeMismatchException();
00099 for (V2 v: args) add(v);
00100 }
00101
00105 public VarMatrix(JavaSpace home, int h, int w, Class<? extends Var> type) {
00106 super(h*w);
00107 height = h; width = w;
00108 try {
00109 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class);
00110
00111 for (int i = 0; i < h*w; ++i) {
00112 add(ctor.newInstance(home));
00113 }
00114 } catch(NoSuchMethodException ex) {
00115 throw new FatalException(ex);
00116 } catch(InstantiationException ex) {
00117 throw new FatalException(ex);
00118 } catch(IllegalAccessException ex) {
00119 throw new FatalException(ex);
00120 } catch(InvocationTargetException ex) {
00121 throw new FatalException(ex);
00122 }
00123 }
00124
00128 public VarMatrix(JavaSpace home, int h, int w, Class<? extends Var> type, Object arg) {
00129 super(h*w);
00130 height = h; width = w;
00131 try {
00132 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class, arg.getClass());
00133
00134 for (int i = 0; i < h*w; ++i) {
00135 add(ctor.newInstance(home, arg));
00136 }
00137 } catch(NoSuchMethodException ex) {
00138 throw new FatalException(ex);
00139 } catch(InstantiationException ex) {
00140 throw new FatalException(ex);
00141 } catch(IllegalAccessException ex) {
00142 throw new FatalException(ex);
00143 } catch(InvocationTargetException ex) {
00144 throw new FatalException(ex);
00145 }
00146 }
00147
00151 public VarMatrix(JavaSpace home, int h, int w, Class<? extends Var> type,
00152 Object arg1, Object arg2) {
00153 super(h*w);
00154 height = h; width = w;
00155 try {
00156 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class,
00157 arg1.getClass(),
00158 arg2.getClass());
00159
00160 for (int i = 0; i < h*w; ++i) {
00161 add(ctor.newInstance(home, arg1, arg2));
00162 }
00163 } catch(NoSuchMethodException ex) {
00164 throw new FatalException(ex);
00165 } catch(InstantiationException ex) {
00166 throw new FatalException(ex);
00167 } catch(IllegalAccessException ex) {
00168 throw new FatalException(ex);
00169 } catch(InvocationTargetException ex) {
00170 throw new FatalException(ex);
00171 }
00172 }
00173
00177 public VarMatrix(JavaSpace home, int h, int w, Class<? extends Var> type,
00178 Object arg1, Object arg2, Object arg3) {
00179 super(h*w);
00180 height = h; width = w;
00181 try {
00182 Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class,
00183 arg1.getClass(),
00184 arg2.getClass(),
00185 arg3.getClass());
00186
00187 for (int i = 0; i < h*w; ++i) {
00188 add(ctor.newInstance(home, arg1, arg2, arg3));
00189 }
00190 } catch(NoSuchMethodException ex) {
00191 throw new FatalException(ex);
00192 } catch(InstantiationException ex) {
00193 throw new FatalException(ex);
00194 } catch(IllegalAccessException ex) {
00195 throw new FatalException(ex);
00196 } catch(InvocationTargetException ex) {
00197 throw new FatalException(ex);
00198 }
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00254
00255
00256
00257
00258
00259
00262
00263
00264
00265
00268
00269
00270
00271
00274
00275
00276
00277
00280
00281
00282
00283
00284
00285
00286
00289
00290
00291
00292
00293
00294
00295
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333