Example #1
0
/*
 * Expand arithmetic expression.
 */
static char *
expari(char *p, int flag, struct worddest *dst)
{
	char *q, *start;
	arith_t result;
	int begoff;
	int quoted;
	int adj;

	quoted = *p++ == '"';
	begoff = expdest - stackblock();
	p = argstr(p, 0, NULL);
	STPUTC('\0', expdest);
	start = stackblock() + begoff;

	q = grabstackstr(expdest);
	result = arith(start);
	ungrabstackstr(q, expdest);

	start = stackblock() + begoff;
	adj = start - expdest;
	STADJUST(adj, expdest);

	CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest);
	fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result);
	adj = strlen(expdest);
	STADJUST(adj, expdest);
	if (!quoted)
		reprocess(expdest - adj - stackblock(), flag, VSNORMAL, 0, dst);
	return p;
}
Example #2
0
static char *
evalvar(char *p, int flag, struct worddest *dst)
{
	int subtype;
	int varflags;
	char *var;
	const char *val;
	int patloc;
	int c;
	int set;
	int special;
	int startloc;
	int varlen;
	int varlenb;
	char buf[21];

	varflags = (unsigned char)*p++;
	subtype = varflags & VSTYPE;
	var = p;
	special = 0;
	if (! is_name(*p))
		special = 1;
	p = strchr(p, '=') + 1;
again: /* jump here after setting a variable with ${var=text} */
	if (varflags & VSLINENO) {
		set = 1;
		special = 1;
		val = NULL;
	} else if (special) {
		set = varisset(var, varflags & VSNUL);
		val = NULL;
	} else {
		val = bltinlookup(var, 1);
		if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
			val = NULL;
			set = 0;
		} else
			set = 1;
	}
	varlen = 0;
	startloc = expdest - stackblock();
	if (!set && uflag && *var != '@' && *var != '*') {
		switch (subtype) {
		case VSNORMAL:
		case VSTRIMLEFT:
		case VSTRIMLEFTMAX:
		case VSTRIMRIGHT:
		case VSTRIMRIGHTMAX:
		case VSLENGTH:
			error("%.*s: parameter not set", (int)(p - var - 1),
			    var);
		}
	}
	if (set && subtype != VSPLUS) {
		/* insert the value of the variable */
		if (special) {
			if (varflags & VSLINENO) {
				if (p - var > (ptrdiff_t)sizeof(buf))
					abort();
				memcpy(buf, var, p - var - 1);
				buf[p - var - 1] = '\0';
				strtodest(buf, flag, subtype,
				    varflags & VSQUOTE, dst);
			} else
				varvalue(var, varflags & VSQUOTE, subtype, flag,
				    dst);
			if (subtype == VSLENGTH) {
				varlenb = expdest - stackblock() - startloc;
				varlen = varlenb;
				if (localeisutf8) {
					val = stackblock() + startloc;
					for (;val != expdest; val++)
						if ((*val & 0xC0) == 0x80)
							varlen--;
				}
				STADJUST(-varlenb, expdest);
			}
		} else {
			if (subtype == VSLENGTH) {
				for (;*val; val++)
					if (!localeisutf8 ||
					    (*val & 0xC0) != 0x80)
						varlen++;
			}
			else
				strtodest(val, flag, subtype,
				    varflags & VSQUOTE, dst);
		}
	}

	if (subtype == VSPLUS)
		set = ! set;

	switch (subtype) {
	case VSLENGTH:
		cvtnum(varlen, buf);
		strtodest(buf, flag, VSNORMAL, varflags & VSQUOTE, dst);
		break;

	case VSNORMAL:
		break;

	case VSPLUS:
	case VSMINUS:
		if (!set) {
			argstr(p, flag | (flag & EXP_SPLIT ? EXP_SPLIT_LIT : 0) |
			    (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0), dst);
			break;
		}
		break;

	case VSTRIMLEFT:
	case VSTRIMLEFTMAX:
	case VSTRIMRIGHT:
	case VSTRIMRIGHTMAX:
		if (!set)
			break;
		/*
		 * Terminate the string and start recording the pattern
		 * right after it
		 */
		STPUTC('\0', expdest);
		patloc = expdest - stackblock();
		subevalvar_trim(p, patloc, subtype, startloc);
		reprocess(startloc, flag, VSNORMAL, varflags & VSQUOTE, dst);
		if (flag & EXP_SPLIT && *var == '@' && varflags & VSQUOTE)
			dst->state = WORD_QUOTEMARK;
		break;

	case VSASSIGN:
	case VSQUESTION:
		if (!set) {
			if (subevalvar_misc(p, var, subtype, startloc,
			    varflags)) {
				varflags &= ~VSNUL;
				goto again;
			}
			break;
		}
		break;

	case VSERROR:
		c = p - var - 1;
		error("${%.*s%s}: Bad substitution", c, var,
		    (c > 0 && *p != CTLENDVAR) ? "..." : "");

	default:
		abort();
	}

	if (subtype != VSNORMAL) {	/* skip to end of alternative */
		int nesting = 1;
		for (;;) {
			if ((c = *p++) == CTLESC)
				p++;
			else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
				if (set)
					argbackq = argbackq->next;
			} else if (c == CTLVAR) {
				if ((*p++ & VSTYPE) != VSNORMAL)
					nesting++;
			} else if (c == CTLENDVAR) {
				if (--nesting == 0)
					break;
			}
		}
	}
	return p;
}
Example #3
0
  // LEARNING FUNCTION: add new patterns and run optimization steps selected with adaptative schedule
  virtual int add (int x_id, int yi)
  {
    ++nb_seen_examples;
    // create a new output object if this one has never been seen before 
    if (!getOutput (yi))
      {
	outputs.insert (std::make_pair (yi, LaRankOutput ()));
	LaRankOutput *cur = getOutput (yi);
	cur->initialize (kfunc, cache);
	if (outputs.size () == 1)
	  y0 = outputs.begin ()->first;
	// link the cache of this new output to a buddy 
	if (outputs.size () > 1)
	  {
	    LaRankOutput *out0 = getOutput (y0);
	    cur->set_kernel_buddy (out0->getKernel ());
	  }
      }

    LaRankPattern tpattern (x_id, yi);
    LaRankPattern & pattern = (patterns.isPattern (x_id)) ? patterns.getPattern (x_id) : tpattern;

    // ProcessNew with the "fresh" pattern
    double time1 = getTime ();
    process_return_t pro_ret = process (pattern, processNew);
    double dual_increase = pro_ret.dual_increase;
    double duration = (getTime () - time1);
    double coeff = dual_increase / (0.00001 + duration);
    dual += dual_increase;
    n_pro++;
    w_pro = 0.05 * coeff + (1 - 0.05) * w_pro;

    // ProcessOld & Optimize until ready for a new processnew
    // (Adaptative schedule here)
    for (;;)
      {
	double w_sum = w_pro + w_rep + w_opt;
	double prop_min = w_sum / 20;
	if (w_pro < prop_min)
	  w_pro = prop_min;
	if (w_rep < prop_min)
	  w_rep = prop_min;
	if (w_opt < prop_min)
	  w_opt = prop_min;
	w_sum = w_pro + w_rep + w_opt;
	double r = rand () / (double) RAND_MAX * w_sum;
	if (r <= w_pro)
	  {
	    break;
	  }
	else if ((r > w_pro) && (r <= w_pro + w_rep))	// ProcessOld here
	  {
	    double time1 = getTime ();
	    double dual_increase = reprocess ();
	    double duration = (getTime () - time1);
	    double coeff = dual_increase / (0.00001 + duration);
	    dual += dual_increase;
	    n_rep++;
	    w_rep = 0.05 * coeff + (1 - 0.05) * w_rep;
	  }
	else			// Optimize here 
	  {
	    double time1 = getTime ();
	    double dual_increase = optimize ();
	    double duration = (getTime () - time1);
	    double coeff = dual_increase / (0.00001 + duration);
	    dual += dual_increase;
	    n_opt++;
	    w_opt = 0.05 * coeff + (1 - 0.05) * w_opt;
	  }
      }
    if (nb_seen_examples % 100 == 0)	// Cleanup useless Support Vectors/Patterns sometimes
      nb_removed += cleanup ();
    return pro_ret.ypred;
  }