Skip to content

Commit 87dfbf3

Browse files
author
Nathan Merrill
committed
Changed Neighborhoods to return an immutable set, and map neighbors to return an UnsortedSetIterable.
1 parent 2da74e3 commit 87dfbf3

File tree

12 files changed

+45
-38
lines changed

12 files changed

+45
-38
lines changed

src/main/java/com/nmerrill/kothcomm/game/maps/generators/mazes/Ellers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ private void connectSets(){
7070
larger = currentSet;
7171
}
7272
larger.addAll(smaller);
73-
smaller.forEach(i -> currentLine.set(i, adjacentSet));
73+
smaller.forEach(i -> currentLine.set(i, larger));
74+
7475
}
7576
}
7677

src/main/java/com/nmerrill/kothcomm/game/maps/generators/mazes/GrowingTree.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.nmerrill.kothcomm.game.maps.generators.mazes;
22

3-
import com.nmerrill.kothcomm.game.maps.generators.Generator;
43
import com.nmerrill.kothcomm.game.maps.MapPoint;
4+
import com.nmerrill.kothcomm.game.maps.generators.Generator;
55
import com.nmerrill.kothcomm.game.maps.graphmaps.GraphGraphMap;
66
import com.nmerrill.kothcomm.utils.iterables.Itertools;
77
import org.eclipse.collections.api.list.MutableList;
@@ -21,20 +21,20 @@ public static int selectOldest(MutableList<? extends MapPoint> points){
2121
return 0;
2222
}
2323
public static int selectNewest(MutableList<? extends MapPoint> points){
24-
return points.size()-1;
25-
}
26-
public static <U extends MapPoint> ToIntFunction<MutableList<U>> randomSelection(){
27-
return randomSelection(new Random());
28-
}
29-
public static <U extends MapPoint> ToIntFunction<MutableList<U>> randomSelection(Random random){
30-
return (MutableList<U> list) -> random.nextInt(list.size());
31-
}
24+
return points.size()-1;
25+
}
26+
public static <U extends MapPoint> ToIntFunction<MutableList<U>> randomSelection(){
27+
return randomSelection(new Random());
28+
}
29+
public static <U extends MapPoint> ToIntFunction<MutableList<U>> randomSelection(Random random){
30+
return (MutableList<U> list) -> random.nextInt(list.size());
31+
}
3232

33-
private final ToIntFunction<MutableList<U>> selectionMethod;
34-
private final Random random;
33+
private final ToIntFunction<MutableList<U>> selectionMethod;
34+
private final Random random;
3535
public GrowingTree(ToIntFunction<MutableList<U>> selectionMethod, Random random){
36-
this.selectionMethod = selectionMethod;
37-
this.random = random;
36+
this.selectionMethod = selectionMethod;
37+
this.random = random;
3838
}
3939
public GrowingTree(ToIntFunction<MutableList<U>> selectionMethod){
4040
this(selectionMethod, new Random());
@@ -55,6 +55,7 @@ public void generate(GraphGraphMap<U, ?> map) {
5555
MutableList<U> visited = Lists.mutable.with();
5656
MutableSet<Twin<U>> connections = Sets.mutable.empty();
5757
visited.add(start);
58+
5859

5960
while (!borders.isEmpty()){
6061
U point = borders.remove(selectionMethod.applyAsInt(borders));

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/BijectiveGraphMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.nmerrill.kothcomm.game.maps.MapPoint;
66
import org.eclipse.collections.api.list.MutableList;
77
import org.eclipse.collections.api.set.MutableSet;
8+
import org.eclipse.collections.api.set.UnsortedSetIterable;
89

910

1011
public class BijectiveGraphMap<U extends MapPoint, T, V extends MapPoint, W>
@@ -22,7 +23,7 @@ public boolean isEmpty(U point) {
2223
}
2324

2425
@Override
25-
public MutableSet<U> getNeighbors(U origin) {
26+
public UnsortedSetIterable<U> getNeighbors(U origin) {
2627
return map.getNeighbors(key.to(origin)).collect(key::from);
2728
}
2829

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/GraphMap.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.eclipse.collections.api.list.MutableList;
1010
import org.eclipse.collections.api.map.MutableMap;
1111
import org.eclipse.collections.api.set.MutableSet;
12+
import org.eclipse.collections.api.set.UnsortedSetIterable;
1213
import org.eclipse.collections.impl.factory.Lists;
1314
import org.eclipse.collections.impl.factory.Maps;
1415
import org.eclipse.collections.impl.factory.Sets;
@@ -17,9 +18,9 @@
1718

1819
public interface GraphMap<U extends MapPoint, T> extends GameMap<U, T>, Iterable<U> {
1920

20-
MutableSet<U> getNeighbors(U origin);
21+
UnsortedSetIterable<U> getNeighbors(U origin);
2122

22-
static <U> MutableSet<U> getNeighbors(U origin, int maxDistance, Function<U, ? extends Collection<U>> neighborFunction){
23+
static <U> MutableSet<U> getNeighbors(U origin, int maxDistance, Function<U, ? extends Iterable<U>> neighborFunction){
2324
MutableSet<U> points = Sets.mutable.empty();
2425
points.add(origin);
2526
MutableSet<U> borders = points.clone();
@@ -34,7 +35,7 @@ static <U> MutableSet<U> getNeighbors(U origin, int maxDistance, Function<U, ? e
3435
return points;
3536
}
3637

37-
default MutableSet<U> getNeighbors(U origin, int maxDistance){
38+
default UnsortedSetIterable<U> getNeighbors(U origin, int maxDistance){
3839
return getNeighbors(origin, maxDistance, this::getNeighbors);
3940
}
4041

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/GraphMapView.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.collections.api.list.MutableList;
88
import org.eclipse.collections.api.map.MutableMap;
99
import org.eclipse.collections.api.set.MutableSet;
10+
import org.eclipse.collections.api.set.UnsortedSetIterable;
1011
import org.eclipse.collections.api.tuple.Pair;
1112
import org.eclipse.collections.impl.factory.Lists;
1213

@@ -23,12 +24,12 @@ public GraphMapView(GraphMap<U, T> map, Region<U> region){
2324
this.map = map;
2425
}
2526

26-
public MutableSet<U> getNeighbors(U origin){
27+
public UnsortedSetIterable<U> getNeighbors(U origin){
2728
region.checkBounds(origin);
2829
return map.getNeighbors(origin).select(region::inBounds);
2930
}
3031

31-
public MutableSet<U> getNeighbors(U origin, int maxDistance){
32+
public UnsortedSetIterable<U> getNeighbors(U origin, int maxDistance){
3233
region.checkBounds(origin);
3334
return map.getNeighbors(origin, maxDistance).select(region::inBounds);
3435
}

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/NeighborhoodGraphMap.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.nmerrill.kothcomm.game.maps.graphmaps;
22

3-
import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.Region;
43
import com.nmerrill.kothcomm.game.maps.MapPoint;
4+
import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.Region;
55
import com.nmerrill.kothcomm.game.maps.graphmaps.neighborhoods.Neighborhood;
66
import org.eclipse.collections.api.list.MutableList;
77
import org.eclipse.collections.api.map.MutableMap;
8+
import org.eclipse.collections.api.set.ImmutableSet;
89
import org.eclipse.collections.api.set.MutableSet;
910
import org.eclipse.collections.impl.factory.Maps;
1011

@@ -29,7 +30,7 @@ public boolean isEmpty(U point) {
2930
}
3031

3132
@Override
32-
public MutableSet<U> getNeighbors(U origin) {
33+
public ImmutableSet<U> getNeighbors(U origin) {
3334
region.checkBounds(origin);
3435
return neighborhood.getAdjacencies(origin).select(region::inBounds);
3536
}

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/ReadonlyGraphMap.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.nmerrill.kothcomm.game.maps.graphmaps.bounds.Region;
77
import org.eclipse.collections.api.list.MutableList;
88
import org.eclipse.collections.api.set.MutableSet;
9+
import org.eclipse.collections.api.set.UnsortedSetIterable;
910

1011
public class ReadonlyGraphMap<U extends MapPoint, T> extends ReadonlyGameMap<U, T>{
1112
private final GraphMap<U, T> map;
@@ -14,11 +15,11 @@ public ReadonlyGraphMap(GraphMap<U, T> map){
1415
this.map = map;
1516
}
1617

17-
public MutableSet<U> getNeighbors(U origin){
18+
public UnsortedSetIterable<U> getNeighbors(U origin){
1819
return map.getNeighbors(origin);
1920
}
2021

21-
public MutableSet<U> getNeighbors(U origin, int maxDistance){
22+
public UnsortedSetIterable<U> getNeighbors(U origin, int maxDistance){
2223
return map.getNeighbors(origin, maxDistance);
2324
}
2425

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/neighborhoods/HexagonalNeighborhood.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.nmerrill.kothcomm.game.maps.graphmaps.neighborhoods;
22

33
import com.nmerrill.kothcomm.game.maps.Point2D;
4-
import org.eclipse.collections.api.set.MutableSet;
4+
import org.eclipse.collections.api.set.ImmutableSet;
55
import org.eclipse.collections.impl.factory.Sets;
66

77
public final class HexagonalNeighborhood implements Neighborhood<Point2D> {
88

99
@Override
10-
public MutableSet<Point2D> getAdjacencies(Point2D point) {
11-
return Sets.mutable.of(
10+
public ImmutableSet<Point2D> getAdjacencies(Point2D point) {
11+
return Sets.immutable.of(
1212
point.move(-1, -1),
1313
point.move(-1, 0),
1414
point.move(0, -1),

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/neighborhoods/MooreNeighborhood.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.nmerrill.kothcomm.game.maps.graphmaps.neighborhoods;
22

33
import com.nmerrill.kothcomm.game.maps.Point2D;
4-
import org.eclipse.collections.api.set.MutableSet;
4+
import org.eclipse.collections.api.set.ImmutableSet;
55
import org.eclipse.collections.impl.factory.Sets;
66

77
public final class MooreNeighborhood implements Neighborhood<Point2D> {
88
@Override
9-
public MutableSet<Point2D> getAdjacencies(Point2D point) {
10-
return Sets.mutable.of(
9+
public ImmutableSet<Point2D> getAdjacencies(Point2D point) {
10+
return Sets.immutable.of(
1111
point.move(-1, -1),
1212
point.move(-1, 0),
1313
point.move(-1, 1),
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.nmerrill.kothcomm.game.maps.graphmaps.neighborhoods;
22

33
import com.nmerrill.kothcomm.game.maps.MapPoint;
4-
import org.eclipse.collections.api.set.MutableSet;
4+
import org.eclipse.collections.api.set.ImmutableSet;
55

66

77
public interface Neighborhood<U extends MapPoint> {
8-
MutableSet<U> getAdjacencies(U point);
8+
ImmutableSet<U> getAdjacencies(U point);
99
}

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/neighborhoods/TriangleNeighborhood.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.nmerrill.kothcomm.game.maps.graphmaps.neighborhoods;
22

33
import com.nmerrill.kothcomm.game.maps.Point2D;
4-
import org.eclipse.collections.api.set.MutableSet;
4+
import org.eclipse.collections.api.set.ImmutableSet;
55
import org.eclipse.collections.impl.factory.Sets;
66

77

88
public final class TriangleNeighborhood implements Neighborhood<Point2D> {
99
@Override
10-
public MutableSet<Point2D> getAdjacencies(Point2D point) {
10+
public ImmutableSet<Point2D> getAdjacencies(Point2D point) {
1111
boolean pointUp = (point.getX()+point.getY())%2 == 0;
12-
return Sets.mutable.of(
12+
return Sets.immutable.of(
1313
point.move(-1, 0),
1414
point.move(0, pointUp?-1:1),
1515
point.move(1, 0)

src/main/java/com/nmerrill/kothcomm/game/maps/graphmaps/neighborhoods/VonNeumannNeighborhood.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.nmerrill.kothcomm.game.maps.graphmaps.neighborhoods;
22

33
import com.nmerrill.kothcomm.game.maps.Point2D;
4-
import org.eclipse.collections.api.set.MutableSet;
4+
import org.eclipse.collections.api.set.ImmutableSet;
55
import org.eclipse.collections.impl.factory.Sets;
66

77
public final class VonNeumannNeighborhood implements Neighborhood<Point2D> {
88

99
@Override
10-
public MutableSet<Point2D> getAdjacencies(Point2D point) {
11-
return Sets.mutable.of(
10+
public ImmutableSet<Point2D> getAdjacencies(Point2D point) {
11+
return Sets.immutable.of(
1212
point.move(-1, 0),
1313
point.move(0, -1),
1414
point.move(0, 1),

0 commit comments

Comments
 (0)