Skip to content

Commit db3b704

Browse files
committed
Optimised add()
- Removed unnecessary updateIndex() call, when we can easily determine the new entries' index value
1 parent 323178d commit db3b704

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/main/java/com/lewdev/probabilitylib/ProbabilityCollection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@ public void add(E object, int probability) {
117117
throw new IllegalArgumentException("Probability must be greater than 0");
118118
}
119119

120-
this.collection.add(new ProbabilitySetElement<E>(object, probability));
121-
this.totalProbability += probability;
120+
ProbabilitySetElement<E> entry = new ProbabilitySetElement<E>(object, probability);
121+
entry.setIndex(totalProbability + 1);
122122

123-
this.updateIndexes();
123+
this.collection.add(entry);
124+
this.totalProbability += probability;
124125
}
125126

126127
/**

0 commit comments

Comments
 (0)