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