-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBurrowsWheelerTransform.java
170 lines (121 loc) · 4.53 KB
/
BurrowsWheelerTransform.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package burrowswheeler;
import java.util.Arrays;
/**
*
* @author rumana
*/
class BurrowsWheelerTransform {
private String firstSymbols;
private int[] index;
private String[] CyclicShiftFirst;
private String[] combination;
private String[] cyclicCombination;
private char[] tempDecodedSeq;
public char[] getTempDecodedSeq() {
return tempDecodedSeq;
}
public String[] getCyclicCombination() {
return cyclicCombination;
}
public String getFirstSymbols() {
return firstSymbols;
}
public int[] getIndex() {
return index;
}
public String[] getCyclicFirst()
{
return CyclicShiftFirst;
}
public String[] getCombination() {
return combination;
}
public void encode(String input) {
combination = new String[input.length()];
CyclicShiftFirst=new String[input.length()];
int x, i;
x = input.length();
char c[] = input.toCharArray();
int atPosition = x - 1;
for (i = 0; i < x; i++) {
char startChar = input.charAt(atPosition);
String lastPart = input.substring(0, atPosition);
combination[i] = startChar + lastPart;
input = combination[i];
}
System.out.println("Output of the first step:Cyclic Shif of input");
for (i = 0; i < combination.length; i++) {
CyclicShiftFirst[i]=combination[i];
System.out.println("Before Sorting: " + combination[i]);
}
Arrays.sort(combination);
System.out.println("Output of the second step: Sorting");
for (i = 0; i < combination.length; i++) {
System.out.println("After Sorting: " + combination[i]);
}
/*Cyclic Shift*/
String[] tempCombination = new String[combination.length];
for (i = 0; i < combination.length; i++) {
tempCombination[i] = combination[i];
}
cyclicCombination = new String[combination.length];
//int current = 0;
for (i = 0; i < x; i++) {
int position = (x - 1);
char atPos = tempCombination[i].charAt(position);
String lastPart = tempCombination[i].substring(0, position);
tempCombination[i] = atPos + lastPart;
cyclicCombination[i] = tempCombination[i];
//combination[current]=cyclicCombination[i];
}
System.out.println("Output of third step:Cyclic shift of the sorted array");
for (i = 0; i < combination.length; i++) {
System.out.println("After cyclic Shifting " + cyclicCombination[i]);
}
//storing the first symbols
char[] temp = new char[combination.length];
for (i = 0; i < combination.length; i++) {
temp[i] = cyclicCombination[i].charAt(0);
}
firstSymbols = new String(temp);
System.out.println("Final output of encoding stage:String of first symbols");
System.out.println("First symbols:" + firstSymbols);
//Creating the index array
index = new int[combination.length];
for (i = 0; i < combination.length; i++) {
boolean isMatched = false;
for (int j = 0; j < combination.length; j++) {
if (combination[i].equals(cyclicCombination[j])) {
index[i] = j;
isMatched = true;
break;
}
}
if (!isMatched) {
index[i] = -1;
}
}
System.out.println("Final output of encoding step:Index array");
for (i = 0; i < combination.length; i++) {
System.out.println("Index array# " + i + "is" + index[i]);
}
}
/*******************DECODING FUNCTION*******************/
public void decode(String lastSymbol, int index[]) {
int len = lastSymbol.length();
//String DecodedSeq=new String();
//DecodedSeq.charAt(len)=lastSymbol.charAt(len);
char[] c = lastSymbol.toCharArray();
tempDecodedSeq = new char[len + 1];
tempDecodedSeq[len] = c[len];
System.out.println("each component" + tempDecodedSeq[len]);
int k = len;
for (int j = 1; j <= len; j++) {
k = index[k];
tempDecodedSeq[len - j] = c[k];
System.out.println("each component" + tempDecodedSeq[len - j]);
}
String decodedSeq=tempDecodedSeq.toString();
System.out.println("The Original Sequence"+ decodedSeq);
}
}