Ejemplo n.º 1
0
/* Examine NODE in a red-black tree.  *COUNT is increased by the
   number of nodes in the tree, including the current one.  If the
   node is the root of the tree, PARENT should be INT_MIN, otherwise
   it should be the parent node value.  If this node is a left child
   of its parent node, DIR should be -1.  Otherwise, if it is not the
   root, it is a right child and DIR must be +1.  Sets *OKAY to 0 if
   an error is found.  Returns the black-height of the tree rooted at
   NODE. */
int
recurse_tree(struct rb_node *node, int *count, int parent, int dir, int *okay)
{
  int lh, rh;

  if (node == NULL)
    return 1;

  assert(count != NULL);
  (*count)++;

  lh = recurse_tree(node->link[0], count, node->data, -1, okay);
  rh = recurse_tree(node->link[1], count, node->data, +1, okay);
  if (node->color != RB_RED && node->color != RB_BLACK) {
    printf(" Node %d is neither red nor black (%d).\n",
	   node->data, (int) node->color);
    *okay = 0;
  }

  if (node->color == RB_RED && node->link[0]
      && node->link[0]->color == RB_RED) {
    printf(" Red node %d has red left child %d\n", node->data,
	   node->link[0]->data);
    *okay = 0;
  }

  if (node->color == RB_RED && node->link[1]
      && node->link[1]->color == RB_RED) {
    printf(" Red node %d has red right child %d\n", node->data,
	   node->link[1]->data);
    *okay = 0;
  }

  if (lh != rh) {
    printf
	(" Node %d has two different black-heights: left bh=%d, "
	 "right bh=%d\n", node->data, lh, rh);
    *okay = 0;
  }

  if (parent != INT_MIN) {
    assert(dir == -1 || dir == +1);
    if (dir == -1 && node->data > parent) {
      printf(" Node %d is smaller than its left child %d.\n",
	     parent, node->data);
      *okay = 0;
    }
    else if (dir == +1 && node->data < parent) {
      printf(" Node %d is larger than its right child %d.\n",
	     parent, node->data);
      *okay = 0;
    }
  }

  return (node->color == RB_BLACK) + lh;
}
Ejemplo n.º 2
0
/* Checks that TREE's structure is kosher and verifies that the values
   in ARRAY are actually in the tree.  There must be as many elements
   in ARRAY as there are nodes in the tree.  Exits the program if an
   error was encountered. */
void verify_tree(struct rb_tree *tree, int array[])
{
  int count = 0;
  int okay = 1;

  recurse_tree(tree->root, &count, INT_MIN, 0, &okay);
  if (count != tree->count) {
    printf(" Tree has %d nodes, but tree count is %d.\n", count, tree->count);
    okay = 0;
  }

  if (okay) {
    int i;

    for (i = 0; i < tree->count; i++)
      if (!rb_search(tree, array[i])) {
	printf("Tree does not contain expected value %d.\n", array[i]);
	okay = 0;
      }
  }

  if (!okay) {
    printf("Error(s) encountered, aborting execution.\n");
    exit(EXIT_FAILURE);
  }
}
Ejemplo n.º 3
0
static void
closedownLogout(Display * display, int screens)
{
	Atom        __SWM_DELETE_WINDOW = None;
	Atom        __SWM_PROTOCOLS = None;
	Atom        __SWM_STATE = None;
	int         j;

#if 0
	/* synchronize -- so I'm aware of errors immediately */
	XSynchronize(display, True);

	/* use my error handler from here on out */
	(void) XSetErrorHandler(err_handler);
#endif

	/* init atoms */
	__SWM_STATE = XInternAtom(display, "__SWM_STATE", False);
	__SWM_PROTOCOLS = XInternAtom(display, "__SWM_PROTOCOLS", False);
	__SWM_DELETE_WINDOW = XInternAtom(display, "__SWM_DELETE_WINDOW", False);

	/* start looking for windows to kill -- be nice on pass 1 */
	for (j = 0; j < screens; j++)
		recurse_tree(display, RootWindow(display, j),
			  __SWM_STATE, __SWM_PROTOCOLS, __SWM_DELETE_WINDOW);

	/* wait for things to clean themselves up */
	(void) sleep(NAP_TIME);

	/* this will forcibly kill anything that's still around --
	   this second pass may or may not be needed... */
	for (j = 0; j < screens; j++)
		kill_tree(display, RootWindow(display, j));
	(void) sleep(NAP_TIME);
}
Ejemplo n.º 4
0
/* Examine NODE in a binary tree.  *COUNT is increased by the number
   of nodes in the tree, including the current one.  If the node is
   the root of the tree, PARENT should be INT_MIN, otherwise it should
   be the parent node value.  If this node is a left child of its
   parent node, DIR should be -1.  Otherwise, if it is not the root,
   it is a right child and DIR must be +1.  Sets *OKAY to 0 if an
   error is found. */
void
recurse_tree(struct pbin_node *node, int *count, int parent,
	     int dir, int *okay)
{
  if (node == NULL)
    return;

  assert(count != NULL);
  (*count)++;

  recurse_tree(node->left, count, node->data, -1, okay);
  recurse_tree(node->right, count, node->data, +1, okay);

  if (parent != INT_MIN) {
    assert(dir == -1 || dir == +1);
    if (dir == -1 && node->data > parent) {
      printf(" Node %d is smaller than its left child %d.\n",
	     parent, node->data);
      *okay = 0;
    }
    else if (dir == +1 && node->data < parent) {
      printf(" Node %d is larger than its right child %d.\n",
	     parent, node->data);
      *okay = 0;
    }

    if (node->parent == NULL) {
      printf
	  (" Node %d has null parent but should have parent %d.\n",
	   node->data, parent);
      *okay = 0;
    }
    else if (node->parent->data != parent) {
      printf
	  (" Node %d has parent %d but should have parent %d.\n",
	   node->data, node->parent->data, parent);
      *okay = 0;
    }
  }
  else if (node->parent) {
    printf
	(" Node %d has parent %d but should have null parent.\n",
	 node->data, node->parent->data);
    *okay = 0;
  }
}
Ejemplo n.º 5
0
void
bind_names ( node_t *root )
{
    //Initial scope for the functions
    scope_add();

    declare_functions(root);
    recurse_tree(root);
    
    //Remove the function scope
    scope_remove();
}
Ejemplo n.º 6
0
void 
recurse_tree(node_t* root){
if(root == NULL) return;
    if(root->type.index == DECLARATION){
        declare_tree(root);
    }else if(root->type.index == BLOCK){
        next_stack_offset = -4; 
        scope_add();
    }else if(root->type.index == FUNCTION){
        next_stack_offset = -4;
        scope_add();
        //function is defined already, but not the parameters.
        define_parameters(root);
    }else if(root->type.index == EXPRESSION
          || root->type.index == ASSIGNMENT_STATEMENT){
        for(int i = 0; i< root->n_children;i++){
            node_t* child = root->children[i];
            if(child == NULL) continue;
            if(child->type.index == VARIABLE){
                symbol_t* symbol = symbol_get((char*)child->data);
                root->entry = symbol;
            }
        }
    }else if(root->type.index == TEXT){
        char* text = (char*) root->data;

        //For future ease of use, I will not store 
        //the index directly in the pointer.
        int32_t* intpoint = malloc(sizeof(int32_t));
        *intpoint = strings_add(text); 
        //Store the string index instead of the string
        root->data = intpoint;
    }

    for(int i = 0; i < root->n_children; i++){
        node_t* child = root->children[i];
        recurse_tree(child); 
    }

    if(root->type.index == BLOCK
    || root->type.index == FUNCTION){
        scope_remove();
    }
}
Ejemplo n.º 7
0
static void
recurse_tree(Display * display, Window window,
	     Atom state_atom, Atom protocols_atom, Atom delete_window_atom)
{
	Window      root, parent, *kids;
	unsigned int nkids;
	int         j;
	int         swm_state;

	for (;;) {
		XQueryTree(display, window, &root, &parent, &kids, &nkids);
		if (err_occurred) {
			err_occurred = False;
			return;
		}
		for (j = 0; j < nkids; j++) {
			swm_state = has_property(display, kids[j], state_atom);

			if (err_occurred)
				break;

			if (swm_state) {
				handle_top_level(display, kids[j], protocols_atom, delete_window_atom);
				if (err_occurred)
					break;
			} else
				recurse_tree(display, kids[j],
					     state_atom, protocols_atom, delete_window_atom);
		}

		XFree((caddr_t) kids);

		/* when I get all the way through a level without an error, I'm done  */

		if (err_occurred)
			err_occurred = False;
		else
			return;

	}
}