/* Split hybrid node - this is similar operation to burst. */ static void hattrie_split_h(node_ptr parent, node_ptr node) { /* Find split point. */ unsigned left_m, right_m; unsigned char j = hattrie_split_mid(node, &left_m, &right_m); /* now split into two node cooresponding to ranges [0, j] and * [j + 1, TRIE_MAXCHAR], respectively. */ /* create new left and right nodes * one node may reuse existing if it keeps hybrid flag * hybrid -> pure always needs a new table */ unsigned char c0 = node.b->c0, c1 = node.b->c1; node_ptr left, right; if (j + 1 == c1) { /* right will be pure */ right.b = ahtable_create(); if (j == c0) { /* left will be pure as well */ left.b = ahtable_create(); } else { /* left will be hybrid */ left.b = node.b; } } else { /* right will be hybrid */ right.b = node.b; left.b = ahtable_create(); } /* setup created nodes */ left.b->c0 = c0; left.b->c1 = j; left.b->flag = c0 == j ? NODE_TYPE_PURE_BUCKET : NODE_TYPE_HYBRID_BUCKET; // need to force it right.b->c0 = j + 1; right.b->c1 = c1; right.b->flag = right.b->c0 == right.b->c1 ? NODE_TYPE_PURE_BUCKET : NODE_TYPE_HYBRID_BUCKET; /* update the parent's pointer */ unsigned int c; for (c = c0; c <= j; ++c) parent.t->xs[c] = left; for (; c <= c1; ++c) parent.t->xs[c] = right; /* fill new tables */ hattrie_split_fill(node, left, right, j); if (node.b != left.b && node.b != right.b) { ahtable_free(node.b); } }
void hattrie_clear(hattrie_t* T) { hattrie_free_node(T->root); node_ptr node; node.b = ahtable_create(); node.b->flag = NODE_TYPE_HYBRID_BUCKET; node.b->c0 = 0x00; node.b->c1 = 0xff; T->root.t = alloc_trie_node(T, node); }
/* initialize root node */ static void hattrie_initroot(hattrie_t *T) { node_ptr node; if (T->bsize > 0) { node.b = ahtable_create(); node.b->flag = NODE_TYPE_HYBRID_BUCKET; node.b->c0 = 0x00; node.b->c1 = TRIE_MAXCHAR; T->root.t = alloc_trie_node(T, node); } else { T->root.t = alloc_empty_node(T); } }
hattrie_t* hattrie_create() { hattrie_t* T = malloc_or_die(sizeof(hattrie_t)); T->m = 0; node_ptr node; node.b = ahtable_create(); node.b->flag = NODE_TYPE_HYBRID_BUCKET; node.b->c0 = 0x00; node.b->c1 = NODE_MAXCHAR; T->root.t = alloc_trie_node(T, node); return T; }
void setup() { fprintf(stderr, "generating %zu keys ... ", n); xs = malloc(n * sizeof(char*)); size_t i; size_t m; for (i = 0; i < n; ++i) { m = m_low + rand() % (m_high - m_low); xs[i] = malloc(m + 1); randstr(xs[i], m); } T = ahtable_create(); M = str_map_create(); fprintf(stderr, "done.\n"); }