2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 import java.util.NoSuchElementException;
24 * This class gives the linear order 0,1,etc or alternatively k-1, k-2, etc.,
25 * depending on the constructor.
27 public class LinearIterator implements IndexIterator {
30 // ======================= private fields ============================
31 // ===================================================================
33 private final boolean reverse;
37 private int pointer = 0;
40 // ======================= initialization ============================
41 // ===================================================================
45 * Construct an iterator for an empty set of numbers.
46 * You have to call {@link #reset} to actually fully initialize the object.
47 * The numbers returned by consecutive calls to {@link #next} are 0,1,...
49 public LinearIterator() { reverse=false; }
51 // -------------------------------------------------------------------
54 * Construct an interator for an empty set of numbers.
55 * You have to call {@link #reset} to actually fully initialize the object.
56 * If parameter is true then the numbers returned by consecutive calls to
57 * {@link #next} are k-1,k-2,..., otherwise 0,1,...
59 public LinearIterator( boolean rev ) { reverse=rev; }
62 // ======================= public methods ============================
63 // ===================================================================
66 public void reset(int k) {
69 pointer = (reverse ? len-1 : 0);
72 // -------------------------------------------------------------------
75 * Returns next index. The indices are returned in increasing or decreasing
76 * order depending on the parameter given at construction time.
80 if( !hasNext() ) throw new NoSuchElementException();
82 return (reverse ? pointer-- : pointer++);
85 // -------------------------------------------------------------------
87 public boolean hasNext() { return (reverse ? pointer >= 0 : pointer < len); }
89 // -------------------------------------------------------------------
92 public static void main( String pars[] ) throws Exception {
94 LinearIterator rp = new LinearIterator(pars[0].equals("rev"));
96 int k = Integer.parseInt(pars[1]);
98 while(rp.hasNext()) System.out.println(rp.next());
100 System.out.println();
102 k = Integer.parseInt(pars[2]);
104 while(rp.hasNext()) System.out.println(rp.next());
105 System.out.println(rp.next());