Skip to content

Commit 9e99bf2

Browse files
Removed the IndexOf function from Multiplexer.java, by adding a consumerIndex variable to FlowEdge.java (#275)
1 parent a2caf51 commit 9e99bf2

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/Multiplexer.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ private static double redistributeSupply(
127127
*/
128128
@Override
129129
public void addConsumerEdge(FlowEdge consumerEdge) {
130+
consumerEdge.setConsumerIndex(this.consumerEdges.size());
131+
130132
this.consumerEdges.add(consumerEdge);
131133
this.demands.add(0.0);
132134
this.supplies.add(0.0);
@@ -145,7 +147,7 @@ public void addSupplierEdge(FlowEdge supplierEdge) {
145147

146148
@Override
147149
public void removeConsumerEdge(FlowEdge consumerEdge) {
148-
int idx = this.consumerEdges.indexOf(consumerEdge);
150+
int idx = consumerEdge.getConsumerIndex();
149151

150152
if (idx == -1) {
151153
return;
@@ -157,6 +159,11 @@ public void removeConsumerEdge(FlowEdge consumerEdge) {
157159
this.demands.remove(idx);
158160
this.supplies.remove(idx);
159161

162+
// update the consumer index for all consumerEdges higher than this.
163+
for (int i = idx; i < this.consumerEdges.size(); i++) {
164+
this.consumerEdges.get(i).setConsumerIndex(i);
165+
}
166+
160167
this.invalidate();
161168
}
162169

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

170177
@Override
171178
public void handleDemand(FlowEdge consumerEdge, double newDemand) {
172-
int idx = consumerEdges.indexOf(consumerEdge);
179+
int idx = consumerEdge.getConsumerIndex();
173180

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

202209
@Override
203210
public void pushSupply(FlowEdge consumerEdge, double newSupply) {
204-
int idx = consumerEdges.indexOf(consumerEdge);
211+
int idx = consumerEdge.getConsumerIndex();
205212

206213
if (idx == -1) {
207214
System.out.println("Error (Multiplexer): pushing supply to an unknown consumer");

opendc-simulator/opendc-simulator-flow/src/main/java/org/opendc/simulator/engine/FlowEdge.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class FlowEdge {
3232
private FlowConsumer consumer;
3333
private FlowSupplier supplier;
3434

35+
private int consumerIndex = -1;
36+
private int supplierIndex = -1;
37+
3538
private double demand = 0.0;
3639
private double supply = 0.0;
3740

@@ -86,6 +89,22 @@ public double getSupply() {
8689
return this.supply;
8790
}
8891

92+
public int getConsumerIndex() {
93+
return consumerIndex;
94+
}
95+
96+
public void setConsumerIndex(int consumerIndex) {
97+
this.consumerIndex = consumerIndex;
98+
}
99+
100+
public int getSupplierIndex() {
101+
return supplierIndex;
102+
}
103+
104+
public void setSupplierIndex(int supplierIndex) {
105+
this.supplierIndex = supplierIndex;
106+
}
107+
89108
/**
90109
* Push new demand from the Consumer to the Supplier
91110
*/

0 commit comments

Comments
 (0)