001/*
002 * $Id: WordPair.java 4150 2012-09-01 09:18:23Z kredel $
003 */
004
005package edu.jas.gb;
006
007import java.io.Serializable;
008
009import edu.jas.structure.RingElem;
010import edu.jas.poly.Word;
011import edu.jas.poly.GenWordPolynomial;
012
013
014/**
015 * Serializable subclass to hold pairs of word polynomials.
016 * @param <C> coefficient type
017 * @author Heinz Kredel.
018 */
019public class WordPair<C extends RingElem<C> > implements Comparable<WordPair> {
020
021
022    public final GenWordPolynomial<C> pi;
023    public final GenWordPolynomial<C> pj;
024    public final int i;
025    public final int j;
026
027    protected int n;
028
029
030    /**
031     * WordPair constructor.
032     * @param a word polynomial i.
033     * @param b word polynomial j.
034     * @param i first index.
035     * @param j second index.
036     */
037    public WordPair(GenWordPolynomial<C> a, GenWordPolynomial<C> b, 
038                    int i, int j) {
039        pi = a;
040        pj = b;
041        this.i = i;
042        this.j = j;
043        this.n = 0;
044    }
045
046
047    /**
048     * toString.
049     */
050    @Override
051    public String toString() {
052        return "wordPair(" + i + "," + j + ",{" + pi.length() + "," + pj.length() + "},"  + n + ")"; 
053    }
054
055
056    /**
057     * Set removed pair number.
058     * @param n number of this pair generated in OrderedPairlist.
059     */
060    public void pairNumber(int n) {
061        this.n = n;
062    }
063
064
065    /**
066     * Get removed pair number.
067     * @return n number of this pair generated in OrderedPairlist.
068     */
069    public int getPairNumber() {
070        return n;
071    }
072
073
074    /**
075     * equals.
076     * @param ob an Object.
077     * @return true if this is equal to o, else false.
078     */
079    @Override
080    public boolean equals(Object ob) {
081        if ( ! (ob instanceof WordPair) ) {
082           return false;
083           // throw new ClassCastException("Pair "+n+" o "+o);
084        }
085        return 0 == compareTo( (WordPair)ob );
086    }
087
088
089    /**
090     * compareTo used in TreeMap // not used at moment.
091     * Comparison is based on the number of the pairs.
092     * @param p a WordPair.
093     * @return 1 if (this &lt; o), 0 if (this == o), -1 if (this &gt; o).
094     */
095    public int compareTo(WordPair p) {
096        int x = p.getPairNumber();
097        if ( n > x ) { 
098           return 1;
099        }
100        if ( n < x ) { 
101           return -1;
102        }
103        return 0;
104    }
105
106
107    /**
108     * Hash code for this WordPair.
109     * @see java.lang.Object#hashCode()
110     */
111    @Override
112    public int hashCode() {
113        return (i << 16) + j;
114    }
115
116}
117