コード例 #1
0
ファイル: chooser.cpp プロジェクト: Break-Neck/ant_algo
chooser::pitem chooser::splay(pitem p) {
	if (!p) {
		return nullptr;
	}
	while (p->parent) {
		if (!p->parent->parent) {
			if (is_left_child(p)) {
				right_rotale(p->parent);
			} else {
				left_rotale(p->parent);
			}
		} else if (is_left_child(p) ^ is_left_child(p->parent)) {
			if (is_left_child(p)) {
				right_rotale(p->parent);
				left_rotale(p->parent);
			} else {
				left_rotale(p->parent);
				right_rotale(p->parent);
			}
		} else {
			if (is_left_child(p)) {
				right_rotale(p->parent->parent);
				right_rotale(p->parent);
			} else {
				left_rotale(p->parent->parent);
				left_rotale(p->parent);
			}
		}
	}
	return p;
}
コード例 #2
0
ファイル: chooser.cpp プロジェクト: Break-Neck/ant_algo
chooser::pitem chooser::delete_element(pitem p) {
	pitem res = nullptr;
	if (!p->L && !p->R) {
		if (p->parent) {
			res = p->parent;
			if (is_left_child(p)) {
				res->L = nullptr;
			} else {
				res->R = nullptr;
			}
		}
		delete p;
	} else if (!p->L || !p->R) {
		if (p->parent) {
			res = p->parent;
			if (is_left_child(p)) {
				res->L = p->L ? p->L : p->R;
			} else {
				res->R = p->L ? p->L : p->R;
			}
			p->L = p->R = nullptr;
			delete p;
		} else {
			if (p->L) {
				right_rotale(p);
			} else {
				left_rotale(p);
			}
			return delete_element(p);
		}
	} else {
		pitem q = leftmost_child(p->R);
		if (q != p->R) {
			std::swap(p->L, q->L);
			std::swap(p->R, q->R);
			recalc(p);
			recalc(q);
			const pitem p_par = p->parent;
			make_parent(q->parent, p);
			make_parent(p_par, q);
		} else {
			left_rotale(p);
		}
		assert(!p->L || !p->R);
		delete_element(p);
		res = q->parent ? q->parent : q;
	}
	recalc(res);
	return splay(res);
}
コード例 #3
0
ファイル: common-rbtree0.c プロジェクト: cahirwpz/mneme
node_t *node_remove(node_t *n)
{
	if (is_leaf(n))
	{
		// przypadek 1: jest liściem

		// usuń wskaźnik z ojca
		if (is_left_child(n))
			parent(n)->left = NULL;
		else
			parent(n)->right = NULL;
	}
	else if (has_one_child(n))
	{
		// przypadek 2: tylko jedno dziecko
		node_t *child = (!is_nil(n->left)) ? (n->left) : (n->right);

		// niech dziadek stanie się ojcem
		parent(child) = parent(n);

		// podczep dziecko jako lewego lub prawego syna rodzica usuwanego węzła
		if (is_left_child(n))
			parent(child)->left = child;
		else
			parent(child)->right = child;
	}
	else
	{
		// przypadek 3: dwoje dzieci

		// znajdź poprzednika
		node_t *next = n->left;

		while (!is_nil(next->right))
			next = next->right;

		// zamień dane następnika z węzłem
		node_swap_data(next, n);

		n = node_remove(next);
	}

	n->left = NULL;
	n->right = NULL;
	parent(n) = NULL;

	return n;
}
コード例 #4
0
ファイル: common-rbtree0.c プロジェクト: cahirwpz/mneme
void node_rotate_left(tree_t *tree, node_t *x)
{
	//printf("@rotate-left %d\n", x->key);

	node_t *y;
	
	y = x->right;

	x->right = y->left;
	
	if (!is_nil(y->left))
		parent(y->left) = x;

	parent(y) = parent(x);

	if (is_nil(parent(x)))
	{
		tree->root = y;
	}
	else
	{
		if (is_left_child(x))
			parent(x)->left = y;
		else
			parent(x)->right = y;
	}
	
	y->left = x;

	parent(x) = y;
}
コード例 #5
0
ファイル: stateful_sequence.cpp プロジェクト: CCJY/engine-1
bool stateful_sequence::evaluate_true(cep_state* source, cep_state* target, tuple_ptr match) {
        universal_state* target2 = (universal_state*)target;
        if(is_left_child(source)) {
                // start a right-hand expression with match as context
		cep_state_ptr next = get_right_child()->create_state(match, target2);
		boost::any_cast<state_type_ptr>(target2->get_property("right_states"))->insert(next);
                return false;
        } else { 
                if(selection == selection::FIRST) {
                        if(boost::any_cast<bool>(target2->get_property("block"))) {
                                // return first result and no more (block=false)
                                target2->set_property("block", bool(false));
                                send_true(target, match);
                                
                                // selection = FIRST and consumption = ALL is faster
                                // when we stop all child nodes immediately
                                if(consumption == consumption::ALL) {
                                        boost::any_cast<state_type_ptr>(target2->get_property("right_states"))->clear();
                                }
                        }
                } else { /* selection == selection::ALL */
                        send_true(target, match);
                }
                
                if(consumption == consumption::MATCHING) {
                        boost::any_cast<state_type_ptr>(target2->get_property("right_states"))->erase(cep_state_ptr(target));
                }
	}
}
コード例 #6
0
ファイル: common-rbtree0.c プロジェクト: cahirwpz/mneme
void rb_tree_insert_repair(tree_t *tree, node_t *n)
{
	// przypadek 1: korzeń
    if (is_nil(parent(n)))
	{
        n->color = BLACK;
		return;
	}
    
	// przypadek 2: rodzic czarny
	if (parent(n)->color == BLACK)
		return;

	// przypadek 3: rodzic czerwony; wuj czerwony;
	if (!is_nil(uncle(n)) && uncle(n)->color == RED)
	{
		parent(n)->color = BLACK;
		uncle(n)->color = BLACK;
		grandparent(n)->color = RED;

		rb_tree_insert_repair(tree, grandparent(n));
	}
	else
	// przypadek 4: rodzic czerwony; wuj czarny;
	{
		if (is_right_child(n) && is_left_child(parent(n)))
		{
			node_rotate_left(tree, parent(n));
			n = n->left;
		}
		else if (is_left_child(n) && is_right_child(parent(n)))
		{
			node_rotate_right(tree, parent(n));
			n = n->right;
		}

		// case 5: wuj czarny; nowy, rodzic, dziadek na prostej; nowy i rodzic czerwoni;
		parent(n)->color = BLACK;
		grandparent(n)->color = RED;

		if (is_left_child(n) && is_left_child(parent(n)))
			node_rotate_right(tree, grandparent(n));
		else
			node_rotate_left(tree, grandparent(n));
	}
}
コード例 #7
0
		Bit side() const {
			if (is_left_child()) {
				return Bit::zero;
			} else {
				assert(is_right_child());
				return Bit::one;
			}
		}
コード例 #8
0
ファイル: cowtree.cpp プロジェクト: caomw/slam-4
cowtree::editor::~editor () {
    if (!subtree_ptr->is_black()) {
        if (is_root()) {
            subtree_ptr->set_black(true);
        }
        else if (!parent_ptr->subtree_ptr->is_black()) {
            if (is_left_child() != parent_ptr->is_left_child()) {
                rotate();
            }
            subtree_ptr->set_black(true);
            parent_ptr->rotate();
        }
    }
}
コード例 #9
0
ファイル: cowtree.cpp プロジェクト: caomw/slam-4
void cowtree::editor::rotate () {
    assert (!is_root());
    if (is_left_child()) {
        parent_ptr->subtree_ptr->swap(subtree_ptr->right());
        parent_ptr->subtree_ptr->swap(*subtree_ptr);
        subtree_ptr = &parent_ptr->subtree_ptr->right();
    }
    else {
        parent_ptr->subtree_ptr->swap(subtree_ptr->left());
        parent_ptr->subtree_ptr->swap(*subtree_ptr);
        subtree_ptr = &parent_ptr->subtree_ptr->left();
    }
    assert (subtree_ptr != nullptr);
}
コード例 #10
0
ファイル: common-rbtree0.c プロジェクト: cahirwpz/mneme
node_t *rb_tree_remove_node(tree_t *tree, node_t *n)
{
	node_t *c, *old_n = n;

	if (!is_nil(n->left) && !is_nil(n->right))
	{
		// znajdź następnika
		node_t *next = n->right;

		while (!is_nil(next->left))
			next = next->left;

		old_n = n;
		n = next;
	}

	c = (!is_nil(n->left)) ? (n->left) : (n->right);

	if (is_nil(c))
		parent(c) = n;

	parent(c) = parent(n);

	if (is_nil(parent(n)))
	{
		tree->root = c;
	}
	else
	{
		if (is_left_child(n))
			parent(n)->left = c;
		else
			parent(n)->right = c;
	}

	if (old_n != n)
		node_swap_data(old_n, n);

	if (n->color == BLACK)
		rb_tree_delete_repair(tree, c);

	n->left = NULL;
	n->right = NULL;
	parent(n) = NULL;

	return n;
}
コード例 #11
0
ファイル: heap.c プロジェクト: saurabhd14/tinyos-1.x
node_t* prev(node_t* node) {
  node_t* next_node = node;
  
  if (!is_root(node)) {
    while (is_left_child(next_node)) {
      next_node = next_node->parent;
    }
  }

  if (!is_root(next_node)) {
    next_node = next_node->parent->left;
  }

  while (next_node->right != NULL) {
    next_node = next_node->right;
  }
  return next_node;
}
コード例 #12
0
ファイル: chooser.cpp プロジェクト: Break-Neck/ant_algo
inline void chooser::right_rotale(pitem p) {
	assert(p->L);
	pitem q = p->L;
	if (p->parent) {
		if (is_left_child(p)) {
			p->parent->L = q;
		} else {
			p->parent->R = q;
		}
	}
	p->L = q->R;
	q->R = p;
	recalc(p->L);
	recalc(p);
	make_parent(p->parent, q);
	make_parent(q, p);
	recalc(q);
	recalc(q->parent);
}
コード例 #13
0
ファイル: rbtree.c プロジェクト: drewt/navi-scheme
static struct rb_node *rb_sibling(struct rb_node *n)
{
	if (is_left_child(n))
		return n->parent->right;
	return n->parent->left;
}
コード例 #14
0
ファイル: common-rbtree0.c プロジェクト: cahirwpz/mneme
void rb_tree_delete_repair(tree_t *tree, node_t *n)
{
	// przypadek 1: korzeń lub czerwony
    if (is_nil(parent(n)) || n->color == RED)
        return;
    
	// przypadek 2:
    if (sibling(n)->color == RED)
	{
        parent(n)->color = RED;
        sibling(n)->color = BLACK;

        if (is_left_child(n))
            node_rotate_left(tree, parent(n));
        else
            node_rotate_right(tree, parent(n));
    }

	// przypadek 3: 
    if (parent(n)->color == BLACK && sibling(n)->color == BLACK && sibling(n)->left->color == BLACK && sibling(n)->right->color == BLACK)
    {
        sibling(n)->color = RED;
        rb_tree_delete_repair(tree, parent(n));
		return;
    }

	// przypadek 4:
    if (parent(n)->color == RED && sibling(n)->color == BLACK && sibling(n)->left->color == BLACK && sibling(n)->right->color == BLACK)
    {
        sibling(n)->color = RED;
        parent(n)->color = BLACK;
		return;
    }

	// przypadek 5
    if (is_left_child(n) && sibling(n)->color == BLACK && sibling(n)->left->color == RED && sibling(n)->right->color == BLACK)
    {
        sibling(n)->color = RED;
        sibling(n)->left->color = BLACK;

        node_rotate_right(tree, sibling(n));
    }
    else if (is_right_child(n) && sibling(n)->color == BLACK &&	sibling(n)->right->color == RED && sibling(n)->left->color == BLACK)
    {
        sibling(n)->color = RED;
        sibling(n)->right->color = BLACK;

        node_rotate_left(tree, sibling(n));
    }

	// przypadek 6:
    sibling(n)->color = parent(n)->color;
    parent(n)->color = BLACK;

    if (is_left_child(n))
	{
        sibling(n)->right->color = BLACK;

        node_rotate_left(tree, parent(n));
    }
    else
    {
        sibling(n)->left->color = BLACK;

        node_rotate_right(tree, parent(n));
    }
}