Skip to content
Snippets Groups Projects
Commit b9be657e authored by Robin Mohr's avatar Robin Mohr
Browse files

minor changes and code cleanup

parent c3e2e334
Branches
Tags
1 merge request!72Dc paradigm
package teetime.framework;
import teetime.stage.taskfarm.ITaskFarmDuplicable;
import com.carrotsearch.hppc.IntObjectHashMap;
import com.carrotsearch.hppc.IntObjectMap;
/**
* Represents a stage to provide functionality for the divide and conquer paradigm
*
* @since 2.x
*
* @author Christian Wulf, Nelson Tavares de Sousa, Robin Mohr
* @author Robin Mohr
*
* @param <I>
* type of elements to be processed.
* @param <P>
* type of elements that represent a problem to be solved.
*
* @param <S>
* type of elements that represent the solution to a problem.
*/
public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS IDUPLICABLE (anderer Branch)
// TODO arraylist!
// BETTER private final I[] buffer but see next TODO (l38)
private I leftBuffer = null;
private I rightBuffer = null;
public abstract class AbstractDCStage<P, S> extends AbstractStage implements ITaskFarmDuplicable<P, S> { // FIXME check compatibility of interface
private final IntObjectMap<S> solutionBuffer = new IntObjectHashMap<S>();
private final DynamicConfigurationContext context;
protected final InputPort<I> inputPort = this.createInputPort();
protected final InputPort<I> leftInputPort = this.createInputPort();
protected final InputPort<I> rightInputPort = this.createInputPort();
protected final InputPort<P> inputPort = this.createInputPort();
protected final InputPort<S> leftInputPort = this.createInputPort();
protected final InputPort<S> rightInputPort = this.createInputPort();
protected final OutputPort<I> outputPort = this.createOutputPort();
protected final OutputPort<I> leftOutputPort = this.createOutputPort();
protected final OutputPort<I> rightOutputPort = this.createOutputPort();
protected final OutputPort<S> outputPort = this.createOutputPort();
protected final OutputPort<P> leftOutputPort = this.createOutputPort();
protected final OutputPort<P> rightOutputPort = this.createOutputPort();
/**
* Divide and Conquer stages need the configuration context upon creation
......@@ -36,107 +39,59 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I
if (null == context) {
throw new IllegalArgumentException("Context may not be null.");
}
// TODO this.buffer = (I[]) new Object[size]; but can't figure out 'size'
this.context = context;
// connect to self instead of dummy pipe upon creation
context.connectPorts(leftOutputPort, leftInputPort);
context.connectPorts(rightOutputPort, rightInputPort);
}
public final InputPort<I> getInputPort() {
@Override
public final InputPort<P> getInputPort() {
return this.inputPort;
}
public final InputPort<I> getLeftInputPort() {
public final InputPort<S> getLeftInputPort() {
return this.leftInputPort;
}
public final InputPort<I> getRightInputPort() {
public final InputPort<S> getRightInputPort() {
return this.rightInputPort;
}
public final OutputPort<I> getOutputPort() {
@Override
public final OutputPort<S> getOutputPort() {
return this.outputPort;
}
public final OutputPort<I> getleftOutputPort() {
public final OutputPort<P> getleftOutputPort() {
return this.leftOutputPort;
}
public final OutputPort<I> getrightOutputPort() {
public final OutputPort<P> getrightOutputPort() {
return this.rightOutputPort;
}
@Override
protected final void executeStage() {
// TODO STRUKTUR!
final I element = this.getInputPort().receive();
final I eLeft = this.getLeftInputPort().receive();
final I eRight = this.getRightInputPort().receive();
if (eLeft != null) {
this.logger.debug("Left " + eLeft.toString());
if (eRight != null) {
this.logger.debug("Right " + eRight.toString());
conquer(eLeft, eRight);
} else {
if (rightBuffer != null) {
this.logger.debug("RightB " + rightBuffer.toString());
conquer(eLeft, rightBuffer);
} else {
leftBuffer = eLeft;
}
}
} else if (eRight != null) {
if (leftBuffer != null) {
this.logger.debug("LeftB " + leftBuffer.toString());
conquer(leftBuffer, eRight);
} else {
rightBuffer = eRight;
}
} else if (element != null) {
this.logger.debug("E " + element.toString());
if (splitCondition(element)) {
this.logger.debug("[DC]" + this.getId() + "_" + "passed splitcondition_" + element.toString());
// SPLITCOUNT THREASHHOLD NUMTHREADS
makeCopy(leftOutputPort, leftInputPort);
makeCopy(rightOutputPort, rightInputPort);
this.logger.debug("[DC]" + this.getId() + "_" + "DIVIDING_" + element.toString());
divide(element);
} else {
this.logger.debug("[DC]" + this.getId() + "_" + "SOLVING_" + element.toString());
solve(element);
}
} else {
this.logger.debug("NO ELEMENT RECEIVED!");
returnNoElement();
}
// TODO
}
/**
* A method to add a new copy (new instance) of this stage to the configuration, which should be executed in a own thread.
*
*/
private void makeCopy(final OutputPort<I> out, final InputPort<I> in) {
final AbstractDCStage<I> newStage = debugCreateMethod();
private void makeCopy(final OutputPort<P> out, final InputPort<S> in) {
final AbstractDCStage<P, S> newStage = this;
context.connectPorts(out, newStage.getInputPort());
context.connectPorts(newStage.getOutputPort(), in);
context.beginThread(newStage);
context.sendSignals(out);
}
// BETTER Write the function code in this class instead
protected abstract AbstractDCStage<I> debugCreateMethod();
/**
* Method to divide the given input and send to the left and right output ports.
*
* @param element
* An element to be split and further processed
*/
protected abstract void divide(final I element);
protected abstract void divide(final P problem);
/**
* Method to process the given input and send to the output port.
......@@ -144,7 +99,7 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I
* @param element
* An element to be processed
*/
protected abstract void solve(final I element);
protected abstract S solve(final P problem);
/**
* Method to join the given inputs together and send to the output port.
......@@ -154,7 +109,7 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I
* @param eRight
* Second half of the resulting element.
*/
protected abstract void conquer(final I eLeft, final I eRight);
protected abstract void combine(final S s1, final S s2);
/**
* Determines whether or not to split the input problem by examining the given element
......@@ -162,11 +117,12 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I
* @param element
* The element whose properties determine the split condition
*/
protected abstract boolean splitCondition(final I element);
protected abstract boolean isBaseCase(final P problem);
@Override
public abstract ITaskFarmDuplicable<P, S> duplicate();
// TODO get rid of this
public DynamicConfigurationContext getContext() {
// TODO Auto-generated method stub
return this.context;
}
}
......@@ -2,79 +2,72 @@ package teetime.stage;
import teetime.framework.AbstractDCStage;
import teetime.framework.DynamicConfigurationContext;
import teetime.stage.taskfarm.ITaskFarmDuplicable;
import teetime.stage.util.QuicksortProblem;
public final class QuicksortStage extends AbstractDCStage<QuicksortProblem> {
public final class QuicksortStage extends AbstractDCStage<QuicksortProblem, QuicksortProblem> {
public QuicksortStage(final DynamicConfigurationContext context) {
super(context);
}
// TODO Get rid of this
@Override
protected AbstractDCStage<QuicksortProblem> debugCreateMethod() {
return new QuicksortStage(super.getContext());
protected boolean isBaseCase(final QuicksortProblem quickSortProblem) {
return (quickSortProblem.getHigh() - quickSortProblem.getLow() >= 1 ? false : true);
}
@Override
protected void divide(final QuicksortProblem qsp) {
final int low = qsp.getLow();
final int high = qsp.getHigh();
final int[] arr = qsp.getArr();
protected void divide(final QuicksortProblem problem) {
final int low = problem.getLow();
final int high = problem.getHigh();
final int[] numbers = problem.getNumbers();
// pick the pivot
final int middle = low + (high - low) / 2;
final int pivot = arr[middle];
final int pivot = numbers[middle];
// make left < pivot and right > pivot
int i = low, j = high;
int i = low;
int j = high;
while (i <= j) {
while (arr[i] < pivot) {
while (numbers[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
while (numbers[j] > pivot) {
j--;
}
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
i++;
j--;
}
}
// recursively sort two sub parts
QuicksortProblem newQuicksortProblem1 = new QuicksortProblem(low, j, arr);
// FIXME Put following code in AbstraceDCStage
QuicksortProblem newQuicksortProblem1 = new QuicksortProblem(low, j, numbers);
leftOutputPort.send(newQuicksortProblem1);
QuicksortProblem newQuicksortProblem2 = new QuicksortProblem(i, high, arr);
QuicksortProblem newQuicksortProblem2 = new QuicksortProblem(i, high, numbers);
rightOutputPort.send(newQuicksortProblem2);
}
@Override
protected void solve(final QuicksortProblem qsp) {
this.outputPort.send(qsp);
protected QuicksortProblem solve(final QuicksortProblem problem) {
return problem;
}
@Override
protected void conquer(final QuicksortProblem eLeft, final QuicksortProblem eRight) {
final int rlow = eRight.getLow();
final int rhigh = eRight.getHigh();
final int[] arr1 = eLeft.getArr();
final int[] arr2 = eRight.getArr();
for (int j = rlow; j <= rhigh; j++) {
arr1[j] = arr2[j];
}
QuicksortProblem newQuicksortProblem = new QuicksortProblem(eLeft.getLow(), rhigh, arr1);
this.outputPort.send(newQuicksortProblem);
protected void combine(final QuicksortProblem s1, final QuicksortProblem s2) {
s1.setHigh(s2.getHigh());
this.getOutputPort().send(s1);
}
@Override
protected boolean splitCondition(final QuicksortProblem qsp) {
return (((qsp.getHigh() - qsp.getLow()) >= 1) ? true : false);
public ITaskFarmDuplicable<QuicksortProblem, QuicksortProblem> duplicate() {
return new QuicksortStage(this.getContext());
}
}
......@@ -8,9 +8,9 @@ package teetime.stage.util;
*/
public final class QuicksortProblem {
private final int low;
private final int high;
private final int[] arr;
private int low;
private int high;
private final int[] numbers;
/**
* An implementation of a quicksort problem.
......@@ -19,13 +19,13 @@ public final class QuicksortProblem {
* Pointer to the lower bound of indices to be compared in the array
* @param high
* Pointer to the upper bound of indices to be compared in the array
* @param arr
* @param numbers
* Array to be sorted
*/
public QuicksortProblem(final int low, final int high, final int[] arr) {
public QuicksortProblem(final int low, final int high, final int[] numbers) {
this.low = low;
this.high = high;
this.arr = arr;
this.numbers = numbers;
}
public int getLow() {
......@@ -36,7 +36,15 @@ public final class QuicksortProblem {
return this.high;
}
public int[] getArr() {
return this.arr;
public void setHigh(final int high) {
this.high = high;
}
public void setLow(final int low) {
this.low = low;
}
public int[] getNumbers() {
return this.numbers;
}
}
package teetime.stage.util;
/**
* @since 2.x
*
* @author Robin Mohr
*
*/
public final class QuicksortSolution {
private final int low;
private final int high;
private final int[] numbers;
/**
* An implementation of a quicksort solution.
*
* @param low
* Pointer to the lower bound of indices to be compared in the array
* @param high
* Pointer to the upper bound of indices to be compared in the array
* @param numbers
* Array to be sorted
*/
public QuicksortSolution(final int low, final int high, final int[] numbers) {
this.low = low;
this.high = high;
this.numbers = numbers;
}
public int getLow() {
return this.low;
}
public int getHigh() {
return this.high;
}
public int[] getNumbers() {
return this.numbers;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment