/// <summary> Copies a node.</summary> /// <param name="n1">- destination node to copy /// </param> /// <param name="n2">- source node to copy /// </param> private void node_copy(ST_NODE n1, ST_NODE n2) { n1.child = n2.child; n1.CS = n2.CS; n1.I = n2.I; n1.K = n2.K; }
private void InitBlock(Simti enclosingInstance) { this.enclosingInstance = enclosingInstance; node = new ST_NODE(enclosingInstance); free = new ST_FREE(enclosingInstance); }
/// <summary> Searches the specified word in the SIMTI structure.</summary> /// <param name="word">- word to search /// </param> /// <returns> the number of words found, 0: not found /// </returns> public virtual int search(char[] word) { int i, j, k; ST_NODE tmpnode = new ST_NODE(this); ST_NODE rnode = null; int child; sbyte cs; for (i = 0, j = 0; j < word.Length && i < this.search_end; i++) { if (word[j] == this.search_word[i]) j++; else break; } this.search_end = i; if (this.search_end == 0) { cs = this.head.s_node.CS; child = this.head.s_node.child; } else { child = this.search_idx[this.search_end - 1]; cs = this.nf[child].node.CS; child = this.nf[child].node.child; } while (j < word.Length && cs != 0) { tmpnode.K = word[j]; rnode = null; for (k = child; k < child + cs; k++) { if (tmpnode.K == this.nf[k].node.K) { rnode = this.nf[k].node; break; } } if (rnode == null) break; else { this.search_word[this.search_end] = word[j]; this.search_idx[this.search_end] = k; this.search_end++; j++; child = this.nf[k].node.child; cs = this.nf[k].node.CS; } } return this.search_end; }
/// <summary> Compares the keys of the specified two nodes.</summary> /// <param name="a">- the first node to compare /// </param> /// <param name="b">- the second node to compare /// </param> /// <returns> a.key - b.key /// </returns> public virtual int kcomp(ST_NODE a, ST_NODE b) { return (int) a.K - (int) b.K; }
/// <summary> Inserts the word to the SIMTI structure with the specified information.</summary> /// <param name="word">- word to insert /// </param> /// <param name="I">- information to insert on the word /// </param> /// <returns> -1: fail, 0: duplicated, 1: success /// </returns> public virtual int insert(char[] word, int I) { int child_index, new_index; int i, j, k; sbyte cs; ST_NODE parent; ST_NODE tmp_node = new ST_NODE(this); tmp_node.child = 0; tmp_node.CS = 0; tmp_node.I = 0; tmp_node.K = (char) (0); k = 0; if (word.Length == 0) return - 1; search(word); k += this.search_end; if (this.search_end == 0) { parent = this.head.s_node; } else { parent = this.nf[this.search_idx[this.search_end - 1]].node; } while (k < word.Length) { cs = parent.CS; if (cs == 0) { // no child new_index = alloc(1); node_copy(this.nf[new_index].node, tmp_node); this.nf[new_index].node.K = word[k]; parent.CS = 1; parent.child = new_index; this.search_idx[this.search_end] = new_index; this.search_word[this.search_end] = word[k]; this.search_end++; k++; parent = this.nf[new_index].node; } else { new_index = alloc(cs + 1); child_index = parent.child; for (i = 0; i < cs; i++) { if (this.nf[child_index + i].node.K < word[k]) { ST_NODE node = this.nf[new_index + i].node; this.nf[new_index + i].node = this.nf[child_index + i].node; this.nf[child_index + i].node = node; } else { break; } } node_copy(this.nf[new_index + i].node, tmp_node); this.nf[new_index + i].node.K = word[k]; this.search_idx[this.search_end] = new_index + i; this.search_word[this.search_end] = word[k]; this.search_end++; k++; for (j = i; j < cs; j++) { ST_NODE node = this.nf[new_index + j + 1].node; this.nf[new_index + j + 1].node = this.nf[child_index + j].node; this.nf[child_index + j].node = node; } parent.child = new_index; parent.CS = (sbyte) (cs + 1); free(child_index, cs); parent = this.nf[new_index + i].node; } } if (parent.I == 0) { parent.I = I; return 1; } else { return 0; } }
/// <summary> Deletes the specified word in the SIMTI structure.</summary> /// <param name="word">- word to delete /// </param> /// <returns> 1: done successfully, -1: failed, 0: not found /// </returns> public virtual int delete(char[] word) { int i, d, j; int idx, newidx; sbyte size; ST_NODE temp; ST_NODE node = new ST_NODE(this); search(word); if (this.search_end < word.Length || word.Length == 0) return 0; temp = this.nf[this.search_idx[this.search_end - 1]].node; if (temp.I == 0) return 0; node_copy(node, temp); for (i = this.search_end - 1; i > 0 && node.CS == 0 && node.I == 0; i--) { this.search_end--; if (i == 1) { node_copy(node, this.head.s_node); } else { node_copy(node, this.nf[this.search_idx[i - 1]].node); } if (node.CS == 1) { free(node.child, 1); node.CS = 0; node.child = 0; } else { idx = node.child; d = this.search_idx[i] - idx; /* distance */ size = node.CS; newidx = alloc(size - 1); for (j = 0; j < d; j++) { ST_NODE tmp = this.nf[newidx + j].node; this.nf[newidx + j].node = this.nf[idx + j].node; this.nf[idx + j].node = tmp; } for (j = 0; j < size - d - 1; j++) { ST_NODE tmp = this.nf[newidx + j].node; this.nf[newidx + j].node = this.nf[idx + j].node; this.nf[idx + j].node = tmp; } free(idx, size); node.CS--; node.child = newidx; } if (i == 1) node_copy(this.head.s_node, node); else node_copy(this.nf[this.search_idx[i - 1]].node, node); } return 1; }
/// <summary> Searches the specified word in the SIMTI structure.</summary> /// <param name="word">- word to search /// </param> /// <returns> the number of words found, 0: not found /// </returns> public virtual int search(char[] word) { int i, j, k; ST_NODE tmpnode = new ST_NODE(this); ST_NODE rnode = null; int child; sbyte cs; for (i = 0, j = 0; j < word.Length && i < this.search_end; i++) { if (word[j] == this.search_word[i]) { j++; } else { break; } } this.search_end = i; if (this.search_end == 0) { cs = this.head.s_node.CS; child = this.head.s_node.child; } else { child = this.search_idx[this.search_end - 1]; cs = this.nf[child].node.CS; child = this.nf[child].node.child; } while (j < word.Length && cs != 0) { tmpnode.K = word[j]; rnode = null; for (k = child; k < child + cs; k++) { if (tmpnode.K == this.nf[k].node.K) { rnode = this.nf[k].node; break; } } if (rnode == null) { break; } else { this.search_word[this.search_end] = word[j]; this.search_idx[this.search_end] = k; this.search_end++; j++; child = this.nf[k].node.child; cs = this.nf[k].node.CS; } } return(this.search_end); }
/// <summary> Compares the keys of the specified two nodes.</summary> /// <param name="a">- the first node to compare /// </param> /// <param name="b">- the second node to compare /// </param> /// <returns> a.key - b.key /// </returns> public virtual int kcomp(ST_NODE a, ST_NODE b) { return((int)a.K - (int)b.K); }
/// <summary> Inserts the word to the SIMTI structure with the specified information.</summary> /// <param name="word">- word to insert /// </param> /// <param name="I">- information to insert on the word /// </param> /// <returns> -1: fail, 0: duplicated, 1: success /// </returns> public virtual int insert(char[] word, int I) { int child_index, new_index; int i, j, k; sbyte cs; ST_NODE parent; ST_NODE tmp_node = new ST_NODE(this); tmp_node.child = 0; tmp_node.CS = 0; tmp_node.I = 0; tmp_node.K = (char)(0); k = 0; if (word.Length == 0) { return(-1); } search(word); k += this.search_end; if (this.search_end == 0) { parent = this.head.s_node; } else { parent = this.nf[this.search_idx[this.search_end - 1]].node; } while (k < word.Length) { cs = parent.CS; if (cs == 0) { // no child new_index = alloc(1); node_copy(this.nf[new_index].node, tmp_node); this.nf[new_index].node.K = word[k]; parent.CS = 1; parent.child = new_index; this.search_idx[this.search_end] = new_index; this.search_word[this.search_end] = word[k]; this.search_end++; k++; parent = this.nf[new_index].node; } else { new_index = alloc(cs + 1); child_index = parent.child; for (i = 0; i < cs; i++) { if (this.nf[child_index + i].node.K < word[k]) { ST_NODE node = this.nf[new_index + i].node; this.nf[new_index + i].node = this.nf[child_index + i].node; this.nf[child_index + i].node = node; } else { break; } } node_copy(this.nf[new_index + i].node, tmp_node); this.nf[new_index + i].node.K = word[k]; this.search_idx[this.search_end] = new_index + i; this.search_word[this.search_end] = word[k]; this.search_end++; k++; for (j = i; j < cs; j++) { ST_NODE node = this.nf[new_index + j + 1].node; this.nf[new_index + j + 1].node = this.nf[child_index + j].node; this.nf[child_index + j].node = node; } parent.child = new_index; parent.CS = (sbyte)(cs + 1); free(child_index, cs); parent = this.nf[new_index + i].node; } } if (parent.I == 0) { parent.I = I; return(1); } else { return(0); } }
private void InitBlock(Simti enclosingInstance) { this.enclosingInstance = enclosingInstance; s_free = new ST_FREE(enclosingInstance); s_node = new ST_NODE(enclosingInstance); }
/// <summary> Deletes the specified word in the SIMTI structure.</summary> /// <param name="word">- word to delete /// </param> /// <returns> 1: done successfully, -1: failed, 0: not found /// </returns> public virtual int delete(char[] word) { int i, d, j; int idx, newidx; sbyte size; ST_NODE temp; ST_NODE node = new ST_NODE(this); search(word); if (this.search_end < word.Length || word.Length == 0) { return(0); } temp = this.nf[this.search_idx[this.search_end - 1]].node; if (temp.I == 0) { return(0); } node_copy(node, temp); for (i = this.search_end - 1; i > 0 && node.CS == 0 && node.I == 0; i--) { this.search_end--; if (i == 1) { node_copy(node, this.head.s_node); } else { node_copy(node, this.nf[this.search_idx[i - 1]].node); } if (node.CS == 1) { free(node.child, 1); node.CS = 0; node.child = 0; } else { idx = node.child; d = this.search_idx[i] - idx; /* distance */ size = node.CS; newidx = alloc(size - 1); for (j = 0; j < d; j++) { ST_NODE tmp = this.nf[newidx + j].node; this.nf[newidx + j].node = this.nf[idx + j].node; this.nf[idx + j].node = tmp; } for (j = 0; j < size - d - 1; j++) { ST_NODE tmp = this.nf[newidx + j].node; this.nf[newidx + j].node = this.nf[idx + j].node; this.nf[idx + j].node = tmp; } free(idx, size); node.CS--; node.child = newidx; } if (i == 1) { node_copy(this.head.s_node, node); } else { node_copy(this.nf[this.search_idx[i - 1]].node, node); } } return(1); }