Added new method in RandomUtil + Added AABBBlockGroup to ease manipulation of collection of AABBBlocks
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package fr.pandacube.lib.core.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class IteratorIterator<T> implements Iterator<T> {
|
||||
|
||||
public static <T> IteratorIterator<T> ofCollectionOfIterable(Collection<Iterable<T>> coll) {
|
||||
return new IteratorIterator<>(coll.stream().map(i -> i.iterator()).iterator());
|
||||
}
|
||||
|
||||
public static <T> IteratorIterator<T> ofCollectionOfIterator(Collection<Iterator<T>> coll) {
|
||||
return new IteratorIterator<>(new ArrayList<>(coll).iterator());
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> IteratorIterator<T> ofArrayOfIterable(Iterable<T>... arr) {
|
||||
return new IteratorIterator<>(Arrays.stream(arr).map(i -> i.iterator()).iterator());
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> IteratorIterator<T> ofArrayOfIterator(Iterator<T>... arr) {
|
||||
return new IteratorIterator<>(Arrays.asList(arr).iterator());
|
||||
}
|
||||
|
||||
private Iterator<Iterator<T>> iterators;
|
||||
|
||||
private Iterator<T> currentIterator = null;
|
||||
|
||||
private IteratorIterator(Iterator<Iterator<T>> iterators) {
|
||||
this.iterators = iterators;
|
||||
}
|
||||
|
||||
private void fixCurrentIterator() {
|
||||
if (currentIterator != null && !currentIterator.hasNext()) {
|
||||
currentIterator = null;
|
||||
}
|
||||
}
|
||||
private void fixState() {
|
||||
fixCurrentIterator();
|
||||
while (currentIterator == null) {
|
||||
if (iterators.hasNext()) {
|
||||
currentIterator = iterators.next();
|
||||
fixCurrentIterator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
fixState();
|
||||
return currentIterator != null && currentIterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (!hasNext())
|
||||
throw new NoSuchElementException("No next value found in iterator.");
|
||||
return currentIterator.next();
|
||||
}
|
||||
|
||||
}
|
@@ -42,5 +42,31 @@ public class RandomUtil {
|
||||
}
|
||||
throw new RuntimeException("Should never go to this line of code");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a value between 0 and the number of parameter minus 1, using the provided frequencies.
|
||||
*
|
||||
* The probability of each value to be returned depends of the frequencies provided.
|
||||
* @param frequencies the frequencies of each entries
|
||||
* @return the index of an entry, or -1 if it is unable to pick anything (all the frequencies are 0 or there is not provided frequency)
|
||||
*/
|
||||
public static int randomIndexOfFrequencies(double... frequencies) {
|
||||
if (frequencies == null)
|
||||
return -1;
|
||||
double sum = 0;
|
||||
for (double f : frequencies)
|
||||
sum += f;
|
||||
if (sum == 0)
|
||||
return -1;
|
||||
double r = rand.nextDouble() * sum;
|
||||
int i = -1;
|
||||
double limit = frequencies[++i];
|
||||
while (i < frequencies.length) {
|
||||
if (r < limit)
|
||||
return i;
|
||||
limit += frequencies[++i];
|
||||
}
|
||||
return frequencies.length - 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user