MCPcopy Create free account
hub / github.com/MolinDeng/Princeton-algs4 / CircularSuffixArray

Class CircularSuffixArray

LabEnv/10Lab/CircularSuffixArray.java:12–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10import java.util.Arrays;
11
12public class CircularSuffixArray {
13 private String text;
14 private int n;
15 private int[] index;
16
17 private final static int R = 256;
18
19 private class CircularSuffix implements Comparable<CircularSuffix> {
20 int offset;
21
22 CircularSuffix(int i) {
23 offset = i;
24 }
25
26 public int compareTo(CircularSuffix that) {
27 if (that == this) return 0;
28 for (int i = 0; i < n; i++) {
29 if (this.charAt(i) > that.charAt(i))
30 return 1;
31 else if (this.charAt(i) < that.charAt(i))
32 return -1;
33 }
34 return 0;
35 }
36
37 public char charAt(int i) {
38 return text.charAt(i + offset);
39 }
40 }
41
42 private void lsdSort(CircularSuffix[] a) {
43 int sz = a.length;
44 int w = n;
45 CircularSuffix[] aux = new CircularSuffix[sz];
46
47 for (int d = w - 1; d >= 0; d--) {
48 // sort by key-indexed counting on dth character
49
50 // compute frequency counts
51 int[] count = new int[R + 1];
52 for (int i = 0; i < sz; i++)
53 count[a[i].charAt(d) + 1]++;
54
55 // compute cumulates
56 for (int r = 0; r < R; r++)
57 count[r + 1] += count[r];
58
59 // move data
60 for (int i = 0; i < sz; i++)
61 aux[count[a[i].charAt(d)]++] = a[i];
62
63 // copy back
64 for (int i = 0; i < sz; i++)
65 a[i] = aux[i];
66 }
67 }
68
69 // circular suffix array of s

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected