github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

AlistairIsrael / jcombinatorics

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 1
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

JCombinatorics provides useful combinatorics algorithms (permutations & combinations) and functions for Java. — Read more

  cancel

http://github.com/AlistairIsrael/jcombinatorics

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Added comments 
AlistairIsrael (author)
Tue Sep 22 03:14:16 -0700 2009
commit  99a5facce4da2623585672f817a68f51dc49a4b8
tree    091b5db4ee527190f36e995b1ea6155bde5149d3
parent  c0b8dd84ebcaf9a8692bb4883fca4fed8b2da06e
jcombinatorics / src / main / java / jcombinatorics / permutations / SepaPnkIterator.java src/main/java/jcombinatorics/permutations/SepaPnkIterator.java
100644 176 lines (157 sloc) 3.915 kb
edit raw blame history
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);
        }
 
    }
}
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server