Posted to tcl by kbk at Wed Oct 16 20:01:22 GMT 2019view pretty
static inline unsigned char
Patricia_bit(unsigned char* s, size_t l, int b)
{
if (b < 0 || b >= 8*l) {
return 0;
} else {
int c = b>>3;
int cb = b & 0x7;
return !!(s[c] & (0x80 >> cb));
}
}
int
Patricia_search(const Patricia* tree, unsigned char* name)
{
int p = 0;
size_t len = strlen(name);
int x = tree[p].left;
while (tree[p].bit < tree[x].bit) {
p = x;
x = (Patricia_bit(name, len, tree[x].bit)) ?
tree[x].right : tree[x].left;
}
if (!strcmp(name, tree[x].key)) {
return x;
} else {
return -1;
}
}