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.math.la;
018
019import java.util.Objects;
020
021/**
022 * A mutable tuple used to avoid allocation when iterating a matrix.
023 * <p>
024 * While it does implement hashcode, don't use it as a key as the hashcode is data dependent.
025 */
026public class MatrixTuple {
027
028    public int i;
029    public int j;
030
031    public double value;
032
033    public MatrixTuple() {
034        this.i = -1;
035        this.j = -1;
036        this.value = Double.NaN;
037    }
038
039    public MatrixTuple(int i, int j, int value) {
040        this.i = i;
041        this.j = j;
042        this.value = value;
043    }
044
045    @Override
046    public boolean equals(Object o) {
047        if (o instanceof MatrixTuple) {
048            MatrixTuple otherM = (MatrixTuple) o;
049            if ((i == otherM.i) && (j == otherM.j)) {
050                return Math.abs(value - otherM.value) < 1e-12;
051            } else {
052                return false;
053            }
054        } else {
055            return false;
056        }
057    }
058
059    @Override
060    public int hashCode() {
061        return Objects.hash(i, j, value);
062    }
063
064    @Override
065    public String toString() {
066        return "MatrixTuple(i="+i+",j="+j+",value="+value+")";
067    }
068}