Exemplo n.º 1
0
expr_let::expr_let(name const & n, expr const & t, expr const & v, expr const & b, tag g):
    expr_composite(expr_kind::Let,
                   ::lean::hash(::lean::hash(t.hash(), v.hash()), b.hash()),
                   t.has_expr_metavar()   || v.has_expr_metavar() || b.has_expr_metavar(),
                   t.has_univ_metavar()   || v.has_univ_metavar() || b.has_univ_metavar(),
                   t.has_local()          || v.has_local() || b.has_local(),
                   t.has_param_univ()     || v.has_param_univ() || b.has_param_univ(),
                   inc_weight(add_weight(add_weight(get_weight(t), get_weight(v)), get_weight(b))),
                   std::max(std::max(get_free_var_range(t), get_free_var_range(v)), dec(get_free_var_range(b))),
                   g),
    m_name(n), m_type(t), m_value(v), m_body(b) {
    m_hash = ::lean::hash(m_hash, m_weight);
}
Exemplo n.º 2
0
void
add_weights(int32_t *refs)
{
	int i;
	for (i = 0; i < NUM_WT; i++) {
		add_weight(refs[i], i);
	}
}
Exemplo n.º 3
0
void Router::update_weights(
    const std::vector<float>& probs,
    const std::unordered_map<std::string, size_t>& symbol_counts,
    const std::unordered_map<Ob, size_t>& ob_counts,
    std::vector<float>& symbol_weights, std::vector<float>& ob_weights,
    float reltol) const {
    POMAGMA_INFO("Updating weights");
    const size_t symbol_count = m_types.size();
    const size_t ob_count = m_carrier.item_count();
    POMAGMA_ASSERT_EQ(probs.size(), 1 + ob_count);
    POMAGMA_ASSERT_EQ(symbol_weights.size(), symbol_count);
    POMAGMA_ASSERT_EQ(ob_weights.size(), 1 + ob_count);
    const float max_increase = 1.0 + reltol;

    std::vector<float> temp_symbol_weights(symbol_weights.size());
    std::vector<float> temp_ob_weights(ob_weights.size());

update_weights_loop : {
    POMAGMA_DEBUG("distributing route weight");

    std::fill(temp_symbol_weights.begin(), temp_symbol_weights.end(), 0);
    for (size_t i = 0; i < symbol_count; ++i) {
        temp_symbol_weights[i] = map_get(symbol_counts, m_types[i].name, 0);
    }

    std::fill(temp_ob_weights.begin(), temp_ob_weights.end(), 0);
    for (const auto& pair : ob_counts) {
        temp_ob_weights[pair.first] = pair.second;
    }

#pragma omp parallel for schedule(dynamic, 1)
    for (size_t i = 0; i < ob_count; ++i) {
        Ob ob = 1 + i;

        const float weight = ob_weights[ob] / probs[ob];
        for (const Segment& segment : iter_val(ob)) {
            float part = weight * get_prob(segment, probs);
            add_weight(part, segment, temp_symbol_weights, temp_ob_weights);
        }
    }

    std::swap(symbol_weights, temp_symbol_weights);
    std::swap(ob_weights, temp_ob_weights);

    for (size_t i = 0; i < symbol_count; ++i) {
        if (symbol_weights[i] > temp_symbol_weights[i] * max_increase) {
            goto update_weights_loop;
        }
    }

    for (size_t i = 0; i < ob_count; ++i) {
        Ob ob = 1 + i;
        if (ob_weights[ob] > temp_ob_weights[ob] * max_increase) {
            goto update_weights_loop;
        }
    }
}
}
Exemplo n.º 4
0
void Thing_Data :: To( content_array* where )
{
  if( array == NULL ) {
    *where += this;
    array   = where;
    }

  add_weight( this, number );
}
Exemplo n.º 5
0
expr_app::expr_app(expr const & fn, expr const & arg, tag g):
    expr_composite(expr_kind::App, ::lean::hash(fn.hash(), arg.hash()),
                   fn.has_expr_metavar() || arg.has_expr_metavar(),
                   fn.has_univ_metavar() || arg.has_univ_metavar(),
                   fn.has_local()        || arg.has_local(),
                   fn.has_param_univ()   || arg.has_param_univ(),
                   inc_weight(add_weight(get_weight(fn), get_weight(arg))),
                   std::max(get_free_var_range(fn), get_free_var_range(arg)),
                   g),
    m_fn(fn), m_arg(arg) {
    m_hash = ::lean::hash(m_hash, m_weight);
}
Exemplo n.º 6
0
expr_binding::expr_binding(expr_kind k, name const & n, expr const & t, expr const & b, binder_info const & i, tag g):
    expr_composite(k, ::lean::hash(t.hash(), b.hash()),
                   t.has_expr_metavar()   || b.has_expr_metavar(),
                   t.has_univ_metavar()   || b.has_univ_metavar(),
                   t.has_local()          || b.has_local(),
                   t.has_param_univ()     || b.has_param_univ(),
                   inc_weight(add_weight(get_weight(t), get_weight(b))),
                   std::max(get_free_var_range(t), dec(get_free_var_range(b))),
                   g),
    m_binder(n, t, i),
    m_body(b) {
    m_hash = ::lean::hash(m_hash, m_weight);
    lean_assert(k == expr_kind::Lambda || k == expr_kind::Pi);
}
Exemplo n.º 7
0
expr_macro::expr_macro(macro_definition const & m, unsigned num, expr const * args, tag g):
    expr_composite(expr_kind::Macro,
                   lean::hash(num, [&](unsigned i) { return args[i].hash(); }, m.hash()),
                   std::any_of(args, args+num, [](expr const & e) { return e.has_expr_metavar(); }),
                   std::any_of(args, args+num, [](expr const & e) { return e.has_univ_metavar(); }),
                   std::any_of(args, args+num, [](expr const & e) { return e.has_local(); }),
                   std::any_of(args, args+num, [](expr const & e) { return e.has_param_univ(); }),
                   inc_weight(add_weight(num, args)),
                   get_free_var_range(num, args),
                   g),
    m_definition(m),
    m_num_args(num) {
    m_args = new expr[num];
    for (unsigned i = 0; i < m_num_args; i++)
        m_args[i] = args[i];
}
Exemplo n.º 8
0
static fz_weights *
make_weights(fz_context *ctx, int src_w, float x, float dst_w, fz_scale_filter *filter, int vertical, int dst_w_int, int patch_l, int patch_r, int n, int flip)
{
	fz_weights *weights;
	float F, G;
	float window;
	int j;

	if (dst_w < src_w)
	{
		/* Scaling down */
		F = dst_w / src_w;
		G = 1;
	}
	else
	{
		/* Scaling up */
		F = 1;
		G = src_w / dst_w;
	}
	window = filter->width / F;
	DBUG(("make_weights src_w=%d x=%g dst_w=%g patch_l=%d patch_r=%d F=%g window=%g\n", src_w, x, dst_w, patch_l, patch_r, F, window));
	weights	= new_weights(ctx, filter, src_w, dst_w, patch_r-patch_l, n, flip, patch_l);
	if (!weights)
		return NULL;
	for (j = patch_l; j < patch_r; j++)
	{
		/* find the position of the centre of dst[j] in src space */
		float centre = (j - x + 0.5f)*src_w/dst_w - 0.5f;
		int l, r;
		l = ceilf(centre - window);
		r = floorf(centre + window);
		DBUG(("%d: centre=%g l=%d r=%d\n", j, centre, l, r));
		init_weights(weights, j);
		for (; l <= r; l++)
		{
			add_weight(weights, j, l, filter, x, F, G, src_w, dst_w);
		}
		check_weights(weights, j, dst_w_int, x, dst_w);
		if (vertical)
		{
			reorder_weights(weights, j, src_w);
		}
	}
	weights->count++; /* weights->count = dst_w_int now */
	return weights;
}
Exemplo n.º 9
0
unsigned light_lt_manager::get_weight_core(expr const & e) {
    switch (e.kind()) {
    case expr_kind::Var:  case expr_kind::Constant: case expr_kind::Sort:
    case expr_kind::Meta: case expr_kind::Local:
        return 1;
    case expr_kind::Lambda: case expr_kind::Pi:
        return safe_add(1, safe_add(get_weight(binding_domain(e)), get_weight(binding_body(e))));
    case expr_kind::Macro:
        return safe_add(1, add_weight(macro_num_args(e), macro_args(e)));
    case expr_kind::App:
        buffer<expr> args;
        expr fn = get_app_args(e, args);
        if (is_constant(fn)) {
            unsigned const * light_arg = m_lrs.find(const_name(fn));
            if (light_arg && args.size() > *light_arg) return get_weight(args[*light_arg]);
        }
        return safe_add(1, safe_add(get_weight(app_fn(e)), get_weight(app_arg(e))));
    }
    lean_unreachable(); // LCOV_EXCL_LINE
}
Exemplo n.º 10
0
static unsigned add_weight(unsigned num, expr const * args) {
    unsigned r = 0;
    for (unsigned i = 0; i < num; i++)
        r = add_weight(r, get_weight(args[i]));
    return r;
}
Exemplo n.º 11
0
static fz_weights *
make_weights(fz_context *ctx, int src_w, float x, float dst_w, fz_scale_filter *filter, int vertical, int dst_w_int, int patch_l, int patch_r, int n, int flip, fz_scale_cache *cache)
{
	fz_weights *weights;
	float F, G;
	float window;
	int j;

	if (cache)
	{
		if (cache->src_w == src_w && cache->x == x && cache->dst_w == dst_w &&
			cache->filter == filter && cache->vertical == vertical &&
			cache->dst_w_int == dst_w_int &&
			cache->patch_l == patch_l && cache->patch_r == patch_r &&
			cache->n == n && cache->flip == flip)
		{
			return cache->weights;
		}
		cache->src_w = src_w;
		cache->x = x;
		cache->dst_w = dst_w;
		cache->filter = filter;
		cache->vertical = vertical;
		cache->dst_w_int = dst_w_int;
		cache->patch_l = patch_l;
		cache->patch_r = patch_r;
		cache->n = n;
		cache->flip = flip;
		fz_free(ctx, cache->weights);
		cache->weights = NULL;
	}

	if (dst_w < src_w)
	{
		/* Scaling down */
		F = dst_w / src_w;
		G = 1;
	}
	else
	{
		/* Scaling up */
		F = 1;
		G = src_w / dst_w;
	}
	window = filter->width / F;
	weights	= new_weights(ctx, filter, src_w, dst_w, patch_r-patch_l, n, flip, patch_l);
	if (!weights)
		return NULL;
	for (j = patch_l; j < patch_r; j++)
	{
		/* find the position of the centre of dst[j] in src space */
		float centre = (j - x + 0.5f)*src_w/dst_w - 0.5f;
		int l, r;
		l = ceilf(centre - window);
		r = floorf(centre + window);
		init_weights(weights, j);
		for (; l <= r; l++)
		{
			add_weight(weights, j, l, filter, x, F, G, src_w, dst_w);
		}
		check_weights(weights, j, dst_w_int, x, dst_w);
		if (vertical)
		{
			reorder_weights(weights, j, src_w);
		}
	}
	weights->count++; /* weights->count = dst_w_int now */
	if (cache)
	{
		cache->weights = weights;
	}
	return weights;
}
Exemplo n.º 12
0
void
dump_collate(void)
{
	FILE			*f;
	int			i, j, n;
	size_t			sz;
	int32_t			pri;
	collelem_t		*ce;
	collchar_t		*cc;
	subst_t			*sb;
	char			vers[COLLATE_STR_LEN];
	collate_char_t		chars[UCHAR_MAX + 1];
	collate_large_t		*large;
	collate_subst_t		*subst[COLL_WEIGHTS_MAX];
	collate_chain_t		*chain;

	/*
	 * We have to run throught a preliminary pass to identify all the
	 * weights that we use for each sorting level.
	 */
	for (i = 0; i < NUM_WT; i++) {
		add_weight(pri_ignore, i);
	}
	for (i = 0; i < NUM_WT; i++) {
		for (sb = avl_first(&substs[i]); sb;
		    sb = AVL_NEXT(&substs[i], sb)) {
			for (j = 0; sb->ref[j]; j++) {
				add_weight(sb->ref[j], i);
			}
		}
	}
	for (ce = avl_first(&elem_by_expand);
	    ce != NULL;
	    ce = AVL_NEXT(&elem_by_expand, ce)) {
		add_weights(ce->ref);
	}
	for (cc = avl_first(&collchars); cc; cc = AVL_NEXT(&collchars, cc)) {
		add_weights(cc->ref);
	}

	/*
	 * Now we walk the entire set of weights, removing the gaps
	 * in the weights.  This gives us optimum usage.  The walk
	 * occurs in priority.
	 */
	for (i = 0; i < NUM_WT; i++) {
		weight_t *w;
		for (w = avl_first(&weights[i]); w;
		    w = AVL_NEXT(&weights[i], w)) {
			w->opt = nweight[i];
			nweight[i] += 1;
		}
	}

	(void) memset(&chars, 0, sizeof (chars));
	(void) memset(vers, 0, COLLATE_STR_LEN);
	(void) strlcpy(vers, COLLATE_VERSION, sizeof (vers));

	/*
	 * We need to make sure we arrange for the UNDEFINED field
	 * to show up.  Also, set the total weight counts.
	 */
	for (i = 0; i < NUM_WT; i++) {
		if (resolve_pri(pri_undefined[i]) == -1) {
			set_pri(pri_undefined[i], -1, RESOLVED);
			/* they collate at the end of everything else */
			collinfo.undef_pri[i] = COLLATE_MAX_PRIORITY;
		}
		collinfo.pri_count[i] = nweight[i];
	}

	collinfo.pri_count[NUM_WT] = max_wide();
	collinfo.undef_pri[NUM_WT] = COLLATE_MAX_PRIORITY;
	collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED;

	/*
	 * Ordinary character priorities
	 */
	for (i = 0; i <= UCHAR_MAX; i++) {
		if ((cc = get_collchar(i, 0)) != NULL) {
			for (j = 0; j < NUM_WT; j++) {
				chars[i].pri[j] = get_weight(cc->ref[j], j);
			}
		} else {
			for (j = 0; j < NUM_WT; j++) {
				chars[i].pri[j] =
				    get_weight(pri_undefined[j], j);
			}
			/*
			 * Per POSIX, for undefined characters, we
			 * also have to add a last item, which is the
			 * character code.
			 */
			chars[i].pri[NUM_WT] = i;
		}
	}

	/*
	 * Substitution tables
	 */
	for (i = 0; i < NUM_WT; i++) {
		collate_subst_t *st = NULL;
		n = collinfo.subst_count[i] = avl_numnodes(&substs[i]);
		if ((st = calloc(sizeof (collate_subst_t) * n, 1)) == NULL) {
			errf(_("out of memory"));
			return;
		}
		n = 0;
		for (sb = avl_first(&substs[i]); sb;
		    sb = AVL_NEXT(&substs[i], sb)) {
			if ((st[n].key = resolve_pri(sb->key)) < 0) {
				/* by definition these resolve! */
				INTERR;
			}
			if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) {
				INTERR;
			}
			for (j = 0; sb->ref[j]; j++) {
				st[n].pri[j] = get_weight(sb->ref[j], i);
			}
			n++;
		}
		if (n != collinfo.subst_count[i])
			INTERR;
		subst[i] = st;
	}


	/*
	 * Chains, i.e. collating elements
	 */
	collinfo.chain_count = avl_numnodes(&elem_by_expand);
	chain = calloc(sizeof (collate_chain_t), collinfo.chain_count);
	if (chain == NULL) {
		errf(_("out of memory"));
		return;
	}
	for (n = 0, ce = avl_first(&elem_by_expand);
	    ce != NULL;
	    ce = AVL_NEXT(&elem_by_expand, ce), n++) {
		(void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN);
		for (i = 0; i < NUM_WT; i++) {
			chain[n].pri[i] = get_weight(ce->ref[i], i);
		}
	}
	if (n != collinfo.chain_count)
		INTERR;

	/*
	 * Large (> UCHAR_MAX) character priorities
	 */
	large = calloc(sizeof (collate_large_t) * avl_numnodes(&collchars), 1);
	if (large == NULL) {
		errf(_("out of memory"));
		return;
	}

	i = 0;
	for (cc = avl_first(&collchars); cc; cc = AVL_NEXT(&collchars, cc)) {
		int	undef = 0;
		/* we already gathered those */
		if (cc->wc <= UCHAR_MAX)
			continue;
		for (j = 0; j < NUM_WT; j++) {
			if ((pri = get_weight(cc->ref[j], j)) < 0) {
				undef = 1;
			}
			if (undef && (pri >= 0)) {
				/* if undefined, then all priorities are */
				INTERR;
			} else {
				large[i].pri.pri[j] = pri;
			}
		}
		if (!undef) {
			large[i].val = cc->wc;
			collinfo.large_count = i++;
		}
	}

	if ((f = open_category()) == NULL) {
		return;
	}

	/* Time to write the entire data set out */

	if ((wr_category(vers, COLLATE_STR_LEN, f) < 0) ||
	    (wr_category(&collinfo, sizeof (collinfo), f) < 0) ||
	    (wr_category(&chars, sizeof (chars), f) < 0)) {
		delete_category(f);
		return;
	}

	for (i = 0; i < NUM_WT; i++) {
		sz =  sizeof (collate_subst_t) * collinfo.subst_count[i];
		if (wr_category(subst[i], sz, f) < 0) {
			delete_category(f);
			return;
		}
	}
	sz = sizeof (collate_chain_t) * collinfo.chain_count;
	if (wr_category(chain, sz, f) < 0) {
		delete_category(f);
		return;
	}
	sz = sizeof (collate_large_t) * collinfo.large_count;
	if (wr_category(large, sz, f) < 0) {
		delete_category(f);
		return;
	}

	close_category(f);
}