Skip to content

Commit

Permalink
Removed the IndexOf function from Multiplexer.java, by adding a consu…
Browse files Browse the repository at this point in the history
…merIndex variable to FlowEdge.java (#275)
  • Loading branch information
DanteNiewenhuis authored Nov 28, 2024
1 parent a2caf51 commit 9e99bf2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ private static double redistributeSupply(
*/
@Override
public void addConsumerEdge(FlowEdge consumerEdge) {
consumerEdge.setConsumerIndex(this.consumerEdges.size());

this.consumerEdges.add(consumerEdge);
this.demands.add(0.0);
this.supplies.add(0.0);
Expand All @@ -145,7 +147,7 @@ public void addSupplierEdge(FlowEdge supplierEdge) {

@Override
public void removeConsumerEdge(FlowEdge consumerEdge) {
int idx = this.consumerEdges.indexOf(consumerEdge);
int idx = consumerEdge.getConsumerIndex();

if (idx == -1) {
return;
Expand All @@ -157,6 +159,11 @@ public void removeConsumerEdge(FlowEdge consumerEdge) {
this.demands.remove(idx);
this.supplies.remove(idx);

// update the consumer index for all consumerEdges higher than this.
for (int i = idx; i < this.consumerEdges.size(); i++) {
this.consumerEdges.get(i).setConsumerIndex(i);
}

this.invalidate();
}

Expand All @@ -169,7 +176,7 @@ public void removeSupplierEdge(FlowEdge supplierEdge) {

@Override
public void handleDemand(FlowEdge consumerEdge, double newDemand) {
int idx = consumerEdges.indexOf(consumerEdge);
int idx = consumerEdge.getConsumerIndex();

if (idx == -1) {
System.out.println("Error (Multiplexer): Demand pushed by an unknown consumer");
Expand Down Expand Up @@ -201,7 +208,7 @@ public void pushDemand(FlowEdge supplierEdge, double newDemand) {

@Override
public void pushSupply(FlowEdge consumerEdge, double newSupply) {
int idx = consumerEdges.indexOf(consumerEdge);
int idx = consumerEdge.getConsumerIndex();

if (idx == -1) {
System.out.println("Error (Multiplexer): pushing supply to an unknown consumer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class FlowEdge {
private FlowConsumer consumer;
private FlowSupplier supplier;

private int consumerIndex = -1;
private int supplierIndex = -1;

private double demand = 0.0;
private double supply = 0.0;

Expand Down Expand Up @@ -86,6 +89,22 @@ public double getSupply() {
return this.supply;
}

public int getConsumerIndex() {
return consumerIndex;
}

public void setConsumerIndex(int consumerIndex) {
this.consumerIndex = consumerIndex;
}

public int getSupplierIndex() {
return supplierIndex;
}

public void setSupplierIndex(int supplierIndex) {
this.supplierIndex = supplierIndex;
}

/**
* Push new demand from the Consumer to the Supplier
*/
Expand Down

0 comments on commit 9e99bf2

Please sign in to comment.