Esempio n. 1
0
void find_next(node **now, int &matched, int &remain, int index){
    if(remain == 0){
        char next_char = pattern[index];
        if((*now)->child.find(next_char) == (*now)->child.end()){
            matched--;
            if((*now) == root){
                matched = 0;
            }
            else{
                (*now) = (*now)->suffix_link;
                find_next(now, matched, remain, index);
            }
        }
        else{
            node *next = (*now)->child[next_char];
            remain++;
            matched++;
            if(edge_len(next) == remain){
                (*now) = next;
                remain = 0;
            }
        }
    }
    else{
        int start = index - remain;
        node *next = (*now)->child[pattern[start]];
        if(pattern[index] == str[next->start + remain]){
            remain++;
            matched++;
            if(edge_len(next) == remain){
                (*now) = next;
                remain = 0;
            }
        }
        else{
            matched--;
            if((*now) == root){
                int start = index - remain;
                node *next = (*now)->child[pattern[start]];
                start = next->start + 1;
                remain--;
                walk_down(now, remain, start);
                find_next(now, matched, remain, index);
            }
            else{
                int start = (next)->start;
                (*now) = (*now)->suffix_link;
                //printf("%d %d\n", remain, start);
                walk_down(now, remain, start);
                //printf("%d %d\n", remain, start);
                find_next(now, matched, remain, index);
            }
        }
    }
}
Esempio n. 2
0
void find_next(node **now, int &remain_len, int &base, int index) {
    if(remain_len == 0) {
        if((*now)->child[to_int(pattern[index])] == NULL) {
            if((*now) == root) {
                matched=0;
            }
            else {
                matched--;
                (*now) = (*now)->suffix_link;
                find_next(now,remain_len, base, index);
            }
        }
        else {
            node *child = (*now)->child[to_int(pattern[index])];
            base = child->start;
            matched++;
            remain_len++;
            if(remain_len == edge_len(child)) {
                remain_len = 0;
                base = child->start;
                (*now) = child;
            }
        }
    }
    else {
        node *child = (*now)->child[to_int(str[base])];
        if(str[child->start + remain_len] == pattern[index]) {
            matched++;
            remain_len++;
            if(remain_len == edge_len(child)) {
                remain_len = 0;
                base = child->start;
                (*now) = child;
            }
        }
        else {
            matched--;
            if((*now) == root) {
                base = index - remain_len + 1;
                remain_len--;
                walk_down(now, remain_len, base);
                find_next(now,remain_len, base, index);
            }
            else {
                base = child->start;
                (*now) = (*now)->suffix_link;
                walk_down(now, remain_len, base);
                find_next(now,remain_len, base, index);
            }
        }
    }
}
Esempio n. 3
0
/**
 * Find a location of the suffix in the tree.
 * @param st the suffixtree in question
 * @param j the extension number counting from 0
 * @param i the current phase - 1
 * @param log the log to record errors in
 * @return the position (combined node and edge-offset)
 */ 
static pos *find_beta( suffixtree *st, int j, int i, plugin_log *log )
{
    pos *p;
    if ( st->old_j > 0 && st->old_j == j )
    {
        p = pos_create();
        p->loc = st->old_beta.loc;
        p->v = st->old_beta.v;
    }
    else if ( j>i )  // empty string
    {
        p = pos_create();
        p->loc = 0;
        p->v = st->root;
    }
    else if ( j==0 )    // entire string
    {
        p = pos_create();
        p->loc = i;
        p->v = st->f;
    }
    else // walk across tree
    {
        node *v = st->last.v;
        int len = st->last.loc-node_start(st->last.v)+1;
        path *q = path_create( node_start(v), len, log );
        v = node_parent( v );
        while ( v != st->root && node_link(v)==NULL )
        {
            path_prepend( q, node_len(v) );
            v = node_parent( v );
        }
        if ( v != st->root )
        {
            v = node_link( v );
            p = walk_down( st, v, q );
        }
        else
        {
            path_dispose( q );
            p = walk_down( st, st->root, path_create(j,i-j+1,log) );
        }
    }
    st->last = *p;
    return p;
}
Esempio n. 4
0
/**
 * Find a location of the suffix in the tree.
 * @param j the extension number counting from 0
 * @param i the current phase - 1
 * @return the position (combined node and edge-offset)
 */
static pos *find_beta( int j, int i )
{
    pos *p;
    if ( old_j > 0 && old_j == j )
    {
        p = pos_create();
        p->loc = old_beta.loc;
        p->v = old_beta.v;
    }
    else if ( j>i )  // empty string
    {
        p = pos_create();
        p->loc = 0;
        p->v = root;
    }
    else if ( j==0 )    // entire string
    {
        p = pos_create();
        p->loc = i;
        p->v = f;
    }
    else // walk across tree
    {
        node *v = last.v;
        int len = last.loc-node_start(last.v)+1;
        path *q = path_create( node_start(v), len );
        v = node_parent( v );
        while ( v != root && node_link(v)==NULL )
        {
            path_prepend( q, node_len(v) );
            v = node_parent( v );
        }
        if ( v != root )
        {
            v = node_link( v );
            p = walk_down( v, q );
        }
        else
        {
            path_dispose( q );
            p = walk_down( root, path_create(j,i-j+1) );
        }
    }
    last = *p;
    return p;
}
/*
 *display contents of data files
 *
 * mode = 0: just display number of pointers at each level
 * mode = 1: display all data
 */
void show_btree(Btree *bt, FILE *f, int mode) {
    outfile = f;
    if (mode) {
        show_basic(bt, NDX);
        show_basic(bt, DAT);
    }

    index_blksize = SIZES(NDX, block_size);
    walk_down(bt, NDX, FDATA(NDX, root_block), 0, mode);
}
Esempio n. 6
0
void getSpecialKeyPress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_UP:
		walk_up();
		break;
	case GLUT_KEY_DOWN:
		walk_down();
		break;
	case GLUT_KEY_LEFT:
		walk_left();
		break;
	case GLUT_KEY_RIGHT:
		walk_right();
		break;
	}
}
Esempio n. 7
0
void getKeyboardPress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
	case 'w':
		walk_up();
		break;
	case 's':
		walk_down();
		break;
	case 'a':
		//cout << "a";
		walk_left();
		break;
	case 'd':
		walk_right();
		break;
	case 'q':
		turn_left();
		break;
	case 'e':
		turn_right();
		break;
	case 'f':
		if ((CreatFlower == false) && (CreatTree == false))
		{
			Flower* tem = new Flower;
			int temx, temy, temz;
			get_Groung_position(mouse_x, mouse_y, temx, temy, temz);
			tem->setPosition(temx, temz);
			tem->setAlpha(0.35f);
			flower_display_list.push_back(tem);
			CreatFlower = true;
		}
		coutstat();
		break;
	case 't':
		if ((CreatFlower == false) && (CreatTree == false))
		{
			Tree* tem = new Tree;
			int temx, temy, temz;
			get_Groung_position(mouse_x, mouse_y, temx, temy, temz);
			tem->setPosition(temx, temz);
			tree_display_list.push_back(tem);
			CreatTree = true;
		}
		coutstat();
		break;
	case 'z':
		save_all();
		break;
	case 'l':
		load_all();
		break;
	case 'r':
		if (CreatFlower == true)
		{
			flower_display_list[flower_display_list.size() - 1]->change_petal_R(0.1f);
		}
		coutstat();
		break;
	case 'g':
		if (CreatFlower == true)
		{
			flower_display_list[flower_display_list.size() - 1]->change_petal_G(0.1f);
		}
		coutstat();
		break;

	case 'b':
		if (CreatFlower == true)
		{
			flower_display_list[flower_display_list.size() - 1]->change_petal_B(0.1f);
		}
		coutstat();
		break;

	case 'n':
		if (CreatFlower == true)
		{
			flower_display_list[flower_display_list.size() - 1]->change_petal_NUM(1);
		}
		if (CreatTree == true)
		{
			tree_display_list[tree_display_list.size() - 1]->change_tree_level(1);
		}
		coutstat();
		break;

	case 'm':
		if (CreatFlower == true)
		{
			flower_display_list[flower_display_list.size() - 1]->change_petal_NUM(-1);
		}
		if (CreatTree == true)
		{
			tree_display_list[tree_display_list.size() - 1]->change_tree_level(-1);
		}
		coutstat();
		break;

	case 'p':
		is_snow = 1 - is_snow;
		snowcount = 0;
		coutstat();
		break;

	case 'o':
		if (have_wind) stop_wind();
		else start_wind();
		coutstat();
		break;

	case 'i':
		snow_down();
		break;

	case 'j':
		change_snow_level(-5);
		coutstat();
		break;

	case 'k':
		change_snow_level(5);
		coutstat();
		break;
	}

	

}
Esempio n. 8
0
void build(){
    //build s[0]
    node *first = new node(0, e, NULL, root, 0);
    root->child[str[0]] = first;
    current = first;
    last_non = 0;
    remain_len = 1;
    pre = NULL;
    //build phase s[i + 1] from s[i]
    for(int i = 0; i < len - 1; ++i){
        //implicity do extensions to s[0, last_non]
        *e = i + 1;
        //build s[j + 1, i + 1] from s[j, i]
        //current is the last node(s[j, i] for consitency)
        //for the s[last_non + 1, i + 1], current is the s[ last_non, i]
        current = root->child[str[last_non]];
        remain_len = walk_down(&current, i - last_non + 1);
        printf("last_non remain_len %d %d\n", last_non, remain_len);
        for(int j = last_non + 1; j <= i + 1; ++j){
            printf("current->leaf_index %d\n", current->leaf_index);
            if(current->leaf_index < 0){ // at the internal node
                    current = current->suffix_link;
            }
            else{
                current = current->parent;
                printf("current is root %d\n", current == root);
                if(current != root){
                    remain_len = remain_len + edge_len(current);
                    current = current->suffix_link;
                }
                else{
                    current = root->child[str[j]];
                    remain_len--;
                }
            }
            printf("origin remain_len %d\n", remain_len);
            remain_len = walk_down(&current, remain_len);
            printf("after remain_len %d\n", remain_len);
            if(pre != NULL){
                if(current->leaf_index >= 0){ //now at a leaf
                    pre->suffix_link = current->parent;
                }
                else{
                    pre->suffix_link = current;
                }
            }
            if(edge_len(current) == remain_len){ //rule 1
                puts("rule 1");
                current->end = e;
                last_non = j;
                pre = NULL;
                current->leaf_index = j;
            }
            else{
                if( str[current->start + remain_len] == str[i + 1]){ //rule 3
                    puts("rule 3");
                    last_non = j - 1;
                    pre = NULL;
                    break;
                }
                else{ //rule 2
                    puts("rule 2");
                    parent = current->parent;
                    int* middle_end = new int(current->start + remain_len - 1);
                    printf("middle_end, remain_len %d %d\n", *middle_end, remain_len);
                    node* middle = new node(current->start, middle_end, NULL, parent, -1);
                    parent->child[str[current->start]] = middle;
                    current->start = (*middle_end) + 1;
                    middle->child[str[current->start]] = current;
                    node *new_leaf = new node(i + 1, e, NULL, middle, j);
                    middle->child[str[i + 1]] = new_leaf;
                    if(pre != NULL){
                        pre->suffix_link = middle;
                    }
                    pre = middle;
                    current = middle->parent;
                    remain_len = edge_len(middle) + edge_len(current);
                    last_non = j;
                }
            }
            //else{ //at the leaf node
                //parent = current->parent;
                //remain_len = remain_len + edge_len(parent);
                //if(parent != NULL){
                    //current = parent->suffix_link;
                    //remain_len = walk_down(&current, remain_len);
                //}
            //}
        }
    }
}
void walk_down(Btree *bt, int f, DISKOFFSET block,
                int level, int mode) {
    char *s, *key;
    BlockControl *bc;
    unsigned offset, keysize, keycount = 0;
    DISKOFFSET *pDof;

    prefix(level);
    if (f == NDX)
        fprintf(outfile,
                "Level %d index block %ld at offset %ld.",
                level, block/index_blksize, block);
    else
        fprintf(outfile, "Data block %ld at offset %ld.",
                          block/SIZES(DAT, block_size), block);

    if (mode)
        fprintf(outfile, "\n");

    s = bt_getblock4read(bt, f, block);
    if (!s) {
        bt->error_code = 7;
        return;
    }

    bc = (BlockControl *) s;

    if (mode) {
        prefix(level);
        fprintf(outfile,
           "blockstate: %s, free data: "
           "%d, parent: %ld, next: %ld\n",
           BlockStates[bc->blockstate],
           bc->bfree,
           bc->parent,
           bc->next);
    }

    if (f == NDX) {
        offset = sizeof(BlockControl);
        while (offset < bc->bfree) {
            keysize = GETKEYSIZE(s + offset);
            key = s + offset;
            pDof = (DISKOFFSET *) (key + keysize);
            if (mode) {
                prefix(level);
                fprintf(outfile,
                       "Offset %d: key '%s', DISKOFFSET %ld\n",
                        offset, key, *pDof);
            }
            offset += keysize + sizeof(DISKOFFSET);
            keycount++;
        }
        if (!mode)
            fprintf(outfile, " %u entries\n", keycount);

        offset = sizeof(BlockControl);
        while (offset < bc->bfree) {
            keysize = GETKEYSIZE(s + offset);
            walk_down(bt,
                      bc->blockstate == sLeaf ? DAT : NDX,
                      *((DISKOFFSET *) (s + offset + keysize)),
                      level + 1, mode);

            /* recursive calls may invalidate our buffer */
            s = bt_getblock4read(bt, f, block);
            bc = (BlockControl *) s;
            offset += keysize + sizeof(DISKOFFSET);
        }
    }
    else { /* f == DAT */
        offset = sizeof(BlockControl);
        while (offset < bc->bfree) {
            keysize = GETRECSIZE(s + offset);
            if (mode) {
                prefix(level);
                fprintf(outfile,
                    "Offset %d: key '%s', name: %s, addr: %s\n",
                    offset,
                       ((UR *) (s + offset))->key,
                       ((UR *) (s + offset))->name,
                       ((UR *) (s + offset))->addr);
            }
            offset += keysize;
            keycount++;
        }
        if (!mode)
            fprintf(outfile, " %u entries\n", keycount);
    }
}
Esempio n. 10
0
void build() {
    //build s[0];
    root->child[to_int(str[0])] = get_next(0, e, NULL, root, 0);
    //update s[j - 1, i - 1] to s[j, i - 1]
    //build s[j, i] from s[j, i - 1]
    //current is at s[j - 1, i - 1] (leaf node or the internal node)
    //for the s[ last_non + 1, i], current should be s[ last_non, i - 1]
    current = root->child[to_int(str[0])];
    //last_non is the last non extension in the s[ j - 1, i - 1]
    //remain_len is the left part in current node
    //base is the index in the str to start match
    int last_non = 0, remain_len = 0, base = 0;
    for(int i = 1; i <= len; ++i) {
        pre = NULL;
        current = root;
        base = last_non;
        remain_len = i - last_non;
        walk_down(&current, remain_len, base);
        for(int j = last_non + 1; j <= i; ++j) {
            //explict extentions for last_non + 1...i
            //update s[ j - 1, i - 1] to s[j, i - 1]
            if(current->leaf_index >= 0) {
                //now at the leaf, should go up to the internal node
                base = current->start;
                remain_len = edge_len(current);
                current = current->parent;
            }
            if(current == root) {
                base = j;
                remain_len = i - j;
            }
            else {
                current = current->suffix_link;
            }
            walk_down(&current, remain_len, base);
            if(pre != NULL) {
                pre->suffix_link = current;
            }
            if(remain_len == 0) {
                if(current->child[to_int(str[i])] == NULL) {
                    //rule 2
                    //puts("rule 2");
                    //printf("j %d, str[j] %c, current->leafindex %d current->start %d\n", j, str[j], current->leaf_index, current->start);
                    node *new_node = get_next(i, e, NULL, current, j);
                    current->child[to_int(str[i])] = new_node;
                    pre = NULL;
                    last_non = j;
                }
                else {
                    //rule 3
                    //puts("rule 3");
                    pre = NULL;
                    break;
                }
            }
            else {
                node *next = current->child[to_int(str[base])];
                //printf("remain_len %d\n", remain_len);
                if(str[next->start + remain_len] != str[i]) {
                    //rule 2
                    //puts("split rule 2");
                    int *middle_end = new int(next->start + remain_len - 1);
                    //printf("next->start %d middle_end %d\n", next->start, *middle_end);
                    node *new_middle = get_next(next->start, middle_end, NULL, current, -1);
                    current->child[to_int(str[base])] = new_middle;
                    next->start = (*middle_end) + 1;
                    node *new_leaf = get_next(i, e, NULL, new_middle, j);
                    new_middle->child[to_int(str[i])] = new_leaf;
                    new_middle->child[to_int(str[next->start])] = next;
                    //printf("new_middle start %d end %d leaf_index %d\n", new_middle->start, *(new_middle->end), new_middle->leaf_index);
                    last_non = j;
                    if(pre != NULL) {
                        pre->suffix_link = new_middle;
                    }
                    pre = new_middle;
                }
                else {
                    //rule 3
                    //puts("rule 3");
                    pre = NULL;
                    break;
                }
            }
        }
        //implict extentions for 0...last_non extensions
        (*e) = i;
    }
}
Esempio n. 11
0
void build(){
    //build s[0]
    node *first = get_next(0, e, NULL, root, 0);
    root->child[str[0]] = first;
    current = root;
    base = 0;
    last_non = 0;
    remain_len = 0;
    pre = NULL;
    int last_extension = 2;
    node *last_current = root;
    int last_base = 0, last_remain = 1;
    //build phase s[i + 1] from s[i]
    for(int i = 0; i < len; ++i){
        //build s[j + 1, i + 1] from s[j, i]
        //current is the last node(s[j, i] for consitency)
        //for the s[last_non + 1, i + 1], current is the s[ last_non, i]
        base = last_non;
        remain_len = i - last_non + 1;
        current = root;
        walk_down(&current, remain_len, base);
        if(current->leaf_index >= 0){
            remain_len = edge_len(current);
            base = current->start;
            current = current->parent;
        }
        //current = last_current;
        //remain_len = last_remain;
        //base = last_base;
        //if(last_extension == -1){
            //remain_len = i -last_non + 1;
        //}
        //base = i - remain_len + 1;
        //printf("current->leafindex, last_extension %d %d\n", current->leaf_index, last_extension);
        //printf("remain_len, base %d %d\n", remain_len, base);
        pre = NULL;
        //printf("remain_len, base %d %d\n", remain_len, base);
        last_extension = -1;
        for(int j = last_non + 1; j <= i + 1; ++j){
            //printf("current->leaf_index %d\n", current->leaf_index);
            if(current == root){
                base = j;
                remain_len = i - j + 1;
            }
            else{
                current = current->suffix_link;
            }
            //printf("origin remain_len, base %d %d\n", remain_len, base);
            walk_down(&current, remain_len, base);
            //printf("after remain_len,base %d %d\n", remain_len, base);
            if(pre != NULL){
                if(current->leaf_index >= 0){ //now at a leaf
                    pre->suffix_link = current->parent;
                }
                else{
                    pre->suffix_link = current;
                }
            }
            if(current->leaf_index >= 0){ //rule 1
                puts("rule 1");
                remain_len = edge_len(current);
                current->end = e;
                last_non = j;
                base = current->start;
                pre = NULL;
                current->leaf_index = j;
                current = current->parent;
                last_extension = 1;
                last_current = current;
                last_base = base;
                last_remain = remain_len + 1;
            }
            else{
                if(remain_len == 0){
                    if(current->child.find(str[i + 1]) == current->child.end()){
                        //puts("new leaf added in(rule 2)");
                        node* new_leaf = get_next(i + 1, e, NULL, current, j);
                        current->child[str[i + 1]] = new_leaf;
                        last_non = j;
                        remain_len = 0;
                        base = new_leaf->start;
                        pre = NULL;
                        current = new_leaf->parent;
                        last_extension = 2;
                        last_current = current;
                        last_base = base;
                        last_remain = 1;
                        continue;
                    }
                    else{
                        //puts("rule 3");
                        //node *next = current->child[str[i + 1]];
                        //base = next->start;
                        //remain_len = 0;
                        last_non = j - 1;
                        pre = NULL;
                        break;
                    }
                }
                node *next = current->child[str[base]];
                //printf("next->leafindex %d\n", next->leaf_index);
                if( str[next->start + remain_len] == str[i + 1]){ //rule 3
                    //puts("rule 3");
                    //base = next->start;
                    last_non = j - 1;
                    pre = NULL;
                    break;
                }
                else{ //rule 2
                    //puts("rule 2");
                    int* middle_end = new int(next->start + remain_len - 1);
                    //printf("middle_end, remain_len %d %d\n", *middle_end, remain_len);
                    node* middle = get_next(next->start, middle_end, NULL, current, -1);
                    //printf("next->start, middle_end , remain_len %d %d %d\n", next->start, *middle_end, remain_len);
                    current->child[str[next->start]] = middle;
                    next->start = (*middle_end) + 1;
                    middle->child[str[next->start]] = next;
                    node *new_leaf = get_next(i + 1, e, NULL, middle, j);
                    middle->child[str[i + 1]] = new_leaf;
                    if(pre != NULL){
                        pre->suffix_link = middle;
                    }
                    pre = middle;
                    current = middle->parent;
                    remain_len = edge_len(middle);
                    base = middle->start;
                    last_non = j;
                    last_extension = 2;
                    last_current = current;
                    last_base = new_leaf->start - remain_len;
                    //printf("last_base %d\n", last_base);
                    last_remain = remain_len + 1;
                    //puts("rule 2 finished");
                }
            }
        }
        //implicity do extensions to s[0, last_non]
        *e = i + 1;
    }
}