diff options
author | Andrej Shadura <andrewsh@debian.org> | 2019-03-09 22:30:41 +0000 |
---|---|---|
committer | Andrej Shadura <andrewsh@debian.org> | 2019-03-09 22:30:41 +0000 |
commit | 38212b3127e90751fb39cda34250bc11be62b76c (patch) | |
tree | dc1397346030e9695bd763dddc93b3be527cd643 /elki/src/main/java/de/lmu/ifi/dbs/elki/utilities/pairs/DoubleDoublePair.java | |
parent | 337087b668d3a54f3afee3a9adb597a32e9f7e94 (diff) |
Import Upstream version 0.7.0
Diffstat (limited to 'elki/src/main/java/de/lmu/ifi/dbs/elki/utilities/pairs/DoubleDoublePair.java')
-rw-r--r-- | elki/src/main/java/de/lmu/ifi/dbs/elki/utilities/pairs/DoubleDoublePair.java | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/elki/src/main/java/de/lmu/ifi/dbs/elki/utilities/pairs/DoubleDoublePair.java b/elki/src/main/java/de/lmu/ifi/dbs/elki/utilities/pairs/DoubleDoublePair.java new file mode 100644 index 00000000..af10137b --- /dev/null +++ b/elki/src/main/java/de/lmu/ifi/dbs/elki/utilities/pairs/DoubleDoublePair.java @@ -0,0 +1,185 @@ +package de.lmu.ifi.dbs.elki.utilities.pairs; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2015 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import java.util.Comparator; + +/** + * Pair storing two doubles. + * + * @author Erich Schubert + * + * @apiviz.has Comparator + */ +public class DoubleDoublePair implements Comparable<DoubleDoublePair> { + /** + * first value + */ + public double first; + + /** + * second value + */ + public double second; + + /** + * Constructor + * + * @param first First value + * @param second Second value + */ + public DoubleDoublePair(double first, double second) { + super(); + this.first = first; + this.second = second; + } + + /** + * Clone constructor. + * + * @param other Existing pair. + */ + public DoubleDoublePair(DoubleDoublePair other) { + this(other.first, other.second); + } + + /** + * Trivial equals implementation + * + * @param obj Object to compare to + */ + @Override + public boolean equals(Object obj) { + if(this == obj) { + return true; + } + if(obj == null) { + return false; + } + if(getClass() != obj.getClass()) { + return false; + } + + DoubleDoublePair other = (DoubleDoublePair) obj; + return (this.first == other.first) && (this.second == other.second); + } + + /** + * Trivial hashCode implementation mixing the two integers. + */ + @Override + public final int hashCode() { + // convert to longs + long firsthash = Double.doubleToLongBits(first); + firsthash = firsthash ^ (firsthash >> 32); + long secondhash = Double.doubleToLongBits(second); + secondhash = secondhash ^ (secondhash >> 32); + // primitive hash function mixing the two integers. + // this number does supposedly not have any factors in common with 2^32 + return (int) (firsthash * 2654435761L + secondhash); + } + + /** + * Implementation of comparable interface, sorting by first then second. + * + * @param other Object to compare to + * @return comparison result + */ + @Override + public int compareTo(DoubleDoublePair other) { + int fdiff = Double.compare(this.first, other.first); + if(fdiff != 0) { + return fdiff; + } + return Double.compare(this.second, other.second); + } + + /** + * Implementation of comparableSwapped interface, sorting by second then + * first. + * + * @param other Object to compare to + * @return comparison result + */ + public int compareSwappedTo(DoubleDoublePair other) { + int fdiff = Double.compare(this.second, other.second); + if(fdiff != 0) { + return fdiff; + } + return Double.compare(this.first, other.first); + } + + /** + * Set first value + * + * @param first new value + */ + public final void setFirst(double first) { + this.first = first; + } + + /** + * Set second value + * + * @param second new value + */ + public final void setSecond(double second) { + this.second = second; + } + + @Override + public String toString() { + return "DoubleDoublePair("+first+","+second+")"; + } + + /** + * Comparator to compare by second component only + */ + public static final Comparator<DoubleDoublePair> BYFIRST_COMPARATOR = new Comparator<DoubleDoublePair>() { + @Override + public int compare(DoubleDoublePair o1, DoubleDoublePair o2) { + return Double.compare(o1.first, o2.first); + } + }; + + /** + * Comparator to compare by second component only + */ + public static final Comparator<DoubleDoublePair> BYSECOND_COMPARATOR = new Comparator<DoubleDoublePair>() { + @Override + public int compare(DoubleDoublePair o1, DoubleDoublePair o2) { + return Double.compare(o1.second, o2.second); + } + }; + + /** + * Comparator to compare by swapped components + */ + public static final Comparator<DoubleDoublePair> SWAPPED_COMPARATOR = new Comparator<DoubleDoublePair>() { + @Override + public int compare(DoubleDoublePair o1, DoubleDoublePair o2) { + return o1.compareSwappedTo(o2); + } + }; +}
\ No newline at end of file |