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
No related branches found
No related tags found
1 merge request!72Dc paradigm
package teetime.framework; 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 * Represents a stage to provide functionality for the divide and conquer paradigm
* *
* @since 2.x * @since 2.x
* *
* @author Christian Wulf, Nelson Tavares de Sousa, Robin Mohr * @author Robin Mohr
* *
* @param <I> * @param <P>
* type of elements to be processed. * 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) public abstract class AbstractDCStage<P, S> extends AbstractStage implements ITaskFarmDuplicable<P, S> { // FIXME check compatibility of interface
// TODO arraylist!
// BETTER private final I[] buffer but see next TODO (l38)
private I leftBuffer = null;
private I rightBuffer = null;
private final IntObjectMap<S> solutionBuffer = new IntObjectHashMap<S>();
private final DynamicConfigurationContext context; private final DynamicConfigurationContext context;
protected final InputPort<I> inputPort = this.createInputPort(); protected final InputPort<P> inputPort = this.createInputPort();
protected final InputPort<I> leftInputPort = this.createInputPort(); protected final InputPort<S> leftInputPort = this.createInputPort();
protected final InputPort<I> rightInputPort = this.createInputPort(); protected final InputPort<S> rightInputPort = this.createInputPort();
protected final OutputPort<I> outputPort = this.createOutputPort(); protected final OutputPort<S> outputPort = this.createOutputPort();
protected final OutputPort<I> leftOutputPort = this.createOutputPort(); protected final OutputPort<P> leftOutputPort = this.createOutputPort();
protected final OutputPort<I> rightOutputPort = this.createOutputPort(); protected final OutputPort<P> rightOutputPort = this.createOutputPort();
/** /**
* Divide and Conquer stages need the configuration context upon creation * Divide and Conquer stages need the configuration context upon creation
...@@ -36,107 +39,59 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I ...@@ -36,107 +39,59 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I
if (null == context) { if (null == context) {
throw new IllegalArgumentException("Context may not be null."); throw new IllegalArgumentException("Context may not be null.");
} }
// TODO this.buffer = (I[]) new Object[size]; but can't figure out 'size'
this.context = context; 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; return this.inputPort;
} }
public final InputPort<I> getLeftInputPort() { public final InputPort<S> getLeftInputPort() {
return this.leftInputPort; return this.leftInputPort;
} }
public final InputPort<I> getRightInputPort() { public final InputPort<S> getRightInputPort() {
return this.rightInputPort; return this.rightInputPort;
} }
public final OutputPort<I> getOutputPort() { @Override
public final OutputPort<S> getOutputPort() {
return this.outputPort; return this.outputPort;
} }
public final OutputPort<I> getleftOutputPort() { public final OutputPort<P> getleftOutputPort() {
return this.leftOutputPort; return this.leftOutputPort;
} }
public final OutputPort<I> getrightOutputPort() { public final OutputPort<P> getrightOutputPort() {
return this.rightOutputPort; return this.rightOutputPort;
} }
@Override @Override
protected final void executeStage() { protected final void executeStage() {
// TODO
// 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();
}
} }
/** /**
* A method to add a new copy (new instance) of this stage to the configuration, which should be executed in a own thread. * 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) { private void makeCopy(final OutputPort<P> out, final InputPort<S> in) {
final AbstractDCStage<I> newStage = debugCreateMethod(); final AbstractDCStage<P, S> newStage = this;
context.connectPorts(out, newStage.getInputPort()); context.connectPorts(out, newStage.getInputPort());
context.connectPorts(newStage.getOutputPort(), in); context.connectPorts(newStage.getOutputPort(), in);
context.beginThread(newStage); context.beginThread(newStage);
context.sendSignals(out); 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. * Method to divide the given input and send to the left and right output ports.
* *
* @param element * @param element
* An element to be split and further processed * 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. * 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 ...@@ -144,7 +99,7 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I
* @param element * @param element
* An element to be processed * 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. * 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 ...@@ -154,7 +109,7 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I
* @param eRight * @param eRight
* Second half of the resulting element. * 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 * 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 ...@@ -162,11 +117,12 @@ public abstract class AbstractDCStage<I> extends AbstractStage { // IMPLEMENTS I
* @param element * @param element
* The element whose properties determine the split condition * 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() { public DynamicConfigurationContext getContext() {
// TODO Auto-generated method stub
return this.context; return this.context;
} }
} }
...@@ -2,79 +2,72 @@ package teetime.stage; ...@@ -2,79 +2,72 @@ package teetime.stage;
import teetime.framework.AbstractDCStage; import teetime.framework.AbstractDCStage;
import teetime.framework.DynamicConfigurationContext; import teetime.framework.DynamicConfigurationContext;
import teetime.stage.taskfarm.ITaskFarmDuplicable;
import teetime.stage.util.QuicksortProblem; 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) { public QuicksortStage(final DynamicConfigurationContext context) {
super(context); super(context);
} }
// TODO Get rid of this
@Override @Override
protected AbstractDCStage<QuicksortProblem> debugCreateMethod() { protected boolean isBaseCase(final QuicksortProblem quickSortProblem) {
return new QuicksortStage(super.getContext()); return (quickSortProblem.getHigh() - quickSortProblem.getLow() >= 1 ? false : true);
} }
@Override @Override
protected void divide(final QuicksortProblem qsp) { protected void divide(final QuicksortProblem problem) {
final int low = qsp.getLow(); final int low = problem.getLow();
final int high = qsp.getHigh(); final int high = problem.getHigh();
final int[] arr = qsp.getArr(); final int[] numbers = problem.getNumbers();
// pick the pivot // pick the pivot
final int middle = low + (high - low) / 2; final int middle = low + (high - low) / 2;
final int pivot = arr[middle]; final int pivot = numbers[middle];
// make left < pivot and right > pivot // make left < pivot and right > pivot
int i = low, j = high; int i = low;
int j = high;
while (i <= j) { while (i <= j) {
while (arr[i] < pivot) { while (numbers[i] < pivot) {
i++; i++;
} }
while (arr[j] > pivot) { while (numbers[j] > pivot) {
j--; j--;
} }
if (i <= j) { if (i <= j) {
int temp = arr[i]; int temp = numbers[i];
arr[i] = arr[j]; numbers[i] = numbers[j];
arr[j] = temp; numbers[j] = temp;
i++; i++;
j--; j--;
} }
} }
// recursively sort two sub parts // 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); leftOutputPort.send(newQuicksortProblem1);
QuicksortProblem newQuicksortProblem2 = new QuicksortProblem(i, high, arr); QuicksortProblem newQuicksortProblem2 = new QuicksortProblem(i, high, numbers);
rightOutputPort.send(newQuicksortProblem2); rightOutputPort.send(newQuicksortProblem2);
} }
@Override @Override
protected void solve(final QuicksortProblem qsp) { protected QuicksortProblem solve(final QuicksortProblem problem) {
this.outputPort.send(qsp); return problem;
} }
@Override @Override
protected void conquer(final QuicksortProblem eLeft, final QuicksortProblem eRight) { protected void combine(final QuicksortProblem s1, final QuicksortProblem s2) {
final int rlow = eRight.getLow(); s1.setHigh(s2.getHigh());
final int rhigh = eRight.getHigh(); this.getOutputPort().send(s1);
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);
} }
@Override @Override
protected boolean splitCondition(final QuicksortProblem qsp) { public ITaskFarmDuplicable<QuicksortProblem, QuicksortProblem> duplicate() {
return (((qsp.getHigh() - qsp.getLow()) >= 1) ? true : false); return new QuicksortStage(this.getContext());
} }
} }
...@@ -8,9 +8,9 @@ package teetime.stage.util; ...@@ -8,9 +8,9 @@ package teetime.stage.util;
*/ */
public final class QuicksortProblem { public final class QuicksortProblem {
private final int low; private int low;
private final int high; private int high;
private final int[] arr; private final int[] numbers;
/** /**
* An implementation of a quicksort problem. * An implementation of a quicksort problem.
...@@ -19,13 +19,13 @@ public final class QuicksortProblem { ...@@ -19,13 +19,13 @@ public final class QuicksortProblem {
* Pointer to the lower bound of indices to be compared in the array * Pointer to the lower bound of indices to be compared in the array
* @param high * @param high
* Pointer to the upper bound of indices to be compared in the array * Pointer to the upper bound of indices to be compared in the array
* @param arr * @param numbers
* Array to be sorted * 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.low = low;
this.high = high; this.high = high;
this.arr = arr; this.numbers = numbers;
} }
public int getLow() { public int getLow() {
...@@ -36,7 +36,15 @@ public final class QuicksortProblem { ...@@ -36,7 +36,15 @@ public final class QuicksortProblem {
return this.high; return this.high;
} }
public int[] getArr() { public void setHigh(final int high) {
return this.arr; 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