Skip to content

Commit c13c120

Browse files
committed
Added Permutation.Mechanic class
1 parent 99d99ee commit c13c120

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

lib/jpt1.jar

578 Bytes
Binary file not shown.

src/org/cicirello/permutations/Permutation.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,80 @@ public int hashCode() {
626626
}
627627

628628
private final int[] permutation;
629+
630+
/**
631+
* <p>The Permutation.Mechanic class provides a means of adding application-specific
632+
* operations on Permutations without the need to directly alter the Permutation
633+
* class. It is similar to a Visitor from the Visitor pattern.</p>
634+
*
635+
* <p>The methods of the Permutation.Mechanic class are unsafe, and if used incorrectly
636+
* can result in invalid Permutation state, and/or runtime exceptions. The methods of
637+
* this nested class do not perform any bounds checks. Additionally, they do not check
638+
* whether the usage produces a valid permutation.</p>
639+
*
640+
* <p>The Permutation.Mechanic class cannot be instantiated directly. To use, you must
641+
* define a subtype by extending the Permutation.Mechanic class. It is the responsibility
642+
* of the subclass to ensure that all of the methods of the subclass are safe. For example,
643+
* one of the set methods of the Permutation.Mechanic class enables setting a specific
644+
* index of a Permutation to any integer value. Individual calls to that method will
645+
* produce an invalid Permutation. It is the responsibility of any public method of the subclass to ensure
646+
* that a combination of calls to the set method is performed that results in a valid Permutation by the
647+
* time that subclass method returns.</p>
648+
*
649+
* <p>Here's the Mechanic analogy: Imagine that you take your car in for service. The mechanic
650+
* opens the hood, and starts disconnecting and removing stuff. During much of that time,
651+
* your car is inoperable. However, by the time you pick up your car, it has been put back
652+
* together properly, and is in functioning order. Perhaps it has new tires, or a new timing belt, etc,
653+
* so its state has changed.</p>
654+
*
655+
* <p>Consider a subclass, Subclass, of the Permutation.Mechanic. An object of Subclass is like a mechanic,
656+
* and the Permutation passed to its methods is the car. Each call to a public method of Subclass is like
657+
* a visit to the service station. Just like a mechanic can take your car apart during that service visit,
658+
* a public method of Subclass can similarly, temporarily, create a non-functioning Permutation. However,
659+
* that public method is expected to ensure that the Permutation is fully valid before returning.</p>
660+
*
661+
* @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>
662+
* @version 1.19.5.13
663+
* @since 1.3
664+
*/
665+
public static class Mechanic {
666+
667+
/**
668+
* The default constructor can only be called by subclasses.
669+
*/
670+
protected Mechanic() {}
671+
672+
/**
673+
* Changes the state of the Permutation according to the contents
674+
* of an array of int values. The caller of this method is responsible
675+
* to ensure that the array is the same length as the Permutation,
676+
* and that the elements are a valid permutation of the integers from the
677+
* set { 0, 1, ..., n-1 } where n is the length of the permutation.
678+
* The behavior of the Permutation object may become unstable otherwise.
679+
*
680+
* @param p Permutation object to change.
681+
* @param permutation An array of int values, assumed the be a valid permutation
682+
* of the integers from 0 to n-1.
683+
*/
684+
protected final void set(Permutation p, int[] permutation) {
685+
for (int i = 0; i < permutation.length; i++) {
686+
p.permutation[i] = permutation[i];
687+
}
688+
}
689+
690+
/**
691+
* Changes the integer in one specific location of a Permutation. Individual calls
692+
* to this method will, in most cases, produce an invalid Permutation. The caller
693+
* is responsible for making a combination of calls to this method that together produce
694+
* a valid Permutation. The behavior of the Permutation object may become unstable otherwise.
695+
* The caller should not return until the state of the Permutation object is again valid.
696+
*
697+
* @param p Permutation object to change.
698+
* @param index The index of the position to change.
699+
* @param value The value to change that position to.
700+
*/
701+
protected final void set(Permutation p, int index, int value) {
702+
p.permutation[index] = value;
703+
}
704+
}
629705
}

0 commit comments

Comments
 (0)