| 1 |
package repeat; |
| 2 |
|
| 3 |
import shared.Tools; |
| 4 |
import structures.ByteBuilder; |
| 5 |
|
| 6 |
public class Palindrome implements Comparable<Palindrome>, Cloneable { |
| 7 |
|
| 8 |
public Palindrome(){} |
| 9 |
|
| 10 |
public Palindrome(int a_, int b_, int matches_, int mismatches_){ |
| 11 |
set(a_, b_, matches_, mismatches_); |
| 12 |
} |
| 13 |
|
| 14 |
public void set(int a_, int b_, int matches_, int mismatches_){ |
| 15 |
a=a_; |
| 16 |
b=b_; |
| 17 |
matches=matches_; |
| 18 |
mismatches=mismatches_; |
| 19 |
} |
| 20 |
|
| 21 |
public void setFrom(Palindrome p){ |
| 22 |
assert(p!=this); |
| 23 |
set(p.a, p.b, p.matches, p.mismatches); |
| 24 |
} |
| 25 |
|
| 26 |
public String toString() { |
| 27 |
return "("+a+"-"+b+",matches="+matches+",mismatches="+mismatches+")"; |
| 28 |
} |
| 29 |
|
| 30 |
public String toString(final int a0, final int b0) { |
| 31 |
int tail1=a-a0, tail2=b0-b; |
| 32 |
return "("+a+"-"+b+",matches="+matches+",mismatches="+mismatches+ |
| 33 |
",loop="+loop()+",tail1="+Tools.min(tail1,tail2)+ |
| 34 |
",tail2="+Tools.max(tail1,tail2)+",taildif="+Tools.absdif(tail1,tail2)+")"; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public ByteBuilder appendTo(ByteBuilder bb, final int a0, final int b0) { |
| 52 |
int tail1=a-a0, tail2=b0-b; |
| 53 |
int tmin=Tools.min(tail1, tail2), tmax=Tools.max(tail1, tail2); |
| 54 |
|
| 55 |
bb.append('(').append(a).dash().append(b).comma(); |
| 56 |
bb.append('P','=').append(matches+mismatches).comma(); |
| 57 |
bb.append('M','=').append(matches).comma(); |
| 58 |
bb.append('L','=').append(loop()).comma(); |
| 59 |
bb.append('T','=').append(tmin).plus().append(tmax); |
| 60 |
return bb.append(')'); |
| 61 |
} |
| 62 |
|
| 63 |
public int plen() {return matches+mismatches;} |
| 64 |
|
| 65 |
public int length() {return b-a+1;} |
| 66 |
|
| 67 |
public int loop() {return length()-2*matches;} |
| 68 |
|
| 69 |
public Palindrome clear() { |
| 70 |
a=b=matches=mismatches=0; |
| 71 |
return this; |
| 72 |
} |
| 73 |
|
| 74 |
public Palindrome clone() { |
| 75 |
Palindrome p=null; |
| 76 |
try {p=(Palindrome) super.clone();} |
| 77 |
catch (CloneNotSupportedException e) {e.printStackTrace();} |
| 78 |
return p; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public int compareTo(Palindrome p) { |
| 86 |
if(p==null) {return 1;} |
| 87 |
if(matches!=p.matches) {return matches-p.matches;} |
| 88 |
if(mismatches!=p.mismatches) {return p.mismatches-mismatches;} |
| 89 |
int lenDif=length()-p.length(); |
| 90 |
if(lenDif!=0) {return lenDif;} |
| 91 |
return p.a-a; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
public int a=0; |
| 96 |
|
| 97 |
public int b=0; |
| 98 |
|
| 99 |
public int matches=0; |
| 100 |
|
| 101 |
public int mismatches=0; |
| 102 |
|
| 103 |
} |