Exemplo n.º 1
0
void splay(int x){
	int s = 1,i = x,y; tmp[1] = i;
	while(!isroot(i)) tmp[++s] = i = f[i];
	while(s) pb(tmp[s--]);
	while(!isroot(x)){
		y = f[x];
		if (!isroot(y)){
			if ((son[f[y]][0] == y) ^ (son[y][0] == x))
			rotate(x); else rotate(y);
		}
		rotate(x);
	}
	up(x);
}
Exemplo n.º 2
0
/** 
 * tmrca - calculate time to most recent common ancestor. This is
 * simply the time recorded in the root node
 * 
 * @param n 
 * 
 * @return 
 */
double tmrca(struct Node *n)
{
	if (isroot(n))
		return n->time;
	else
		fprintf (stderr, "ERROR: %i is not root!\n", n->id);
}
Exemplo n.º 3
0
static void recolor(JRB n)
{
    JRB p, gp, s;
    int done = 0;

    while (!done) {
        if (isroot(n)) {
            setblack(n);
            return;
        }

        p = n->parent;

        if (isblack(p))
            return;

        if (isroot(p)) {
            setblack(p);
            return;
        }

        gp = p->parent;
        s = sibling(p);
        if (isred(s)) {
            setblack(p);
            setred(gp);
            setblack(s);
            n = gp;
        }
        else {
            done = 1;
        }
    }
    /* p's sibling is black, p is red, gp is black */

    if ((isleft(n) == 0) == (isleft(p) == 0)) {
        single_rotate(gp, isleft(n));
        setblack(p);
        setred(gp);
    }
    else {
        single_rotate(p, isleft(n));
        single_rotate(gp, isleft(n));
        setblack(n);
        setred(gp);
    }
}
Exemplo n.º 4
0
JRB lprev(JRB n)
{
  if (ishead(n)) return n;
  while (!isroot(n)) {
    if (isright(n)) return n->parent;
    n = n->parent;
  }
  return n->parent;
}
Exemplo n.º 5
0
 Node *splay() {
     D();
     while (!isroot()) {
         if (!fa->isroot())
             d()==fa->d() ? fa->rot() : rot();
         rot();
     }
     return this;
 }
Exemplo n.º 6
0
JRB rprev(JRB n)
{
  if (ishead(n)) return n;
  while (!isroot(n)) {
    if (isleft(n)) return n->parent;
    n = n->parent;
  }
  return n->parent;
}
Exemplo n.º 7
0
Rb_node lprev(Rb_node n)
{
  if (ishead(n)) return n;
  while (!isroot(n)) {
    if (isright(n)) return n->p.parent;
    n = n->p.parent;
  }
  return n->p.parent;
}
Exemplo n.º 8
0
 inline void splay() {
     while(!isroot()) {
         if(!fa->isroot()) {
             if(fa->dir() == dir()) fa->rot();
             else rot();
         }
         rot();
     }
     pushup();
 }
Exemplo n.º 9
0
/** 
 * 
 * 
 * @param N number of samples
 * @param theta mutation rate
 * @param Tb "bottleneck" time
 * @param f fraction of bottleneck population compared to current
 * 
 * @return root node
 * 
 */
struct Node *coalescent_tree(unsigned int N, double theta, double Tb, double f)
{
	// http://users.stat.umn.edu/~geyer/rc/
	GetRNGstate();
	int k;
	unsigned int i, j, n_nodes;
	// there are 2N-2 branches (rooted tree), which implies 2N-1 nodes
	n_nodes = 2*N - 1;
	struct Node *nodes[n_nodes];
	for (k=0; k < n_nodes; k++)
		nodes[k] = new_node(k);
	
	double rate, Ti, Ttot;
	Ttot = 0.0;
	i = j = N;
	while (i > 1) {
		// Set the coalescence time
		rate = i * (i - 1) / 2.0;
		Ti = rexp(1/rate);
		if (f != 1.0) {
			// this part models expansion/bottleneck
			if (Ttot > Tb) {
				rate = f * i * (i - 1) / 2.0;
				Ti = rexp(1/rate) + Tb;
			}
		}
		/* rate is in unit 1/s; want unit s so invert */
		Ttot += Ti;
		nodes[j]->time = Ttot;

		// Make a number of mutations and sprinkle them out
		int n_mut, l;
		// Remember; we need to multiply rate also by the number of
		// branches; otherwise we're only generating a number of
		// mutations proportional to TMRCA, not TBL
		n_mut = (int) rpois((double) (theta / 2 * Ti * i));
		for (k=0; k<n_mut; k++) {
			l = (int) runif(0.0, (double) i);
			nodes[l]->mutations++;
		}

		// Note that the coalescent event can be performed after
		// setting the time and placing mutations as we know the id of
		// the parent beforehand
		coalesce(nodes, j, i);
		i--;
		j++;
	}

	PutRNGstate();
	for (k=0; k < n_nodes; k++) {
		if (isroot(nodes[k]))
			return nodes[k];
	}
}
Exemplo n.º 10
0
local void long_close(matrix* m, lie_Index first, lie_Index last)
{ lie_Index i,j;  entry* root_i,* root_j,* t=mkintarray(s);
  for (i=first; i<last; ++i)
  { root_i=m->elm[i]; if (Norm(root_i)>1) continue;
    for (j=i+1; j<last; ++j)
    { root_j=m->elm[j]; if (Norm(root_j)>1) continue;
      subrow(root_i,root_j,t,s);
      if (isroot(t))
	if (isposroot(t)) 
	{ copyrow(t,root_i,s); break;  } /* need not consider more |j|'s */
	else add_xrow_to(root_j,-1,root_i,s);
    }
  }
  freearr(t);
}
Exemplo n.º 11
0
static void single_rotate(JRB y, int l)
{
    int rl, ir;
    JRB x, yp;
    char *tmp;

    ir = isroot(y);
    yp = y->parent;
    if (!ir) {
        rl = isleft(y);
    }

    if (l) {
        x = y->flink;
        y->flink = x->blink;
        setleft(y->flink);
        y->flink->parent = y;
        x->blink = y;
        setright(y);
    }
    else {
        x = y->blink;
        y->blink = x->flink;
        setright(y->blink);
        y->blink->parent = y;
        x->flink = y;
        setleft(y);
    }

    x->parent = yp;
    y->parent = x;
    if (ir) {
        yp->parent = x;
        setnormal(y);
        setroot(x);
    }
    else {
        if (rl) {
            yp->flink = x;
            setleft(x);
        }
        else {
            yp->blink = x;
            setright(x);
        }
    }
}
Exemplo n.º 12
0
matrix* Closure(matrix* m, boolean close, group* lie_type)
{ matrix* result;  lie_Index i,j;
  group* tp=(s=Ssrank(grp), lie_type==NULL ? mkgroup(s) : lie_type);

  tp->toraldim=Lierank(grp); tp->ncomp=0; /* start with maximal torus */
  m=copymatrix(m);
  
  if (close)
    if (type_of(grp)==SIMPGRP) close = two_lengths(grp->s.lietype);
    else
    { for (i=0; i<grp->g.ncomp; i++)
        if (two_lengths(Liecomp(grp,i)->lietype)) break;
      close= i<grp->g.ncomp;
    }
  
  { entry* t;
    for (i=0; i<m->nrows; i++)
    if (!isroot(t=m->elm[i]))
      error("Set of root vectors contains a non-root\n");
    else if (!isposroot(t=m->elm[i]))
      for (j=0; j<m->ncols; j++) t[j]= -t[j]; /* make positive root */
    Unique(m,cmpfn);
  }
  { lie_Index next;
    for (i=0; i<m->nrows; i=next)
    
    { lie_Index d,n=0;  simpgrp* c;
      next=isolcomp(m,i);
      fundam(m,i,&next);
      if (close) long_close(m,i,next),fundam(m,i,&next);
      c=simp_type(&m->elm[i],d=next-i);
      
      { j=tp->ncomp++;
        while(--j>=0 && grp_less(tp->liecomp[j],c))
          n += (tp->liecomp[j+1]=tp->liecomp[j])->lierank;
        tp->liecomp[++j]=c; tp->toraldim -= d;
          /* insert component and remove rank from torus */
        cycle_block(m,i-n,next,n);
          /* move the |d| rows down across |n| previous rows */
      }
    }
  }
  if (lie_type==NULL)
    return result=copymatrix(m),freemem(m),freemem(tp),result;
  else return freemem(m),(matrix*)NULL; /* |Cartan_type| doesn't need |m| */
}
Exemplo n.º 13
0
static void single_rotate(Rb_node y, int l)
{
  int rl, ir;
  Rb_node x, yp;
 
  ir = isroot(y);
  yp = y->p.parent;
  if (!ir) {
    rl = isleft(y);
  }
  
  if (l) {
    x = y->c.child.left;
    y->c.child.left = x->c.child.right;
    setleft(y->c.child.left);
    y->c.child.left->p.parent = y;
    x->c.child.right = y;
    setright(y);  
  } else {
    x = y->c.child.right;
    y->c.child.right = x->c.child.left;
    setright(y->c.child.right);
    y->c.child.right->p.parent = y;
    x->c.child.left = y;
    setleft(y);  
  }
 
  x->p.parent = yp;
  y->p.parent = x;
  if (ir) {
    yp->p.root = x;
    setnormal(y);
    setroot(x);
  } else {
    if (rl) {
      yp->c.child.left = x;
      setleft(x);
    } else {
      yp->c.child.right = x;
      setright(x);
    }
  }
}
Exemplo n.º 14
0
/** 
 * newick - get newick string representation for a node
 * 
 * @param n - node for which newick representation is desired
 * 
 * @return recursively generate newick string representation of node
 * and its descendants
 */
char *newick(struct Node *n)
{
	char *retval;
	if (isleaf(n)) {
		retval = malloc (sizeof (char) * NEWICK_BUFSIZE);
		snprintf(retval, NEWICK_BUFSIZE, "%i:%.4f", n->id, n->parent->time - n->time);
	} else {
		char *left, *right;
		left = newick(n->left);
		right = newick(n->right);
		int BUFSIZE = strlen(left) + strlen(right) + 12;		
		retval = malloc (sizeof (char) * BUFSIZE);
		if (isroot(n))
			snprintf(retval, BUFSIZE, "(%s,%s):%.i;", left, right, n->id);
		else
			snprintf(retval, BUFSIZE, "(%s,%s):%.4f", left, right, n->parent->time - n->time);
		free(left);
		free(right);
	}
	return retval;
}
Exemplo n.º 15
0
Arquivo: spr.c Projeto: pcordes/allspr
/* A wrapper around dospr():
 * return the tree to its original topology if needed.
 * rejects some useless SPRs (e.g. that don't change the topology)
 * save info so unspr can get back to original topology.
 * returns TRUE if dospr() succeeds and the tree is modified.
 *
 * It's probably easy to make a mess if you call this directly while root
 * moving is going on. e.g. tree->lastspr is checked for some things.
 * Root moving was hacked in as an afterthought.
 */
int spr( struct spr_tree *tree, struct spr_node *src, struct spr_node *dest )
{
	int tmp, unspr_success=FALSE;

	if (tree->unspr_dest){	// back to starting tree
		unspr_success = dospr(tree->unspr_src, tree->unspr_dest);
		tree->root = spr_findroot(tree->unspr_dest);
		if (spr_debug>=2){
			fputs("  unspr back to: ", stderr);
			newickprint(tree->root, stderr);
		}
		assert( unspr_success );
		tree->unspr_dest = NULL;
	}
	if (!src && !dest) return unspr_success;

	// We used to exclude dest==root, but it doesn't break unspr or anything.
	// It always has the same (unrooted) topology as two other trees that spr_next_spr finds.
	// (the root node is the "extra" node, for unrooted vs. rooted tree) */
	if ( !src || !dest ||	// protect against silly callers
	     spr_isancestor(src, dest) || // does this really always catch !(src->parent)?
	     src->parent == dest->parent)  // don't switch siblings
		return FALSE;

	tree->unspr_src = src;
	tree->unspr_dest = sibling(src);

	tmp = dospr(src, dest);
	if (tmp){
		if (!isroot(tree->root)){
			tree->root = spr_findroot(dest);
			if (spr_debug>=2) fputs("allspr: tree has new root!\n", stderr);
		}
		if (spr_debug>=1)
			printf("  did spr %s -> %s\n", src->data->name, dest->data->name);
	}else
		tree->unspr_dest = NULL;

	return tmp;
}
Exemplo n.º 16
0
/** 
 * remove_tree - free up memory
 *
 * Traverse tree in a depth-first fashion. Delete a node if it is a
 * leaf, keeping track of whether the node is a left child of its
 * parent. Set current node to the parent and delete the child. Note
 * that we cannot use the visit_node function as we delete nodes
 * during tree traversal
 *
 * @param n - Node
 */
void remove_tree(struct Node *n)
{
	int ndel = 0;
	boolean left = FALSE;
	while (n != NULL) {
		if (isleaf(n)) {
			if (isroot(n)) {
				ndel += 1;
				delete_node(n);
				n = NULL;
			} else {
				// Is current node left child of parent?
				left = TRUE;
				if (n->parent->left == NULL)
					left = FALSE;
				else if (n->parent->left->id != n->id)
					left = FALSE;
				// set current node to parent and delete the relevant
				// child (=previously the current node)
				n = n->parent;
				(left == TRUE) ? delete_node(n->left) : delete_node(n->right);
				// finally, reset the pointer to the deleted child to NULL
				if (left == TRUE) 
					n->left = NULL;
				else
					n->right = NULL;
				ndel += 1;
			}
		} else if (n->left != NULL) {
			n = n->left;
		} else if (n->right != NULL) {
			n = n->right;
		} else {
			n = NULL;
		}
	}
	// debug macro wanted
	// fprintf(stderr, "deleted %i nodes\n", ndel);
}
Exemplo n.º 17
0
void jrb_delete_node(JRB n)
{
  JRB s, p, gp;
  char ir;

  if (isint(n)) {
    fprintf(stderr, "Cannot delete an internal node: 0x%p\n", (void *)n);
    exit(1);
  }
  if (ishead(n)) {
    fprintf(stderr, "Cannot delete the head of an jrb_tree: 0x%p\n", (void *)n);
    exit(1);
  }
  delete_item(n); /* Delete it from the list */
  p = n->parent;  /* The only node */
  if (isroot(n)) {
    p->parent = p;
    free(n);
    return;
  }
  s = sibling(n);    /* The only node after deletion */
  if (isroot(p)) {
    s->parent = p->parent;
    s->parent->parent = s;
    setroot(s);
    free(p);
    free(n);
    return;
  }
  gp = p->parent;  /* Set parent to sibling */
  s->parent = gp;
  if (isleft(p)) {
    gp->flink = s;
    setleft(s);
  } else {
    gp->blink = s;
    setright(s);
  }
  ir = isred(p);
  free(p);
  free(n);

  if (isext(s)) {      /* Update proper rext and lext values */
    p = lprev(s);
    if (!ishead(p)) setrext(p, s);
    p = rprev(s);
    if (!ishead(p)) setlext(p, s);
  } else if (isblack(s)) {
    fprintf(stderr, "DELETION PROB -- sib is black, internal\n");
    exit(1);
  } else {
    p = lprev(s);
    if (!ishead(p)) setrext(p, s->flink);
    p = rprev(s);
    if (!ishead(p)) setlext(p, s->blink);
    setblack(s);
    return;
  }

  if (ir) return;

  /* Recolor */

  n = s;
  p = n->parent;
  s = sibling(n);
  while(isblack(p) && isblack(s) && isint(s) &&
        isblack(s->flink) && isblack(s->blink)) {
    setred(s);
    n = p;
    if (isroot(n)) return;
    p = n->parent;
    s = sibling(n);
  }

  if (isblack(p) && isred(s)) {  /* Rotation 2.3b */
    single_rotate(p, isright(n));
    setred(p);
    setblack(s);
    s = sibling(n);
  }

  { JRB x, z; char il;

    if (isext(s)) {
      fprintf(stderr, "DELETION ERROR: sibling not internal\n");
      exit(1);
    }

    il = isleft(n);
    x = il ? s->flink : s->blink ;
    z = sibling(x);

    if (isred(z)) {  /* Rotation 2.3f */
      single_rotate(p, !il);
      setblack(z);
      if (isred(p)) setred(s); else setblack(s);
      setblack(p);
    } else if (isblack(x)) {   /* Recoloring only (2.3c) */
      if (isred(s) || isblack(p)) {
        fprintf(stderr, "DELETION ERROR: 2.3c not quite right\n");
        exit(1);
      }
      setblack(p);
      setred(s);
      return;
    } else if (isred(p)) { /* 2.3d */
      single_rotate(s, il);
      single_rotate(p, !il);
      setblack(x);
      setred(s);
      return;
    } else {  /* 2.3e */
      single_rotate(s, il);
      single_rotate(p, !il);
      setblack(x);
      return;
    }
  }
}
Exemplo n.º 18
0
Arquivo: grpdata.c Projeto: d4g33z/lie
void checkroot(entry* alpha)
{ if (!isroot(alpha))
  { printarr(alpha,Ssrank(grp)); error (" is not a root.\n"); }
}
Exemplo n.º 19
0
char *locate(const char *name, int type)
{
	char fname[256], *newpath, *newname, *fspec, *title;
	boolean result = FALSE;
	int ex_flags;

	if (type == L_FOLDER)
	{
		if ((fspec = fn_make_path(name, "*")) == NULL)		/* HR 271102 */
			return NULL;
		fname[0] = 0;
		ex_flags = EX_DIR;
	}
	else
	{
		if (   (fspec = fn_make_newname(
		                        name, 
		                        (  (type == L_LOADCFG) || (type == L_SAVECFG) )
		                          ? CFG_EXT
		                          : "*"					/* HR 271102 */
		                               )
		       ) == NULL
		   )
			return NULL;
		strcpy(fname, fn_get_name(name));
		ex_flags = EX_FILE;
	}

	rsrc_gaddr(R_STRING, FSTLFILE + type, &title);

	do
	{
		newpath = xfileselector(fspec, fname, title);

		free(fspec);

		if (newpath == NULL)
			return NULL;

		if (type == L_FOLDER)
		{
			if (((newname = fn_get_path(newpath)) != NULL) && (isroot(newname) == TRUE))
			{
				alert_printf(1, MNOROOT);
				free(newname);
			}
			else
				result = TRUE;
		}
		else
		{
			if ((type == L_PROGRAM) && (prg_isprogram(fname) == FALSE))
				alert_printf(1, MFNPRG, fname);
			else
			{
				if (((newname = fn_make_newname(newpath, fname)) != NULL) && (type != L_SAVECFG))
				{
					result = x_exist(newname, ex_flags);
					if (result == FALSE)
					{
						alert_printf(1, MFNEXIST, fname);
						free(newname);
					}
				}
				else
					result = TRUE;
			}
		}
		fspec = newpath;
	}
	while (result == FALSE);

	free(newpath);

	return newname;
}