Skip to content

Commit 41f2ea2

Browse files
committed
Added toArray, invert, getInverseP methods to Permutation class
1 parent 0b2f8c6 commit 41f2ea2

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

lib/jpt1.jar

98 Bytes
Binary file not shown.

src/org/cicirello/permutations/Permutation.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public BigInteger toBigInteger() {
251251
return result;
252252
}
253253

254-
/**
254+
/**
255255
* Computes the inverse of the permutation.
256256
*
257257
* @return The inverse of the permutation, such that for all i, if
@@ -265,6 +265,31 @@ public int[] getInverse() {
265265
return inverse;
266266
}
267267

268+
/**
269+
* Computes a Permutation that is the inverse of this Permutation.
270+
* Specifically, this.get(i) == j iff inverse.get(j) == i.
271+
*
272+
* @return The inverse of the permutation, such that for all i,
273+
* this.get(i) == j iff inverse.get(j) == i.
274+
* @since 1.3
275+
*/
276+
public Permutation getInversePermutation() {
277+
return new Permutation(getInverse());
278+
}
279+
280+
/**
281+
* Inverts the Permutation, such that if p1 is the Permutation immediately
282+
* prior to the call to invert, and if p2 is the Permutation immediately after
283+
* the call to invert, then p1.get(i) == j iff p2.get(j) == i, for all i, j.
284+
* @since 1.3
285+
*/
286+
public void invert() {
287+
int[] inverse = getInverse();
288+
for (int i = 0; i < inverse.length; i++) {
289+
permutation[i] = inverse[i];
290+
}
291+
}
292+
268293
/**
269294
* Randomly shuffles the permutation. Uses
270295
* java.util.concurrent.ThreadLocalRandom as
@@ -387,6 +412,20 @@ public int get(int i) {
387412
return permutation[i];
388413
}
389414

415+
/**
416+
* Generates an array of int values from the interval [0, n) in the same order
417+
* that they occur in this Permutation. The array that is returned is independent of
418+
* the object state (i.e., changes to its contents will not affect the Permutation).
419+
*
420+
* @return an int array containing the Permutation elements in the same order that they appear in
421+
* the Permutation.
422+
*
423+
* @since 1.3
424+
*/
425+
public int[] toArray() {
426+
return permutation.clone();
427+
}
428+
390429
/**
391430
* Retrieves the length of the permutation.
392431
* @return length of the permutation

tests/org/cicirello/permutations/PermutationTestCases.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,61 @@ public void testPermutationEquals() {
210210

211211
@Test
212212
public void testPermutationInverse() {
213-
Permutation p = new Permutation(new int[] {4, 2, 5, 0, 3, 1});
213+
int[] before = {4, 2, 5, 0, 3, 1};
214+
int[] beforeCopy = before.clone();
215+
Permutation p = new Permutation(before);
214216
int[] expected = {3, 5, 1, 4, 0, 2};
215217
int[] inv = p.getInverse();
216218
assertArrayEquals("inverse", expected, inv);
219+
assertArrayEquals("confirm inverse didn't change original", beforeCopy, before);
217220
int[] array = {0, 1, 2, 3, 4, 5};
218221
p = new Permutation(array);
219222
assertArrayEquals("inverse", array, p.getInverse());
220223
}
221224

225+
@Test
226+
public void testPermutationInverseP() {
227+
int[] before = {4, 2, 5, 0, 3, 1};
228+
Permutation p = new Permutation(before);
229+
Permutation p2 = new Permutation(before);
230+
int[] expected = {3, 5, 1, 4, 0, 2};
231+
Permutation pExpected = new Permutation(expected);
232+
Permutation inv = p.getInversePermutation();
233+
assertEquals("inverse", pExpected, inv);
234+
assertEquals("confirm inverse didn't change original", p2, p);
235+
int[] array = {0, 1, 2, 3, 4, 5};
236+
p = new Permutation(array);
237+
assertEquals("inverse", p, p.getInversePermutation());
238+
}
239+
240+
@Test
241+
public void testPermutationInvert() {
242+
int[] before = {4, 2, 5, 0, 3, 1};
243+
Permutation p = new Permutation(before);
244+
int[] expected = {3, 5, 1, 4, 0, 2};
245+
Permutation pExpected = new Permutation(expected);
246+
p.invert();
247+
assertEquals("inverse", pExpected, p);
248+
int[] array = {0, 1, 2, 3, 4, 5};
249+
p = new Permutation(array);
250+
pExpected = new Permutation(array);
251+
p.invert();
252+
assertEquals("inverse", pExpected, p);
253+
}
254+
255+
@Test
256+
public void testToArray() {
257+
int[] init = {4, 2, 5, 0, 3, 1};
258+
Permutation p = new Permutation(init);
259+
int[] array = p.toArray();
260+
assertArrayEquals("should be equal to current state", init, array);
261+
for (int i = 0; i < array.length; i++) {
262+
// change the array to confirm did not affect Permutation
263+
array[i] = i;
264+
}
265+
assertArrayEquals("should still be equal to current state", init, p.toArray());
266+
}
267+
222268
@Test
223269
public void testPermutationRemoveInsert() {
224270
Permutation p = new Permutation(5);

0 commit comments

Comments
 (0)