-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs_suffix_trie.min.js
16 lines (15 loc) · 4.27 KB
/
js_suffix_trie.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var JsSuffixTrie;
JsSuffixTrie=function(){function d(){this.count=0;this.structure={};this.prefix=""}d.prototype.add=function(a){var b,c,f,e,g;g=this.structure;f=a.length;for(c=0;c<f;){b=a[c++];if(e=g[b])g=e;else{g[b]={};g=g[b]}}if(g.terminator)return false;else{g.terminator=true;this.count++;return true}};d.prototype.remove=function(a){var b,c,f,e;e=this.structure;f=a.length;for(c=0;c<f;){b=a[c++];e=e[b];if(!e)return false}if(e.terminator){delete e.terminator;this.count--;return true}else return false};d.prototype.contains=
function(a){a=this.findNode(a);return a!==null&&a.terminator};d.prototype.subTrie=function(a){var b,c;b=this.findNode(a);c=new d;c.structure=b;c.prefix=a;return c};d.prototype.find=function(a){return d.nodeToArray(this.findNode(a),a)};d.prototype.findNode=function(a){var b,c,f,e;e=this.structure;f=a.length;for(c=0;c<f;){b=a[c++];e=e[b];if(!e)return null}return e};d.prototype.each=function(a){return d.each(a,this.structure,0,this.prefix)};d.each=function(a,b,c,f){var e;b.terminator&&a(c++,f);for(e in b)c=
this.each(a,b[e],c,f+e);return c};d.prototype.size=function(){return this.count};d.prototype.calculateSize=function(a){var b,c;if(a==null)a=this.structure;c=a.terminator?1:0;for(b in a)c+=this.calculateSize(a[b]);return c};d.fromArray=function(a){var b,c,f;f=new d;c=a.length;for(b=0;b<c;)f.add(a[b++]);f.count=b;return f};d.prototype.toArray=function(){return d.nodeToArray(this.structure,this.prefix)};d.nodeToArray=function(a,b){var c;c=[];this.each(function(f,e){return c[f]=e},a,0,b);return c};d.fromJSON=
function(a){var b;b=new d;b.structure=JSON.parse(a);b.calculateSize();return b};d.prototype.toJSON=function(){return JSON.stringify(this.structure)};return d}();
var JsSuffixTrie;JsSuffixTrie=(function(){function JsSuffixTrie(){this.count=0;this.structure={};this.prefix="";}
JsSuffixTrie.prototype.add=function(string){var chr,index,length,next,node;node=this.structure;length=string.length;index=0;while(index<length){chr=string[index++];next=node[chr];if(next){node=next;}else{node[chr]={};node=node[chr];}}
if(node.terminator){return false;}else{node.terminator=true;this.count++;return true;}};JsSuffixTrie.prototype.remove=function(string){var chr,index,length,node;node=this.structure;length=string.length;index=0;while(index<length){chr=string[index++];node=node[chr];if(!node){return false;}}
if(node.terminator){delete node.terminator;this.count--;return true;}else{return false;}};JsSuffixTrie.prototype.contains=function(string){var node;node=this.findNode(string);return node!==null&&node.terminator;};JsSuffixTrie.prototype.subTrie=function(prefix){var node,subTrie;node=this.findNode(prefix);subTrie=new JsSuffixTrie;subTrie.structure=node;subTrie.prefix=prefix;return subTrie;};JsSuffixTrie.prototype.find=function(prefix){return JsSuffixTrie.nodeToArray(this.findNode(prefix),prefix);};JsSuffixTrie.prototype.findNode=function(string){var currentChar,index,length,node;node=this.structure;length=string.length;index=0;while(index<length){currentChar=string[index++];node=node[currentChar];if(!node){return null;}}
return node;};JsSuffixTrie.prototype.each=function(callback){return JsSuffixTrie.each(callback,this.structure,0,this.prefix);};JsSuffixTrie.each=function(callback,node,index,string){var property;if(node&&node.terminator){callback(index++,string);}
for(property in node){index=this.each(callback,node[property],index,string+property);}
return index;};JsSuffixTrie.prototype.size=function(){return this.count;};JsSuffixTrie.prototype.calculateSize=function(node){var property,size;if(node==null){node=this.structure;}
size=node.terminator?1:0;for(property in node){size+=this.calculateSize(node[property]);}
return size;};JsSuffixTrie.fromArray=function(array){var i,length,trie;trie=new JsSuffixTrie;length=array.length;i=0;while(i<length){trie.add(array[i++]);}
trie.count=i;return trie;};JsSuffixTrie.prototype.toArray=function(){return JsSuffixTrie.nodeToArray(this.structure,this.prefix);};JsSuffixTrie.nodeToArray=function(node,prefix){var array;array=[];this.each(function(index,value){return array[index]=value;},node,0,prefix);return array;};JsSuffixTrie.fromJSON=function(json){var trie;trie=new JsSuffixTrie;trie.structure=JSON.parse(json);trie.calculateSize();return trie;};JsSuffixTrie.prototype.toJSON=function(){return JSON.stringify(this.structure);};return JsSuffixTrie;})();