Skip to content

Commit d248b80

Browse files
authored
Merge pull request #1 from ZoftWhere/feature/release-2.0.0
Release v2.0.0
2 parents 13292f6 + ad52708 commit d248b80

34 files changed

Lines changed: 2257 additions & 690 deletions

.idea/codeStyles/Project.xml

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

main-java/app/zoftwhere/combinatoric/AbstractPermutation.java

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,130 @@
33
import java.util.Arrays;
44
import java.util.List;
55

6-
public abstract class AbstractPermutation<T> {
6+
import static app.zoftwhere.combinatoric.Generator.empty;
7+
8+
abstract class AbstractPermutation<T> implements Permutation<T> {
79

810
private final int[] index;
911

1012
private final int size;
1113

14+
private final int kSize;
15+
16+
private final List<T> list;
17+
1218
/**
1319
* User PermutationBuilder to initialize a new permutation.
14-
*
15-
* @param index the index array for permutations.
1620
*/
17-
AbstractPermutation(int[] index) {
21+
AbstractPermutation(int[] index, List<T> list, int kSize) {
1822
this.index = index;
1923
this.size = index.length;
24+
this.list = list;
25+
this.kSize = kSize;
2026
}
2127

22-
public int getSize() {
28+
/**
29+
* Returns an immutable instance with the values in the clone.
30+
*/
31+
protected abstract Permutation<T> newInstance(int[] index, List<T> list, int kSize);
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public int size() {
2337
return size;
2438
}
2539

40+
/**
41+
* {@inheritDoc}
42+
*/
43+
public int kSize() {
44+
return kSize;
45+
}
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
2650
public boolean isEmpty() {
2751
return size == 0;
2852
}
2953

54+
/**
55+
* {@inheritDoc}
56+
*/
3057
public boolean isPresent() {
3158
return size != 0;
3259
}
3360

34-
public int[] getIndex() {
61+
/**
62+
* {@inheritDoc}
63+
*/
64+
public int[] index() {
3565
int[] push = new int[size];
3666
System.arraycopy(index, 0, push, 0, size);
3767
return push;
3868
}
3969

40-
public int getIndex(int position) {
70+
/**
71+
* {@inheritDoc}
72+
*/
73+
public int index(int position) {
4174
return index[position];
4275
}
4376

44-
public abstract List<T> getValue();
77+
/**
78+
* {@inheritDoc}
79+
*/
80+
public abstract List<T> value();
4581

46-
public abstract T getValue(int position);
82+
/**
83+
* {@inheritDoc}
84+
*/
85+
public abstract T value(int position);
86+
87+
/**
88+
* {@inheritDoc}
89+
*/
90+
public Permutation<T> next() {
91+
if (size < 2) {
92+
return empty();
93+
}
94+
return next(Math.min(size - 2, kSize - 1));
95+
}
96+
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public Permutation<T> next(int position) {
101+
checkPosition(position);
102+
103+
int current = Math.min(position, kSize - 1);
104+
Permutation<T> next;
47105

48-
public abstract AbstractPermutation<T> next(int position);
106+
while (current >= 0) {
107+
next = progress(current);
108+
current--;
109+
110+
if (next.isPresent()) {
111+
return next;
112+
}
113+
}
114+
115+
return progress(0);
116+
}
117+
118+
/**
119+
* {@inheritDoc}
120+
*/
121+
public abstract Permutation<T> progress(int position);
122+
123+
boolean checkPosition(int position) {
124+
if (position < 0 || position >= size) {
125+
throw new ArrayIndexOutOfBoundsException(String.format("Index %d out of bounds for length %d", position, size));
126+
}
127+
128+
return (position < size - 1) && (position < kSize);
129+
}
49130

50131
/**
51132
* Helper method for advancing at a give index, and resetting (sorting) trailing positions.
@@ -55,7 +136,7 @@ public int getIndex(int position) {
55136
* @param right the replacement position index array index
56137
* @return the index array after advancement
57138
*/
58-
int[] next(int[] index, int left, int right) {
139+
int[] advance(int[] index, int left, int right) {
59140
int[] push = new int[size];
60141
System.arraycopy(index, 0, push, 0, size);
61142
push[left] = index[right];
@@ -64,4 +145,18 @@ int[] next(int[] index, int left, int right) {
64145
return push;
65146
}
66147

148+
/**
149+
* Creates an ordered index array for the permutation.
150+
*
151+
* @param size the size of the array
152+
* @return an ordered index array
153+
*/
154+
static int[] orderedArray(int size) {
155+
final int[] index = new int[size];
156+
for (int i = 0; i < size; i++) {
157+
index[i] = i;
158+
}
159+
return index;
160+
}
161+
67162
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package app.zoftwhere.combinatoric;
2+
3+
abstract class AbstractSequence<T, L> implements Sequence<T, L>, Cloneable {
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+
AbstractSequence(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+
public T base() {
21+
return base;
22+
}
23+
24+
public T increment() {
25+
return increment;
26+
}
27+
28+
public int exponent() {
29+
return exponent;
30+
}
31+
32+
public L length() {
33+
return length;
34+
}
35+
36+
public abstract T sum();
37+
38+
/**
39+
* Returns an immutable instance with the values in the clone.
40+
*
41+
* @param base base value
42+
* @param increment increment value
43+
* @param exponent exponent value
44+
* @param length sequence length
45+
* @return immutable instance
46+
*/
47+
protected abstract Sequence<T, L> newInstance(T base, T increment, int exponent, L length);
48+
49+
/**
50+
* @param value the value to convert
51+
* @return the converted value of type <code>T</code>
52+
*/
53+
protected abstract T convertValue(int value);
54+
55+
/**
56+
* @param value the value to convert
57+
* @return the converted value of type <code>T</code>
58+
*/
59+
protected abstract T convertValue(long value);
60+
61+
/**
62+
* @param length the value to convert
63+
* @return the converted value of type <code>L</code>
64+
*/
65+
protected abstract L convertLength(int length);
66+
67+
/**
68+
* @param length the value to convert
69+
* @return the converted value of type <code>L</code>
70+
*/
71+
protected abstract L convertLength(long length);
72+
73+
/**
74+
* Create a sequence, based on the current, with a different base value.
75+
*
76+
* @param base the base value
77+
* @return a new immutable instance with the new base
78+
*/
79+
public Sequence<T, L> base(int base) {
80+
return newInstance(convertValue(base), increment(), exponent(), length());
81+
}
82+
83+
/**
84+
* Create a sequence, based on the current, with a different base value.
85+
*
86+
* @param base the base value
87+
* @return a new immutable instance with the new base
88+
*/
89+
public Sequence<T, L> base(long base) {
90+
return newInstance(convertValue(base), increment(), exponent(), length());
91+
}
92+
93+
/**
94+
* Create a sequence, based on the current, with a different base value.
95+
*
96+
* @param base the base value
97+
* @return a new immutable instance with the new base
98+
*/
99+
public Sequence<T, L> base(T base) {
100+
return newInstance(base, increment(), exponent(), length());
101+
}
102+
103+
/**
104+
* Create a sequence, based on the current, with a different increment value.
105+
*
106+
* @param increment the increment value
107+
* @return a new immutable instance with the new increment
108+
*/
109+
public Sequence<T, L> increment(int increment) {
110+
return newInstance(base(), convertValue(increment), exponent(), length());
111+
}
112+
113+
/**
114+
* Create a sequence, based on the current, with a different increment value.
115+
*
116+
* @param increment the increment value
117+
* @return a new immutable instance with the new increment
118+
*/
119+
public Sequence<T, L> increment(long increment) {
120+
return newInstance(base(), convertValue(increment), exponent(), length());
121+
}
122+
123+
/**
124+
* Create a sequence, based on the current, with a different increment value.
125+
*
126+
* @param increment the increment value
127+
* @return a new immutable instance with the new increment
128+
*/
129+
public Sequence<T, L> increment(T increment) {
130+
return newInstance(base(), increment, exponent(), length());
131+
}
132+
133+
/**
134+
* Create a sequence, based on the current, with a different exponent value.
135+
*
136+
* @param exponent the increment value
137+
* @return a new immutable instance with the new exponent
138+
*/
139+
public Sequence<T, L> exponent(int exponent) {
140+
return newInstance(base(), increment(), exponent, length());
141+
}
142+
143+
/**
144+
* Create a sequence, based on the current, with a different length.
145+
*
146+
* @param length the increment value
147+
* @return a new immutable instance with the new length
148+
*/
149+
public Sequence<T, L> length(int length) {
150+
return newInstance(base(), increment(), exponent(), convertLength(length));
151+
}
152+
153+
/**
154+
* Create a sequence, based on the current, with a different length.
155+
*
156+
* @param length the increment value
157+
* @return a new immutable instance with the new length
158+
*/
159+
public Sequence<T, L> length(long length) {
160+
return newInstance(base(), increment(), exponent(), convertLength(length));
161+
}
162+
163+
/**
164+
* Create a sequence, based on the current, with a different length.
165+
*
166+
* @param length the increment value
167+
* @return a new immutable instance with the new length
168+
*/
169+
public Sequence<T, L> length(L length) {
170+
return newInstance(base(), increment(), exponent(), length);
171+
}
172+
173+
}

0 commit comments

Comments
 (0)