Bool trie_store (Trie *trie, const TrieChar *key, TrieData data) { TrieIndex s, t; short suffix_idx; const TrieChar *p; size_t len; /* 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, *p)) return trie_branch_in_branch (trie, s, p, data); if ('\0' == *p) break; } /* walk through tail */ t = trie_da_get_tail_index (trie->da, s); if ('\0' != *p) { suffix_idx = 0; len = strlen ((const char *) p) + 1; /* including null-terminator */ if (tail_walk_str (trie->tail, t, &suffix_idx, p, len) != len) return trie_branch_in_tail (trie, s, p, data); } /* duplicated key, overwrite val */ tail_set_data (trie->tail, t, data); return TRUE; }
static Bool trie_store_conditionally (Trie *trie, const AlphaChar *key, TrieData data, Bool is_overwrite) { TrieIndex s, t; short suffix_idx; const AlphaChar *p, *sep; /* 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))) { TrieChar *key_str; Bool res; key_str = alpha_map_char_to_trie_str (trie->alpha_map, p); res = trie_branch_in_branch (trie, s, key_str, data); free (key_str); return res; } if (0 == *p) break; } /* walk through tail */ sep = p; 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))) { TrieChar *tail_str; Bool res; tail_str = alpha_map_char_to_trie_str (trie->alpha_map, sep); res = trie_branch_in_tail (trie, s, tail_str, data); free (tail_str); return res; } if (0 == *p) break; } /* duplicated key, overwrite val if flagged */ if (!is_overwrite) { return FALSE; } tail_set_data (trie->tail, t, data); trie->is_dirty = TRUE; return TRUE; }
static int trie_store_maybe(trie_t trie, const char *key, trie_data_t d, int overwritep) { trie_idx_t s, t; short int suffix_idx; const char *p, *sep; /* 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) { char *key_str; int res; key_str = alpha_map_char_to_trie_str(p); res = trie_branch_in_branch(trie, s, key_str, d); free(key_str); return res; } if (*p == '\0') { break; } } /* walk through tail */ sep = p; 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) { char *tail_str; int res; tail_str = alpha_map_char_to_trie_str(sep); res = trie_branch_in_tail(trie, s, tail_str, d); free(tail_str); return res; } if (*p == '\0') { break; } } /* duplicated key, overwrite val if flagged */ if (!overwritep) { return -1; } tail_set_data(trie->tail, t, d); trie->dirtyp = 1; return 0; }
static int trie_branch_in_branch( trie_t trie, trie_idx_t sep_node, const char *suffix, trie_data_t data) { trie_idx_t new_da, new_tail; new_da = da_insert_branch(trie->da, sep_node, *suffix); if (new_da == TRIE_INDEX_ERROR) { return -1; } if (*suffix != '\0') { ++suffix; } new_tail = tail_add_suffix(trie->tail, suffix); tail_set_data(trie->tail, new_tail, data); trie_da_set_tail_index(trie->da, new_da, new_tail); trie->dirtyp = 1; return 0; }
static Bool trie_branch_in_branch (Trie *trie, TrieIndex sep_node, const TrieChar *suffix, TrieData data) { TrieIndex new_da, new_tail; new_da = da_insert_branch (trie->da, sep_node, *suffix); if (TRIE_INDEX_ERROR == new_da) return FALSE; if ('\0' != *suffix) ++suffix; new_tail = tail_add_suffix (trie->tail, suffix); tail_set_data (trie->tail, new_tail, data); trie_da_set_tail_index (trie->da, new_da, new_tail); trie->is_dirty = TRUE; return TRUE; }