diff --git a/src/main/java/dev/osm/mapsplit/AbstractOsmMap.java b/src/main/java/dev/osm/mapsplit/AbstractOsmMap.java index 399985c..6308eaa 100644 --- a/src/main/java/dev/osm/mapsplit/AbstractOsmMap.java +++ b/src/main/java/dev/osm/mapsplit/AbstractOsmMap.java @@ -89,41 +89,49 @@ public List getAllTiles(long key) { int tx = tileX(value); int ty = tileY(value); - int neighbour = neighbour(value); List result; if ((value & TILE_EXT_MASK) != 0) { int idx = (int) (value & TILE_EXT_INDEX_MASK); - result = new ArrayList<>(asList(extendedSet[idx])); + result = asList(extendedSet[idx]); + result.add(TileCoord.encode(tx, ty)); } else { result = parseMarker(value); + result.add(TileCoord.encode(tx, ty)); + // add possible neighbours + int neighbour = neighbour(value); + if ((neighbour & OsmMap.NEIGHBOURS_EAST) != 0) { + addTileIfNotPresent(tx, ty, result); + } + if ((neighbour & OsmMap.NEIGHBOURS_SOUTH) != 0) { + addTileIfNotPresent(tx, ty + 1, result); + } + if ((neighbour & OsmMap.NEIGHBOURS_SOUTH_EAST) == OsmMap.NEIGHBOURS_SOUTH_EAST) { + addTileIfNotPresent(tx + 1, ty + 1, result); + } } + return result; + } - // TODO: some tiles (neighbour-tiles) might be double-included in the list, is this a problem?! - - // add the tile (and possible neighbours) - result.add(TileCoord.encode(tx, ty)); - if ((neighbour & OsmMap.NEIGHBOURS_EAST) != 0) { - result.add(TileCoord.encode(tx + 1, ty)); - } - if ((neighbour & OsmMap.NEIGHBOURS_SOUTH) != 0) { - result.add(TileCoord.encode(tx, ty + 1)); - } - if ((neighbour & OsmMap.NEIGHBOURS_SOUTH_EAST) == OsmMap.NEIGHBOURS_SOUTH_EAST) { - result.add(TileCoord.encode(tx + 1, ty + 1)); + /** + * Add an encoded tile to result if not already present + * + * @param tx tile x coord + * @param ty tile y coord + * @param result a List holding the tiles + */ + public void addTileIfNotPresent(int tx, int ty, @NotNull List result) { + int t = TileCoord.encode(tx + 1, ty); + if (!result.contains(t)) { + result.add(t); } - - return result; } @Override public void updateInt(long key, Collection tiles) { - List longTiles = tiles.stream().map(tile -> createValue(TileCoord.decodeX(tile), TileCoord.decodeY(tile), NEIGHBOURS_NONE)).collect(toList()); - this.update(key, longTiles); - } /** @@ -361,12 +369,9 @@ private static int[] merge(@NotNull int[] old, @NotNull int[] add, int len) { @NotNull private static List asList(@NotNull int[] set) { List result = new ArrayList<>(); - for (int i = 0; i < set.length; i++) { result.add(set[i]); } - return result; } - }