Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ private short getShortId(Location location) {
if (shortId == null) {
shortId = idGenerator;
locationToId.put(location, shortId);
location.setIndex(this, shortId);
idGenerator++;
}
// The matrix is usually filled before the location is attached to it,
// so the id may already exist while the location has not cached it yet.
location.setIndex(this, shortId);
Comment thread
triceo marked this conversation as resolved.
return shortId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ void cacheShortIndexInLocation() {
assertThat((int) b.getIndex(distanceMatrix)).isEqualTo(1);
}

@Test
void cacheShortIndexInLocationAttachedAfterMatrixIsFilled() {
LongArrayDistanceMatrix distanceMatrix = new LongArrayDistanceMatrix(2);
// The converters fill the matrix with their own location instances before the model's locations are attached.
Location responseA = new Location(0.0, 1.0);
Location responseB = new Location(1.0, 0.0);
distanceMatrix.put(responseA, responseB, 7L);
distanceMatrix.put(responseB, responseA, 9L);

Location a = new Location(0.0, 1.0);
Location b = new Location(1.0, 0.0);
a.setDistanceMatrix(distanceMatrix);
b.setDistanceMatrix(distanceMatrix);

assertThat((int) a.getIndex(distanceMatrix)).isZero();
assertThat((int) b.getIndex(distanceMatrix)).isEqualTo(1);
assertThat(distanceMatrix.get(a, b)).isEqualTo(7L);
assertThat(distanceMatrix.get(b, a)).isEqualTo(9L);
}

@Test
void putNoResize() {
LongArrayDistanceMatrix distanceMatrix = new LongArrayDistanceMatrix(4);
Expand Down
Loading