001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.util;
018
019import java.util.Comparator;
020
021/**
022 * A Pair of a primitive int and a primitive double.
023 */
024public final class IntDoublePair {
025
026    public final int index;
027    public final double value;
028
029    public IntDoublePair(int index, double value) {
030        this.index = index;
031        this.value = value;
032    }
033
034    /**
035     * Compare pairs by index.
036     * @return Comparator over indices.
037     */
038    public static Comparator<IntDoublePair> pairIndexComparator() {
039        return Comparator.comparingInt(a -> a.index);
040    }
041
042    /**
043     * Compare pairs by value. Ascending order.
044     * @return Comparator over absolute values.
045     */
046    public static Comparator<IntDoublePair> pairValueComparator() {
047        return Comparator.comparingDouble(a -> Math.abs(a.value));
048    }
049
050    /**
051     * Compare pairs by value. Descending order.
052     * @return Comparator over absolute values.
053     */
054    public static Comparator<IntDoublePair> pairDescendingValueComparator() {
055        return (IntDoublePair a, IntDoublePair b) -> Double.compare(Math.abs(b.value), Math.abs(a.value));
056    }
057
058    @Override
059    public String toString() {
060        return "(" + index + "," + value + ")";
061    }
062}