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

RangeIterator.java

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Guido Tack <tack@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Guido Tack, 2006
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-09-11 17:06:50 +0200 (Mon, 11 Sep 2006) $ by $Author: zayenz $
00010  *     $Revision: 3650 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 package org.gecode;
00023 
00024 import java.util.ArrayList;
00025 import java.util.Iterator;
00026 import java.util.NoSuchElementException;
00027 
00032 public abstract class RangeIterator implements Iterable<Range> {
00033 
00036     public abstract boolean notDone();
00039     public abstract void next();
00040     
00043     public abstract int min();
00046     public abstract int max();
00049     public abstract long width();
00050 
00053     public Range[] toRanges() {
00054         ArrayList<Range> rs = new ArrayList<Range>();
00055         while (notDone()) {
00056             rs.add(new Range(min(), max()));
00057             next();
00058         }
00059         return rs.toArray(new Range[0]);
00060     }
00061 
00066   public Iterator<Range> iterator() {
00067     return new JavaRangeIterator(this);
00068   }
00069   
00073   class JavaRangeIterator implements Iterator<Range> {
00074     RangeIterator ri;
00075     Range cur;
00076     public JavaRangeIterator(RangeIterator ri0) {
00077       ri = ri0;
00078       advance();
00079     }
00080     public boolean hasNext() {
00081       return cur != null;
00082     }
00083     public Range next() {
00084       if (cur == null)
00085         throw new NoSuchElementException();
00086       Range res = cur;
00087       advance();
00088       return res;
00089     }
00090     private void advance() {
00091       if (ri.notDone()) {
00092         cur = new Range(ri.min(), ri.max());
00093         ri.next();
00094       } else {
00095         cur = null;
00096       }
00097     }
00098     public void remove() {
00099       throw new UnsupportedOperationException();
00100     }
00101   }
00102 }