Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / peersim / util / LinearIterator.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18                 
19 package peersim.util;
20
21 import java.util.NoSuchElementException;
22
23 /**
24 * This class gives the linear order 0,1,etc or alternatively k-1, k-2, etc.,
25 * depending on the constructor.
26 */
27 public class LinearIterator implements IndexIterator {
28
29
30 // ======================= private fields ============================
31 // ===================================================================
32
33 private final boolean reverse;
34
35 private int len = 0;
36
37 private int pointer = 0;
38
39
40 // ======================= initialization ============================
41 // ===================================================================
42
43
44 /**
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,...
48 */
49 public LinearIterator() { reverse=false; }
50
51 // -------------------------------------------------------------------
52
53 /**
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,...
58 */
59 public LinearIterator( boolean rev ) { reverse=rev; }
60
61
62 // ======================= public methods ============================
63 // ===================================================================
64
65
66 public void reset(int k) {
67         
68         len = k;
69         pointer = (reverse ? len-1 : 0);
70 }
71
72 // -------------------------------------------------------------------
73
74 /**
75 * Returns next index. The indices are returned in increasing or decreasing
76 * order depending on the parameter given at construction time.
77 */
78 public int next() {
79         
80         if( !hasNext() ) throw new NoSuchElementException();
81         
82         return (reverse ? pointer-- : pointer++);
83 }
84
85 // -------------------------------------------------------------------
86
87 public boolean hasNext() { return (reverse ? pointer >= 0 : pointer < len); }
88
89 // -------------------------------------------------------------------
90
91 /*
92 public static void main( String pars[] ) throws Exception {
93         
94         LinearIterator rp = new LinearIterator(pars[0].equals("rev"));
95         
96         int k = Integer.parseInt(pars[1]);
97         rp.reset(k);
98         while(rp.hasNext()) System.out.println(rp.next());
99         
100         System.out.println();
101
102         k = Integer.parseInt(pars[2]);
103         rp.reset(k);
104         while(rp.hasNext()) System.out.println(rp.next());
105         System.out.println(rp.next());
106 }
107 */
108 }