Skip to content

Commit 0b67bca

Browse files
chore: review comments
1 parent 9ea9561 commit 0b67bca

8 files changed

Lines changed: 33 additions & 33 deletions

File tree

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/TupleRecorder.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package ai.timefold.solver.core.impl.bavet.common;
22

3-
import java.util.ArrayList;
43
import java.util.IdentityHashMap;
5-
import java.util.LinkedHashMap;
64
import java.util.List;
75
import java.util.function.UnaryOperator;
86

@@ -18,13 +16,9 @@ public record TupleRecorder<Tuple_ extends Tuple>(List<Tuple_> recordedTupleList
1816
public void recordTuple(Tuple_ tuple) {
1917
var outTuple = inputTupleToOutputTuple.computeIfAbsent(tuple, mapper);
2018
if (tuple.getIndictmentSource() != IndictmentSource.DISABLED) {
21-
if (outTuple.getIndictmentSource() == IndictmentSource.DISABLED) {
22-
outTuple.setIndictmentSource(
23-
new IndictmentSource.AggregateIndictmentSource(new ArrayList<>(), new LinkedHashMap<>()));
24-
}
2519
// Precompute uses an independent node network, so we need to aggregate its supports to not interfere with the
2620
// outer node network support
27-
var aggregateIndictmentSource = (IndictmentSource.AggregateIndictmentSource) outTuple.getIndictmentSource();
21+
var aggregateIndictmentSource = IndictmentSource.getPrecomputeAggregation(outTuple);
2822
tuple.getIndictmentSource()
2923
.visitAllSources(source -> aggregateIndictmentSource.sourceList().add(IndictmentSource.of(source)));
3024
}

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/UniversalTuple.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ final class UniversalTuple<A, B, C, D>
3030
this.store = storeSize > 0 ? new Object[storeSize] : EMPTY_STORE;
3131
}
3232

33-
int cardinality() {
34-
return cardinality;
35-
}
36-
37-
int storeSize() {
38-
return store.length;
39-
}
40-
4133
@Override
4234
public void setA(@Nullable A a) {
4335
this.a = a;

core/src/main/java/ai/timefold/solver/core/impl/bavet/common/tuple/indictment/IndictmentSource.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ static IndictmentSource joining(Tuple left, Tuple right) {
6868
return new JoinedIndictmentSource(left.getIndictmentSource(), right.getIndictmentSource(), new LinkedHashMap<>());
6969
}
7070

71+
static AggregateIndictmentSource getPrecomputeAggregation(Tuple outTuple) {
72+
if (outTuple.getIndictmentSource() != DISABLED) {
73+
return (AggregateIndictmentSource) outTuple.getIndictmentSource();
74+
}
75+
var out = new AggregateIndictmentSource(new ArrayList<>(), new LinkedHashMap<>());
76+
outTuple.setIndictmentSource(out);
77+
return out;
78+
}
79+
7180
static IndictmentSource aggregating(Tuple elementTuple, Tuple groupTuple) {
7281
if (elementTuple.getIndictmentSource() == DISABLED) {
7382
return DISABLED;

core/src/main/java/ai/timefold/solver/core/impl/score/constraint/ConstraintMatch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static java.util.Objects.requireNonNull;
44

5-
import java.util.List;
5+
import java.util.SequencedSet;
66

77
import ai.timefold.solver.core.api.score.Score;
88
import ai.timefold.solver.core.api.score.stream.ConstraintJustification;
@@ -30,7 +30,7 @@ public final class ConstraintMatch<Score_ extends Score<Score_>> implements Comp
3030

3131
private final ConstraintRef constraintRef;
3232
private final @Nullable ConstraintJustification justification;
33-
private final @Nullable List<Object> indictedObjects;
33+
private final @Nullable SequencedSet<Object> indictedObjects;
3434
private final Score_ score;
3535

3636
/**
@@ -39,7 +39,7 @@ public final class ConstraintMatch<Score_ extends Score<Score_>> implements Comp
3939
* @param score penalty or reward associated with the constraint match
4040
*/
4141
public ConstraintMatch(ConstraintRef constraintRef, @Nullable ConstraintJustification justification,
42-
@Nullable List<Object> indictedObjects, Score_ score) {
42+
@Nullable SequencedSet<Object> indictedObjects, Score_ score) {
4343
this.constraintRef = requireNonNull(constraintRef);
4444
this.justification = justification;
4545
this.indictedObjects = indictedObjects;
@@ -67,7 +67,7 @@ public ConstraintRef getConstraintRef() {
6767
return (Justification_) justification;
6868
}
6969

70-
public @Nullable List<Object> getIndictedObjects() {
70+
public @Nullable SequencedSet<Object> getIndictedObjects() {
7171
return indictedObjects;
7272
}
7373

core/src/main/java/ai/timefold/solver/core/impl/score/constraint/ConstraintMatchTotal.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.LinkedHashSet;
66
import java.util.List;
7+
import java.util.SequencedSet;
78
import java.util.Set;
89

910
import ai.timefold.solver.core.api.score.Score;
@@ -68,7 +69,8 @@ public Score_ getScore() {
6869
* @return never null
6970
*/
7071
public ConstraintMatch<Score_> addConstraintMatch(List<Object> justifications, Score_ score) {
71-
return addConstraintMatch(DefaultConstraintJustification.of(score, justifications), justifications, score);
72+
return addConstraintMatch(DefaultConstraintJustification.of(score, justifications),
73+
new LinkedHashSet<>(justifications), score);
7274
}
7375

7476
/**
@@ -78,7 +80,8 @@ public ConstraintMatch<Score_> addConstraintMatch(List<Object> justifications, S
7880
* @param score never null
7981
* @return never null
8082
*/
81-
public ConstraintMatch<Score_> addConstraintMatch(ConstraintJustification justification, List<Object> indictedObjects,
83+
public ConstraintMatch<Score_> addConstraintMatch(ConstraintJustification justification,
84+
SequencedSet<Object> indictedObjects,
8285
Score_ score) {
8386
var constraintMatch = new ConstraintMatch<>(constraintRef, justification, indictedObjects, score);
8487
addConstraintMatch(constraintMatch);

core/src/main/java/ai/timefold/solver/core/impl/score/stream/common/inliner/ConstraintMatchSupplier.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package ai.timefold.solver.core.impl.score.stream.common.inliner;
22

3-
import java.util.ArrayList;
43
import java.util.Arrays;
54
import java.util.Collections;
65
import java.util.LinkedHashSet;
7-
import java.util.List;
86
import java.util.Objects;
7+
import java.util.SequencedSet;
98
import java.util.function.BiFunction;
109
import java.util.stream.Collectors;
1110

@@ -43,15 +42,15 @@
4342
public interface ConstraintMatchSupplier<Score_ extends Score<Score_>>
4443
extends BiFunction<Constraint, Score_, ConstraintMatch<Score_>> {
4544

46-
static @Nullable List<Object> collectIndictments(Constraint constraint, Tuple tuple) {
45+
static @Nullable SequencedSet<Object> collectIndictments(Constraint constraint, Tuple tuple) {
4746
if (tuple.getIndictmentSource() == IndictmentSource.DISABLED) {
4847
return null;
4948
}
5049
var out = new LinkedHashSet<>();
5150
var abstractConstraint = (AbstractConstraint<?, ?, ?>) constraint;
5251
var involvedNodeIds = Objects.requireNonNull(abstractConstraint.getInvolvedNodeIds());
5352
tuple.getIndictmentSource().visitSources(involvedNodeIds, out::add);
54-
return new ArrayList<>(out);
53+
return out;
5554
}
5655

5756
/**
@@ -62,7 +61,7 @@ public interface ConstraintMatchSupplier<Score_ extends Score<Score_>>
6261
*/
6362
static <Score_ extends Score<Score_>> ConstraintMatchSupplier<Score_> empty() {
6463
return (constraint, impact) -> new ConstraintMatch<>(constraint.getConstraintRef(), null,
65-
Collections.emptyList(), impact);
64+
Collections.emptySortedSet(), impact);
6665
}
6766

6867
static <A, Score_ extends Score<Score_>> ConstraintMatchSupplier<Score_> of(

core/src/test/java/ai/timefold/solver/core/impl/score/constraint/ConstraintMatchTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static ai.timefold.solver.core.api.score.SimpleScore.ONE;
44
import static ai.timefold.solver.core.api.score.SimpleScore.ZERO;
55

6+
import java.util.LinkedHashSet;
67
import java.util.List;
78

89
import ai.timefold.solver.core.api.score.Score;
@@ -28,7 +29,7 @@ private <Score_ extends Score<Score_>> ConstraintMatch<Score_> buildConstraintMa
2829
Object... facts) {
2930
return new ConstraintMatch<>(ConstraintRef.of(constraintName),
3031
DefaultConstraintJustification.of(score, facts),
31-
List.of(facts),
32+
new LinkedHashSet<>(List.of(facts)),
3233
score);
3334
}
3435

core/src/test/java/ai/timefold/solver/core/impl/score/stream/common/AbstractConstraintStreamTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import static org.assertj.core.api.Assertions.fail;
55

66
import java.util.Arrays;
7+
import java.util.LinkedHashSet;
78
import java.util.List;
89
import java.util.Objects;
10+
import java.util.SequencedSet;
911
import java.util.Set;
1012
import java.util.function.Function;
1113
import java.util.stream.Collectors;
@@ -129,17 +131,17 @@ protected static class AssertableMatch {
129131
private final int score;
130132
private final ConstraintRef constraintRef;
131133
private final List<Object> justificationList;
132-
private List<Object> indictmentList;
134+
private SequencedSet<Object> indictmentSet;
133135

134136
public AssertableMatch(int score, ConstraintRef constraintRef, Object... justifications) {
135137
this.justificationList = Arrays.asList(justifications);
136138
this.constraintRef = constraintRef;
137139
this.score = score;
138-
this.indictmentList = justificationList;
140+
this.indictmentSet = new LinkedHashSet<>(justificationList);
139141
}
140142

141143
public AssertableMatch withIndictedObjects(Object... indictedObjects) {
142-
this.indictmentList = Arrays.asList(indictedObjects);
144+
this.indictmentSet = new LinkedHashSet<>(Arrays.asList(indictedObjects));
143145
return this;
144146
}
145147

@@ -173,16 +175,16 @@ public boolean isEqualTo(ConstraintMatch<?> constraintMatch, boolean indictments
173175
return true;
174176
}
175177
var indictedObjects = constraintMatch.getIndictedObjects();
176-
if (indictedObjects.size() != indictmentList.size()) {
178+
if (indictedObjects.size() != indictmentSet.size()) {
177179
return false;
178180
}
179-
return indictmentList.containsAll(indictedObjects);
181+
return indictmentSet.containsAll(indictedObjects);
180182
}
181183

182184
@Override
183185
public String toString() {
184186
return "%s %s=%d (indicting %s)".formatted(constraintRef, justificationList, score,
185-
indictmentList);
187+
indictmentSet);
186188
}
187189

188190
}

0 commit comments

Comments
 (0)