Compare commits

...

1 Commits

Author SHA1 Message Date
Kilian Schuettler
02380503fe RED-7141: replaced arrayList with array access 2024-03-08 09:49:13 +01:00

View File

@ -14,6 +14,8 @@ public class NearestNeighbourService {
private static final int NUMBER_OF_NEIGHBOURS = 8; private static final int NUMBER_OF_NEIGHBOURS = 8;
private static final double STEP = 16.0; private static final double STEP = 16.0;
private int neighborInsertionIndex;
private int neighborCount;
public void findNearestNeighbors(List<Character> characters) { public void findNearestNeighbors(List<Character> characters) {
@ -31,8 +33,9 @@ public class NearestNeighbourService {
for (int i = 0; i < characters.size(); i++) { for (int i = 0; i < characters.size(); i++) {
List<Neighbor> candidates = new ArrayList<>(); Neighbor[] candidates = new Neighbor[maxNeighborCount + 1];
neighborInsertionIndex = 0;
neighborCount = 0;
int start = i; int start = i;
int end = i + 1; int end = i + 1;
@ -45,42 +48,83 @@ public class NearestNeighbourService {
while (start > 0 && characters.get(i).getX() - characters.get(start - 1).getX() < searchDistance) { while (start > 0 && characters.get(i).getX() - characters.get(start - 1).getX() < searchDistance) {
start--; start--;
candidates.add(new Neighbor(characters.get(start), characters.get(i))); candidates[neighborInsertionIndex] = new Neighbor(characters.get(start), characters.get(i));
clearMostDistant(candidates, maxNeighborCount); neighborCount++;
if (neighborCount > maxNeighborCount) {
neighborInsertionIndex = clearMostDistant(candidates);
neighborCount--;
} else {
neighborInsertionIndex++;
}
newCandidatesFound = true; newCandidatesFound = true;
} }
while (end < characters.size() && characters.get(end).getX() - characters.get(i).getX() < searchDistance) { while (end < characters.size() && characters.get(end).getX() - characters.get(i).getX() < searchDistance) {
candidates.add(new Neighbor(characters.get(end), characters.get(i))); candidates[neighborInsertionIndex] = new Neighbor(characters.get(end), characters.get(i));
clearMostDistant(candidates, maxNeighborCount); neighborCount++;
if (neighborCount > maxNeighborCount) {
neighborInsertionIndex = clearMostDistant(candidates);
neighborCount--;
} else {
neighborInsertionIndex++;
}
end++; end++;
newCandidatesFound = true; newCandidatesFound = true;
} }
if (newCandidatesFound && candidates.size() >= maxNeighborCount) { if (newCandidatesFound && neighborCount >= maxNeighborCount) {
distance = candidates.stream().mapToDouble(Neighbor::getDistance).max().orElse(Double.POSITIVE_INFINITY); distance = maxDistance(candidates);
} }
} }
clearMostDistant(candidates, maxNeighborCount); if (neighborCount < maxNeighborCount) {
characters.get(i).setNeighbors(new ArrayList<>(candidates)); clearMostDistant(candidates);
neighborCount--;
}
List<Neighbor> candidatesList = new ArrayList<>(maxNeighborCount);
for (Neighbor candidate : candidates) {
if (candidate != null) {
candidatesList.add(candidate);
}
}
candidatesList.sort(Comparator.comparingDouble(Neighbor::getDistance));
assert candidatesList.size() == maxNeighborCount;
characters.get(i).setNeighbors(candidatesList);
} }
} }
private void clearMostDistant(List<Neighbor> candidates, int maxNeighborCount) { private double maxDistance(Neighbor[] candidates) {
if (candidates.size() > maxNeighborCount) { double maxDistance = 0;
double maxDistance = 0; for (Neighbor candidate : candidates) {
int maxIndex = 0; if (candidate == null) {
for (int i = 0; i < candidates.size(); i++) { continue;
Neighbor candidate = candidates.get(i); }
if (candidate.getDistance() > maxDistance) { if (candidate.getDistance() > maxDistance) {
maxDistance = candidate.getDistance(); maxDistance = candidate.getDistance();
maxIndex = i;
}
} }
candidates.remove(maxIndex);
} }
return maxDistance;
}
private int clearMostDistant(Neighbor[] candidates) {
double maxDistance = 0;
int maxIndex = 0;
for (int i = 0; i < candidates.length; i++) {
Neighbor candidate = candidates[i];
if (candidate == null) {
continue;
}
if (candidate.getDistance() > maxDistance) {
maxDistance = candidate.getDistance();
maxIndex = i;
}
}
candidates[maxIndex] = null;
return maxIndex;
} }
} }