GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — failure-function/src/build.js at main · string-data-structure/failure-function

Expand file tree

/

build.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
50 lines (45 loc) · 977 Bytes
/

build.js

Copy path

File metadata and controls

50 lines (45 loc) · 977 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* Computes the failure function for input string.
*
* This is the "next[j]" table found in
* "Fast pattern matching in strings" by Knuth, Morris, and Pratt,
* although here indices are 0-based hence all indices and inputs are one less
* than in that paper.
*
* NOTE The main loop is somewhat unrolled for faster execution. This was not
* benchmarked.
*
* @param {ArrayLike} p
* @param {number} pi
* @param {number} pj
* @param {number[]} t
* @param {number} ti
*/
const build = (p, pi, pj, t, ti) => {
t[ti] = -1;
if (pi === pj) return;
const p0 = pi;
if (++pi === pj) {
t[++ti] = 0;
return;
}
const t0 = ti;
t[++ti] = p[pi] === p[p0] ? -1 : 0;
let m = 0;
--pj;
while (pi < pj) {
while (p[pi] !== p[p0 + m]) {
m = t[t0 + m];
if (m === -1) break;
}
++m;
t[++ti] = p[++pi] === p[p0 + m] ? t[t0 + m] : m;
}
while (p[pj] !== p[p0 + m]) {
m = t[t0 + m];
if (m === -1) break;
}
++m;
t[++ti] = m;
};
export default build;
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.