Skip to content

Commit b502828

Browse files
committed
review
1 parent 537bcf0 commit b502828

2 files changed

Lines changed: 13 additions & 22 deletions

File tree

core/src/main/java/ai/timefold/solver/core/impl/util/ElementAwareArrayList.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ private Entry getEntry(int index) {
8686
} else if (gapCount == 0 || index < firstGapPosition) {
8787
return (Entry) entries[index];
8888
}
89-
return partialCompact(index);
89+
partialCompact(index);
90+
return (Entry) entries[index];
9091
}
9192

9293
/**
@@ -103,10 +104,10 @@ void compact() {
103104
/**
104105
* Avoid calling this when {@code gapCount == 0}.
105106
*/
106-
private Entry partialCompact(int rightBoundaryPosition) {
107+
private void partialCompact(int rightBoundaryPosition) {
107108
if (rightBoundaryPosition < firstGapPosition) {
108109
// The entire target range is in the already-compacted prefix; no work needed.
109-
return (Entry) entries[rightBoundaryPosition];
110+
return;
110111
}
111112
var encounteredGaps = 0;
112113
var lastNonNullPosition = firstGapPosition - 1; // firstGapPosition non-nulls are already in place before us.
@@ -131,7 +132,7 @@ private Entry partialCompact(int rightBoundaryPosition) {
131132
} else {
132133
firstGapPosition = rightBoundaryPosition + 1;
133134
}
134-
return entry;
135+
return;
135136
}
136137
}
137138
}
@@ -174,7 +175,7 @@ public void add(int index, T element) {
174175
}
175176
// Compact prefix [0, index-1] so physical position k == logical position k for all k < index.
176177
if (index > 0) {
177-
partialCompact(index - 1); // Increases modCount.
178+
partialCompact(index - 1);
178179
}
179180
if (entries[index] == null) {
180181
// Gap at the target position: fill it directly without shifting the array.
@@ -385,7 +386,9 @@ private ElementAwareListIterator(int startingPosition) {
385386
"The index (%d) must be >= 0 and <= size (%d).".formatted(startingPosition, currentSize));
386387
}
387388
if (startingPosition > 0 && gapCount > 0) {
388-
currentPosition = partialCompact(startingPosition - 1).position + 1;
389+
var index = startingPosition - 1;
390+
partialCompact(index);
391+
currentPosition = ((Entry) entries[index]).position + 1;
389392
} else {
390393
currentPosition = startingPosition;
391394
}

core/src/test/java/ai/timefold/solver/core/impl/util/ElementAwareArrayListTest.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,6 @@ void addEntryAfterChurnAppendsInOrder() {
125125
assertThat(entryX.toString()).contains("@1");
126126
}
127127

128-
@Test
129-
@DisplayName("remove-to-empty frees large backing array (length > RETAIN_THRESHOLD)")
130-
void removeToEmptyFreesLargeBackingArray() {
131-
var list = new ElementAwareArrayList<String>();
132-
// 30 elements force entries.length to 32 (> RETAIN_THRESHOLD=16), triggering the free path on empty.
133-
List<ElementAwareArrayList<String>.Entry> entryList = new ArrayList<>();
134-
for (var i = 0; i < 30; i++) {
135-
entryList.add(list.addEntry("e" + i));
136-
}
137-
for (var entry : entryList) {
138-
entry.remove();
139-
}
140-
assertThat(list).isEmpty();
141-
}
142-
143128
@Test
144129
@DisplayName("add after free-path empty re-allocates and preserves insertion order")
145130
void addAfterLargeArrayFreePathPreservesOrder() {
@@ -1520,8 +1505,11 @@ void stressTestAgainstReferenceArrayList(long seed) {
15201505
list.compact();
15211506
assertThat(list).containsExactlyElementsOf(reference);
15221507
}
1508+
default -> {
1509+
throw new IllegalStateException("Unexpected operation code: %d".formatted(op));
1510+
}
15231511
}
1524-
assertThat(list.size()).isEqualTo(reference.size());
1512+
assertThat(list).hasSameSizeAs(reference);
15251513
}
15261514
assertThat(list).containsExactlyElementsOf(reference);
15271515
}

0 commit comments

Comments
 (0)