private void addToIslands(Map<Coordinate, Island> coordinateMapper, Coordinate c) { int diff = -1; VariantGraph.Vertex neighbor = null; try { neighbor = vertexAt(c.row + diff, c.column + diff); } catch (IndexOutOfBoundsException e) { // ignored } if (neighbor != null) { Coordinate neighborCoordinate = new Coordinate(c.row + diff, c.column + diff, new Match(neighbor, null)); Island island = coordinateMapper.get(neighborCoordinate); if (island == null) { // LOG.debug("new island"); Island island0 = new Island(); island0.add(neighborCoordinate); island0.add(c); coordinateMapper.put(neighborCoordinate, island0); coordinateMapper.put(c, island0); } else { // LOG.debug("add to existing island"); island.add(c); coordinateMapper.put(c, island); } } }
// Since the coordinates in allMatches are ordered from upper left to lower right, // we don't need to check the lower right neighbor. @Override public Set<Island> getIslands() { Map<Coordinate, Island> coordinateMapper = new HashMap<>(); List<Coordinate> allMatches = allMatches(); for (Coordinate c : allMatches) { // LOG.debug("coordinate {}", c); addToIslands(coordinateMapper, c); } Set<Coordinate> smallestIslandsCoordinates = new HashSet<>(allMatches); smallestIslandsCoordinates.removeAll(coordinateMapper.keySet()); for (Coordinate coordinate : smallestIslandsCoordinates) { Island island = new Island(); island.add(coordinate); coordinateMapper.put(coordinate, island); } return new HashSet<>(coordinateMapper.values()); }