lphy.base.function.Sort Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lphy-base Show documentation
Show all versions of lphy-base Show documentation
The standard library of LPhy, which contains the required generative distributions and basic functions.
The newest version!
package lphy.base.function;
import lphy.base.ParameterNames;
import lphy.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.GeneratorInfo;
import lphy.core.model.annotation.ParameterInfo;
import java.util.Arrays;
import java.util.Collections;
public class Sort extends DeterministicFunction {
private final String sortByParamName = ParameterNames.DecreasingParamName;
public Sort(@ParameterInfo(name = ParameterNames.ArrayParamName, description = "1d-array to sort.") Value x,
@ParameterInfo(name = sortByParamName,
description = "sort the array by increasing (as default) or decreasing.",
optional = true) Value decreasing) {
if (x == null) throw new IllegalArgumentException("The array can't be null!");
T[] value = x.value();
if (value == null || value.length < 1)
throw new IllegalArgumentException("Must have at least 1 element in the array!");
setParam(ParameterNames.ArrayParamName, x);
if (decreasing != null)
setParam(sortByParamName, decreasing);
}
@GeneratorInfo(name = "sort", description = "The sort function sorts an array " +
"by increasing (as default) or decreasing order.")
public Value apply() {
T[] arr = (T[]) getParams().get(ParameterNames.ArrayParamName).value();
Value decrVal = getParams().get(sortByParamName);
if (decrVal != null && decrVal.value())
Arrays.sort(arr, Collections.reverseOrder());
else
Arrays.sort(arr);
return new Value<>( null, arr, this);
}
}