Commit a98ffe98 authored by Sebastian Vollbrecht's avatar Sebastian Vollbrecht

Removed obsolete MathUtils class.

The remaining method has been moved to the JavaUtils class.
parent 14582279
......@@ -21,7 +21,6 @@ import graphgen.datastructures.SeededRandom;
import graphgen.generator.GraphGenerator;
import graphgen.graph.LayerStructure;
import graphgen.util.JavaUtils;
import graphgen.util.MathUtils;
import java.util.ArrayList;
import java.util.List;
......@@ -89,7 +88,7 @@ public class RandomShapeLayerCreator extends LayerCreator {
}
while (!possibleIDs.isEmpty()) {
int layer = MathUtils.pickBounded(0, height - 1, rng);
int layer = JavaUtils.pickBounded(0, height - 1, rng);
builder.addNode(layer, JavaUtils.removeRandomElement(possibleIDs, rng));
}
......@@ -102,7 +101,7 @@ public class RandomShapeLayerCreator extends LayerCreator {
@Override
protected int getTargetHeight() {
return MathUtils.pickBounded(1, n / minCountPerLayer, rng);
return JavaUtils.pickBounded(1, n / minCountPerLayer, rng);
}
@Override
......
......@@ -223,6 +223,25 @@ public class JavaUtils {
}
}
/**
* Randomly picks a number from the inclusive [min, max] range.
*
* @param min the minimum possible value
* @param max the maximum possible value
* @param rng the source of randomness
* @return a number <i>n</i> with min &lt;= <i>n</i> &lt;= max
* @throws IllegalArgumentException if min is greater than max
*/
public static int pickBounded(int min, int max, SeededRandom rng) {
if (min > max) {
throw new IllegalArgumentException(
"Min value must be less than or equal to the max value.\nMin: " + min + "\nMax: " + max);
}
return rng.nextInt(max - min + 1) + min;
}
private JavaUtils() {
}
......
/*
* Copyright 2018 Sebastian Vollbrecht, Julian Oppermann
* Embedded Systems and Applications Group, TU Darmstadt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package graphgen.util;
import graphgen.datastructures.SeededRandom;
import java.util.Collection;
/**
* A class containing useful methods related to simple mathematics.
*
* @author Sebastian Vollbrecht
*/
public class MathUtils {
/**
* Calculates the sum of the given collection of integers.
*
* @param c the collection to sum
* @return the sum of its elements
*/
public static int sumInteger(Collection<Integer> c) {
return c.stream().mapToInt(Integer::intValue).sum();
}
/**
* Randomly picks a number from the inclusive [min, max] range.
*
* @param min the minimum possible value
* @param max the maximum possible value
* @param rng the source of randomness
* @return a number <i>n</i> with min &lt;= <i>n</i> &lt;= max
* @throws IllegalArgumentException if min is greater than max
*/
public static int pickBounded(int min, int max, SeededRandom rng) {
if (min > max)
throw new IllegalArgumentException(
"Min value must be less than or equal to the max value.\nMin: " + min + "\nMax: " + max);
return rng.nextInt(max - min + 1) + min;
}
private MathUtils() {
}
}
......@@ -27,7 +27,7 @@ import java.util.stream.IntStream;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class MathUtilsTest {
public class JavaUtilsTest {
@Test
public void testPickBounded() {
......@@ -51,7 +51,7 @@ public class MathUtilsTest {
SeededRandom rng = new SeededRandom(42);
for (int n = 0; n <= maxPicks && !numbersLeft.isEmpty(); n++) {
int picked = MathUtils.pickBounded(min, max, rng);
int picked = JavaUtils.pickBounded(min, max, rng);
assertTrue(min <= picked && picked <= max);
numbersLeft.remove(picked);
......@@ -60,5 +60,4 @@ public class MathUtilsTest {
if (!numbersLeft.isEmpty())
fail("Numbers that have not been picked: " + numbersLeft);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment