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