Example #1
0
Node* tree_copy(Node* ptr)
{
  if (ptr == 0) {
    return 0;
  } else {
    Node* left_ptr = tree_copy(ptr->left_ptr());
    Node* right_ptr = tree_copy(ptr->right_ptr());
    return new Node(ptr->data(), left_ptr, right_ptr);
  }
}// tree_copy
Example #2
0
/**
 * For binary operations, there are two cases that need special treatment:
 *     a = k # a
 *     a = b # a
 * If # is commutable then these can be reversed.  Otherwise they will be
 * translated to t = a; a = k/b # t.
 * After the first phase there are three cases that are already in i386 form:
 *     a = a # a
 *     a = a # b
 *     a = a # k
 * There are five cases that need processing:
 *     a = k # k   (should really be handled by constant optimisation)
 *     a = k # b
 *     a = b # k
 *     a = b # b
 *     a = b # c
 * All are translated to a = (k/b); a = a # (k/b/c)
 */
static int i386ify_binary_operation(MODULE *module, FUNCTION *func, NODE *vertex)
{
    GRAPH *graph = func->graph;
    VARIABLE *dest = tree_get_child(vertex, 0);
    EXPRESSION *expr = tree_get_child(vertex, 1);
    EXPRESSION *arg0 = tree_get_child(expr, 0);
    EXPRESSION *arg1 = tree_get_child(expr, 1);
    
    int source_line = CAST_TO_AST(vertex)->source_line;
    
    /* First deal with special cases, where the first argument is not the
       destination but the second argument is. */
    if (!is_same_var(CAST_TO_EXPRESSION(dest), arg0) && is_same_var(CAST_TO_EXPRESSION(dest), arg1))
    {
        if (is_commutable_op(expr))
        {
            tree_get_child(expr, 0) = arg1;
            tree_get_child(expr, 1) = arg0;
        }
        else
        {
            TYPE *new_temp_type = arg1->type;
            EXPRESSION *new_temp = make_new_temp(module, func, new_temp_type, source_line);
            STATEMENT *new_assign = make_assignment(new_temp, arg1, source_line);
            tree_get_child(expr, 1) = tree_copy(new_temp);
            add_vertex(graph, CAST_TO_NODE(new_assign));
            replace_backward(graph, vertex, CAST_TO_NODE(new_assign), 0);
            add_edge(graph, CAST_TO_NODE(new_assign), vertex, 0);
        }
        
        /* Reset these as the above operations may have changed them, */
        arg0 = tree_get_child(expr, 0);
        arg1 = tree_get_child(expr, 1);
    }
    
    /* If instruction is already in i386 form, we don't need to process it. */
    if (is_same_var(CAST_TO_EXPRESSION(dest), arg0))
        return 0;
    
    /* Otherwise, translate a = b # c to a = b; a = a # c. */
    STATEMENT *new_assign = make_assignment(CAST_TO_EXPRESSION(tree_copy(dest)), arg0, source_line);
    tree_get_child(expr, 0) = tree_copy(dest);
    add_vertex(graph, CAST_TO_NODE(new_assign));
    replace_backward(graph, vertex, CAST_TO_NODE(new_assign), 0);
    add_edge(graph, CAST_TO_NODE(new_assign), vertex, 0);
        
    return 1;
}
rbtnode* redblacktree::tree_copy(rbtnode* const node, rbtnode* const senti,rbtnode* const newsenti)const{
  if(node == senti ){
    return newsenti;
  }

  rbtnode* result;
  if(node->getfather() == senti){
    result = new rbtnode( newsenti, node->getvalue(), node->getcolor() );
  }
  else{
    result = new rbtnode( node->getfather(), node->getvalue(), node->getcolor() );
  }

  result->setleft(tree_copy(node->getleft(), senti, newsenti));
  result->setright(tree_copy(node->getright(), senti, newsenti));
  return result;
}
Example #4
0
/**
 * For unary operations, one case is already in i386 form:
 *     a = #a
 * There are two cases which need processing:
 *     a = #k   (should really be handled by constant optimisation)
 *     a = #b
 * These are both translated to a = k/b; a = #a
 */
static int i386ify_unary_operation(MODULE *module, FUNCTION *func, NODE *vertex)
{
    GRAPH *graph = func->graph;
    VARIABLE *dest = tree_get_child(vertex, 0);
    EXPRESSION *expr = tree_get_child(vertex, 1);
    EXPRESSION *arg0 = tree_get_child(expr, 0);
    
    if (is_same_var(CAST_TO_EXPRESSION(dest), arg0))
        return 0;
    
    STATEMENT *new_assign = make_assignment(CAST_TO_EXPRESSION(tree_copy(dest)), arg0, CAST_TO_AST(vertex)->source_line);
    tree_get_child(expr, 0) = tree_copy(dest);
    add_vertex(graph, CAST_TO_NODE(new_assign));
    replace_backward(graph, vertex, CAST_TO_NODE(new_assign), 0);
    add_edge(graph, CAST_TO_NODE(new_assign), vertex, 0);
    
    return 1;
}
Example #5
0
File: parser.c Project: edma2/skm
Expr *expr_copy(Expr *orig) {
        Expr *copy;

        if (orig == NULL)
                return NULL;
        copy = tree_copy(orig);
        tree_traverse(copy, expr_copy_helper);

        return copy;
}
Example #6
0
static STATEMENT *reduce_statement(MODULE *module, FUNCTION *func, BLOCK *block, STATEMENT *stmt)
{
    if (stmt == NULL)
        return stmt;
    
    if (tree_is_type(stmt, STMT_ASSIGN))
    {
        EXPRESSION *expr = tree_get_child(stmt, 1);
        expr = simplify_expression(module, func, block, expr, stmt);
        tree_get_child(stmt, 1) = expr;
    }
    else if (tree_is_type(stmt, STMT_IF))
    {
        EXPRESSION *cond = tree_get_child(stmt, 0);
        cond = simplify_expression(module, func, block, cond, stmt);
        tree_get_child(stmt, 0) = cond;
        reduce_block(module, func, tree_get_child(stmt, 1));
        reduce_block(module, func, tree_get_child(stmt, 2));
    }
    else if (tree_is_type(stmt, STMT_WHILE))
    {
        EXPRESSION *cond = tree_get_child(stmt, 0);
        BLOCK *body = tree_get_child(stmt, 1);
        if (!is_atomic(cond))
        {
            EXPRESSION *old_cond = cond;
            cond = atomise_expression(module, func, block, cond, stmt);
            tree_get_child(stmt, 0) = cond;
            STATEMENT *new_assign = make_assignment(cond, CAST_TO_EXPRESSION(tree_copy(old_cond)), CAST_TO_AST(cond)->source_line);
            tree_add_child(body, new_assign);
        }
        reduce_block(module, func, body);
    }
    else if (tree_is_type(stmt, STMT_RETURN))
    {
        EXPRESSION *expr = tree_get_child(stmt, 0);
        expr = atomise_expression(module, func, block, expr, stmt);
        tree_get_child(stmt, 0) = expr;
    }
    else if (tree_is_type(stmt, STMT_RESTART))
    {
        /* Do nothing. */
    }
    else
        error("Not sure how to reduce statement of type %d\n", tree_type(stmt));
    
    return stmt;
}
void TestTTreeFunction(TTreeNode<TestData>* root)
{
	std::cout << std::endl << "======= Test TTree =======" << std::endl;

	TTree<TestData> tree(*root);

	// copy
	TTree<TestData> tree_copy(tree);
	tree_copy.GetRoot()->PreorderTraversal(0, PrintTreeNode);
	tree_copy = tree;
	tree_copy.GetRoot()->PreorderTraversal(0, PrintTreeNode);

	// move
	TTree<TestData> tree_swap(std::move(tree_copy));
	MASSERT(tree_copy.GetRoot() == nullptr);
	tree_swap.GetRoot()->PreorderTraversal(0, PrintTreeNode);
	tree_swap = std::move(tree);
	MASSERT(tree.GetRoot() == nullptr);
	tree_swap.GetRoot()->PreorderTraversal(0, PrintTreeNode);
}
binary_tree_node* binary_search_tree_copy(const binary_tree_node* source_root_ptr)
{
    binary_tree_node* n = tree_copy(source_root_ptr);
    return n;
}
void init(binary_tree_node*& root_ptr, const binary_tree_node* source_root_ptr)
// Library facilities used: bintree.h
{
    root_ptr = tree_copy(source_root_ptr);
}
Example #10
0
static cb_ret_t
tree_execute_cmd (WTree * tree, long command)
{
    cb_ret_t res = MSG_HANDLED;

    if (command != CK_Search)
        tree->searching = 0;

    switch (command)
    {
    case CK_Help:
        {
            ev_help_t event_data = { NULL, "[Directory Tree]" };
            mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
        }
        break;
    case CK_Forget:
        tree_forget (tree);
        break;
    case CK_ToggleNavigation:
        tree_toggle_navig (tree);
        break;
    case CK_Copy:
        tree_copy (tree, "");
        break;
    case CK_Move:
        tree_move (tree, "");
        break;
    case CK_Up:
        tree_move_up (tree);
        break;
    case CK_Down:
        tree_move_down (tree);
        break;
    case CK_Top:
        tree_move_home (tree);
        break;
    case CK_Bottom:
        tree_move_end (tree);
        break;
    case CK_PageUp:
        tree_move_pgup (tree);
        break;
    case CK_PageDown:
        tree_move_pgdn (tree);
        break;
    case CK_Enter:
        tree_chdir_sel (tree);
        break;
    case CK_Reread:
        tree_rescan (tree);
        break;
    case CK_Search:
        tree_start_search (tree);
        break;
    case CK_Delete:
        tree_rmdir (tree);
        break;
    case CK_Quit:
        if (!tree->is_panel)
            dlg_stop (WIDGET (tree)->owner);
        return res;
    default:
        res = MSG_NOT_HANDLED;
    }

    show_tree (tree);

    return res;
}
Example #11
0
File: tree.c Project: ebichu/dd-wrt
static void
tree_copy_cmd (void *data)
{
    WTree *tree = data;
    tree_copy (tree, "");
}
Example #12
0
	bag<Item>::bag(const bag& source) {
		root_ptr = tree_copy(source.root_ptr);
	}