summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/misc/oktavia/src/sais.jsx
blob: 9d8fa8fb64aa48534618af3c10352c34aa2582ff (plain)
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
/* Original source code:
 * G. Nong, S. Zhang and W. H. Chan, Two Efficient Algorithms for Linear Time Suffix Array Construction, IEEE Transactions on Computers, To Appear
 * http://www.cs.sysu.edu.cn/nong/index.files/Two%20Efficient%20Algorithms%20for%20Linear%20Suffix%20Array%20Construction.pdf
 */

import "bit-vector.jsx";

class OArray
{
    var offset : int;
    var array : int[];

    function constructor (array : int[])
    {
        this.array = array;
        this.offset = 0;
    }

    function constructor (array : int[], offset : int)
    {
        this.array = array;
        this.offset = offset;
    }

    function get (index : int) : int
    {
        return this.array[index + this.offset];
    }

    function set (index : int, value : int) : void
    {
        this.array[index + this.offset] = value;
    }

    function isS (index : int) : boolean
    {
        return this.array[index + this.offset] < this.array[index + this.offset + 1];
    }

    function compare (index1 : int, index2 : int) : boolean
    {
        return this.array[index1 + this.offset] == this.array[index2 + this.offset];
    }
}


class SAIS
{
    static function _isLMS (t : BitVector, i : int) : boolean
    {
        return i > 0 && t.get(i) && !t.get(i - 1);
    }

    // find the start or end of each bucket
    static function _getBuckets(s : OArray, bkt : int[], n : int, K : int, end : boolean) : void
    {
        var sum = 0;
        for (var i = 0; i <= K; i++)
        {
            bkt[i] = 0; // clear all buckets
        }
        for (var i = 0; i < n; i++)
        {
            bkt[s.get(i)]++; // compute the size of each bucket
        }
        for (var i = 0; i <= K; i++)
        {
            sum += bkt[i];
            bkt[i] = end ? sum : sum - bkt[i];
        }
    }

    // compute SAl
    static function _induceSAl(t : BitVector, SA : int[], s : OArray, bkt : int[], n : int, K : int, end : boolean) : void
    {
        SAIS._getBuckets(s, bkt, n, K, end); // find starts of buckets
        for (var i = 0; i < n; i++)
        {
            var j = SA[i] - 1;
            if (j >= 0 && !t.get(j))
            {
                SA[bkt[s.get(j)]++] = j;
            }
        }
   } 

    // compute SAs
    static function _induceSAs(t : BitVector, SA : int[], s : OArray, bkt : int[], n : int, K : int, end : boolean) : void
    {
        SAIS._getBuckets(s, bkt, n, K, end); // find ends of buckets
        for (var i = n - 1; i >= 0; i--)
        {
            var j = SA[i] - 1;
            if (j >=0 && t.get(j))
            {
                SA[--bkt[s.get(j)]] = j;
            }
        }
    }

    // find the suffix array SA of s[0..n-1] in {1..K}^n
    // require s[n-1]=0 (the sentinel!), n>=2
    // use a working space (excluding s and SA) of at most 2.25n+O(1) for a constant alphabet

    static function make(source : string) : int[]
    {
        var charCodes = [] : int[];
        charCodes.length = source.length;
        var maxCode = 0;
        for (var i = 0; i < source.length; i++)
        {
            var code = source.charCodeAt(i);
            charCodes[i] = code;
            maxCode = (code > maxCode) ? code : maxCode;
        }
        var SA = [] : int[];
        SA.length = source.length;
        var s = new OArray(charCodes);
        SAIS._make(s, SA, source.length, maxCode);
        return SA;
    }

    static function _make(s : OArray, SA : int[], n : int, K : int) : void
    {
        // Classify the type of each character
        var t = new BitVector();
        t.set(n - 2, false);
        t.set(n - 1, true); // the sentinel must be in s1, important!!!
        for (var i = n - 3; i >= 0; i--)
        {
            t.set(i, (s.isS(i) || (s.compare(i, i + 1) && t.get(i + 1))));
        }

        // stage 1: reduce the problem by at least 1/2
        // sort all the S-substrings
        var bkt = [] : int[];
        bkt.length = K + 1;
        SAIS._getBuckets(s, bkt, n, K, true); // find ends of buckets
        for (var i = 0; i < n; i++)
        {
            SA[i] = -1;
        }
        for (var i = 1; i < n; i++)
        {
            if (SAIS._isLMS(t, i))
            {
                SA[--bkt[s.get(i)]] = i;
            }
        }
        SAIS._induceSAl(t, SA, s, bkt, n, K, false);
        SAIS._induceSAs(t, SA, s, bkt, n, K, true);
        // compact all the sorted substrings into the first n1 items of SA
        // 2*n1 must be not larger than n (proveable)
        var n1 = 0;
        for (var i = 0; i < n; i++)
        {
            if (SAIS._isLMS(t, SA[i]))
            {
                SA[n1++] = SA[i];
            }
        }

        // find the lexicographic names of all substrings
        for (var i = n1; i < n; i++)
        {
            SA[i]=-1; // init the name array buffer
        }
        var name = 0;
        var prev = -1;
        for (i = 0; i < n1; i++)
        {
            var pos = SA[i];
            var diff = false;
            for (var d = 0; d < n; d++)
            {
                if (prev == -1 || !s.compare(pos + d, prev + d) || t.get(pos + d) != t.get(prev + d))
                {
                    diff = true;
                    break;
                }
                else if (d > 0 && (SAIS._isLMS(t, pos+d) || SAIS._isLMS(t, prev + d)))
                {
                    break;
                }
            }
            if (diff)
            {
                name++;
                prev = pos;
            }
            pos = (pos % 2 == 0) ? pos / 2 : (pos - 1) / 2;
            SA[n1 + pos] = name - 1;
        }
        for (var i = n - 1, j = n - 1; i >= n1; i--)
        {
            if (SA[i] >= 0)
            {
                SA[j--] = SA[i];
            }
        }

        // stage 2: solve the reduced problem
        // recurse if names are not yet unique
        var SA1 = SA;
        var s1 = new OArray(SA, n - n1);

        if (name < n1)
        {
            SAIS._make(s1, SA1, n1, name - 1);
        }
        else
        {
            // generate the suffix array of s1 directly
            for (i = 0; i < n1; i++)
            {
                SA1[s1.get(i)] = i;
            }
        }

        // stage 3: induce the result for the original problem

        bkt = [] : int[];
        bkt.length = K + 1;
        // put all left-most S characters into their buckets
        SAIS._getBuckets(s, bkt, n, K, true); // find ends of buckets
        for (i = 1, j = 0; i < n; i++)
        {
            if (SAIS._isLMS(t, i))
            {
                s1.set(j++, i); // get p1
            }
        }
        for (i = 0; i < n1; i++)
        {
            SA1[i] = s1.get(SA1[i]); // get index in s
        }
        for (i = n1; i < n; i++)
        {
            SA[i] = -1; // init SA[n1..n-1]
        }
        for (i = n1 - 1; i >= 0; i--)
        {
            j = SA[i];
            SA[i] = -1;
            SA[--bkt[s.get(j)]] = j;
        }
        SAIS._induceSAl(t, SA, s, bkt, n, K, false);
        SAIS._induceSAs(t, SA, s, bkt, n, K, true);
    }
}