int hattrie_del(hattrie_t* T, const char* key, size_t len) { node_ptr parent = T->root; assert(*parent.flag & NODE_TYPE_TRIE); /* find node for deletion */ node_ptr node = hattrie_find(T, &key, &len); if (node.flag == NULL) { return -1; } /* if consumed on a trie node, clear the value */ if (*node.flag & NODE_TYPE_TRIE) { return hattrie_clrval(T, node); } /* remove from bucket */ size_t m_old = ahtable_size(node.b); int ret = ahtable_del(node.b, key, len); T->m -= (m_old - ahtable_size(node.b)); /* merge empty buckets */ /*! \todo */ return ret; }
value_t* hattrie_tryget(hattrie_t* T, const char* key, size_t len) { /* find node for given key */ node_ptr node = hattrie_find(T, &key, &len); if (node.flag == NULL) { return NULL; } /* if the trie node consumes value, use it */ if (*node.flag & NODE_TYPE_TRIE) { return &node.t->val; } return ahtable_tryget(node.b, key, len); }
hattrie_iter_t* hattrie_iter_with_prefix(const hattrie_t* T, bool sorted, const char* prefix, size_t prefix_len) { int found; node_ptr node = hattrie_find((hattrie_t*)T, &prefix, &prefix_len, &found); hattrie_iter_t* i = malloc_or_die(sizeof(hattrie_iter_t)); i->T = T; i->sorted = sorted; i->i = NULL; i->keysize = 16; i->key = malloc_or_die(i->keysize * sizeof(char)); i->level = 0; i->has_nil_key = false; i->nil_val = 0; i->prefix_len = prefix_len; if (prefix_len) { i->prefix = (char*)malloc_or_die(prefix_len); memcpy(i->prefix, prefix, prefix_len); } else { i->prefix = NULL; } i->stack = malloc_or_die(sizeof(hattrie_node_stack_t)); i->stack->next = NULL; i->stack->node = node; i->stack->c = '\0'; i->stack->level = 0; hattrie_iter_step(i); if (i->prefix_len && hattrie_iter_prefix_not_match(i)) { hattrie_iter_next(i); } return i; }