All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unit16.z.math.OnToDoubleFunction Maven / Gradle / Ivy

package com.unit16.z.math;

import java.util.function.ToDoubleFunction;

import com.unit16.z.indexed.Indexed;

public final class OnToDoubleFunction
{
    private final ToDoubleFunction td;
    public OnToDoubleFunction(ToDoubleFunction td_) { td = td_; }
    
    public final double[] from(Indexed idx)
    {
        final double[] r = new double[idx.size()];
        into(idx, r);
        return r;
    }
    
    public void into(Indexed idx, double[] dst)
    {
        final int n = idx.size();
        for (int i = 0; i < n; i++) { dst[i] = td.applyAsDouble(idx.get(i)); }
    }
    
    public double min(Iterable src)
    {
        double m = Double.MAX_VALUE;
        for (B b : src) { m = Math.min(m, td.applyAsDouble(b)); }
        return m;
    }
    
    public double max(Iterable src)
    {
        double m = Double.MIN_VALUE;
        for (B b : src) { m = Math.max(m, td.applyAsDouble(b)); }
        return m;
    }
}