Generated on Fri Oct 6 16:26:44 2006 for Gecode/J by doxygen 1.4.7

VarMatrix.java

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2006
00008  *     Guido Tack, 2006 
00009  *  
00010  *
00011  *  Last modified:
00012  *     $Date: 2006-04-26 11:04:05 +0200 (Wed, 26 Apr 2006) $ by $Author: zayenz $
00013  *     $Revision: 3217 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  See the file "LICENSE" for information on usage and
00020  *  redistribution of this file, and for a
00021  *     DISCLAIMER OF ALL WARRANTIES.
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     /* * Construct and array with as.length elements, the elements having
00202      * class type. The element at position i is initialized as Var(home, as[i]).
00203      * /
00204     public <A> VarMatrix(JavaSpace home, Class<? extends Var> type, A[][] ass) {
00205         super(ass.length > 0 ? ass.length * ass[0].length : 0);
00206         if (ass.length == 0 || ass[0].length == 0) return;
00207         try {
00208             Constructor<? extends Var> ctor = type.getConstructor(JavaSpace.class, ass[0][0].getClass());
00209             for (A[] as: ass)
00210                 for (A a: as) {
00211                     add(ctor.newInstance(home, a));
00212                 }
00213         } catch(NoSuchMethodException ex) {
00214             throw new FatalException(ex);
00215         } catch(InstantiationException ex) {
00216             throw new FatalException(ex);
00217         } catch(IllegalAccessException ex) {
00218             throw new FatalException(ex);
00219         } catch(InvocationTargetException ex) {
00220             throw new FatalException(ex);
00221         }
00222     }
00223 
00254     public void swap(int a, int b) {
00255         Var tmp = get(a);
00256         set(a, get(b));
00257         set(b, tmp);
00258     }
00259 
00262     public void swap(int r1, int c1, int r2, int c2) {
00263         swap(getPos(r1, c1), getPos(r2, c2));
00264     }
00265  
00268     public Var get(int r, int c) {
00269         return get(getPos(r,c));
00270     }
00271 
00274     public void set(int r, int c, Var v) {
00275         set(getPos(r,c), v);
00276     }
00277 
00280     public VarArray<Var> row(int r) {
00281         VarArray<Var> res = new VarArray<Var>(width);
00282         for (int i = 0; i < width; ++i)
00283             res.add(get(r, i));
00284         return res;
00285     }
00286 
00289     public VarArray<Var> col(int c) {
00290         VarArray<Var> res = new VarArray<Var>(height);
00291         for (int i = 0; i < height; ++i)
00292             res.add(get(i, c));
00293         return res;
00294     }
00295 
00299     public VarMatrix<Var> slice(int r1, int c1, int r2, int c2) {
00300         int h = r2-r1, w = c2-c1;
00301         VarMatrix<Var> res = new VarMatrix<Var>(h, w);
00302         for (int i=0; i<h; i++) {
00303             for (int j=0; j<w; j++) {
00304                 res.add(get(r1+i, c1+j));
00305             }
00306         }
00307         return res;
00308     }
00309 
00312     public String toString() {
00313         String ret = "[[";
00314         int n = size();
00315         for (int i=0; i < n; i++) {
00316             GecodeVar v = get(i);
00317             String name = v.getName();
00318             if (!name.equals(""))
00319                 ret += name+"=";
00320             ret += v.toString();
00321             if ((i+1)%height == 0) {
00322                 if (i+height<size()) {
00323                     ret += "], [";
00324                 } else {
00325                     ret += "]";
00326                 }
00327             } else {
00328                 ret += ",";
00329             }
00330         }
00331         return ret+"]";
00332     }
00333 }