TreeTrie getBest String implementation

This commit is contained in:
Greg Wilkins 2015-03-26 10:41:41 +11:00
parent 5d14d0ca7a
commit eca04f401a
1 changed files with 37 additions and 3 deletions

View File

@ -224,9 +224,43 @@ public class TreeTrie<V> extends AbstractTrie<V>
@Override
public V getBest(String s, int offset, int len)
{
// TODO inefficient
byte[] b=s.substring(offset,offset+len).getBytes(StandardCharsets.ISO_8859_1);
return getBest(b,0,b.length);
TreeTrie<V> t = this;
for(int i=0; i < len; i++)
{
byte c=(byte)(0xff&s.charAt(offset+i));
int index=c>=0&&c<0x7f?__lookup[c]:-1;
if (index>=0)
{
if (t._nextIndex[index] == null)
break;
t = t._nextIndex[index];
}
else
{
TreeTrie<V> n=null;
for (int j=t._nextOther.size();j-->0;)
{
n=t._nextOther.get(j);
if (n._c==c)
break;
n=null;
}
if (n==null)
break;
t=n;
}
// Is the next Trie is a match
if (t._key!=null)
{
// Recurse so we can remember this possibility
V best=t.getBest(s,offset+i+1,len-i-1);
if (best!=null)
return best;
break;
}
}
return t._value;
}
@Override