static VALUE walk_all_paths_with_values(Trie *trie, VALUE children, TrieState *state, char *prefix, int prefix_size) { int c; for(c = 1; c < 256; c++) { if(trie_state_is_walkable(state,c)) { TrieState *next_state = trie_state_clone(state); trie_state_walk(next_state, c); prefix[prefix_size] = c; prefix[prefix_size + 1] = 0; if(trie_state_is_terminal(next_state)) { TrieState *end_state = trie_state_clone(next_state); trie_state_walk(end_state, '\0'); char *word = (char*) malloc(prefix_size + 2); memcpy(word, prefix, prefix_size + 2); VALUE tuple = rb_ary_new(); rb_ary_push(tuple, rb_str_new2(word)); TrieData trie_data = trie_state_get_data(end_state); rb_ary_push(tuple, (VALUE)trie_data); rb_ary_push(children, tuple); trie_state_free(end_state); } walk_all_paths_with_values(trie, children, next_state, prefix, prefix_size + 1); prefix[prefix_size] = 0; trie_state_free(next_state); } } }
/* nodoc */ static VALUE rb_trie_node_initialize_copy(VALUE self, VALUE from) { RDATA(self)->data = trie_state_clone(RDATA(from)->data); VALUE state = rb_iv_get(from, "@state"); rb_iv_set(self, "@state", state == Qnil ? Qnil : rb_str_dup(state)); VALUE full_state = rb_iv_get(from, "@full_state"); rb_iv_set(self, "@full_state", full_state == Qnil ? Qnil : rb_str_dup(full_state)); return self; }
/* * call-seq: * value * * Attempts to get the value at this node of the Trie. This only works if the node is a terminal * (i.e. end of a key), otherwise it returns nil. * */ static VALUE rb_trie_node_value(VALUE self) { TrieState *state; TrieState *dup; Data_Get_Struct(self, TrieState, state); dup = trie_state_clone(state); trie_state_walk(dup, 0); TrieData trie_data = trie_state_get_data(dup); trie_state_free(dup); return TRIE_DATA_ERROR == trie_data ? Qnil : (VALUE)trie_data; }
Bool trie_state_is_leaf (const TrieState *s) { TrieState *t; Bool ret; t = trie_state_clone (s); ret = trie_state_walk (t, TRIE_CHAR_TERM); trie_state_free (t); return ret; }
static int brk_shot_init (BrkShot *dst, const BrkShot *src) { dst->dict_state = trie_state_clone (src->dict_state); dst->str_pos = src->str_pos; if (!(dst->brk_pos = (int *) malloc (src->n_brk_pos * sizeof (int)))) return -1; memcpy (dst->brk_pos, src->brk_pos, src->cur_brk_pos * sizeof (int)); dst->n_brk_pos = src->n_brk_pos; dst->cur_brk_pos = src->cur_brk_pos; dst->penalty = src->penalty; return 0; }
SBTrieState * sb_trie_state_clone (const SBTrieState *s) { SBTrieState *new_state; new_state = (SBTrieState *) malloc (sizeof (SBTrieState)); if (!new_state) return NULL; new_state->sb_trie = s->sb_trie; new_state->trie_state = trie_state_clone (s->trie_state); return new_state; }
/* * call-seq: * children_with_values(key) -> [ [key,value], ... ] * * Finds all keys with their respective values in the Trie beginning with the given prefix. * */ static VALUE rb_trie_children_with_values(VALUE self, VALUE prefix) { if(NIL_P(prefix)) return rb_ary_new(); StringValue(prefix); Trie *trie; Data_Get_Struct(self, Trie, trie); int prefix_size = RSTRING_LEN(prefix); TrieChar *char_prefix = (TrieChar*)RSTRING_PTR(prefix); VALUE children = rb_ary_new(); TrieState *state = trie_root(trie); const TrieChar *iterator = char_prefix; while(*iterator != 0) { if(!trie_state_is_walkable(state, *iterator)) return rb_ary_new(); trie_state_walk(state, *iterator); iterator++; } if(trie_state_is_terminal(state)) { TrieState *end_state = trie_state_clone(state); trie_state_walk(end_state, '\0'); VALUE tuple = rb_ary_new(); rb_ary_push(tuple, prefix); TrieData trie_data = trie_state_get_data(end_state); rb_ary_push(tuple, (VALUE)trie_data); rb_ary_push(children, tuple); trie_state_free(end_state); } char prefix_buffer[1024]; memcpy(prefix_buffer, char_prefix, prefix_size); prefix_buffer[prefix_size] = 0; walk_all_paths_with_values(trie, children, state, prefix_buffer, prefix_size); trie_state_free(state); return children; }
static VALUE walk_all_paths(Trie *trie, VALUE children, TrieState *state, char *prefix, int prefix_size) { int c; for(c = 1; c < 256; c++) { if(trie_state_is_walkable(state,c)) { TrieState *next_state = trie_state_clone(state); trie_state_walk(next_state, c); prefix[prefix_size] = c; prefix[prefix_size + 1] = 0; if(trie_state_is_terminal(next_state)) { char *word = (char*) malloc(prefix_size + 2); memcpy(word, prefix, prefix_size + 2); rb_ary_push(children, rb_str_new2(word)); } walk_all_paths(trie, children, next_state, prefix, prefix_size + 1); prefix[prefix_size] = 0; trie_state_free(next_state); } } }
/** * @brief Move trie iterator to the next entry * * @param iter : an iterator * * @return boolean value indicating the availability of the entry * * Move trie iterator to the next entry. * On return, the iterator @a iter is updated to reference to the new entry * if successfully moved. * * Available since: 0.2.6 */ Bool trie_iterator_next (TrieIterator *iter) { TrieState *s = iter->state; TrieIndex sep; /* first iteration */ if (!s) { s = iter->state = trie_state_clone (iter->root); /* for tail state, we are already at the only entry */ if (s->is_suffix) return TRUE; iter->key = trie_string_new (20); sep = da_first_separate (s->trie->da, s->index, iter->key); if (TRIE_INDEX_ERROR == sep) return FALSE; s->index = sep; return TRUE; } /* no next entry for tail state */ if (s->is_suffix) return FALSE; /* iter->state is a separate node */ sep = da_next_separate (s->trie->da, iter->root->index, s->index, iter->key); if (TRIE_INDEX_ERROR == sep) return FALSE; s->index = sep; return TRUE; }
/* nodoc */ static VALUE rb_trie_node_initialize_copy(VALUE self, VALUE from) { RDATA(self)->data = trie_state_clone(RDATA(from)->data); return self; }
ATTrie::Position ATTrie :: clonePosition(ATTrie::Position p) { return trie_state_clone( (const TrieState*)p); }