From 9e99bf257d3bd0aad70306dedd75c77b0e81a4c9 Mon Sep 17 00:00:00 2001 From: Dante Niewenhuis Date: Thu, 28 Nov 2024 11:52:16 +0100 Subject: [PATCH] Removed the IndexOf function from Multiplexer.java, by adding a consumerIndex variable to FlowEdge.java (#275) --- .../org/opendc/simulator/Multiplexer.java | 13 ++++++++++--- .../org/opendc/simulator/engine/FlowEdge.java | 19 +++++++++++++++++++ 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 dd4c2d111..8cd2fa6f3 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 @@ 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); @@ -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; @@ -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(); } @@ -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"); @@ -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"); 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 d99cd78e4..95fe79285 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 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 */