59 lines
1.2 KiB
Java
59 lines
1.2 KiB
Java
/**
|
|
* Patchboard for a Enigma.
|
|
*/
|
|
public class PatchBoard implements EnigmaComponent {
|
|
/**
|
|
* Permutation for the patchboard.
|
|
*/
|
|
private int[] permu;
|
|
|
|
/**
|
|
* Inverse permutation for the patchboard.
|
|
*/
|
|
private int[] invPermu;
|
|
|
|
/**
|
|
* Constructor for the patchboard.
|
|
* @param permu permutation
|
|
*/
|
|
PatchBoard(final int[] permu) {
|
|
this.permu = permu;
|
|
this.invPermu = calcInvPermu(permu);
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public final int encode(final int i) {
|
|
return permu[i];
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public final int decode(final int i) {
|
|
return invPermu[i];
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public final void tick() {
|
|
}
|
|
/**
|
|
* Calculates the inverese permutation.
|
|
* @param permu permutation
|
|
* @return <code>inverse permutation</code>
|
|
*/
|
|
private int[] calcInvPermu(final int[] permu) {
|
|
int[] array = new int[permu.length];
|
|
for (int i = 0; i < permu.length; i++) {
|
|
for (int k = 0; k < permu.length; k++) {
|
|
if (i == permu[k]) {
|
|
array[i] = k;
|
|
}
|
|
}
|
|
}
|
|
return array;
|
|
}
|
|
} |