-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBorderSubstring.java.txt
379 lines (326 loc) · 12.3 KB
/
BorderSubstring.java.txt
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import java.util.Arrays;
/**
* A substring that can compute border information from its suffixes. See
* \cite{Efficient_detection_of_unusual_words} for notation and algorithmics.
*/
public abstract class BorderSubstring extends RightMaximalSubstring {
/**
* A representation of $right_v=\{(a,a[v) : a \in \Sigma, a[v \neq \epsilon\}$,
* sorted by increasing $a$. $rightPointers[i]$ is the address of
* $serialized(rightCharacters[i][v)$ in the bit stream. $longestBorderCharacter$ is
* the symbol $a \in \Sigma$ with largest $|a[v|$ in $right_v$.
*/
public long rightLength;
public long longestBorderCharacter;
public final long[] rightCharacters;
public final long[] rightPointers;
public long longestBorderLength;
public BorderSubstring(int alphabetLength, int log2alphabetLength, int textLength, int log2textLength) {
super(alphabetLength,log2alphabetLength,textLength,log2textLength);
rightCharacters = new long[alphabetLength];
rightPointers = new long[alphabetLength];
}
public static int serializedSize(int alphabetLength, int log2alphabetLength) {
return Substring.serializedSize()+1+log2alphabetLength+log2alphabetLength+alphabetLength*(log2alphabetLength+MAX_BITS_POINTER);
}
// ----------------- SERIALIZATION/DESERIALIZATION PROCEDURES ------------------------
public void serialize(Stack stack) {
super.serialize(stack);
if (rightLength==0) {
stack.push(0L,1);
return;
}
int log2address = address==0?MAX_BITS_POINTER:Utils.log2(address);
stack.push(1L,1);
stack.push(longestBorderCharacter,LOG2_ALPHABET_LENGTH);
stack.push(rightLength,LOG2_ALPHABET_LENGTH);
for (int i=0; i<rightLength; i++) {
stack.push(rightCharacters[i],LOG2_ALPHABET_LENGTH);
stack.push(rightPointers[i],log2address);
}
}
/**
* Remark: there is no practical performance gain in creating shallower versions of
* this procedure that skip over, rather than read, just few variables.
*/
public void deserialize(Stack stack) {
super.deserialize(stack);
if (stack.read(1)==0L) {
rightLength=0;
longestBorderCharacter=-1;
return;
}
final int log2address = address==0?MAX_BITS_POINTER:Utils.log2(address);
longestBorderCharacter=stack.read(LOG2_ALPHABET_LENGTH);
rightLength=stack.read(LOG2_ALPHABET_LENGTH);
if (rightLength==0L) rightLength=ALPHABET_LENGTH;
for (int i=0; i<rightLength; i++) {
rightCharacters[i]=stack.read(LOG2_ALPHABET_LENGTH);
rightPointers[i]=stack.read(log2address);
}
}
public int skip(Stack stack) {
final int log2address = super.skip(stack);
if (stack.read(1)==1L) {
stack.skip(LOG2_ALPHABET_LENGTH);
long rightLength = stack.read(LOG2_ALPHABET_LENGTH);
if (rightLength==0L) rightLength=ALPHABET_LENGTH;
for (int i=0; i<rightLength; i++) {
stack.skip(LOG2_ALPHABET_LENGTH);
stack.skip(log2address);
}
}
return log2address;
}
// ------------------------------ BORDER PROCEDURES ----------------------------------
/**
* Initializes all the variables of this object, except $address$, from those of
* $suffix$. This procedure is IO-bound: it just deserializes a region of $stack$ that
* is very close to the bottom, and copies an array.
*
* Remark: under the assumption that longest borders are generally short, we expect
* the bottom of $stack$ to be accessed frequently, and portions of it to be cached by
* the processor. Creating a \emph{software cache} of frequently used $BorderSubstring$
* objects would likely remove just the time to deserialize objects from $stack$,
* which should be small anyway because of caching.
*
* @param a first character of $v$;
* @param suffix one-symbol suffix of $v$;
* @param stack stack containing all suffixes of $v$, represented as
* $BorderSubstring$s;
* @param characterStack stack containing all the characters of $v$, from right to
* left going from the bottom of $characterStack$ to the top of $characterStack$;
* @param buffer scratch space; at the end of the procedure it contains $bord(v)$.
*/
public final void initFromSuffix(long a, BorderSubstring suffix, Stack stack, Stack characterStack, BorderSubstring buffer) {
final boolean found;
final long value;
this.suffix=suffix.address;
length=suffix.length+1;
if (length==1) {
// Base case: no border.
rightLength=0;
longestBorderCharacter=-1;
return;
}
found=suffix.getLongestBorderOfLeftExtension(a,stack,buffer);
if (!found) { // $a[suf(v) = \epsilon$
characterStack.setPosition(0);
value=characterStack.read(LOG2_ALPHABET_LENGTH); // First character from the right
if (value!=a) {
// Base case: no border.
longestBorderLength=0;
rightLength=0;
longestBorderCharacter=-1;
return;
}
// Base case: length-one longest border.
longestBorderLength=1;
longestBorderCharacter=characterStack.read(LOG2_ALPHABET_LENGTH); // Second character from the right
rightLength=1;
rightCharacters[0]=longestBorderCharacter;
stack.setPosition(0);
skip(stack);
rightPointers[0]=stack.getPosition(); // Second substring in the stack
return;
}
longestBorderLength=buffer.length;
characterStack.setPosition(buffer.length<<LOG2_LOG2_ALPHABET_LENGTH); // Character immediately following the longest border
longestBorderCharacter=characterStack.read(LOG2_ALPHABET_LENGTH);
rightArrayFromLongestBorder(buffer);
}
/**
* Loads in $out$ the longest border of $av$, $a \in \Sigma$, if
* $a \in rightCharacters$.
*
* @return FALSE if $a \notin rightCharacters$.
*/
private final boolean getLongestBorderOfLeftExtension(long a, Stack stack, BorderSubstring out) {
if (rightLength==0) return false;
final int i = Arrays.binarySearch(rightCharacters,0,(int)rightLength,a);
if (i<0) return false;
stack.setPosition(rightPointers[i]);
skip(stack);
out.deserialize(stack);
return true;
}
/**
* Loads in $out$ the longest border of $v$.
* @return FALSE if $v$ has no border.
*/
public final boolean getLongestBorder(Stack stack, BorderSubstring out) {
return getLongestBorderOfLeftExtension(longestBorderCharacter,stack,out);
}
/**
* Builds $right_v$ from $right_{bord(v)}$, assuming that $longestBorderCharacter$ has
* already been computed. The running time depends on $|right_v|$ rather than on the
* potentially large $|\Sigma|$.
*/
private final void rightArrayFromLongestBorder(BorderSubstring longestBorder) {
final int from, to, length;
final int longestBorderRightLength;
int i;
longestBorderRightLength=(int)longestBorder.rightLength;
if (longestBorderRightLength==0) {
rightLength=1;
rightCharacters[0]=longestBorderCharacter;
rightPointers[0]=longestBorder.address;
return;
}
i=Arrays.binarySearch(longestBorder.rightCharacters,0,longestBorderRightLength,longestBorderCharacter);
if (i<0) {
i=-i-1;
from=i; to=i+1; length=longestBorderRightLength-i;
rightLength=longestBorderRightLength+1;
}
else {
from=i+1; to=i+1; length=longestBorderRightLength-i-1;
rightLength=longestBorderRightLength;
}
if (i>0) {
System.arraycopy(longestBorder.rightCharacters,0,rightCharacters,0,i);
System.arraycopy(longestBorder.rightPointers,0,rightPointers,0,i);
}
rightCharacters[i]=longestBorderCharacter;
rightPointers[i]=longestBorder.address;
if (from<longestBorderRightLength) {
System.arraycopy(longestBorder.rightCharacters,from,rightCharacters,to,length);
System.arraycopy(longestBorder.rightPointers,from,rightPointers,to,length);
}
}
// ----------------------------------- TESTING ---------------------------------------
public static void main(String[] args) {
testCorrectness();
//testStringsPerRegion();
}
/**
* Setting $stringsPerRegion \geq 30$ is good for a random string on 4 characters.
*/
private static final void testStringsPerRegion() {
final int TEXT_LENGTH = 30000;
int stringsPerRegion, i;
double p;
long a, time;
for (stringsPerRegion=10; stringsPerRegion<100; stringsPerRegion+=10) {
String text = "";
for (i=0; i<TEXT_LENGTH; i++) {
p=Math.random();
if (p<0.25) text+='0';
else if (p<0.5) text+='1';
else if (p<0.75) text+='2';
else text+='3';
}
Stack stack = new Stack((BorderSubstring.serializedSize(4,2)*stringsPerRegion)>>>6);
Stack characterStack = new Stack(stringsPerRegion);
BorderSubstring top = new BorderSubstring(4,2,TEXT_LENGTH,32);
BorderSubstring suffix = new BorderSubstring(4,2,TEXT_LENGTH,32);
BorderSubstring buffer = new BorderSubstring(4,2,TEXT_LENGTH,32);
BorderSubstring tmp;
top.serialize(stack);
time=System.currentTimeMillis();
for (i=text.length()-1; i>=0; i--) { // Inserting $test$ from right to left
if (text.charAt(i)=='0') a=0L;
else if (text.charAt(i)=='1') a=1L;
else if (text.charAt(i)=='2') a=2L;
else a=3L;
characterStack.push(a,2);
tmp=suffix; suffix=top; top=tmp;
top.initFromSuffix(a,suffix,stack,characterStack,buffer); // Overwrites all variables in $top$
top.serialize(stack); // Overwrites $address$
}
System.out.println("stringsPerRegion="+stringsPerRegion+", time="+(System.currentTimeMillis()-time));
}
}
private static final void testCorrectness() {
final int TEXT_LENGTH = 10000;
final int N_ITERATIONS = 10;
final int STRINGS_PER_REGION = 30;
boolean longestBorder, isBorder;
int i, j, t, length, nBorders;
long[] borders = new long[4];
long a;
double p;
BorderSubstring top, suffix, buffer, tmp;
for (t=0; t<N_ITERATIONS; t++) {
String text = "";
for (i=0; i<TEXT_LENGTH; i++) {
p=Math.random();
if (p<0.25) text+='0';
else if (p<0.5) text+='1';
else if (p<0.75) text+='2';
else text+='3';
}
// Testing
Stack stack = new Stack((BorderSubstring.serializedSize(4,2)*STRINGS_PER_REGION)>>>6);
Stack characterStack = new Stack(STRINGS_PER_REGION);
top = new BorderSubstring(4,2,TEXT_LENGTH,32);
suffix = new BorderSubstring(4,2,TEXT_LENGTH,32);
buffer = new BorderSubstring(4,2,TEXT_LENGTH,32);
top.serialize(stack);
//top.print();
for (i=text.length()-1; i>=0; i--) { // Inserting $test$ from right to left
if (text.charAt(i)=='0') a=0L;
else if (text.charAt(i)=='1') a=1L;
else if (text.charAt(i)=='2') a=2L;
else a=3L;
characterStack.push(a,2);
// Computing borders using brute force
for (j=0; j<4; j++) borders[j]=-1;
for (length=1; length<text.length()-i; length++) {
isBorder=true;
for (j=0; j<length; j++) {
if (text.charAt(i+j)!=text.charAt(text.length()-length+j)) {
isBorder=false;
break;
}
}
if (isBorder) {
if (text.charAt(text.length()-length-1)=='0') borders[0]=length;
else if (text.charAt(text.length()-length-1)=='1') borders[1]=length;
else if (text.charAt(text.length()-length-1)=='2') borders[2]=length;
else borders[3]=length;
}
}
// Computing borders using recursion
tmp=suffix; suffix=top; top=tmp;
top.initFromSuffix(a,suffix,stack,characterStack,buffer); // Overwrites all variables in $top$
top.serialize(stack); // Overwrites $address$
//top.print();
// Checking correctness
nBorders=0;
for (j=0; j<4; j++) {
if (borders[j]!=-1) nBorders++;
}
if (top.rightLength!=nBorders) {
System.err.println("ERROR at suffix <"+text.substring(i)+">:");
System.err.println("rightLength="+top.rightLength+" (should be "+nBorders+")");
System.exit(1);
}
for (j=0; j<top.rightLength; j++) {
if (borders[(int)top.rightCharacters[j]]==-1) {
System.err.println("ERROR at suffix <"+text.substring(i)+">:");
System.err.println("character "+top.rightCharacters[j]+" should not have a border");
System.exit(1);
}
stack.setPosition(top.rightPointers[j]);
buffer.deserialize(stack);
if (buffer.length!=borders[(int)top.rightCharacters[j]]) {
System.err.println("ERROR at suffix <"+text.substring(i)+">:");
System.err.println("character "+top.rightCharacters[j]+" should have a border of length "+borders[(int)top.rightCharacters[j]]+" rather than "+buffer.length);
System.exit(1);
}
}
}
}
System.out.println("No errors found");
}
public void print() {
super.print();
System.out.println("rightLength="+rightLength);
if (rightLength!=0) {
System.out.println("longestBorderCharacter="+longestBorderCharacter);
for (int i=0; i<rightLength; i++) System.out.print("("+rightCharacters[i]+","+rightPointers[i]+") ");
System.out.println();
}
}
}