001/*
002 * $Id$
003 */
004
005package edu.jas.gb;
006
007
008import java.io.Serializable;
009import java.util.Comparator;
010
011import edu.jas.poly.TermOrder;
012import edu.jas.structure.RingElem;
013
014
015/**
016 * Comparator for critical pairs of polynomials. Immutable objects.
017 * @param <C> coefficient type
018 * @author Heinz Kredel
019 */
020public class CriticalPairComparator<C extends RingElem<C>>
021                implements Serializable, Comparator<AbstractPair<C>> {
022
023
024    public final TermOrder tord;
025
026
027    protected final TermOrder.EVComparator ec;
028
029
030    /**
031     * Constructor.
032     * @param t TermOrder.
033     */
034    public CriticalPairComparator(TermOrder t) {
035        tord = t;
036        ec = tord.getAscendComparator();
037    }
038
039
040    /**
041     * Compare. Compares exponents and if equal, compares polynomial indices.
042     * @param p1 first critical pair.
043     * @param p2 second critical pair.
044     * @return 0 if ( p1 == p2 ), -1 if ( p1 &lt; p2 ) and +1 if ( p1 &gt; p2 ).
045     */
046    public int compare(AbstractPair<C> p1, AbstractPair<C> p2) {
047        int s = ec.compare(p1.e, p2.e);
048        if (s == 0) {
049            /* not ok  
050            if ( p1.j < p2.j ) {
051              s = -1;
052            } else if ( p1.j > p2.j ) {
053               s = 1;
054            } else if ( p1.i < p2.i ) {
055               s = -1;
056            } else if ( p1.i > p2.i ) {
057               s = 1;
058            } else {
059               s = 0;
060            }
061            */
062            /* ok */
063            if (p1.j > p2.j) {
064                s = -1;
065            } else if (p1.j < p2.j) {
066                s = 1;
067            } else if (p1.i > p2.i) {
068                s = -1;
069            } else if (p1.i < p2.i) {
070                s = 1;
071            } else {
072                s = 0;
073            }
074            /* */
075        }
076        return s;
077    }
078
079
080    /**
081     * toString.
082     */
083    @Override
084    public String toString() {
085        return "CriticalPairComparator(" + tord + ")";
086    }
087
088}