Debian Codegraph @

src/dr/util/Permutator.java

raw symbols SCIP
1 /*
2 * Permutator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.util;
27
28 import java.lang.reflect.Array;
29 import java.util.Iterator;
30 import java.util.NoSuchElementException;
31
32 public class Permutator implements Iterator {
33
34 private final int size;
35 private final Object [] elements; // copy of original 0 .. size-1
36 private final Object ar; // array for output, 0 .. size-1
37 private final int [] permutation; // perm of nums 1..size, perm[0]=0
38
39 private boolean next = true;
40
41 // int[], double[] array won't work :-(
42 public Permutator (Object [] e) {
43 size = e.length;
44 elements = new Object [size]; // not suitable for primitives
45 System.arraycopy (e, 0, elements, 0, size);
46 ar = Array.newInstance (e.getClass().getComponentType(), size);
47 System.arraycopy (e, 0, ar, 0, size);
48 permutation = new int [size+1];
49 for (int i=0; i<size+1; i++) {
50 permutation [i]=i;
51 }
52 }
53
54 private void formNextPermutation () {
55 for (int i=0; i<size; i++) {
56 // i+1 because perm[0] always = 0
57 // perm[]-1 because the numbers 1..size are being permuted
58 Array.set (ar, i, elements[permutation[i+1]-1]);
59 }
60 }
61
62 public boolean hasNext() {
63 return next;
64 }
65
66 public void remove() throws UnsupportedOperationException {
67 throw new UnsupportedOperationException();
68 }
69
70 private void swap (final int i, final int j) {
71 final int x = permutation[i];
72 permutation[i] = permutation [j];
73 permutation[j] = x;
74 }
75
76 // does not throw NoSuchElement; it wraps around!
77 public Object next() throws NoSuchElementException {
78
79 formNextPermutation (); // copy original elements
80
81 int i = size-1;
82 while (permutation[i]>permutation[i+1]) i--;
83
84 if (i==0) {
85 next = false;
86 for (int j=0; j<size+1; j++) {
87 permutation [j]=j;
88 }
89 return ar;
90 }
91
92 int j = size;
93
94 while (permutation[i]>permutation[j]) j--;
95 swap (i,j);
96 int r = size;
97 int s = i+1;
98 while (r>s) { swap(r,s); r--; s++; }
99
100 return ar;
101 }
102
103 public String toString () {
104 final int n = Array.getLength(ar);
105 final StringBuffer sb = new StringBuffer ("[");
106 for (int j=0; j<n; j++) {
107 sb.append (Array.get(ar,j).toString());
108 if (j<n-1) sb.append (",");
109 }
110 sb.append("]");
111 return new String (sb);
112 }
113
114 public static void main (String [] args) {
115 for (Iterator i = new Permutator(args); i.hasNext(); ) {
116 i.next();
117 System.out.println (i);
118 }
119 }
120 }