static int trie_branch_in_tail( trie_t trie, trie_idx_t sep_node, const char *suffix, trie_data_t data) { trie_idx_t old_tail, old_da, s; const char *old_suffix, *p; /* adjust separate point in old path */ old_tail = trie_da_get_tail_index(trie->da, sep_node); if ((old_suffix = tail_get_suffix(trie->tail, old_tail)) == NULL) { return -1; } for (p = old_suffix, s = sep_node; *p == *suffix; p++, suffix++) { trie_idx_t t = da_insert_branch(trie->da, s, *p); if (t == TRIE_INDEX_ERROR) { goto fail; } s = t; } old_da = da_insert_branch(trie->da, s, *p); if (old_da == TRIE_INDEX_ERROR) { goto fail; } if (*p != '\0') { p++; } tail_set_suffix(trie->tail, old_tail, p); trie_da_set_tail_index(trie->da, old_da, old_tail); /* insert the new branch at the new separate point */ return trie_branch_in_branch(trie, s, suffix, data); fail: /* failed, undo previous insertions and return error */ da_prune_upto(trie->da, sep_node, s); trie_da_set_tail_index(trie->da, sep_node, old_tail); return -1; }
static Bool trie_branch_in_tail (Trie *trie, TrieIndex sep_node, const TrieChar *suffix, TrieData data) { TrieIndex old_tail, old_da, s; const TrieChar *old_suffix, *p; /* adjust separate point in old path */ old_tail = trie_da_get_tail_index (trie->da, sep_node); old_suffix = tail_get_suffix (trie->tail, old_tail); if (!old_suffix) return FALSE; for (p = old_suffix, s = sep_node; *p == *suffix; p++, suffix++) { TrieIndex t = da_insert_branch (trie->da, s, *p); if (TRIE_INDEX_ERROR == t) goto fail; s = t; } old_da = da_insert_branch (trie->da, s, *p); if (TRIE_INDEX_ERROR == old_da) goto fail; if ('\0' != *p) ++p; tail_set_suffix (trie->tail, old_tail, p); trie_da_set_tail_index (trie->da, old_da, old_tail); /* insert the new branch at the new separate point */ return trie_branch_in_branch (trie, s, suffix, data); fail: /* failed, undo previous insertions and return error */ da_prune_upto (trie->da, sep_node, s); trie_da_set_tail_index (trie->da, sep_node, old_tail); return FALSE; }
/** * @brief Prune the single branch * * @param d : the double-array structure * @param s : the dangling state to prune off * * Prune off a non-separate path up from the final state @a s. * If @a s still has some children states, it does nothing. Otherwise, * it deletes the node and all its parents which become non-separate. */ void da_prune (DArray *d, TrieIndex s) { da_prune_upto (d, da_get_root (d), s); }