The following stage implements the quicksort algorithm and serves as an example for a composite stage. It is included in the TeeTime distribution and can be found here. In order to test a composite stage with our stage testing framework, you need to map the outer port(s) to the inner port(s) as shown below.
package teetime.stage.quicksort;
import teetime.framework.CompositeStage;
import teetime.framework.InputPort;
import teetime.framework.OutputPort;
/**
* A P&F implementation of the iterative Quicksort algorithm.
*
* @author Christian Wulf
*
* @see https://www.geeksforgeeks.org/iterative-quick-sort
*/
public class QuicksortStage extends CompositeStage {
private final InputPort<int[]> inputPort;
private final OutputPort<int[]> outputPort;
public QuicksortStage() {
InitStage initStage = new InitStage();
IfStage ifStage = new IfStage();
SetupRangeStage setupRangeStage = new SetupRangeStage();
PartitionStage partitionStage = new PartitionStage();
PushLeftSideStage pushLeftSideStage = new PushLeftSideStage();
PushRightSideStage pushRightSideStage = new PushRightSideStage();
connectPorts(initStage.getOutputPort(), ifStage.getNewTaskInputPort());
connectPorts(ifStage.getTrueOutputPort(), setupRangeStage.getInputPort());
connectPorts(setupRangeStage.getOutputPort(), partitionStage.getInputPort());
connectPorts(partitionStage.getOutputPort(), pushLeftSideStage.getInputPort());
connectPorts(pushLeftSideStage.getOutputPort(), pushRightSideStage.getInputPort());
connectPorts(pushRightSideStage.getOutputPort(), ifStage.getSubTaskInputPort());
// -> feedback loop to the ifStage
// map outer ports to inner ports
inputPort = createInputPort(initStage.getInputPort());
outputPort = createOutputPort(ifStage.getFalseOutputPort());
}
public InputPort<int[]> getInputPort() {
return inputPort;
}
public OutputPort<int[]> getOutputPort() {
return outputPort;
}
}