/*
 * The MIT License
 *
 * Copyright © 2014 Filip Plesinger, Juraj Jurco.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * Authors: 
 * 
 * Filip Plesinger <fplesinger@isibrno.cz>
 * Institute of Scientific Instruments of the ASCR, v. v. i.
 * Brno, Czech Republic
 * 
 * Juraj Jurco <xjuraj@isibrno.cz>
 * Institute of Scientific Instruments of the ASCR, v. v. i.
 * Brno, Czech Republic
 */

package cz.academyofscience.upt.challenge2014;

/**
 *
 * @author xjuraj
 */
public class Utils {
    
    public static class Pair<K,V>{
        K key;
        V value;

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }

        
    }
    
    /**
     * get minimal value from the array with reference to index where that value is.
     * @param array array where to find minimal value
     * @return Pair<Minimal value, Index in array> with minimal value and its index
     */
    public static Pair<Integer, Double> min(double[] array){
        Pair<Integer, Double> min = new Pair<>();
        min.setKey(-1);
        min.setValue(Double.MAX_VALUE);
        for (int i = 0; i < array.length; i++) {
            if(array[i]<min.getValue()){
                min.setValue(array[i]);
                min.setKey(i);
            }
        }
        return min;
    }
    
    /**
     * get minimal value from the array with reference to index where that value is.
     * @param array array where to find minimal value
     * @return Pair<Minimal value, Index in array> with minimal value and its index
     */
    public static Pair<Integer, Double> max(double[] array){
        Pair<Integer, Double> max = new Pair<>();
        max.setValue(Double.MIN_VALUE);
        max.setKey(-1);
        for (int i = 0; i < array.length; i++) {
            if(array[i]>max.getValue()){
                max.setValue(array[i]);
                max.setKey(i);
            }
        }
        return max;
    }
    
    
    /**
     * get average value.
     * @param array array from which compute mean value
     * @return average value of the array
     */
    public static double avg(double[] array){
        double result = 0.0;
        for (int i = 0; i < array.length; i++) {
            result += array[i];
        }
        return result/array.length;
    }
    
}
