diff options
| author | Dante Niewenhuis <d.niewenhuis@hotmail.com> | 2024-11-28 11:52:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-28 11:52:16 +0100 |
| commit | 9e99bf257d3bd0aad70306dedd75c77b0e81a4c9 (patch) | |
| tree | 8f7c5fcef6f8dd32e907b136d94dc727f5cd10a8 /opendc-simulator | |
| parent | a2caf51f6e4691736e36ee41a178c6f6bbae3d67 (diff) | |
Removed the IndexOf function from Multiplexer.java, by adding a consumerIndex variable to FlowEdge.java (#275)
Diffstat (limited to 'opendc-simulator')
2 files changed, 29 insertions, 3 deletions
diff --git a/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java b/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java index dd4c2d11..8cd2fa6f 100644 --- a/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java +++ b/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java @@ -127,6 +127,8 @@ public class Multiplexer extends FlowNode implements FlowSupplier, FlowConsumer */ @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); @@ -145,7 +147,7 @@ public class Multiplexer extends FlowNode implements FlowSupplier, FlowConsumer @Override public void removeConsumerEdge(FlowEdge consumerEdge) { - int idx = this.consumerEdges.indexOf(consumerEdge); + int idx = consumerEdge.getConsumerIndex(); if (idx == -1) { return; @@ -157,6 +159,11 @@ public class Multiplexer extends FlowNode implements FlowSupplier, FlowConsumer 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(); } @@ -169,7 +176,7 @@ public class Multiplexer extends FlowNode implements FlowSupplier, FlowConsumer @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"); @@ -201,7 +208,7 @@ public class Multiplexer extends FlowNode implements FlowSupplier, FlowConsumer @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"); diff --git a/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java b/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java index d99cd78e..95fe7928 100644 --- a/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java +++ b/opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java @@ -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; @@ -86,6 +89,22 @@ public class FlowEdge { 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 */ |
