Skip to content

Commit 0ae0f2d

Browse files
authored
Merge pull request #230 from cicirello/feat-ops
Added PermutationFullUnaryOperator and PermutationFullBinaryOperator
2 parents a337581 + 19f080b commit 0ae0f2d

File tree

5 files changed

+169
-1
lines changed

5 files changed

+169
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ 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] - 2022-07-24
7+
## [Unreleased] - 2022-08-03
88

99
### Added
10+
* PermutationFullUnaryOperator and PermutationFullBinaryOperator functional interfaces for the purpose
11+
of specifying custom operations on Permutation objects. These are variations of the existing
12+
PermutationUnaryOperator and PermutationBinaryOperator interfaces that were added in 3.2.0, but with
13+
both the raw int arrays and Permutation objects passed to the operators.
14+
* Permutation.apply methods, one for each of the two new PermutationFullUnaryOperator
15+
and PermutationFullBinaryOperator interfaces, for applying such custom Permutation operators.
1016

1117
### Changed
1218

src/main/java/org/cicirello/permutations/Permutation.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ public void apply(PermutationUnaryOperator operator) {
208208
operator.apply(permutation);
209209
}
210210

211+
/**
212+
* Applies a custom unary operator on a Permutation object.
213+
*
214+
* @param operator A unary Permutation operator
215+
*/
216+
public void apply(PermutationFullUnaryOperator operator) {
217+
operator.apply(permutation, this);
218+
}
219+
211220
/**
212221
* Applies a custom binary operator on a pair of Permutation objects.
213222
* The raw int array belonging to this is passed as the first array to
@@ -221,6 +230,19 @@ public void apply(PermutationBinaryOperator operator, Permutation other) {
221230
operator.apply(permutation, other.permutation);
222231
}
223232

233+
/**
234+
* Applies a custom binary operator on a pair of Permutation objects.
235+
* The raw int array belonging to this is passed as the first array to
236+
* operator.apply() and the raw int array belonging to other is passed
237+
* as the second, and this and other are passed as p1 and p2, respectively.
238+
*
239+
* @param operator A binary Permutation operator
240+
* @param other The other Permutation
241+
*/
242+
public void apply(PermutationFullBinaryOperator operator, Permutation other) {
243+
operator.apply(permutation, other.permutation, this, other);
244+
}
245+
224246
/**
225247
* Creates an identical copy of this object.
226248
* @return an identical copy of this object
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* JavaPermutationTools: A Java library for computation on permutations and sequences
3+
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
4+
*
5+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
6+
*
7+
* JavaPermutationTools is free software: you can
8+
* redistribute it and/or modify it under the terms of the GNU
9+
* General Public License as published by the Free Software
10+
* Foundation, either version 3 of the License, or (at your
11+
* option) any later version.
12+
*
13+
* JavaPermutationTools is distributed in the hope
14+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
15+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
* PARTICULAR PURPOSE. See the GNU General Public License for more
17+
* details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
package org.cicirello.permutations;
23+
24+
/**
25+
* A functional interface for defining custom binary operators on Permutations.
26+
* See the {@link Permutation#apply(PermutationFullBinaryOperator,Permutation)} method.
27+
* In this interface, the {@link #apply} method is passed both references to the raw arrays
28+
* of ints that are encapsulated by the Permutations, as well as references to the original
29+
* Permutation objects to enable utilizing the methods of the {@link Permutation}
30+
* class. If the operator that you are implementing only requires the raw arrays of ints,
31+
* then consider implementing the {@link PermutationBinaryOperator} interface instead.
32+
*
33+
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>,
34+
* <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
35+
*/
36+
@FunctionalInterface
37+
public interface PermutationFullBinaryOperator {
38+
39+
/**
40+
* Applies an operator on the raw representations of
41+
* a pair of Permutations. Implementers of this interface are responsible
42+
* for ensuring that the apply method maintains valid
43+
* permutations of the integers in {0, 1, ..., rawPermutation1.length - 1 },
44+
* and likewise for rawPermutation2.
45+
*
46+
* @param rawPermutation1 A reference to the raw array of ints underlying a
47+
* Permutation object. Changes to this array will directly change the Permutation
48+
* object that encapsulates it.
49+
*
50+
* @param rawPermutation2 A reference to the raw array of ints underlying a
51+
* Permutation object. Changes to this array will directly change the Permutation
52+
* object that encapsulates it.
53+
*
54+
* @param p1 A reference to the Permutation object that encapsulates rawPermutation1.
55+
*
56+
* @param p2 A reference to the Permutation object that encapsulates rawPermutation2.
57+
*/
58+
void apply(int[] rawPermutation1, int[] rawPermutation2, Permutation p1, Permutation p2);
59+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* JavaPermutationTools: A Java library for computation on permutations and sequences
3+
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
4+
*
5+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
6+
*
7+
* JavaPermutationTools is free software: you can
8+
* redistribute it and/or modify it under the terms of the GNU
9+
* General Public License as published by the Free Software
10+
* Foundation, either version 3 of the License, or (at your
11+
* option) any later version.
12+
*
13+
* JavaPermutationTools is distributed in the hope
14+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
15+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
* PARTICULAR PURPOSE. See the GNU General Public License for more
17+
* details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
package org.cicirello.permutations;
23+
24+
/**
25+
* A functional interface for defining custom unary operators on Permutations.
26+
* See the {@link Permutation#apply(PermutationFullUnaryOperator)} method. In this
27+
* interface, the {@link #apply} method is passed both a reference to the raw array
28+
* of ints that is encapsulated by the Permutation, as well as a reference to the
29+
* Permutation object itself to enable utilizing the methods of the {@link Permutation}
30+
* class. If the operator that you are implementing only requires the raw array of ints,
31+
* then consider implementing the {@link PermutationUnaryOperator} interface instead.
32+
*
33+
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>,
34+
* <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
35+
*/
36+
@FunctionalInterface
37+
public interface PermutationFullUnaryOperator {
38+
39+
/**
40+
* Applies an operator on the raw representation of
41+
* a Permutation. Implementers of this interface are responsible
42+
* for ensuring that the apply method maintains a valid
43+
* permutation of the integers in {0, 1, ..., rawPermutation.length - 1 }.
44+
*
45+
* @param rawPermutation A reference to the raw array of ints underlying a
46+
* Permutation object. Changes to this array will directly change the Permutation
47+
* object that encapsulates it.
48+
*
49+
* @param p A reference to the Permutation object that encapsulates it.
50+
*/
51+
void apply(int[] rawPermutation, Permutation p);
52+
}

src/test/java/org/cicirello/permutations/PermutationTestCases.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ public void testBinaryOperator() {
7272
}
7373
}
7474

75+
@Test
76+
public void testFullUnaryOperator() {
77+
Permutation p = new Permutation(10);
78+
p.apply((perm, original) -> { for (int i = 0; i < perm.length; i++) { perm[i] = i; assertEquals(perm[i], original.get(i)); }});
79+
for (int i = 0; i < 10; i++) {
80+
assertEquals(i, p.get(i));
81+
}
82+
p.apply((perm, original) -> { for (int i = 0; i < perm.length; i++) { perm[perm.length-1-i] = i; assertEquals(perm[perm.length-1-i], original.get(perm.length-1-i)); }});
83+
for (int i = 0; i < 10; i++) {
84+
assertEquals(9-i, p.get(i));
85+
}
86+
}
87+
88+
@Test
89+
public void testFullBinaryOperator() {
90+
Permutation p1 = new Permutation(10);
91+
Permutation p2 = new Permutation(10);
92+
p1.apply((perm1, perm2, o1, o2) -> { for (int i = 0; i < perm1.length; i++) { perm1[i] = i; perm2[perm1.length-1-i] = i; assertEquals(i, o1.get(i)); assertEquals(i, o2.get(9-i)); }}, p2);
93+
for (int i = 0; i < 10; i++) {
94+
assertEquals(i, p1.get(i));
95+
assertEquals(9-i, p2.get(i));
96+
}
97+
p1.apply((perm1, perm2, o1, o2) -> { for (int i = 0; i < perm1.length; i++) { perm2[i] = i; perm1[perm1.length-1-i] = i; assertEquals(i, o2.get(i)); assertEquals(i, o1.get(9-i)); }}, p2);
98+
for (int i = 0; i < 10; i++) {
99+
assertEquals(i, p2.get(i));
100+
assertEquals(9-i, p1.get(i));
101+
}
102+
}
103+
75104
@Test
76105
public void testZeroLengthPermutations() {
77106
// different ways of constructing 0 length permutations.

0 commit comments

Comments
 (0)