Ejemplo n.º 1
0
void rbtree_node::remove(rbtree_node** root)
{
	rbtree_node* node = this;
	rbtree_node* child;
	rbtree_node* parent;
	int color;

	//
	// Satisfy Precondition: node has at most one non-null child
	//
	if (!node->left) {
		child = node->right;

	} else if (!node->right) {
		child = node->left;

	} else {
		rbtree_node* old = node;

		//
		// Locate a node that satisfies the precondition to swap with the node to be removed
		//
		node = node->right;
		while (node->left)
			node = node->left;

		//
		// Node satisfies precondition
		//
		child = node->right;

		//
		// Swap nodes (swap <-> (node <-> child))
		//
		parent = node->parent();
		color = node->color();

		if (child)
			child->parent(parent);

		if (parent == old) {
			parent->right = child;
			parent = node;
		} else {
			parent->left = child;
		}

		node->_parent = old->_parent;
		node->left = old->left;
		node->right = old->right;

		if (old->parent()) {
			UL_ASSERT((old->parent()->right == old) || (old->parent()->left == old));

			if (old->parent()->left == old) {
				old->parent()->left = node;

			} else {
				old->parent()->right = node;
			}

		} else {
			*root = node;
		}

		old->left->parent(node);
		if (old->right)
			old->right->parent(node);

		goto rm_color;
	}

	//
	// Replace Node with Child
	//
	parent = node->parent();
	color = node->color();
	if (child) {
		child->parent(parent);
	}
	if (parent) {
		UL_ASSERT((parent->right == node) || (parent->left == node));

		if (parent->left == node) {
			parent->left = child;

		} else {
			parent->right = child;
		}

	} else {
		*root = child;
	}

rm_color:
	//
	// Adjust Red-Black tree properties if node is black
	//
	if (color == black) {
		remove_color(child, parent, root);
	}

	UL_ASSERT(!*root || (*root)->color() == black);

	//
	// Bug prevention
	//
	this->_parent = 0;
	this->left = nullptr;
	this->right = nullptr;
}
Ejemplo n.º 2
0
void rbtree_hook::remove(rbtree_hook** root)
{
	rbtree_hook* node = this;
	rbtree_hook* child;
	rbtree_hook* parent;
	int color;

	//
	// Satisfy Precondition: node has at most one non-null child
	//
	if (!node->_left) {
		child = node->_right;

	} else if (!node->_right) {
		child = node->_left;

	} else {
		rbtree_hook* old = node;

		//
		// Locate a node that satisfies the precondition to swap with the node to be removed
		//
		node = node->_right;
		while (node->_left)
			node = node->_left;

		//
		// Node satisfies precondition
		//
		child = node->_right;

		//
		// Swap nodes (swap <-> (node <-> child))
		//
		parent = node->get_parent();
		color = node->_color;

		if (child)
			child->set_parent(parent);

		if (parent == old) {
			parent->_right = child;
			parent = node;
		} else {
			parent->_left = child;
		}

		node->_parent = old->_parent;
		node->_left = old->_left;
		node->_right = old->_right;

		if (old->get_parent()) {
			BOOST_ASSERT((old->get_parent()->_right == old) || (old->get_parent()->_left == old));

			if (old->get_parent()->_left == old) {
				old->get_parent()->_left = node;

			} else {
				old->get_parent()->_right = node;
			}

		} else {
			*root = node;
		}

		old->_left->set_parent(node);
		if (old->_right)
			old->_right->set_parent(node);

		goto rm_color;
	}

	//
	// Replace Node with Child
	//
	parent = node->get_parent();
	color = node->_color;
	if (child) {
		child->set_parent(parent);
	}
	if (parent) {
		BOOST_ASSERT((parent->_right == node) || (parent->_left == node));

		if (parent->_left == node) {
			parent->_left = child;

		} else {
			parent->_right = child;
		}

	} else {
		*root = child;
	}

rm_color:
	//
	// Adjust Red-Black tree properties if node is black
	//
	if (color == black) {
		remove_color(child, parent, root);
	}

	//
	// Sanity check's
	//
	BOOST_ASSERT(!*root || (*root)->_color == black);

#ifndef NDEBUG
	this->_parent = nullptr;
	this->_left = nullptr;
	this->_right = nullptr;
#endif /* NDEBUG */
}
Ejemplo n.º 3
0
input_t dialog_mouse_command(MEVENT event)
{
	Vector2i pos = abs2rel((Vector2i) { event.y, event.x }, dialog_pos), tl;
	bool del = FALSE;
	color_t i;

	if (cidx != CIDX_DEFAULT) {
		if (area_contains(left_pos, DIALOG_BUTTON_WIDTH, DIALOG_BUTTON_HEIGHT, pos)) {
			picked_turn = TURN_LEFT;
			goto exit;
		}
		if (area_contains(right_pos, DIALOG_BUTTON_WIDTH, DIALOG_BUTTON_HEIGHT, pos)) {
			picked_turn = TURN_RIGHT;
			goto exit;
		}
	}

	if (cidx >= 0 && area_contains(delete_pos, DIALOG_DELETE_WIDTH, DIALOG_DELETE_HEIGHT, pos)) {
		del = TRUE;
		goto exit;
	}

	for (i = 0; i < COLOR_COUNT; i++) {
		tl = get_dialog_tile_pos(i);
		if ((cidx == CIDX_DEFAULT || !color_exists(stgs.colors, i))
				&& area_contains(tl, DIALOG_TILE_SIZE, DIALOG_TILE_SIZE, pos)) {
			picked_color = i;
			goto exit;
		}
	}

	return INPUT_NO_CHANGE;

exit:
	switch (cidx) {
	case CIDX_NEWCOLOR:
		if (picked_color != COLOR_NONE && picked_turn != TURN_NONE) {
			add_color(stgs.colors, picked_color, picked_turn);
			close_dialog();
		}
		return INPUT_MENU_CHANGED;
	case CIDX_DEFAULT:
		assert(stgs.colors && stgs.linked_sim->colors);
		colors_delete(stgs.colors);
		stgs.colors = colors_new(picked_color);
		close_dialog();
		return clear_simulation();
	default:
		if (cidx < 0 || cidx >= COLOR_COUNT) {
			return INPUT_NO_CHANGE;
		}
		if (picked_turn != TURN_NONE) {
			if (picked_color != COLOR_NONE) {
				set_color(stgs.colors, cidx, picked_color, picked_turn);
			} else {
				set_turn(stgs.colors, cidx, picked_turn);
			}
			close_dialog();
			return INPUT_MENU_CHANGED;
		} else if (del) {
			remove_color(stgs.colors, get_color_at(stgs.colors, cidx));
			if (!has_enough_colors(stgs.colors)) {
				simulation_halt(stgs.linked_sim);
			}
			close_dialog();
		}
		return INPUT_MENU_CHANGED;
	}
}
Ejemplo n.º 4
0
Archivo: main.c Proyecto: purchae/xtc
int main(int argc, char* argv[]) {

	int screen_height, screen_width;
#ifdef HAVE_DEFINE_KEY
	char key_string[10];
#endif
	char *termtype;

	config_load();
	initscr();
	if ((in_color = has_colors())) start_color();
	hide_cursor();
	raw();        /* immediate char return */
	noecho ();    /* no immediate echo */
	keypad(stdscr,TRUE);
	termtype = getenv("TERM");
	if (termtype && !strncmp(termtype,"xterm",5)) {
		/* A quick and dirty hack to get certain keys working properly in xterm */
		/* For kvt, you might need to set your terminal emulation to "Original Xterm II" */
#ifdef HAVE_DEFINE_KEY
		sprintf(key_string,"%cOH",033); define_key(key_string,KEY_HOME);
		sprintf(key_string,"%cOF",033); define_key(key_string,KEY_END);
		sprintf(key_string,"%cOj",033); define_key(key_string,'*');
		sprintf(key_string,"%cOm",033); define_key(key_string,'-');
		sprintf(key_string,"%cOM",033); define_key(key_string,KEY_ENTER);
		sprintf(key_string,"%c*k",033); define_key(key_string,'+');
#endif
	}
	
	set_special_chars(config.misc.use_ext_chars);

	getmaxyx(stdscr,screen_height,screen_width);
	if (screen_height >= 40) minifl_height = 8;
	else minifl_height = 4;
	
	
	main_window_draw();
	refresh();
	file_window_init();
	info_window_init();
	status_line_init();
	dir_window_init();

#ifdef HAVE_RESIZETERM
	(void) signal(SIGWINCH, resize_screen); /* arrange interrupts to resize */
#endif


	if (argc >= 2) dir_window_set_path(argv[1]);
	else dir_window_set_initial_path();

	
	dir_window_activate();	
	main_loop();
	
	show_cursor();
	remove_color();
	erase();
	refresh();
	noraw();
	endwin();
	return 0;
}