Skip to content

Commit fd5a9e6

Browse files
committed
Initial release.
0 parents  commit fd5a9e6

26 files changed

Lines changed: 2015 additions & 0 deletions

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# IDE files
2+
/.idea
3+
4+
# Maven target
5+
/_target
6+
7+
# Compiled class file
8+
*.class
9+
10+
# Log file
11+
*.log
12+
13+
# BlueJ files
14+
*.ctxt
15+
16+
# Mobile Tools for Java (J2ME)
17+
.mtj.tmp/
18+
19+
# Package Files #
20+
*.jar
21+
*.war
22+
*.nar
23+
*.ear
24+
*.zip
25+
*.tar.gz
26+
*.rar
27+
28+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
29+
hs_err_pid*

.idea/codeStyles/Project.xml

Lines changed: 717 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/combinatoric.iml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

license.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 ZoftWhere
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package app.zoftwhere.combinatoric;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public abstract class AbstractPermutation<T> {
7+
8+
private final int[] index;
9+
10+
private final int size;
11+
12+
/**
13+
* User PermutationBuilder to initialize a new permutation.
14+
*
15+
* @param index the index array for permutations.
16+
*/
17+
AbstractPermutation(int[] index) {
18+
this.index = index;
19+
this.size = index.length;
20+
}
21+
22+
public int getSize() {
23+
return size;
24+
}
25+
26+
public boolean isEmpty() {
27+
return size == 0;
28+
}
29+
30+
public boolean isPresent() {
31+
return size != 0;
32+
}
33+
34+
public int[] getIndex() {
35+
int[] push = new int[size];
36+
System.arraycopy(index, 0, push, 0, size);
37+
return push;
38+
}
39+
40+
public int getIndex(int position) {
41+
return index[position];
42+
}
43+
44+
public abstract List<T> getValue();
45+
46+
public abstract T getValue(int position);
47+
48+
public abstract AbstractPermutation<T> next(int position);
49+
50+
/**
51+
* Helper method for advancing at a give index, and resetting (sorting) trailing positions.
52+
*
53+
* @param index the index array to advance
54+
* @param left the index position to advance
55+
* @param right the replacement position index array index
56+
* @return the index array after advancement
57+
*/
58+
int[] next(int[] index, int left, int right) {
59+
int[] push = new int[size];
60+
System.arraycopy(index, 0, push, 0, size);
61+
push[left] = index[right];
62+
push[right] = index[left];
63+
Arrays.sort(push, left + 1, size);
64+
return push;
65+
}
66+
67+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package app.zoftwhere.combinatoric;
2+
3+
public abstract class AbstractSeries<T, L> implements Series<T, L> {
4+
5+
private final T base;
6+
7+
private final int exponent;
8+
9+
private final T increment;
10+
11+
private final L length;
12+
13+
AbstractSeries(T base, T increment, int exponent, L length) {
14+
this.base = base;
15+
this.increment = increment;
16+
this.exponent = exponent;
17+
this.length = length;
18+
}
19+
20+
@Override
21+
public T base() {
22+
return base;
23+
}
24+
25+
@Override
26+
public T increment() {
27+
return increment;
28+
}
29+
30+
@Override
31+
public int exponent() {
32+
return exponent;
33+
}
34+
35+
@Override
36+
public L length() {
37+
return length;
38+
}
39+
40+
protected abstract AbstractSeries<T, L> clone(T base, T increment, int exponent, L length);
41+
42+
protected abstract T convertValue(int value);
43+
44+
protected abstract T convertValue(long value);
45+
46+
protected abstract L convertLength(int length);
47+
48+
protected abstract L convertLength(long length);
49+
50+
public AbstractSeries<T, L> base(int base) {
51+
return clone(convertValue(base), increment(), exponent(), length());
52+
}
53+
54+
public AbstractSeries<T, L> base(long base) {
55+
return clone(convertValue(base), increment(), exponent(), length());
56+
}
57+
58+
public AbstractSeries<T, L> base(T base) {
59+
return clone(base, increment(), exponent(), length());
60+
}
61+
62+
public AbstractSeries<T, L> increment(int increment) {
63+
return clone(base(), convertValue(increment), exponent(), length());
64+
}
65+
66+
public AbstractSeries<T, L> increment(long increment) {
67+
return clone(base(), convertValue(increment), exponent(), length());
68+
}
69+
70+
public AbstractSeries<T, L> increment(T increment) {
71+
return clone(base(), increment, exponent(), length());
72+
}
73+
74+
public AbstractSeries<T, L> exponent(int exponent) {
75+
return clone(base(), increment(), exponent, length());
76+
}
77+
78+
public AbstractSeries<T, L> length(int length) {
79+
return clone(base(), increment(), exponent(), convertLength(length));
80+
}
81+
82+
public AbstractSeries<T, L> length(long length) {
83+
return clone(base(), increment(), exponent(), convertLength(length));
84+
}
85+
86+
public AbstractSeries<T, L> length(L length) {
87+
return clone(base(), increment(), exponent(), length);
88+
}
89+
90+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package app.zoftwhere.combinatoric;
2+
3+
import java.util.List;
4+
import java.util.NoSuchElementException;
5+
6+
class PermutationBasic<T> extends AbstractPermutation<T> {
7+
8+
private final int size;
9+
10+
private final int[] index;
11+
12+
PermutationBasic(int[] index, int size) {
13+
super(index);
14+
this.index = getIndex();
15+
this.size = size;
16+
}
17+
18+
@Override
19+
public List<T> getValue() {
20+
return null;
21+
}
22+
23+
@Override
24+
public T getValue(int position) {
25+
throw new NoSuchElementException();
26+
}
27+
28+
29+
@Override
30+
public PermutationBasic<T> next(int position) {
31+
if (position < 0 || position >= size) {
32+
return new PermutationBasic<>(new int[0], 0);
33+
}
34+
35+
int min = index[position];
36+
int max = Integer.MAX_VALUE;
37+
int swap = position;
38+
39+
for (int i = position + 1; i < size; i++) {
40+
if (index[i] == min + 1) {
41+
int[] push = next(index, position, i);
42+
return new PermutationBasic<>(push, size);
43+
}
44+
if (index[i] > min && index[i] < max) {
45+
swap = i;
46+
max = index[i];
47+
}
48+
}
49+
50+
if (swap == position) {
51+
return new PermutationBasic<>(new int[0], 0);
52+
}
53+
54+
return new PermutationBasic<>(next(index, position, swap), size);
55+
}
56+
57+
@Override
58+
public String toString() {
59+
if (isEmpty()) {
60+
return "[]";
61+
}
62+
63+
StringBuilder builder = new StringBuilder("[");
64+
builder.append(String.format("%d", index[0]));
65+
for (int i = 1; i < size; i++) {
66+
builder.append(String.format(", %d", index[i]));
67+
}
68+
69+
return builder.append("]").toString();
70+
}
71+
72+
}

0 commit comments

Comments
 (0)