Ejemplo n.º 1
0
/*
 * ALGORITHM
 *
 * Every test of a constant expression gets turned into a jump.
 */ 
int optimise_constant_tests(MODULE *module, FUNCTION *func)
{
    int changed = 0;
    int i;
    
    for (i = 0; i < tree_num_children(func->graph); i++)
    {
        NODE *vertex = tree_get_child(func->graph, i);
        if (!vertex)
            continue;
        
        if (!tree_is_type(vertex, STMT_TEST))
            continue;

        EXPRESSION *expr = tree_get_child(vertex, 0);
        if (!tree_is_type(expr, EXPR_INTEGER))
            continue;
        
        int x = CAST_TO_INTEGER(expr)->value;
        
        EDGE_TYPE target_type = x ? EDGE_YES : EDGE_NO;
        NODE *target_succ = get_successor(func->graph, vertex, target_type);
        
        /* Replace with a jump.  Test vertex remains will be clean up by dead code removal. */
        replace_backward(func->graph, vertex, target_succ, 0);
        changed = 1;
    }
    
    return changed;
};
Ejemplo n.º 2
0
void delete_node(RB_tree *tree, short int key){
    RB_node *node = get_node(tree, key);
    if(node != tree->nil){
        RB_node *y, *x;
        if(node->left == tree->nil || node->right == tree->nil){
            y = node;
        }else{
            y = get_successor(node, tree->nil);
        }
        if(y->left != tree->nil){
            x = y->left;
        }else{
            x = y->right;
        }

        x->parent = y->parent;

        if(y->parent == tree->nil){
            tree->root = x;
        }else if(is_left(y)){
            y->parent->left = x;
        }else{
            y->parent->right = x;
        }
        if(y != node){
            node->key = y->key;
        }

        if(y->color == BLACK){
            RB_delete_fixup(tree, x);
        }

        free(y);
    }
}
Ejemplo n.º 3
0
void RBTree::remove(int v)
{
    rbnode * n = find(v);
    if (n == pNil) {
        return;
    }

    rbnode * y = pNil;
    if (n->lc == pNil || n->rc == pNil) {
        y = n;
    } else {
        y = get_successor(n);
        n->value = y->value;
    }

    rbnode * x = (y->lc == pNil ? y->rc : y->lc);

    x->parent = y->parent;

    rbnode * p = y->parent;
    if (p == pNil) {
        root = x;
    } else if (y == p->lc) {
        p->lc = x;
    } else {
        p->rc = x;
    }

    if (y->color == BLACK) {
        remove_fixup(x);
    }
}
Ejemplo n.º 4
0
int IterativeDeepening::dfs_search(LiteState& current, int& bound, std::stack<LiteState>& path, Timer& timer) {
	
    if(!timer.is_still_valid()){
        return current.get_g();
    }
	
	// Perform heuristic evaluation
    double startTime = omp_get_wtime();
	int h = heuristic->calc_h(std::make_shared<LiteState>(current));
    heuristic_timer += (omp_get_wtime() - startTime);
    current.set_h(h);

	// Uses stack to trace path through state space
	path.push(current);

	// Bound check
	if (current.get_f() > bound){
        // Remove this state from the back of the vector
		path.pop();
		//path.erase(path.begin() + path.size() - 1);
		nodes_rejected++;
		int f = current.get_f();
		state_space.remove(std::shared_ptr<LiteState>(new LiteState(current)));
		return f;
	}

	if (state_space.state_is_goal(current, goals)){

		timer.stop_timing_search();
		print_steps(path);
		solved = true;

        std::cout << "Nodes generated during search: " << nodes_generated << std::endl;
        std::cout << "Nodes expanded during search: " << nodes_expanded << std::endl;

        std::cout << "Sequential search took " << timer.get_search_time() << " seconds." << std::endl;
        std::cout << "Time spent calculating heuristic: " << heuristic_timer << " seconds." << std::endl;
		
		return current.get_f();
	}

	nodes_expanded++;
	int min = Search::protect_goals;

	const std::vector<Operator>& applicable_operators = get_applicable_ops(current);

	for (std::size_t i = 0; i < applicable_operators.size(); i++) {

		const Operator& op = applicable_operators[i];

		// Generate successor state, set path cost
		LiteState child = get_successor(op, current);
		int new_g = current.get_g() + op.get_cost();
		child.set_g(new_g);

		// Check if state has been visited, if it has check if we have reached it in fewer steps (lower g value)
		if (state_space.child_is_duplicate(std::shared_ptr<LiteState>(new LiteState(child)))) {
			nodes_rejected++;
			continue;
		}

		// Record operator
		int op_id = op.get_num();
		child.set_op_id(op_id);

		nodes_generated++;

        int t = 0;
 		// Explore child node
		t = dfs_search(child, bound, path, timer);
		if (t < min){
			min = t;
		}
		// Get out of recursion when done. 
		if (solved){
			break;
		}
	}
	// We have generated no children that have lead to a solution and are backtracking.
	// Erase node as we backtrack through state space
	path.pop();
	return min;
}