/** * @brief Delete an entry from trie * * @param trie : the trie * @param key : the key for the entry to delete * * @return boolean value indicating whether the key exists and is removed * * Delete an entry for the given @a key from @a trie. */ int trie_delete(trie_t trie, const char *key) { trie_idx_t s, t; short int suffix_idx; const char *p; /* walk through branches */ s = da_get_root(trie->da); for (p = key; !trie_da_separate_p(trie->da, s); p++) { if (da_walk(trie->da, &s, alpha_map_char_to_trie(*p)) < 0) { return -1; } if (*p == 0) { break; } } /* walk through tail */ t = trie_da_get_tail_index(trie->da, s); suffix_idx = 0; for ( ; ; p++) { if (tail_walk_char( trie->tail, t, &suffix_idx, alpha_map_char_to_trie(*p)) < 0) { return -1; } if (*p == 0) { break; } } tail_delete(trie->tail, t); da_set_base(trie->da, s, TRIE_INDEX_ERROR); da_prune(trie->da, s); trie->dirtyp = 1; return 0; }
/** * @brief Delete an entry from trie * * @param trie : the trie * @param key : the key for the entry to delete * * @return boolean value indicating whether the key exists and is removed * * Delete an entry for the given @a key from @a trie. */ Bool trie_delete (Trie *trie, const AlphaChar *key) { TrieIndex s, t; short suffix_idx; const AlphaChar *p; /* walk through branches */ s = da_get_root (trie->da); for (p = key; !trie_da_is_separate (trie->da, s); p++) { if (!da_walk (trie->da, &s, alpha_map_char_to_trie (trie->alpha_map, *p))) { return FALSE; } if (0 == *p) break; } /* walk through tail */ t = trie_da_get_tail_index (trie->da, s); suffix_idx = 0; for ( ; ; p++) { if (!tail_walk_char (trie->tail, t, &suffix_idx, alpha_map_char_to_trie (trie->alpha_map, *p))) { return FALSE; } if (0 == *p) break; } tail_delete (trie->tail, t); da_set_base (trie->da, s, TRIE_INDEX_ERROR); da_prune (trie->da, s); trie->is_dirty = TRUE; return TRUE; }