Skip to content

Commit d36c91c

Browse files
committed
added PermutationUnaryOperator
1 parent 4a0d9f9 commit d36c91c

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public final class Permutation implements Serializable, Iterable<Permutation>, C
4444

4545
private static final long serialVersionUID = 1L;
4646

47+
private final int[] permutation;
48+
4749
/**
4850
* Initializes a random permutation of n integers. Uses
4951
* java.util.concurrent.ThreadLocalRandom as the source of efficient random number generation.
@@ -205,6 +207,15 @@ public Permutation(Permutation p, int length) {
205207
}
206208
}
207209

210+
/**
211+
* Applies a custom unary operator on a Permutation object.
212+
*
213+
* @param operator A unary Permutation operator
214+
*/
215+
public void apply(PermutationUnaryOperator operator) {
216+
operator.apply(permutation);
217+
}
218+
208219
/**
209220
* Creates an identical copy of this object.
210221
* @return an identical copy of this object
@@ -904,8 +915,6 @@ public int hashCode() {
904915
return Arrays.hashCode(permutation);
905916
}
906917

907-
private final int[] permutation;
908-
909918
/**
910919
* <p>The Permutation.Mechanic class provides a means of adding application-specific
911920
* operations on Permutations without the need to directly alter the Permutation
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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(PermutationUnaryOperator)} method.
27+
*
28+
* @author <a href=https://www.cicirello.org/ target=_top>Vincent A. Cicirello</a>,
29+
* <a href=https://www.cicirello.org/ target=_top>https://www.cicirello.org/</a>
30+
*/
31+
@FunctionalInterface
32+
public interface PermutationUnaryOperator {
33+
34+
/**
35+
* Applies an operator on the raw representation of
36+
* a Permutation. Implementers of this interface are responsible
37+
* for ensuring that the apply method maintains a valid
38+
* permutation of the integers in {0, 1, ..., rawPermutation.length - 1 }.
39+
*
40+
* @param rawPermutation A reference to the raw array of ints underlying a
41+
* Permutation object. Changes to this array will directly change the Permutation
42+
* object that encapsulates it.
43+
*/
44+
void apply(int[] rawPermutation);
45+
}

0 commit comments

Comments
 (0)