Created
September 23, 2012 01:08
-
-
Save triplefox/3768422 to your computer and use it in GitHub Desktop.
AlphaTrie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AlphaTrie | |
{ | |
// an implementation of a trie specifically for storing and looking up english dictionary words | |
public var chars : Hash<AlphaTrie>; | |
public var ending : Bool; | |
public function new() | |
{ | |
chars = new Hash(); | |
ending = false; | |
} | |
public function find(word : String) | |
{ | |
var cur = this; | |
for (idx in 0...word.length) | |
{ | |
var chr = word.charAt(idx); | |
var next = cur.chars.get(chr); | |
if (next==null) | |
{ | |
return false; | |
} | |
cur = next; | |
} | |
return cur.ending; | |
} | |
public function continuation(word : String) | |
{ | |
var cur = this; | |
for (idx in 0...word.length) | |
{ | |
var chr = word.charAt(idx); | |
var next = cur.chars.get(chr); | |
if (next==null) | |
{ | |
return false; | |
} | |
cur = next; | |
} | |
var ct = Lambda.count(cur.chars); | |
return ct>0; | |
} | |
public function available(word : String) | |
{ | |
var cur = this; | |
for (idx in 0...word.length) | |
{ | |
var chr = word.charAt(idx); | |
var next = cur.chars.get(chr); | |
if (next==null) | |
{ | |
return null; | |
} | |
cur = next; | |
} | |
var result = new Hash<Bool>(); | |
for (k in cur.chars.keys()) | |
result.set(k, true); | |
return result; | |
} | |
public function store(word : String) | |
{ | |
var cur = this; | |
for (idx in 0...word.length) | |
{ | |
var chr = word.charAt(idx); | |
var next = cur.chars.get(chr); | |
if (next==null) | |
{ | |
next = new AlphaTrie(); | |
cur.chars.set(chr, next); | |
} | |
cur = next; | |
} | |
cur.ending = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment