Skip to content

Instantly share code, notes, and snippets.

@allenluce
Created May 1, 2018 15:01
Show Gist options
  • Save allenluce/1dcd8c200f951469496c2b01fe9c265c to your computer and use it in GitHub Desktop.
Save allenluce/1dcd8c200f951469496c2b01fe9c265c to your computer and use it in GitHub Desktop.
This file has been truncated, but you can view the full file.
require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
'use strict'
const bounds = require('binary-search-bounds')
module.exports = createStateMachine
function ACState (symbols, children, next, value) {
this.s = symbols
this.c = children
this.next = next
this.v = value
}
ACState.prototype.push = function (c) {
let state = this
while (true) {
const sym = state.s
const i = bounds.eq(sym, c)
if (i < 0) {
if (state.next === state) {
break
}
state = state.next
} else {
state = state.c[i]
break
}
}
return state
}
function trieToAutomata (trie) {
const symbols = trie.s.slice()
const children = new Array(trie.c.length)
for (let i = 0; i < children.length; ++i) {
children[i] = trieToAutomata(trie.c[i])
}
return new ACState(symbols, children, null, trie.v, null)
}
function createStateMachine (trie) {
// First pass: translate trie to automata
const root = trieToAutomata(trie)
root.next = root
// Second pass: iterate over trie in bfs order, attach prefixes
const Q = root.c.slice()
for (let j = 0; j < Q.length; ++j) {
Q[j].next = root
}
let ptr = 0
while (ptr < Q.length) {
const r = Q[ptr++]
const sym = r.s
const children = r.c
const n = sym.length
for (let i = 0; i < n; ++i) {
const a = sym[i]
const u = children[i]
u.next = root
Q.push(u)
let v = r.next
do {
const j = bounds.eq(v.s, a)
if (j >= 0) {
u.next = v.c[j]
break
} else {
v = v.next
}
} while (v !== root)
}
}
// Done: return root
return root
}
},{"binary-search-bounds":3}],2:[function(require,module,exports){
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment