data: one trie for maps and dictionaries, one loader
The hash map served a single exact-match lookup that the trie already answers, at the price of a second container, a second file loader with its own drift, and a Str-to-UTF-8 conversion on every chain probe. The files are small (kanji.dict is 7.5k lines), so the trie holds both. trieopen validates keys and each space-separated value word against Str and reports path:line; trielookup takes the Str every caller holds and treats a nil trie as an unloaded map; trienew/trieput exist for tests. The overflow guards on growth, Trie.root (always 0) and the per-language init loop written twice are gone.
This commit is contained in:
44
strans.c
44
strans.c
@@ -405,18 +405,16 @@ getlang(int lang)
|
||||
static int
|
||||
maplookup(Trie *t, Str *key, Str *out)
|
||||
{
|
||||
char buf[Maxutf], *v;
|
||||
int klen, match, vlen;
|
||||
char *v;
|
||||
int match, vlen;
|
||||
|
||||
if(out != nil)
|
||||
sclear(out);
|
||||
if(t == nil || key == nil || key->n == 0)
|
||||
sclear(out);
|
||||
if(key->n == 0)
|
||||
return 0;
|
||||
klen = stoutf(key, buf, sizeof(buf));
|
||||
match = trielookup(t, buf, klen, &v, &vlen);
|
||||
match = trielookup(t, key, &v, &vlen);
|
||||
if(match == TrieMiss)
|
||||
return 0;
|
||||
if(match == TrieExact && out != nil && !sinit(out, v, vlen))
|
||||
if(match == TrieExact && !sinit(out, v, vlen))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
@@ -1052,33 +1050,11 @@ imthread(void*)
|
||||
int
|
||||
mapget(Trie *t, Str *key, Str *out)
|
||||
{
|
||||
char buf[Maxutf], *v;
|
||||
int klen, match, vlen;
|
||||
char *v;
|
||||
int vlen;
|
||||
|
||||
if(out != nil)
|
||||
sclear(out);
|
||||
if(key == nil || out == nil || key->n == 0)
|
||||
return 0;
|
||||
klen = stoutf(key, buf, sizeof(buf));
|
||||
match = trielookup(t, buf, klen, &v, &vlen);
|
||||
if(match != TrieExact)
|
||||
sclear(out);
|
||||
if(key->n == 0 || trielookup(t, key, &v, &vlen) != TrieExact)
|
||||
return 0;
|
||||
return sinit(out, v, vlen);
|
||||
}
|
||||
|
||||
void
|
||||
mapinit(char *dir)
|
||||
{
|
||||
char *path;
|
||||
int i;
|
||||
|
||||
for(i = 0; i < nelem(langs); i++){
|
||||
if(langs[i].mapname == nil)
|
||||
continue;
|
||||
path = smprint("%s/%s.map", dir, langs[i].mapname);
|
||||
if(path == nil)
|
||||
die("out of memory");
|
||||
langs[i].map = trieopen(path);
|
||||
free(path);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user