public
Description: JCombinatorics provides useful combinatorics algorithms (permutations & combinations) and functions for Java.
Homepage: http://github.com/AlistairIsrael/jcombinatorics
Clone URL: git://github.com/AlistairIsrael/jcombinatorics.git
jcombinatorics / src / main / java / jcombinatorics / permutations / SepaPnkIterator.java
100644 176 lines (157 sloc) 3.915 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* jcombinatorics:
* Java Combinatorics Library
*
* Copyright (c) 2009 by Alistair A. Israel.
*
* This software is made available under the terms of the MIT License.
* See LICENSE.txt.
*
* Created Sep 2, 2009
*/
package jcombinatorics.permutations;
 
import jcombinatorics.util.ArrayUtils;
import jcombinatorics.util.ReadOnlyIterator;
 
/**
* <p>
* An iterator that enumerates <code>P(n,k)</code>, or all permutations of
* <code>n</code> taken <code>k</code> at a time in lexicographic order. Derived
* from the SEPA P(n) iterator.
* </p>
*
* @author Alistair A. Israel
* @see SepaPnIterator
*/
public class SepaPnkIterator extends ReadOnlyIterator<int[]> {
 
    private boolean hasNext = true;
 
    private final int n;
 
    private final int k;
 
    private final int[] a;
 
    private final int[] result;
 
    /**
* @param n
* the number of elements
* @param k
* taken k at a time
*/
    public SepaPnkIterator(final int n, final int k) {
        if (n < 1) {
            throw new IllegalArgumentException("Need at least 1 element!");
        }
        if (n < k || k < 0) {
            throw new IllegalArgumentException("0 < k <= n!");
        }
        this.n = n;
        this.k = k;
        a = ArrayUtils.identityPermutation(n);
        result = new int[k];
    }
 
    /**
* {@inheritDoc}
*
* @see java.util.Iterator#hasNext()
*/
    public final boolean hasNext() {
        return hasNext;
    }
 
    /**
* {@inheritDoc}
*
* @see java.util.Iterator#next()
*/
    public final int[] next() {
        System.arraycopy(a, 0, result, 0, k);
        computeNext();
        return result;
    }
 
    /**
*
*/
    private void computeNext() {
        int i = k - 1;
        int j = k;
        // find smallest j > k - 1 where a[j] > a[k - 1]
        while (j < n && a[j] < a[i]) {
            ++j;
        }
        if (j < n) {
            swap(i, j);
        } else {
            reverseRightOf(i);
            // i = (k - 1) - 1
            --i;
            while (i >= 0 && a[i] >= a[i + 1]) {
                --i;
            }
            if (i < 0) {
                hasNext = false;
                return;
            }
            // j = n - 1
            --j;
            while (j > i && a[j] <= a[i]) {
                --j;
            }
            swap(i, j);
            reverseRightOf(i);
        }
    }
 
    /**
* Reverse the order of elements from <code>a[start + 1]..a[n-1]</code>.
*
* @param start
* the starting element
*/
    private void reverseRightOf(final int start) {
        int i = start + 1;
        int j = n - 1;
        while (i < j) {
            swap(i, j);
            ++i;
            --j;
        }
    }
 
    /**
* @param x
* first position
* @param y
* second position
*/
    private void swap(final int x, final int y) {
        final int t = a[x];
        a[x] = a[y];
        a[y] = t;
    }
 
    /**
*
* @author Alistair A. Israel
*/
    public static class Factory implements Iterable<int[]> {
 
        private final int n;
 
        private final int k;
 
        /**
* @param n
* the number of elements
* @param k
* taken k at a time
*/
        public Factory(final int n, final int k) {
            if (n < 1) {
                throw new IllegalArgumentException("Need at least 1 element!");
            }
            if (n < k || k < 0) {
                throw new IllegalArgumentException("0 < k <= n!");
            }
            this.n = n;
            this.k = k;
        }
 
        /**
* {@inheritDoc}
*
* @see java.lang.Iterable#iterator()
*/
        public final java.util.Iterator<int[]> iterator() {
            return new SepaPnkIterator(n, k);
        }
 
    }
}