Skip to content

Commit 43c6c7a

Browse files
authored
Merge pull request #112 from cicirello/permutation-mechanic-updates
Permutation.Mechanic updates
2 parents 865f10b + 7b65ac3 commit 43c6c7a

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased] - 2021-03-29
7+
## [Unreleased] - 2021-03-31
88

99
### Added
10+
* Added the `Permutation.Mechanic.set(Permutation, int[], int, int, int)` method.
1011

1112
### Changed
1213

src/org/cicirello/permutations/Permutation.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* manipulate permutations in a variety of ways.
3939
*
4040
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
41-
* @version 3.29.2021
41+
* @version 3.31.2021
4242
*/
4343
public final class Permutation implements Serializable, Iterable<Permutation>, Copyable<Permutation> {
4444

@@ -934,7 +934,7 @@ public int hashCode() {
934934
* that public method is expected to ensure that the Permutation is fully valid before returning.</p>
935935
*
936936
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>, <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
937-
* @version 3.29.2021
937+
* @version 3.31.2021
938938
*/
939939
public static class Mechanic {
940940

@@ -990,5 +990,24 @@ protected final void set(Permutation p, int index, int value) {
990990
protected final void set(Permutation p, int index, int[] subpermutation) {
991991
System.arraycopy(subpermutation, 0, p.permutation, index, subpermutation.length);
992992
}
993+
994+
/**
995+
* Changes a range of permutation elements. This method does not verify that the
996+
* change produces a valid permutation. The caller is responsible for making a combination
997+
* of calls to this method and/or the other methods of the Permutation.Mechanic class that
998+
* together produce a valid Permutation.
999+
* The behavior of the Permutation object may become unstable otherwise.
1000+
* The caller should not return until the state of the Permutation object is again valid.
1001+
*
1002+
* @param p Permutation object to change.
1003+
* @param source An array to copy elements from.
1004+
* @param fromIndex An index into source where copying begins, e.g., the first element
1005+
* copied is source[fromIndex].
1006+
* @param toIndex An index into the Permutation p, where the elements will be copied to.
1007+
* @param numElementsToSet The number of elements to copy from source to p.
1008+
*/
1009+
protected final void set(Permutation p, int[] source, int fromIndex, int toIndex, int numElementsToSet) {
1010+
System.arraycopy(source, fromIndex, p.permutation, toIndex, numElementsToSet);
1011+
}
9931012
}
9941013
}

tests/org/cicirello/permutations/PermutationTestCases.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ public void testSet(Permutation p, int i, int v) {
237237
public void testSet(Permutation p, int i, int[] a) {
238238
set(p, i, a);
239239
}
240+
public void testSet(Permutation p, int[] s, int from, int to, int numElements) {
241+
set(p, s, from, to, numElements);
242+
}
240243
}
241244
MyMech test = new MyMech();
242245

@@ -276,6 +279,42 @@ public void testSet(Permutation p, int i, int[] a) {
276279
assertEquals("elements after end should not change", a[i], p.get(i));
277280
}
278281
}
282+
for (int[] a : arrays) {
283+
Permutation p = new Permutation(a.length, 0);
284+
test.testSet(p, a.clone(), 0, 0, a.length);
285+
validatePermutation(p, a.length);
286+
for (int i = 0; i < a.length; i++) assertEquals("elements should be in same order", a[i], p.get(i));
287+
}
288+
for (int[] a : arrays) {
289+
Permutation p = new Permutation(a);
290+
int[] change = a.length > 2 ? new int[] {7, 5} : new int[] {7};
291+
int start = a.length > 1 ? 1 : 0;
292+
test.testSet(p, change, 0, start, change.length);
293+
for (int i = 0; i < start; i++) {
294+
assertEquals("elements before start should not change", a[i], p.get(i));
295+
}
296+
for (int i = 0; i < change.length; i++) {
297+
assertEquals("checking changed elements", change[i], p.get(start+i));
298+
}
299+
for (int i = start + change.length; i < a.length; i++) {
300+
assertEquals("elements after end should not change", a[i], p.get(i));
301+
}
302+
}
303+
for (int[] a : arrays) {
304+
Permutation p = new Permutation(a);
305+
int[] change = a.length > 2 ? new int[] {9, 9, 7, 5, 9, 9} : new int[] {9, 9, 7, 9, 9};
306+
int start = a.length > 1 ? 1 : 0;
307+
test.testSet(p, change, 2, start, change.length - 4);
308+
for (int i = 0; i < start; i++) {
309+
assertEquals("elements before start should not change", a[i], p.get(i));
310+
}
311+
for (int i = 2; i < change.length-2; i++) {
312+
assertEquals("checking changed elements", change[i], p.get(start+i-2));
313+
}
314+
for (int i = start + change.length - 4; i < a.length; i++) {
315+
assertEquals("elements after end should not change", a[i], p.get(i));
316+
}
317+
}
279318
}
280319

281320
@Test

0 commit comments

Comments
 (0)