Example #1
0
edge_ref *delaunay_edges(int nsites)
{
    edge_ref *array, *stack;
    int edges = 0, top = -1;
    unsigned mark = next_mark;

    assert(array =
           (edge_ref *) malloc((3 * nsites - 5) * sizeof(edge_ref)));
    assert(stack =
           (edge_ref *) malloc((3 * nsites - 6) * sizeof(edge_ref)));
    if (++next_mark == 0)
        next_mark = 1;
    stack[++top] = delaunay_build(nsites);
    while (top != -1) {
        edge_ref e = stack[top--];
        while (MARK(e) != mark) {
            MARK(e) = mark;
            array[edges++] = e;
            stack[++top] = ONEXT(e);
            e = ONEXT(SYM(e));
        }
    }
    array[edges] = 0;
    free(stack);
    return array;
}
Example #2
0
/*
   Cartesian product of option sets with on-the-fly pruning.
   Update rules:

   T = min(T1, T2)
   L = L1 + L2

   Returns freshly created options list Z.
*/
static Options
options_combine(Options Z1, Options Z2)
{
  Options Z, o1, o2;
  Options *tail;
  Nat len=0;
  /* Combine every option element from Z1 with every option element from
     Z2. The merge operation is symmetric so this approach is sufficient
     to generate all possible combinations. The merge results are stored
     in a new list Z.
     Of course this list need not be ordered and very likely contains
     redundant options, therefore must explicitly sort/filter the list.
  */

  tail=&Z;
  for (o1 = Z1; o1; o1 = ONEXT(o1))
    for (o2 = Z2; o2; o2 = ONEXT(o2)) {
      /* Merge o1 and o2 */
        *tail = options_merge(o1, o2); 
        tail=&ONEXT(*tail);
        len++;
    }
  if (debug) options_show(stdout, Z, "test");

  /*   Z=*tail;*/
  Z = options_filter(Z, len); 

  /* Delete original lists: */
  options_free(Z1);
  options_free(Z2);
  return Z;
}
Example #3
0
void destroy_edge(edge_ref e)
{
    edge_ref f = SYM(e);
    if (ONEXT(e) != e)
        splice(e, OPREV(e));
    if (ONEXT(f) != f)
        splice(f, OPREV(f));
    free((char *) (e & MASK));
}
Example #4
0
static Options
options_copy(Options o)
{
  Options Z, *tail = &Z;

  while (o) {
    *tail = option_mk(OPAIR(o));
    tail = &ONEXT(*tail);
    o = ONEXT(o);
  }
  return Z;
}
Example #5
0
static void splice(edge_ref a, edge_ref b)
{
    edge_ref ta, tb;
    edge_ref alpha = ROT(ONEXT(a));
    edge_ref beta = ROT(ONEXT(b));

    ta = ONEXT(a);
    tb = ONEXT(b);
    ONEXT(a) = tb;
    ONEXT(b) = ta;
    ta = ONEXT(alpha);
    tb = ONEXT(beta);
    ONEXT(alpha) = tb;
    ONEXT(beta) = ta;
}
Example #6
0
static Options
option_mk(Pair p)
{
  Options o = malloc(sizeof(*o));

  OPAIR(o) = p;
  ONEXT(o) = NULL;
  return o;
}
Example #7
0
static void
options_free(Options o)
{
  while (o) {
    Options next = ONEXT(o);
    option_free(o);
    o = next;
  }
}
Example #8
0
static Nat
options_len(Options o)
{
  Nat len;

  for (len = 0; o; o = ONEXT(o), len++)
    ;
  return len;
}
Example #9
0
static Options
options_last(Options o)
{
  while (o) {
    Options next = ONEXT(o);

    if (!next)
      return o;
    o = next;
  }
  return NULL;
}
Example #10
0
static edge_ref make_edge(void)
{
    edge_ref e;

    assert(e = (edge_ref) malloc(sizeof(edge_struct)));
    ONEXT(e) = e;
    ROTRNEXT(e) = TOR(e);
    SYMDNEXT(e) = SYM(e);
    TORLNEXT(e) = ROT(e);
    MARK(e) = 0;
    return e;
}
Example #11
0
static void
options_show(FILE *fp, Options o, const char *msg)
{
  fprintf(fp, "%s", msg);
  while (o) {
    Options next;

    pair_show(fp, OPAIR(o));
    next = ONEXT(o);
    if (next)
      fprintf(fp, ", ");
    o = next;
  }
  fprintf(fp, "\n");
}
Example #12
0
/* Add wire segment of length l.
   Update rules:

   T = T - R * C / 2.0 - R * L
   L = L + C

   Returns possibly modified options list Z.
*/
static Options
options_add_wire(Options Z, Length l)
{
  Resistance  R = R_unit * l;
  Capacitance C = C_unit * l;
  Options o;
  Nat len;

  for (o = Z, len = 0; o; o = ONEXT(o), len++) {
    OTIME(o) -= R * (C / 2.0 + OLOAD(o));
    OLOAD(o) += C;
  }
  if (debug)
    options_show(stdout, Z, "Added wire:\n");

  return options_filter(Z, len);
}
Example #13
0
/* Add buffer option.
   Update rules:

   T = T - Dbuf - Rbuf * L
   L = Cbuf

   Returns possibly modified options list Z.
*/
static Options
options_add_buffer(Options Z)
{
  Options *tail, o;
  Nat len;
  Time Tmax;

#ifdef CHICKEN
  /* For each option, calculate new option when buffer is added, and
     insert that new option right after the original one.
     Of course this disrupts the order and also might introduce redundant
     options, therefore must explicitly sort/filter the list.
  */
  for (o = Z, len = 0; o; o = ONEXT(o), len++) {
    Time    T = OTIME(o) - D_buf - R_buf * OLOAD(o);
    Options n = option_mk(pair_mk(T, C_buf));

    /* Insert n after o: */
    ONEXT(n) = ONEXT(o);
    ONEXT(o) = n;
    o = n;
    len++;
  }
  if (debug)
    options_show(stdout, Z, "Added buffer:\n");

  Z = options_filter(Z, len);
#else
  /* Determine Tmax: */

  /* Get reference time by adding buffer to first option element: */
  o = Z;
  Tmax = OTIME(o) - D_buf - R_buf * OLOAD(o);
  /* Compare against rest of elements if any: */
  o = ONEXT(o);
  len = 1;
  while (o) {
    /* Get effect of adding buffer to this option element: */
    Time T = OTIME(o) - D_buf - R_buf * OLOAD(o);

    if (T > Tmax)
      Tmax = T;
    o = ONEXT(o);
    len++;
  }
  /* Note: Tmax <= OTIME(o) for some o elem Z. */

  /* Find location to (possibly) insert buffer option (Tmax,C_buf): */
  tail = &Z;
  o = *tail;
  while (OTIME(o) < Tmax) {
    tail = &ONEXT(o);
    o = *tail;
  }
  /* Here: must have Tmax <= OTIME(o) */

  if (EQUAL(OTIME(o), Tmax)) {
    /* Prune the existing option or the buffer option: */
    if (C_buf < OLOAD(o))
      /* This element o becomes the buffer option: */
      OLOAD(o) = C_buf;
    /* else discard buffer option. */
  }
  else { /* Here: Tmax < OTIME(o) */
    if (C_buf < OLOAD(o)) {
      /* Insert buffer option before element o: */
      *tail = option_mk(pair_mk(Tmax, C_buf));
      ONEXT(*tail) = o;
    }
    /* else discard buffer option. */
  }
  if (debug)
    options_show(stdout, Z, "Added buffer:\n");
#endif
  return Z;
}
Example #14
0
char *
expmatch(char *s, char *re, char *mstring)
	/* s - string to check for a match in */
	/* re - a converted irregular expression */
	/* mstring - where to put whatever matches a \p */
{
    char *cs;		/* the current symbol */
    char *ptr, *s1;	/* temporary pointer */
    boolean matched;	/* a temporary boolean */

    /* initial conditions */
    if (re == NIL)
	return (NIL);
    cs = re;
    matched = FALSE;

    /* loop till expression string is exhausted (or at least pretty tired) */
    while (*cs) {
	switch (*cs & (OPER | STR | META)) {

	/* try to match a string */
	case STR:
	    matched = !STRNCMP (s, SSTR(cs), SCNT(cs));
	    if (matched) {

		/* hoorah it matches */
		s += SCNT(cs);
		cs = SNEXT(cs);
	    } else if (*cs & ALT) {

		/* alternation, skip to next expression */
		cs = SNEXT(cs);
	    } else if (*cs & OPT) {

		/* the match is optional */
		cs = SNEXT(cs);
		matched = 1;		/* indicate a successful match */
	    } else {

		/* no match, error return */
		return (NIL);
	    }
	    break;

	/* an operator, do something fancy */
	case OPER:
	    switch (OSYM(cs)) {

	    /* this is an alternation */
	    case '|':
		if (matched)

		    /* last thing in the alternation was a match, skip ahead */
		    cs = OPTR(cs);
		else

		    /* no match, keep trying */
		    cs = ONEXT(cs);
		break;

	    /* this is a grouping, recurse */
	    case '(':
		ptr = expmatch (s, ONEXT(cs), mstring);
		if (ptr != NIL) {

		    /* the subexpression matched */
		    matched = 1;
		    s = ptr;
		} else if (*cs & ALT) {

		    /* alternation, skip to next expression */
		    matched = 0;
		} else if (*cs & OPT) {

		    /* the match is optional */
		    matched = 1;	/* indicate a successful match */
		} else {

		    /* no match, error return */
		    return (NIL);
		}
		cs = OPTR(cs);
		break;
	    }
	    break;

	/* try to match a metasymbol */
	case META:
	    switch (MSYM(cs)) {

	    /* try to match anything and remember what was matched */
	    case 'p':
		/*
		 *  This is really the same as trying the match the
		 *  remaining parts of the expression to any subset
		 *  of the string.
		 */
		s1 = s;
		do {
		    ptr = expmatch (s1, MNEXT(cs), mstring);
		    if (ptr != NIL && s1 != s) {

			/* we have a match, remember the match */
			strncpy (mstring, s, s1 - s);
			mstring[s1 - s] = '\0';
			return (ptr);
		    } else if (ptr != NIL && (*cs & OPT)) {

			/* it was aoptional so no match is ok */
			return (ptr);
		    } else if (ptr != NIL) {

			/* not optional and we still matched */
			return (NIL);
		    }
		    if (!isidchr(*s1))
			return (NIL);
		    if (*s1 == '\\')
			_escaped = _escaped ? FALSE : TRUE;
		    else
			_escaped = FALSE;
		} while (*s1++);
		return (NIL);

	    /* try to match anything */
	    case 'a':
		/*
		 *  This is really the same as trying the match the
		 *  remaining parts of the expression to any subset
		 *  of the string.
		 */
		s1 = s;
		do {
		    ptr = expmatch (s1, MNEXT(cs), mstring);
		    if (ptr != NIL && s1 != s) {

			/* we have a match */
			return (ptr);
		    } else if (ptr != NIL && (*cs & OPT)) {

			/* it was aoptional so no match is ok */
			return (ptr);
		    } else if (ptr != NIL) {

			/* not optional and we still matched */
			return (NIL);
		    }
		    if (*s1 == '\\')
			_escaped = _escaped ? FALSE : TRUE;
		    else
			_escaped = FALSE;
		} while (*s1++);
		return (NIL);

	    /* fail if we are currently _escaped */
	    case 'e':
		if (_escaped)
		    return(NIL);
		cs = MNEXT(cs); 
		break;

	    /* match any number of tabs and spaces */
	    case 'd':
		ptr = s;
		while (*s == ' ' || *s == '\t')
		    s++;
		if (s != ptr || s == Start) {

		    /* match, be happy */
		    matched = 1;
		    cs = MNEXT(cs); 
		} else if (*s == '\n' || *s == '\0') {

		    /* match, be happy */
		    matched = 1;
		    cs = MNEXT(cs); 
		} else if (*cs & ALT) {

		    /* try the next part */
		    matched = 0;
		    cs = MNEXT(cs);
		} else if (*cs & OPT) {

		    /* doesn't matter */
		    matched = 1;
		    cs = MNEXT(cs);
		} else

		    /* no match, error return */
		    return (NIL);
		break;

	    /* check for end of line */
	    case '$':
		if (*s == '\0' || *s == '\n') {

		    /* match, be happy */
		    s++;
		    matched = 1;
		    cs = MNEXT(cs);
		} else if (*cs & ALT) {

		    /* try the next part */
		    matched = 0;
		    cs = MNEXT(cs);
		} else if (*cs & OPT) {

		    /* doesn't matter */
		    matched = 1;
		    cs = MNEXT(cs);
		} else

		    /* no match, error return */
		    return (NIL);
		break;

	    /* check for start of line */
	    case '^':
		if (s == Start) {

		    /* match, be happy */
		    matched = 1;
		    cs = MNEXT(cs);
		} else if (*cs & ALT) {

		    /* try the next part */
		    matched = 0;
		    cs = MNEXT(cs);
		} else if (*cs & OPT) {

		    /* doesn't matter */
		    matched = 1;
		    cs = MNEXT(cs);
		} else

		    /* no match, error return */
		    return (NIL);
		break;

	    /* end of a subexpression, return success */
	    case ')':
		return (s);
	    }
	    break;
	}
    }
    return (s);
}
Example #15
0
static void
expconv(void)
{
    char *cs;		/* pointer to current symbol in converted exp */
    char c;		/* character being processed */
    char *acs;		/* pinter to last alternate */
    int temp;

    /* let the conversion begin */
    acs = NIL;
    cs = NIL;
    while (*ure != NIL) {
	switch (c = *ure++) {

	case '\\':
	    switch (c = *ure++) {

	    /* escaped characters are just characters */
	    default:
		if (cs == NIL || (*cs & STR) == 0) {
		    cs = ccre;
		    *cs = STR;
		    SCNT(cs) = 1;
		    ccre += 2;
		} else 
		    SCNT(cs)++;
		*ccre++ = c;
		break;

	    /* normal(?) metacharacters */
	    case 'a':
	    case 'd':
	    case 'e':
	    case 'p':
		if (acs != NIL && acs != cs) {
		    do {
			temp = OCNT(acs);
			OCNT(acs) = ccre - acs; 
			acs -= temp;
		    } while (temp != 0);
		    acs = NIL;
		}
		cs = ccre;
		*cs = META;
		MSYM(cs) = c;
		ccre = MNEXT(cs);
		break;
	    }
	    break;
	    
	/* just put the symbol in */
	case '^':
	case '$':
	    if (acs != NIL && acs != cs) {
		do {
		    temp = OCNT(acs);
		    OCNT(acs) = ccre - acs;
		    acs -= temp;
		} while (temp != 0);
		acs = NIL;
	    }
	    cs = ccre;
	    *cs = META;
	    MSYM(cs) = c;
	    ccre = MNEXT(cs);
	    break;

	/* mark the last match sequence as optional */
	case '?':
	    if (cs)
	    	*cs = *cs | OPT;
	    break;

	/* recurse and define a subexpression */
	case '(':
	    if (acs != NIL && acs != cs) {
		do {
		    temp = OCNT(acs);
		    OCNT(acs) = ccre - acs;
		    acs -= temp;
		} while (temp != 0);
		acs = NIL;
	    }
	    cs = ccre;
	    *cs = OPER;
	    OSYM(cs) = '(';
	    ccre = ONEXT(cs);
	    expconv ();
	    OCNT(cs) = ccre - cs;		/* offset to next symbol */
	    break;

	/* return from a recursion */
	case ')':
	    if (acs != NIL) {
		do {
		    temp = OCNT(acs);
		    OCNT(acs) = ccre - acs;
		    acs -= temp;
		} while (temp != 0);
		acs = NIL;
	    }
	    cs = ccre;
	    *cs = META;
	    MSYM(cs) = c;
	    ccre = MNEXT(cs);
	    return;

	/* mark the last match sequence as having an alternate */
	/* the third byte will contain an offset to jump over the */
	/* alternate match in case the first did not fail */
	case '|':
	    if (acs != NIL && acs != cs)
		OCNT(ccre) = ccre - acs;	/* make a back pointer */
	    else
		OCNT(ccre) = 0;
	    *cs |= ALT;
	    cs = ccre;
	    *cs = OPER;
	    OSYM(cs) = '|';
	    ccre = ONEXT(cs);
	    acs = cs;	/* remember that the pointer is to be filles */
	    break;

	/* if its not a metasymbol just build a scharacter string */
	default:
	    if (cs == NIL || (*cs & STR) == 0) {
		cs = ccre;
		*cs = STR;
		SCNT(cs) = 1;
		ccre = SSTR(cs);
	    } else
		SCNT(cs)++;
	    *ccre++ = c;
	    break;
	}
    }
    if (acs != NIL) {
	do {
	    temp = OCNT(acs);
	    OCNT(acs) = ccre - acs;
	    acs -= temp;
	} while (temp != 0);
	acs = NIL;
    }
}
Example #16
0
static int genregs_get(struct task_struct *target,
		   const struct user_regset *regset,
		   unsigned int pos, unsigned int count,
		   void *kbuf, void __user *ubuf)
{
	int ret;
	unsigned int dummy;
	struct pt_regs *regs = task_pt_regs(target);


	if (!regs)
		return -EIO;

	/* The general idea here is that the copyout must happen in
	 * exactly the same order in which the userspace expects these
	 * regs. Now, the sequence in userspace does not match the
	 * sequence in the kernel, so everything past the 32 gprs
	 * happens one at a time.
	 */
	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
				  &regs->r00, 0, 32*sizeof(unsigned long));

#define ONEXT(KPT_REG, USR_REG) \
	if (!ret) \
		ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, \
			KPT_REG, offsetof(struct user_regs_struct, USR_REG), \
			offsetof(struct user_regs_struct, USR_REG) + \
				 sizeof(unsigned long));

	/* Must be exactly same sequence as struct user_regs_struct */
	ONEXT(&regs->sa0, sa0);
	ONEXT(&regs->lc0, lc0);
	ONEXT(&regs->sa1, sa1);
	ONEXT(&regs->lc1, lc1);
	ONEXT(&regs->m0, m0);
	ONEXT(&regs->m1, m1);
	ONEXT(&regs->usr, usr);
	ONEXT(&regs->preds, p3_0);
	ONEXT(&regs->gp, gp);
	ONEXT(&regs->ugp, ugp);
	ONEXT(&pt_elr(regs), pc);
	dummy = pt_cause(regs);
	ONEXT(&dummy, cause);
	ONEXT(&pt_badva(regs), badva);

	/* Pad the rest with zeros, if needed */
	if (!ret)
		ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
					offsetof(struct user_regs_struct, pad1), -1);
	return ret;
}
Example #17
0
void trace_edge(CT_QuadEdgeRef e, void* _) {
  CT_INFO("edge: %zx next: %zx", e, ONEXT(e));
}
Example #18
0
static void
rec_delaunay(site_struct * sites[],
             int sl, int sh, edge_ref * le, edge_ref * re)
{
    if (sh == sl + 2) {
        edge_ref a = make_edge();
        ODATA(a) = sites[sl];
        DDATA(a) = sites[sl + 1];
        *le = a;
        *re = SYM(a);
    } else if (sh == sl + 3) {
        edge_ref a = make_edge();
        edge_ref b = make_edge();
        double ct = ccw(sites[sl], sites[sl + 1], sites[sl + 2]);
        splice(SYM(a), b);
        ODATA(a) = sites[sl];
        DDATA(a) = sites[sl + 1];
        ODATA(b) = sites[sl + 1];
        DDATA(b) = sites[sl + 2];
        if (ct == 0.0) {
            *le = a;
            *re = SYM(b);
        } else {
            edge_ref c = connect(b, a);
            if (ct > 0.0) {
                *le = a;
                *re = SYM(b);
            } else {
                *le = SYM(c);
                *re = c;
            }
        }
    } else {
        edge_ref ldo, ldi, rdi, rdo;
        edge_ref basel, lcand, rcand;

        int sm = (sl + sh) / 2;

        rec_delaunay(sites, sl, sm, &ldo, &ldi);
        rec_delaunay(sites, sm, sh, &rdi, &rdo);

        while (1) {
            if (leftof(ORG(rdi), ldi))
                ldi = LNEXT(ldi);
            else if (rightof(ORG(ldi), rdi))
                rdi = ONEXT(SYM(rdi));
            else
                break;
        }

        basel = connect(SYM(rdi), ldi);
        if (ORG(ldi) == ORG(ldo))
            ldo = SYM(basel);
        if (ORG(rdi) == ORG(rdo))
            rdo = basel;

        while (1) {
            lcand = ONEXT(SYM(basel));
            if (rightof(DEST(lcand), basel))
                while (incircle
                       (DEST(basel), ORG(basel), DEST(lcand),
                        DEST(ONEXT(lcand)))) {
                    edge_ref t = ONEXT(lcand);
                    destroy_edge(lcand);
                    lcand = t;
                }

            rcand = OPREV(basel);
            if (rightof(DEST(rcand), basel))
                while (incircle
                       (DEST(basel), ORG(basel), DEST(rcand),
                        DEST(OPREV(rcand)))) {
                    edge_ref t = OPREV(rcand);
                    destroy_edge(rcand);
                    rcand = t;
                }

            if (!rightof(DEST(lcand), basel)
                && !rightof(DEST(rcand), basel))
                break;

            if (!rightof(DEST(lcand), basel) ||
                (rightof(DEST(rcand), basel) &&
                 incircle(DEST(lcand), ORG(lcand), ORG(rcand),
                          DEST(rcand))))
                basel = connect(rcand, SYM(basel));
            else
                basel = connect(SYM(basel), SYM(lcand));
        }
        *le = ldo;
        *re = rdo;
    }
}