Skip to content

Commit d12ef8a

Browse files
committed
Allow repeated keys in RandomCollection (#2624)
1 parent 28bd065 commit d12ef8a

File tree

5 files changed

+70
-101
lines changed

5 files changed

+70
-101
lines changed

worldedit-core/src/main/java/com/fastasyncworldedit/core/extent/transform/RandomTransform.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
88
import com.sk89q.worldedit.extent.Extent;
99

10-
import java.util.HashMap;
11-
import java.util.LinkedHashSet;
12-
import java.util.Map;
10+
import java.util.ArrayList;
11+
import java.util.List;
1312
import java.util.Set;
13+
import java.util.stream.Collectors;
1414

1515
import static com.google.common.base.Preconditions.checkNotNull;
1616

@@ -20,10 +20,9 @@
2020
public class RandomTransform extends SelectTransform {
2121

2222
private final SimpleRandom random;
23-
private final Map<ResettableExtent, Double> weights = new HashMap<>();
23+
private final List<RandomCollection.Weighted<ResettableExtent>> weights;
2424

2525
private transient RandomCollection<ResettableExtent> collection;
26-
private transient LinkedHashSet<ResettableExtent> extents = new LinkedHashSet<>();
2726

2827
public RandomTransform() {
2928
this(new TrueRandom());
@@ -36,27 +35,27 @@ public RandomTransform() {
3635
*/
3736
public RandomTransform(SimpleRandom random) {
3837
this.random = random;
38+
this.weights = new ArrayList<>();
3939
}
4040

4141
@Override
4242
public AbstractDelegateExtent getExtent(int x, int y, int z) {
43-
return collection.next(x, y, z);
43+
return collection.next(this.random, x, y, z);
4444
}
4545

4646
@Override
4747
public AbstractDelegateExtent getExtent(int x, int z) {
48-
return collection.next(x, 0, z);
48+
return collection.next(this.random, x, 0, z);
4949
}
5050

5151
@Override
5252
public ResettableExtent setExtent(Extent extent) {
5353
if (collection == null) {
54-
collection = RandomCollection.of(weights, random);
55-
extents = new LinkedHashSet<>(weights.keySet());
54+
collection = RandomCollection.of(weights);
5655
}
5756
super.setExtent(extent);
58-
for (ResettableExtent current : extents) {
59-
current.setExtent(extent);
57+
for (RandomCollection.Weighted<ResettableExtent> current : this.weights) {
58+
current.value().setExtent(extent);
6059
}
6160
return this;
6261
}
@@ -72,13 +71,12 @@ public ResettableExtent setExtent(Extent extent) {
7271
*/
7372
public void add(ResettableExtent extent, double chance) {
7473
checkNotNull(extent);
75-
weights.put(extent, chance);
76-
collection = RandomCollection.of(weights, random);
77-
this.extents.add(extent);
74+
weights.add(new RandomCollection.Weighted<>(extent, chance));
75+
collection = RandomCollection.of(weights);
7876
}
7977

8078
public Set<ResettableExtent> getExtents() {
81-
return extents;
79+
return this.weights.stream().map(RandomCollection.Weighted::value).collect(Collectors.toSet());
8280
}
8381

8482
public RandomCollection<ResettableExtent> getCollection() {

worldedit-core/src/main/java/com/fastasyncworldedit/core/util/collection/FastRandomCollection.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import com.fastasyncworldedit.core.util.MathMan;
55

66
import java.util.ArrayList;
7-
import java.util.Map;
7+
import java.util.List;
88
import java.util.Optional;
99

10-
public class FastRandomCollection<T> extends RandomCollection<T> {
10+
public final class FastRandomCollection<T> implements RandomCollection<T> {
1111

1212
private final T[] values;
1313

14-
private FastRandomCollection(T[] values, SimpleRandom random) {
15-
super(random);
14+
private FastRandomCollection(T[] values) {
1615
this.values = values;
1716
}
1817

@@ -22,16 +21,15 @@ private FastRandomCollection(T[] values, SimpleRandom random) {
2221
* {@code Optional} in any case.
2322
*
2423
* @param weights the weight of the values.
25-
* @param random the random generator to use for this collection.
2624
* @param <T> the value type.
2725
* @return an {@link Optional} containing the new collection if it could be created, {@link
2826
* Optional#empty()} otherwise.
2927
* @see RandomCollection for API usage.
3028
*/
31-
public static <T> Optional<RandomCollection<T>> create(Map<T, Double> weights, SimpleRandom random) {
29+
public static <T> Optional<RandomCollection<T>> create(List<Weighted<T>> weights) {
3230
int max = 0;
3331
int[] counts = new int[weights.size()];
34-
Double[] weightDoubles = weights.values().toArray(new Double[0]);
32+
double[] weightDoubles = weights.stream().mapToDouble(Weighted::weight).toArray();
3533
for (int i = 0; i < weightDoubles.length; i++) {
3634
int weight = (int) (weightDoubles[i] * 100);
3735
counts[i] = weight;
@@ -47,21 +45,21 @@ public static <T> Optional<RandomCollection<T>> create(Map<T, Double> weights, S
4745
return Optional.empty();
4846
}
4947
ArrayList<T> parsed = new ArrayList<>();
50-
for (Map.Entry<T, Double> entry : weights.entrySet()) {
51-
int num = (int) (100 * entry.getValue());
48+
for (Weighted<T> entry : weights) {
49+
int num = (int) (100 * entry.weight());
5250
for (int j = 0; j < num / gcd; j++) {
53-
parsed.add(entry.getKey());
51+
parsed.add(entry.value());
5452
}
5553
}
5654
@SuppressWarnings("unchecked")
5755
T[] values = (T[]) parsed.toArray();
58-
FastRandomCollection<T> fastRandomCollection = new FastRandomCollection<>(values, random);
56+
FastRandomCollection<T> fastRandomCollection = new FastRandomCollection<>(values);
5957
return Optional.of(fastRandomCollection);
6058
}
6159

6260
@Override
63-
public T next(int x, int y, int z) {
64-
return values[getRandom().nextInt(x, y, z, values.length)];
61+
public T next(final SimpleRandom random, int x, int y, int z) {
62+
return values[random.nextInt(x, y, z, values.length)];
6563
}
6664

6765
}

worldedit-core/src/main/java/com/fastasyncworldedit/core/util/collection/RandomCollection.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,35 @@
22

33
import com.fastasyncworldedit.core.math.random.SimpleRandom;
44

5-
import java.util.Map;
5+
import java.util.List;
66

77
import static com.google.common.base.Preconditions.checkNotNull;
88

99
/**
1010
* A RandomCollection holds multiple values that can be accessed by using
11-
* {@link RandomCollection#next(int, int, int)}. The returned value is
11+
* {@link RandomCollection#next(SimpleRandom, int, int, int)}. The returned value is
1212
* determined by a given {@link SimpleRandom} implementation.
1313
*
1414
* @param <T> the type of values the collection holds.
1515
*/
16-
public abstract class RandomCollection<T> {
17-
18-
private SimpleRandom random;
19-
20-
protected RandomCollection(SimpleRandom random) {
21-
this.random = random;
22-
}
16+
public sealed interface RandomCollection<T> permits FastRandomCollection, SimpleRandomCollection {
2317

2418
/**
2519
* Return a new RandomCollection. The implementation may differ depending on the
2620
* given arguments but there is no need to differ.
2721
*
2822
* @param weights the weighted map.
29-
* @param random the random number generator.
3023
* @param <T> the type the collection holds.
3124
* @return a RandomCollection using the given weights and the RNG.
3225
*/
33-
public static <T> RandomCollection<T> of(Map<T, Double> weights, SimpleRandom random) {
34-
checkNotNull(random);
35-
return FastRandomCollection.create(weights, random)
36-
.orElseGet(() -> new SimpleRandomCollection<>(weights, random));
26+
static <T> RandomCollection<T> of(List<Weighted<T>> weights) {
27+
return FastRandomCollection.create(weights)
28+
.orElseGet(() -> new SimpleRandomCollection<>(weights));
3729
}
3830

39-
public void setRandom(SimpleRandom random) {
40-
checkNotNull(random);
41-
this.random = random;
42-
}
31+
T next(SimpleRandom random, int x, int y, int z);
4332

44-
public SimpleRandom getRandom() {
45-
return random;
46-
}
47-
48-
public abstract T next(int x, int y, int z);
33+
record Weighted<T>(T value, double weight) {
4934

35+
}
5036
}

worldedit-core/src/main/java/com/fastasyncworldedit/core/util/collection/SimpleRandomCollection.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,39 @@
22

33
import com.fastasyncworldedit.core.math.random.SimpleRandom;
44

5-
import java.util.Map;
5+
import java.util.List;
66
import java.util.NavigableMap;
77
import java.util.TreeMap;
88

9-
public class SimpleRandomCollection<E> extends RandomCollection<E> {
9+
public final class SimpleRandomCollection<T> implements RandomCollection<T> {
1010

11-
private final NavigableMap<Double, E> map = new TreeMap<>();
12-
private double total = 0;
11+
private final NavigableMap<Double, T> map;
12+
private final double total;
1313

1414
/**
1515
* Create a {@link RandomCollection} from a weighted map and a RNG.
16-
* It is recommended to use {@link RandomCollection#of(Map, SimpleRandom)}
16+
* It is recommended to use {@link RandomCollection#of(List)}
1717
* instead of this constructor.
1818
*
1919
* @param weights the weighted map.
20-
* @param random the random number generator.
2120
*/
22-
public SimpleRandomCollection(Map<E, Double> weights, SimpleRandom random) {
23-
super(random);
24-
for (Map.Entry<E, Double> entry : weights.entrySet()) {
25-
add(entry.getValue(), entry.getKey());
21+
public SimpleRandomCollection(List<Weighted<T>> weights) {
22+
this.map = new TreeMap<>();
23+
double total = 0;
24+
for (Weighted<T> entry : weights) {
25+
final double weight = entry.weight();
26+
if (weight <= 0) {
27+
throw new IllegalArgumentException("Weights must be positive");
28+
}
29+
total += weight;
30+
this.map.put(total, entry.value());
2631
}
27-
}
28-
29-
public void add(double weight, E result) {
30-
if (weight <= 0) {
31-
return;
32-
}
33-
total += weight;
34-
map.put(total, result);
32+
this.total = total;
3533
}
3634

3735
@Override
38-
public E next(int x, int y, int z) {
39-
return map.ceilingEntry(getRandom().nextDouble(x, y, z, this.total)).getValue();
36+
public T next(final SimpleRandom random, int x, int y, int z) {
37+
return map.ceilingEntry(random.nextDouble(x, y, z, this.total)).getValue();
4038
}
4139

4240
}

worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/RandomPattern.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import com.sk89q.worldedit.math.BlockVector3;
2828
import com.sk89q.worldedit.world.block.BaseBlock;
2929

30-
import java.util.LinkedHashMap;
31-
import java.util.LinkedHashSet;
32-
import java.util.Map;
30+
import java.util.ArrayList;
31+
import java.util.List;
3332
import java.util.Set;
33+
import java.util.stream.Collectors;
3434

3535
import static com.google.common.base.Preconditions.checkNotNull;
3636

@@ -39,11 +39,10 @@
3939
*/
4040
public class RandomPattern extends AbstractPattern {
4141

42-
//FAWE start - SimpleRandom > Random, LHS<P> > List
42+
//FAWE start - SimpleRandom > Random, RandomCollection
4343
private final SimpleRandom random;
44-
private Map<Pattern, Double> weights = new LinkedHashMap<>();
44+
private final List<RandomCollection.Weighted<Pattern>> weights;
4545
private RandomCollection<Pattern> collection;
46-
private LinkedHashSet<Pattern> patterns = new LinkedHashSet<>();
4746
//FAWE end
4847

4948
//FAWE start
@@ -53,6 +52,7 @@ public RandomPattern() {
5352

5453
public RandomPattern(SimpleRandom random) {
5554
this.random = random;
55+
this.weights = new ArrayList<>();
5656
}
5757

5858
/**
@@ -63,16 +63,10 @@ public RandomPattern(SimpleRandom random) {
6363
*/
6464
public RandomPattern(SimpleRandom random, RandomPattern parent) {
6565
this.random = random;
66-
this.weights = parent.weights;
67-
this.collection = RandomCollection.of(weights, random);
68-
this.patterns = parent.patterns;
69-
}
70-
71-
private RandomPattern(SimpleRandom random, Map<Pattern, Double> weights) {
72-
this.random = random;
73-
this.weights = weights;
74-
this.collection = RandomCollection.of(weights, random);
75-
this.patterns = new LinkedHashSet<>(weights.keySet());
66+
this.weights = parent.weights.stream()
67+
.map(weighted -> new RandomCollection.Weighted<>(weighted.value().fork(), weighted.weight()))
68+
.collect(Collectors.toCollection(ArrayList::new));
69+
this.collection = RandomCollection.of(weights);
7670
}
7771
//FAWE end
7872

@@ -87,18 +81,15 @@ private RandomPattern(SimpleRandom random, Map<Pattern, Double> weights) {
8781
*/
8882
public void add(Pattern pattern, double chance) {
8983
checkNotNull(pattern);
90-
//FAWE start - Double, weights, patterns and collection
91-
Double existingWeight = weights.get(pattern);
92-
if (existingWeight != null) {
93-
chance += existingWeight;
94-
}
95-
weights.put(pattern, chance);
96-
collection = RandomCollection.of(weights, random);
97-
this.patterns.add(pattern);
84+
//FAWE start - Double, weights, repeating patterns, and collection
85+
this.weights.add(new RandomCollection.Weighted<>(pattern, chance));
86+
this.collection = RandomCollection.of(weights);
9887
}
9988

10089
public Set<Pattern> getPatterns() {
101-
return patterns;
90+
return this.weights.stream()
91+
.map(RandomCollection.Weighted::value)
92+
.collect(Collectors.toSet());
10293
}
10394

10495
public RandomCollection<Pattern> getCollection() {
@@ -107,19 +98,17 @@ public RandomCollection<Pattern> getCollection() {
10798

10899
@Override
109100
public BaseBlock applyBlock(BlockVector3 position) {
110-
return collection.next(position.x(), position.y(), position.z()).applyBlock(position);
101+
return collection.next(this.random, position.x(), position.y(), position.z()).applyBlock(position);
111102
}
112103

113104
@Override
114105
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
115-
return collection.next(get.x(), get.y(), get.z()).apply(extent, get, set);
106+
return collection.next(this.random, get.x(), get.y(), get.z()).apply(extent, get, set);
116107
}
117108

118109
@Override
119110
public Pattern fork() {
120-
final LinkedHashMap<Pattern, Double> newWeights = new LinkedHashMap<>();
121-
this.weights.forEach((p, w) -> newWeights.put(p.fork(), w));
122-
return new RandomPattern(this.random, newWeights);
111+
return new RandomPattern(this.random, this);
123112
}
124113

125114
//FAWE end

0 commit comments

Comments
 (0)