Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed IndexOf function from Multiplexer #275

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading