/** * Clones this object. * * @return clone of this object */ public PairOfIntFloat clone() { return new PairOfIntFloat(this.leftElement, this.rightElement); }
@Override protected boolean lessThan(Object obj0, Object obj1) { // If equal scores, break tie by int descending. if (((PairOfIntFloat) obj0).getValue() == ((PairOfIntFloat) obj1).getValue()) { return ((PairOfIntFloat) obj0).getKey() < ((PairOfIntFloat) obj1).getKey() ? true : false; } return (((PairOfIntFloat) obj0).getValue() < ((PairOfIntFloat) obj1).getValue()) ? true : false; } }
/** * Checks two pairs for equality. * * @param obj object for comparison * @return <code>true</code> if <code>obj</code> is equal to this object, <code>false</code> * otherwise */ public boolean equals(Object obj) { PairOfIntFloat pair = (PairOfIntFloat) obj; return leftElement == pair.getLeftElement() && rightElement == pair.getRightElement(); }
/** * Creates a pair. * * @param left the left element * @param right the right element */ public PairOfIntFloat(int left, float right) { set(left, right); }
@Override public void write(DataOutput out) throws IOException { out.writeInt(maxElements); int sz = size(); out.writeInt(sz); // Note: serialization is destructive! PairOfIntFloat pair; for (int i = 0; i < sz; i++) { pair = queue.pop(); out.writeInt(pair.getKey()); out.writeFloat(pair.getValue()); } } }
/** * Defines a natural sort order for pairs. Pairs are sorted first by the left element, and then by * the right element. * * @return a value less than zero, a value greater than zero, or zero if this pair should be * sorted before, sorted after, or is equal to <code>obj</code>. */ public int compareTo(PairOfIntFloat pair) { int pl = pair.getLeftElement(); float pr = pair.getRightElement(); if (leftElement == pl) { if (rightElement < pr) return -1; if (rightElement > pr) return 1; return 0; } if (leftElement < pl) return -1; return 1; }
public void add(int n, float f) { queue.insert(new PairOfIntFloat(n, f)); }