Skip to content

Commit

Permalink
Merge pull request #3578 from steffenaxer/SquareGridZoneSystemFaster
Browse files Browse the repository at this point in the history
Improve SquareGridZoneSystem performance
  • Loading branch information
steffenaxer authored Nov 23, 2024
2 parents a0cb977 + f3ab912 commit 1f019b3
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class SquareGridZoneSystem implements GridZoneSystem {

Expand All @@ -55,6 +56,7 @@ public class SquareGridZoneSystem implements GridZoneSystem {
private final IdMap<Zone, Zone> zones = new IdMap<>(Zone.class);

private final IdMap<Zone, List<Link>> zoneToLinksMap = new IdMap<>(Zone.class);
private final Map<Integer, List<Link>> index2Links;
private final Network network;


Expand All @@ -76,6 +78,7 @@ public SquareGridZoneSystem(Network network, double cellSize, boolean filterByNe
this.rows = Math.max(1, (int) Math.ceil((maxY - minY) / cellSize));
this.cols = Math.max(1, (int)Math.ceil((maxX - minX) / cellSize));
this.internalZones = new Zone[rows * cols +1];
this.index2Links = getIndexToLink(network);

if(filterByNetwork) {
network.getLinks().values().forEach(l -> getOrCreateZone(l.getToNode().getCoord()));
Expand Down Expand Up @@ -126,12 +129,11 @@ private Optional<Zone> getOrCreateZone(Coord coord) {
if(zoneFilter.test(zone)) {
internalZones[index] = zone;
zones.put(zone.getId(), zone);

for (Link link : network.getLinks().values()) {
if (getIndex(link.getToNode().getCoord()) == index) {
List<Link> links = zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId -> new ArrayList<>());
links.add(link);
}
List<Link> linkList = zoneToLinksMap.computeIfAbsent(zone.getId(), zoneId -> new ArrayList<>());
List<Link> links = index2Links.get(index);
if(links!=null)
{
linkList.addAll(links);
}
} else {
return Optional.empty();
Expand All @@ -140,6 +142,11 @@ private Optional<Zone> getOrCreateZone(Coord coord) {
return Optional.of(zone);
}

private Map<Integer, List<Link>> getIndexToLink(Network network) {
return network.getLinks().values().stream()
.collect(Collectors.groupingBy(link -> getIndex(link.getToNode().getCoord())));
}

private PreparedPolygon getGeometry(int r, int c) {
List<Coord> coords = new ArrayList<>();
coords.add(new Coord(minX + c * cellSize, minY + r * cellSize));
Expand Down

0 comments on commit 1f019b3

Please sign in to comment.