Exemple #1
0
static PyObject *
structseq_repr(PyStructSequence *obj)
{
	/* buffer and type size were chosen well considered. */
#define REPR_BUFFER_SIZE 512
#define TYPE_MAXSIZE 100

	PyObject *tup;
	PyTypeObject *typ = Py_TYPE(obj);
	int i, removelast = 0;
	Py_ssize_t len;
	char buf[REPR_BUFFER_SIZE];
	char *endofbuf, *pbuf = buf;

	/* pointer to end of writeable buffer; safes space for "...)\0" */
	endofbuf= &buf[REPR_BUFFER_SIZE-5];

	if ((tup = make_tuple(obj)) == NULL) {
		return NULL;
	}

	/* "typename(", limited to  TYPE_MAXSIZE */
	len = strlen(typ->tp_name) > TYPE_MAXSIZE ? TYPE_MAXSIZE :
						    strlen(typ->tp_name);
	strncpy(pbuf, typ->tp_name, len);
	pbuf += len;
	*pbuf++ = '(';

	for (i=0; i < VISIBLE_SIZE(obj); i++) {
		PyObject *val, *repr;
		char *cname, *crepr;

		cname = typ->tp_members[i].name;
		
		val = PyTuple_GetItem(tup, i);
		if (cname == NULL || val == NULL) {
			return NULL;
		}
		repr = PyObject_Repr(val);
		if (repr == NULL) {
			Py_DECREF(tup);
			return NULL;
		}
		crepr = _PyUnicode_AsString(repr);
		if (crepr == NULL) {
			Py_DECREF(tup);
			Py_DECREF(repr);
			return NULL;
		}
		
		/* + 3: keep space for "=" and ", " */
 		len = strlen(cname) + strlen(crepr) + 3;
		if ((pbuf+len) <= endofbuf) {
			strcpy(pbuf, cname);
			pbuf += strlen(cname);
			*pbuf++ = '=';
			strcpy(pbuf, crepr);
			pbuf += strlen(crepr);
			*pbuf++ = ',';
			*pbuf++ = ' ';
			removelast = 1;
			Py_DECREF(repr);
		}
		else {
			strcpy(pbuf, "...");
			pbuf += 3;
			removelast = 0;
			Py_DECREF(repr);
			break;
		}
	}
	Py_DECREF(tup);
	if (removelast) {
		/* overwrite last ", " */
		pbuf-=2;
	}
	*pbuf++ = ')';
	*pbuf = '\0';

	return PyUnicode_FromString(buf);
}
Network::Network(string undirected_network_file_path, string directed_network_file_path, int num_topics, double lambda)
:num_topics_(num_topics), lambda_(lambda), convergence_(false), likelihood_(numeric_limits<double>::lowest()), round_(0)
{
	cout << "Reading from network files including nodes and links." << endl;
	map<tuple<int, int>, int> link_index_map;
	string line;
  	ifstream undirected_network_file(undirected_network_file_path);
	if (undirected_network_file.is_open()){
	    while (getline(undirected_network_file, line)){
	    	vector<string> elements;
	    	stringstream s(line);
	    	string item;
		    while (std::getline(s, item, '\t'))
		        if (!item.empty())
		            elements.push_back(item);
	    	int node_i = std::stoi(elements[0]);
	    	int node_j = std::stoi(elements[1]);
	    	double weight = std::stoi(elements[2]);

	      	if (node_i >= node_j) swap(node_i, node_j);
	      	tuple<int, int> link = make_tuple(node_i, node_j);
	      	auto iter = link_index_map.find(link);
	      	if (iter == link_index_map.end()) {
	      		weighted_links_.push_back(make_tuple(node_i, node_j, lambda * weight));
	      		link_index_map[link] = weighted_links_.size() - 1;
	      	} else {
	      		get<2>(weighted_links_[iter->second]) += lambda * weight; 
	      	}
	    }
	    undirected_network_file.close();
	}

	map<tuple<int, int>, int> link_directed_weight_map;
  	ifstream directed_network_file(directed_network_file_path);
	if (directed_network_file.is_open()){
	    while (getline(directed_network_file, line)){
	    	vector<string> elements;
	    	stringstream s(line);
	    	string item;
		    while (std::getline(s, item, '\t'))
		        if (!item.empty())
		            elements.push_back(item);
	    	int node_i = std::stoi(elements[0]);
	    	int node_j = std::stoi(elements[1]);
	    	double weight = std::stoi(elements[2]);

	    	tuple<int, int> link = make_tuple(node_i, node_j);
	    	if (link_directed_weight_map.find(link) == link_directed_weight_map.end()) {
	      		link_directed_weight_map[link] = weight;
	      	} else {
	      		link_directed_weight_map[link] += weight;
	      	}

	      	if (node_i >= node_j) swap(node_i, node_j);
	      	link = make_tuple(node_i, node_j);
	      	auto iter = link_index_map.find(link);
	      	if (iter == link_index_map.end()) {
	      		weighted_links_.push_back(make_tuple(node_i, node_j, weight));
	      		link_index_map[link] = weighted_links_.size() - 1;
	      	} else {
	      		get<2>(weighted_links_[iter->second]) += weight;
	      	}
	    }
	    directed_network_file.close();
	}

	num_links_ = weighted_links_.size();
	cout << "Total links: " << num_links_ << endl;
	num_nodes_ = 0;
	for (auto iter = weighted_links_.begin(); iter != weighted_links_.end(); iter++) {
		if (get<1>(*iter) > num_nodes_) num_nodes_ = get<1>(*iter);
	}
	cout << "Total nodes: " << num_nodes_ << endl;
	theta_ = new double*[num_nodes_ + 1];
	for (int i = 0; i < num_nodes_ + 1; ++i)
    	theta_[i] = new double[num_topics_]();
	omega_ = new double*[num_topics_];
	for (int r = 0; r < num_topics_; ++r)
    	omega_[r] = new double[num_topics_]();
	gamma_ = new double[num_nodes_ + 1]();
	link_sum_ = new double[num_nodes_ + 1]();

	for (auto iter = weighted_links_.begin(); iter != weighted_links_.end(); iter++) {
		link_sum_[get<0>(*iter)] += get<2>(*iter);
		link_sum_[get<1>(*iter)] += get<2>(*iter);
	}
	for (int i = 1; i <= num_nodes_; i++) {
		link_sum_[0] += link_sum_[i];
		gamma_[i] = link_sum_[i];
	}

	gamma_[0] += link_sum_[0];
	for (int i = 1; i <= num_nodes_; i++) {
		gamma_[i] /= gamma_[0];
	}
}
Exemple #3
0
Eterm erts_instr_get_memory_map(Process *proc)
{
    MapStatBlock_t *org_mem_anchor;
    Eterm hdr_tuple, md_list, res;
    Eterm *hp;
    Uint hsz;
    MapStatBlock_t *bp;
#ifdef DEBUG
    Eterm *end_hp;
#endif
    
    if (!erts_instr_memory_map)
	return am_false;

    if (!atoms_initialized)
	init_atoms();
    if (!am_n)
	init_am_n();
    if (!am_c)
	init_am_c();
    if (!am_a)
	init_am_a();

    erts_mtx_lock(&instr_x_mutex);
    erts_mtx_lock(&instr_mutex);

    /* Header size */
    hsz = 5 + 1 + (ERTS_ALC_N_MAX+1-ERTS_ALC_N_MIN)*(1 + 4);

    /* Memory data list */
    for (bp = mem_anchor; bp; bp = bp->next) {
	if (is_internal_pid(bp->pid)) {
#if (_PID_NUM_SIZE - 1 > MAX_SMALL)
	    if (internal_pid_number(bp->pid) > MAX_SMALL)
		hsz += BIG_UINT_HEAP_SIZE;
#endif
#if (_PID_SER_SIZE - 1 > MAX_SMALL)
	    if (internal_pid_serial(bp->pid) > MAX_SMALL)
		hsz += BIG_UINT_HEAP_SIZE;
#endif
	    hsz += 4;
	}

	if ((UWord) bp->mem > MAX_SMALL)
	    hsz += BIG_UINT_HEAP_SIZE;
	if (bp->size > MAX_SMALL)
	    hsz += BIG_UINT_HEAP_SIZE;

	hsz += 5 + 2;
    }

    hsz += 3; /* Root tuple */

    org_mem_anchor = mem_anchor;
    mem_anchor = NULL;

    erts_mtx_unlock(&instr_mutex);

    hp = HAlloc(proc, hsz); /* May end up calling map_stat_alloc() */

    erts_mtx_lock(&instr_mutex);

#ifdef DEBUG
    end_hp = hp + hsz;
#endif

    {	/* Build header */
	ErtsAlcType_t n;
	Eterm type_map;
	Uint *hp2 = hp;
#ifdef DEBUG
	Uint *hp2_end;
#endif

	hp += (ERTS_ALC_N_MAX + 1 - ERTS_ALC_N_MIN)*4;

#ifdef DEBUG
	hp2_end = hp;
#endif

	type_map = make_tuple(hp);
	*(hp++) = make_arityval(ERTS_ALC_N_MAX + 1 - ERTS_ALC_N_MIN);

	for (n = ERTS_ALC_N_MIN; n <= ERTS_ALC_N_MAX; n++) {
	    ErtsAlcType_t t = ERTS_ALC_N2T(n);
	    ErtsAlcType_t a = ERTS_ALC_T2A(t);
	    ErtsAlcType_t c = ERTS_ALC_T2C(t);

	    if (!erts_allctrs_info[a].enabled)
		a = ERTS_ALC_A_SYSTEM;

	    *(hp++) = TUPLE3(hp2, am_n[n], am_a[a], am_c[c]);
	    hp2 += 4;
	}

	ASSERT(hp2 == hp2_end);

	hdr_tuple = TUPLE4(hp,
			   am.instr_hdr,
			   make_small(ERTS_INSTR_VSN),
			   make_small(MAP_STAT_BLOCK_HEADER_SIZE),
			   type_map);

	hp += 5;
    }

    /* Build memory data list */

    for (md_list = NIL, bp = org_mem_anchor; bp; bp = bp->next) {
	Eterm tuple;
	Eterm type;
	Eterm ptr;
	Eterm size;
	Eterm pid;

	if (is_not_internal_pid(bp->pid))
	    pid = am_undefined;
	else {
	    Eterm c;
	    Eterm n;
	    Eterm s;

#if (ERST_INTERNAL_CHANNEL_NO > MAX_SMALL)
#error Oversized internal channel number
#endif
	    c = make_small(ERST_INTERNAL_CHANNEL_NO);

#if (_PID_NUM_SIZE - 1 > MAX_SMALL)
	    if (internal_pid_number(bp->pid) > MAX_SMALL) {
		n = uint_to_big(internal_pid_number(bp->pid), hp);
		hp += BIG_UINT_HEAP_SIZE;
	    }
	    else
#endif
		n = make_small(internal_pid_number(bp->pid));

#if (_PID_SER_SIZE - 1 > MAX_SMALL)
	    if (internal_pid_serial(bp->pid) > MAX_SMALL) {
		s = uint_to_big(internal_pid_serial(bp->pid), hp);
		hp += BIG_UINT_HEAP_SIZE;
	    }
	    else
#endif
		s = make_small(internal_pid_serial(bp->pid));
	    pid = TUPLE3(hp, c, n, s);
	    hp += 4;
	}


#if ERTS_ALC_N_MAX > MAX_SMALL
#error Oversized memory type number
#endif
	type = make_small(bp->type_no);

	if ((UWord) bp->mem > MAX_SMALL) {
	    ptr = uint_to_big((UWord) bp->mem, hp);
	    hp += BIG_UINT_HEAP_SIZE;
	}
	else
	    ptr = make_small((UWord) bp->mem);

	if (bp->size > MAX_SMALL) {
	    size = uint_to_big(bp->size, hp);
	    hp += BIG_UINT_HEAP_SIZE;
	}
	else
	    size = make_small(bp->size);

	tuple = TUPLE4(hp, type, ptr, size, pid);
	hp += 5;

	md_list = CONS(hp, tuple, md_list);
	hp += 2;
    }

    res = TUPLE2(hp, hdr_tuple, md_list);
    
    ASSERT(hp + 3 == end_hp);

    if (mem_anchor) {
	for (bp = mem_anchor; bp->next; bp = bp->next)
	    ;
	ASSERT(org_mem_anchor);
	org_mem_anchor->prev = bp; 
	bp->next = org_mem_anchor;
    }
    else {
	mem_anchor = org_mem_anchor;
    }

    erts_mtx_unlock(&instr_mutex);
    erts_mtx_unlock(&instr_x_mutex);

    return res;
}
Exemple #4
0
grammar unroll(const grammar& g, const var_set& scope)
{
    grammar::arules_t ap;
    grammar::brules_t bp;
    grammar::bvrules_t bvp;

    var_set vset = var_set_union(all_bound_variables(g), scope);
    set <set_id> apset(nonstd::powerset(std::get <0> (vset)));
    set <set_id> bpset(nonstd::powerset(std::get <1> (vset)));
    set <set_id> bvpset(nonstd::powerset(std::get <2> (vset)));
    set <var_set> pset = nonstd::cart(apset, bpset, bvpset);

    auto nonterminals = g.nonterm_set();
    set <agsymb_t> ant = std::get <0> (nonterminals);
    set <bgsymb_t> bnt = std::get <1> (nonterminals);
    set <bvgsymb_t> bvnt = std::get <2> (nonterminals);

    map <tuple <agsymb_t, var_set>, agsymb_t> antp;
    map <tuple <bgsymb_t, var_set>, bgsymb_t> bntp;
    map <tuple <bvgsymb_t, var_set>, bvgsymb_t> bvntp;

    for (auto& agsymb : nonstd::cart(ant, pset)) {
        auto symb = agsymb_t(antp.size());
        antp[agsymb] = symb;
    }
    for (auto& bgsymb : nonstd::cart(bnt, pset)) {
        auto symb = bgsymb_t(bntp.size());
        bntp[bgsymb] = symb;
    }
    for (auto& bvgsymb : nonstd::cart(bvnt, pset)) {
        auto symb = bvgsymb_t(bvntp.size(), std::get <0> (bvgsymb).len);
        // bvntp[bvgsymb] = symb;
        bvntp.insert(pair <tuple <bvgsymb_t, var_set>, bvgsymb_t> (bvgsymb, symb));
    }

    for (auto& agsymb : nonstd::cart(ant, pset)) {
        auto prebound_vars = var_set_union(std::get <1> (agsymb), scope);
        ap[antp[agsymb]].clear();
        for (auto& rule : g.arules.at(std::get <0> (agsymb))) {
            if (var_set_subset(rule -> expand -> free_variables(), prebound_vars)) {
                // auto rulep = rule -> unroll(antp, bntp, bvntp, std::get <1> (agsymb));
                auto rulep = rule -> unroll(antp, bntp, bvntp, vset, prebound_vars);
                ap[antp[agsymb]].push_back(rulep);
            }
        }
    }

    for (auto& bgsymb : nonstd::cart(bnt, pset)) {
        auto prebound_vars = var_set_union(std::get <1> (bgsymb), scope);
        bp[bntp[bgsymb]].clear();
        for (auto& rule : g.brules.at(std::get <0> (bgsymb))) {
            if (var_set_subset(rule -> expand -> free_variables(), prebound_vars)) {
                // auto rulep = rule -> unroll(antp, bntp, bvntp, std::get <1> (bgsymb));
                auto rulep = rule -> unroll(antp, bntp, bvntp, vset, std::get <1> (bgsymb));
                bp[bntp[bgsymb]].push_back(rulep);
            }
        }
    }

    for (auto& bvgsymb : nonstd::cart(bvnt, pset)) {
        auto prebound_vars = var_set_union(std::get <1> (bvgsymb), scope);
        bvp[bvntp[bvgsymb]].clear();
        for (auto& rule : g.bvrules.at(std::get <0> (bvgsymb))) {
            if (var_set_subset(rule -> expand -> free_variables(), prebound_vars)) {
                // auto rulep = rule -> unroll(antp, bntp, bvntp, std::get <1> (bvgsymb));
                auto rulep = rule -> unroll(antp, bntp, bvntp, vset, std::get <1> (bvgsymb));
                bvp[bvntp[bvgsymb]].push_back(rulep);
            }
        }
    }

    if (auto start = boost::get <agsymb_t> (&g.start)) {
        auto startp = antp.at(make_tuple(*start, scope));
        return grammar(ap, bp, bvp, startp);
    } else if (auto start = boost::get <bgsymb_t> (&g.start)) {
        auto startp = bntp.at(make_tuple(*start, scope));
        return grammar(ap, bp, bvp, startp);
    } else if (auto start = boost::get <bvgsymb_t> (&g.start)) {
        auto startp = bvntp.at(make_tuple(*start, scope));
        return grammar(ap, bp, bvp, startp);
    } else {
        assert (false);
    }

    /* auto startp = nonstd::get(antp, bntp, bvntp, U()).at(make_tuple(start, scope));
    return grammar(ap, bp, bvp, startp); */
}
Exemple #5
0
		Status& PushSender(const std::string& caller, const char* file, const int line) 
		{ 
			traceback.push_back(make_tuple(caller, file, line));
			return *this;
		}
Exemple #6
0
TEST( Commission , EquivalenceClass )
{
	srand(time(NULL));
	for( int times = 0 ; times < 100000 ; ++times )
	{
		vector<tuple<int,int,int>> dataSets;
		int sets = rand()%10+5;
		bool VALID_FLAG = true;
		bool TERM_FLAG = false;
		bool LOCK_ERR_FLAG = false ,STOCK_ERR_FLAG = false ,BARREL_ERR_FLAG = false;
		for( int i = 0 ; i < sets ; ++i )
		{
			int l = num() , s = num() , b = num();
			if( !L1(l) || !S1(s) || !B1(b) )
				VALID_FLAG = false;
			if( L2(l) ) TERM_FLAG = true;
			if( L3(l) || L4(l) ) LOCK_ERR_FLAG = true;
			if( S2(s) || S3(s) ) STOCK_ERR_FLAG = true;
			if( B2(b) || B3(b) ) BARREL_ERR_FLAG = true;

			dataSets.emplace_back(make_tuple(l,s,b));

			if( TERM_FLAG ) break;
		}

		//add terminator
		if( !TERM_FLAG )
			if( num() % 4 != 3 ) // 75% chance
			{
				dataSets.emplace_back(make_tuple(-1,0,0));
				TERM_FLAG = true;
			}
			else
				VALID_FLAG = false;

		// for any error, result will be invalid
		if( LOCK_ERR_FLAG || STOCK_ERR_FLAG || BARREL_ERR_FLAG || !TERM_FLAG )
			VALID_FLAG = false;

		double result = retrieve(dataSets);
		if( VALID_FLAG == true )
		{
			ASSERT_TRUE( 0 <= result ) << "Result is Valid but return error";
		}
		else
		{
			// error code must match the flag
			switch(cms_error_code) {
				case TERM_ERROR:
					ASSERT_TRUE(!TERM_FLAG);
					break;
				case LOCK_ERROR:
					ASSERT_TRUE(LOCK_ERR_FLAG);
					break;
				case STOCK_ERROR:
					ASSERT_TRUE(STOCK_ERR_FLAG);
					break;
				case BARREL_ERROR:
					ASSERT_TRUE(BARREL_ERR_FLAG);
					break;
				case OK:
					break;
				default:
					ASSERT_TRUE(false) << "Fatel Error ,Code = " << cms_error_code << endl;
			}
		}
	}
}
Exemple #7
0
/* Copy a message to the message area. */
Eterm copy_struct_lazy(Process *from, Eterm orig, Uint offs)
{
    Eterm obj;
    Eterm dest;
#ifdef INCREMENTAL
    int alloc_old = 0;
#else
    int total_need = 0;
#endif

    VERBOSE(DEBUG_MESSAGES,
            ("COPY START; %T is sending a message @ 0x%016x\n%T\n",
             from->id, orig, orig));

#ifndef INCREMENTAL
 copy_start:
#endif
    MA_STACK_PUSH(src,orig);
    MA_STACK_PUSH(dst,&dest);
    MA_STACK_PUSH(offset,offs);

    while (ma_src_top > 0) {
        obj = MA_STACK_POP(src);

        /* copy_struct_lazy should never be called with something that
         * do not need to be copied. Within the loop, nothing that do
         * not need copying should be placed in the src-stack.
         */
        ASSERT(!NO_COPY(obj));

        switch (primary_tag(obj)) {
        case TAG_PRIMARY_LIST: {
            Eterm *hp;
            Eterm *objp;

            GlobalAlloc(from,2,hp);
            objp = list_val(obj);

            MA_STACK_UPDATE(dst,MA_STACK_POP(offset),make_list(hp));
            MA_STACK_POP(dst);

            /* TODO: Byt ordningen nedan så att CDR pushas först. */

            if (NO_COPY(*objp)) {
                hp[0] = *objp;
#ifdef INCREMENTAL
                if (ptr_within(ptr_val(*objp),inc_fromspc,inc_fromend))
                    INC_STORE(gray,hp,2);
#endif
            } else {
                MA_STACK_PUSH(src,*objp);
                MA_STACK_PUSH(dst,hp);
                MA_STACK_PUSH(offset,0);
            }

            objp++;

            if (NO_COPY(*objp)) {
                hp[1] = *objp;
#ifdef INCREMENTAL
                if (ptr_within(ptr_val(*objp),inc_fromspc,inc_fromend))
                    INC_STORE(gray,hp,2);
#endif
            }
            else {
                MA_STACK_PUSH(src,*objp);
                MA_STACK_PUSH(dst,hp);
                MA_STACK_PUSH(offset,1);
            }
            continue;
        }

        case TAG_PRIMARY_BOXED: {
            Eterm *objp = boxed_val(obj);

            switch (*objp & _TAG_HEADER_MASK) {
            case ARITYVAL_SUBTAG: {
                Uint ari = arityval(*objp);
                Uint i;
                Eterm *hp;
                GlobalAlloc(from,ari + 1,hp);
                /* A GC above might invalidate the value of objp */
                objp = boxed_val(obj);
                MA_STACK_UPDATE(dst,MA_STACK_POP(offset),make_tuple(hp));
                MA_STACK_POP(dst);
                *hp = *objp++;
                for (i = 1; i <= ari; i++) {
                    switch (primary_tag(*objp)) {
                    case TAG_PRIMARY_LIST:
                    case TAG_PRIMARY_BOXED:
                        if (NO_COPY(*objp)) {
                            hp[i] = *objp;
#ifdef INCREMENTAL
                            if (ptr_within(ptr_val(*objp),
                                           inc_fromspc,inc_fromend))
                                INC_STORE(gray,hp,BOXED_NEED(hp,*hp));
#endif
                            objp++;
                        } else {
                            MA_STACK_PUSH(src,*objp++);
                            MA_STACK_PUSH(dst,hp);
                            MA_STACK_PUSH(offset,i);
                        }
                        break;
                    default:
                        hp[i] = *objp++;
                    }
                }
                continue;
            }

            case REFC_BINARY_SUBTAG: {
                ProcBin *pb;
                Uint i = thing_arityval(*objp) + 1;
                Eterm *hp;
                GlobalAlloc(from,i,hp);
                /* A GC above might invalidate the value of objp */
                objp = boxed_val(obj);
                MA_STACK_UPDATE(dst,MA_STACK_POP(offset),make_binary(hp));
                MA_STACK_POP(dst);
                pb = (ProcBin*) hp;
                while (i--) {
                    *hp++ = *objp++;
                }
                erts_refc_inc(&pb->val->refc, 2);
                pb->next = erts_global_offheap.mso;
                erts_global_offheap.mso = pb;
                erts_global_offheap.overhead += pb->size / sizeof(Eterm);
                continue;
            }

            case FUN_SUBTAG: {
                ErlFunThing *funp = (ErlFunThing*) objp;
                Uint i = thing_arityval(*objp) + 1;
                Uint j = i + 1 + funp->num_free;
                Uint k = i;
                Eterm *hp, *hp_start;
                GlobalAlloc(from,j,hp);
                /* A GC above might invalidate the value of objp */
                objp = boxed_val(obj);
                hp_start = hp;
                MA_STACK_UPDATE(dst,MA_STACK_POP(offset),make_fun(hp));
                MA_STACK_POP(dst);
                funp = (ErlFunThing*) hp;
                while (i--) {
                    *hp++ = *objp++;
                }
#ifndef HYBRID // FIND ME!
                funp->next = erts_global_offheap.funs;
                erts_global_offheap.funs = funp;
                erts_refc_inc(&funp->fe->refc, 2);
#endif
                for (i = k; i < j; i++) {
                    switch (primary_tag(*objp)) {
                    case TAG_PRIMARY_LIST:
                    case TAG_PRIMARY_BOXED:
                        if (NO_COPY(*objp)) {
#ifdef INCREMENTAL
                            if (ptr_within(ptr_val(*objp),
                                           inc_fromspc,inc_fromend))
                                INC_STORE(gray,hp,BOXED_NEED(hp,*hp));
#endif
                            *hp++ = *objp++;
                        } else {
                            MA_STACK_PUSH(src,*objp++);
                            MA_STACK_PUSH(dst,hp_start);
                            MA_STACK_PUSH(offset,i);
                            hp++;
                        }
                        break;
                    default:
                        *hp++ = *objp++;
                    }
                }
                continue;
            }

            case EXTERNAL_PID_SUBTAG:
            case EXTERNAL_PORT_SUBTAG:
            case EXTERNAL_REF_SUBTAG: {
                ExternalThing *etp;
                Uint i =  thing_arityval(*objp) + 1;
                Eterm *hp;
                GlobalAlloc(from,i,hp);
                /* A GC above might invalidate the value of objp */
                objp = boxed_val(obj);
                MA_STACK_UPDATE(dst,MA_STACK_POP(offset),make_external(hp));
                MA_STACK_POP(dst);
                etp = (ExternalThing*) hp;
                while (i--)  {
                    *hp++ = *objp++;
                }

                etp->next = erts_global_offheap.externals;
                erts_global_offheap.externals = etp;
		erts_refc_inc(&etp->node->refc, 2);
                continue;
            }

            case SUB_BINARY_SUBTAG: {
                ErlSubBin *sb = (ErlSubBin *) objp;
		Eterm *hp;
		Eterm res_binary;
                Eterm real_bin = sb->orig;
                Uint bit_offset = sb->bitoffs;
		Uint bit_size = sb -> bitsize;
		Uint sub_offset = sb->offs;
                size_t size = sb->size;
		Uint extra_bytes;
		Uint real_size;
		Uint sub_binary_heapneed;
		if ((bit_size + bit_offset) > 8) {
		    extra_bytes = 2;
		    sub_binary_heapneed = ERL_SUB_BIN_SIZE;
		} else if ((bit_size + bit_offset) > 0) {
		    extra_bytes = 1;
		    sub_binary_heapneed = ERL_SUB_BIN_SIZE;
		} else {
		    extra_bytes = 0;
		    sub_binary_heapneed = 0;
		}
		
		real_size = size+extra_bytes;
                objp = binary_val(real_bin);
                if (thing_subtag(*objp) == HEAP_BINARY_SUBTAG) {
                    ErlHeapBin *from_bin;
                    ErlHeapBin *to_bin;
                    Uint i = heap_bin_size(real_size);
                    GlobalAlloc(from,i+sub_binary_heapneed,hp);
                    from_bin = (ErlHeapBin *) objp;
                    to_bin = (ErlHeapBin *) hp;
                    to_bin->thing_word = header_heap_bin(real_size);
                    to_bin->size = real_size;
                    sys_memcpy(to_bin->data, ((byte *)from_bin->data) +
                               sub_offset, real_size);
		    res_binary = make_binary(to_bin);
		    hp += i;
                } else {
                    ProcBin *from_bin;
                    ProcBin *to_bin;
                    
                    ASSERT(thing_subtag(*objp) == REFC_BINARY_SUBTAG);
		    from_bin = (ProcBin *) objp;
		    erts_refc_inc(&from_bin->val->refc, 2);
                    GlobalAlloc(from,PROC_BIN_SIZE+sub_binary_heapneed,hp);
                    to_bin = (ProcBin *) hp;
                    to_bin->thing_word = HEADER_PROC_BIN;
                    to_bin->size = real_size;
                    to_bin->val = from_bin->val;
                    to_bin->bytes = from_bin->bytes + sub_offset;
                    to_bin->next = erts_global_offheap.mso;
                    erts_global_offheap.mso = to_bin;
                    erts_global_offheap.overhead += to_bin->size / sizeof(Eterm);
		    res_binary=make_binary(to_bin);
		    hp += PROC_BIN_SIZE;
                }
		if (extra_bytes != 0) {
		    ErlSubBin* res;
		    res = (ErlSubBin *) hp;
		    res->thing_word = HEADER_SUB_BIN;
		    res->size = size;
		    res->bitsize = bit_size;
		    res->bitoffs = bit_offset;
		    res->offs = 0;
		    res->is_writable = 0;
		    res->orig = res_binary;
		    res_binary = make_binary(hp);
		}
		MA_STACK_UPDATE(dst,MA_STACK_POP(offset),res_binary);
		MA_STACK_POP(dst);
                continue;
            }

	    case BIN_MATCHSTATE_SUBTAG:
		erl_exit(ERTS_ABORT_EXIT,
			 "copy_struct_lazy: matchstate term not allowed");

            default: {
                Uint size = thing_arityval(*objp) + 1;
                Eterm *hp;
                GlobalAlloc(from,size,hp);
                /* A GC above might invalidate the value of objp */
                objp = boxed_val(obj);
                MA_STACK_UPDATE(dst,MA_STACK_POP(offset),make_boxed(hp));
                MA_STACK_POP(dst);
                while (size--) {
                    *hp++ = *objp++;
                }
                continue;
            }
            }
            continue;
        }

        case TAG_PRIMARY_HEADER:
        ASSERT((obj & _TAG_HEADER_MASK) == ARITYVAL_SUBTAG);
        {
            Eterm *objp = &obj;
            Uint ari = arityval(obj);
            Uint i;
            Eterm *hp;
            GlobalAlloc(from,ari + 1,hp);
            MA_STACK_UPDATE(dst,MA_STACK_POP(offset),make_tuple(hp));
            MA_STACK_POP(dst);
            *hp = *objp++;
            for (i = 1; i <= ari; i++) {
                switch (primary_tag(*objp)) {
                case TAG_PRIMARY_LIST:
                case TAG_PRIMARY_BOXED:
                    if (NO_COPY(*objp)) {
#ifdef INCREMENTAL
                        if (ptr_within(ptr_val(*objp),inc_fromspc,inc_fromend))
                            INC_STORE(gray,hp,ari + 1);
#endif
                        hp[i] = *objp++;
                    } else {
                        MA_STACK_PUSH(src,*objp++);
                        MA_STACK_PUSH(dst,hp);
                        MA_STACK_PUSH(offset,i);
                    }
                    break;
                default:
                    hp[i] = *objp++;
                }
            }
            continue;
        }

        default:
            erl_exit(ERTS_ABORT_EXIT,
		     "%s, line %d: Internal error in copy_struct_lazy: 0x%08x\n",
                     __FILE__, __LINE__,obj);
        }
    }

    VERBOSE(DEBUG_MESSAGES,
            ("Copy allocated @ 0x%08lx:\n%T\n",
             (unsigned long)ptr_val(dest),dest));

    ma_gc_flags &= ~GC_CYCLE_START;

    ASSERT(eq(orig, dest));
    ASSERT(ma_src_top == 0);
    ASSERT(ma_dst_top == 0);
    ASSERT(ma_offset_top == 0);
    return dest;
}
Exemple #8
0
Eterm copy_shallow(Eterm* ptr, Uint sz, Eterm** hpp, ErlOffHeap* off_heap)
#endif
{
    Eterm* tp = ptr;
    Eterm* hp = *hpp;
    const Eterm res = make_tuple(hp);
#if HALFWORD_HEAP
    const Sint offs = COMPRESS_POINTER(hp - (tp - src_base));
#else
    const Sint offs = (hp - tp) * sizeof(Eterm);
#endif

    while (sz--) {
	Eterm val = *tp++;

	switch (primary_tag(val)) {
	case TAG_PRIMARY_IMMED1:
	    *hp++ = val;
	    break;
	case TAG_PRIMARY_LIST:
	case TAG_PRIMARY_BOXED:
	    *hp++ = byte_offset_ptr(val, offs);
	    break;
	case TAG_PRIMARY_HEADER:
	    *hp++ = val;
	    switch (val & _HEADER_SUBTAG_MASK) {
	    case ARITYVAL_SUBTAG:
		break;
	    case REFC_BINARY_SUBTAG:
		{
		    ProcBin* pb = (ProcBin *) (tp-1);
		    erts_refc_inc(&pb->val->refc, 2);
		    OH_OVERHEAD(off_heap, pb->size / sizeof(Eterm));
		}
		goto off_heap_common;

	    case FUN_SUBTAG:
		{
		    ErlFunThing* funp = (ErlFunThing *) (tp-1);
		    erts_refc_inc(&funp->fe->refc, 2);
		}
		goto off_heap_common;

	    case MAP_SUBTAG:
		*hp++ = *tp++;
		sz--;
		break;
	    case EXTERNAL_PID_SUBTAG:
	    case EXTERNAL_PORT_SUBTAG:
	    case EXTERNAL_REF_SUBTAG:
		{
		    ExternalThing* etp = (ExternalThing *) (tp-1);
		    erts_refc_inc(&etp->node->refc, 2);
		}
	    off_heap_common:
		{
		    struct erl_off_heap_header* ohh = (struct erl_off_heap_header*)(hp-1);
		    int tari = thing_arityval(val);
		    
		    sz -= tari;
		    while (tari--) {
			*hp++ = *tp++;
		    }
		    ohh->next = off_heap->first;
		    off_heap->first = ohh;
		}
		break;
	    default:
		{
		    int tari = header_arity(val);
    
		    sz -= tari;
		    while (tari--) {
			*hp++ = *tp++;
		    }
		}
		break;
	    }
	    break;
	}
    }
    *hpp = hp;

    return res;
}
void Maze::depth_first_search(Mat &maze, Mat &solve, int dest_x, int dest_y)
{
	const int TOLERANCE_SIZE = 10;
	const double WALL_HEURISTIC_ALPHA = 50.0;
	// Mat walls = solve.clone();

	queue<state> q;
	priority_queue<shortest_state, vector<shortest_state>, greater<shortest_state> > q_prime;
	vector< vector<bool> > visited(maze.rows, vector<bool>(maze.cols, false));
	vector< vector<int> > wall_distance(maze.rows, vector<int>(maze.cols, -1));
	this->path = vector< vector< pair<int, int> > >(maze.rows, vector< pair<int, int> >(maze.cols, make_pair(-1, -1)));

	// Compute the wall-distance for each cell
	// Add all cells with wall
	for (int i = 0; i < maze.rows; i++)
	{
		for (int j = 0; j < maze.cols; j++) 
		{
			if (solve.at<unsigned char>(i, j) < 128)
			{
				q.push(make_tuple(j, i, 1, -1));
			}
		}
	}

	// BFS to cover all cells and put their wall-distance value
	while (!q.empty())
	{
		state current = q.front();
		q.pop();

		int x = get<0>(current);
		int y = get<1>(current);
		int current_distance = get<2>(current);

		if (x < 0 || x >= maze.cols || y < 0 || y >= maze.rows) continue;
		if (wall_distance[y][x] != -1) continue;
		
		wall_distance[y][x] = current_distance;

		q.push(make_tuple(x + 1, y, current_distance + 1, -1));
		q.push(make_tuple(x - 1, y, current_distance + 1, -1));
		q.push(make_tuple(x, y + 1, current_distance + 1, -1));
		q.push(make_tuple(x, y - 1, current_distance + 1, -1));
	}

	// ***********************
	// Compute the actual path
	// ***********************

	for (int i = 0; i < TOLERANCE_SIZE; i++) 
	{
		for (int j = 0; j < TOLERANCE_SIZE; j++)
		{
			q_prime.push(make_tuple(0, dest_x + i, dest_y + j, -1, -1));
		}
	}

	while (!q_prime.empty())
	{
		shortest_state current = q_prime.top();
		q_prime.pop();

		double cost = get<0>(current);
		int x = get<1>(current);
		int y = get<2>(current);
		int x_prev = get<3>(current);
		int y_prev = get<4>(current);

		if (x < 0 || x >= maze.cols || y < 0 || y >= maze.rows) continue;
		// if (visited[y][x]) continue;
		if (solve.at<unsigned char>(y, x) < 128 || visited[y][x]) continue;
		
		this->path[y][x] = make_pair(x_prev, y_prev);
		visited[y][x] = true;

		int dist = WALL_HEURISTIC_ALPHA / wall_distance[y][x];

		q_prime.push(make_tuple(cost + dist, x + 1, y, x, y));
		q_prime.push(make_tuple(cost + dist, x - 1, y, x, y));
		q_prime.push(make_tuple(cost + dist, x, y + 1, x, y));
		q_prime.push(make_tuple(cost + dist, x, y - 1, x, y));
	}

	// Second BFS pass through to escape from unreachable places in case it gets stuck
	q = queue<state>();

	// Add already-known places
	for (int i = 0; i < maze.rows; i++)
	{
		for (int j = 0; j < maze.cols; j++) 
		{
			if (visited[i][j])
			{
				q.push(make_tuple(j, i, -1, -1));
			}
		}
	}

	vector< vector<bool> > revisited(maze.rows, vector<bool>(maze.cols, false));

	while (!q.empty())
	{
		state current = q.front();
		q.pop();

		int x = get<0>(current);
		int y = get<1>(current);
		int x_prev = get<2>(current);
		int y_prev = get<3>(current);

		if (x < 0 || x >= maze.cols || y < 0 || y >= maze.rows) continue;
		if (revisited[y][x]) continue;
		revisited[y][x] = true;

		if (!visited[y][x])
		{
			this->path[y][x] = make_pair(x_prev, y_prev);
			visited[y][x] = true;
		}

		q.push(make_tuple(x + 1, y, x, y));
		q.push(make_tuple(x - 1, y, x, y));
		q.push(make_tuple(x, y + 1, x, y));
		q.push(make_tuple(x, y - 1, x, y));
	}
}
///////////////////////////////////////////////////////////////////////////////
//
//      Convert the image to an 8 bit image using populosity quantization.  
//  Return success of operation.
//
///////////////////////////////////////////////////////////////////////////////
bool TargaImage::Quant_Populosity()
{
	unsigned char *rgb = To_RGB_All();
	typedef tuple<int, int, int> triplet;
	int factor = 8; //256/32 levels = 8
	int i, j;
	for (i=0; i<width*height*4; i+=4) {
		for (int c=0; c<3; ++c) {
			data[i+c] = (rgb[i+c]/factor)*factor;
		}
	}
	
	//Find 256 most popular colors
	map < triplet, int > c_counts;
	triplet key;
	//Populate map with color counts use RGB to encode color channels to integer key
	for (i=0; i<width*height*4; i+=4) {
		key = make_tuple(data[i], data[i+1], data[i+2]);
		if(c_counts.find(key) == c_counts.end()) { //Not already in map
			c_counts[triplet(key)] = 1; //Initialize new color
		} else {
			c_counts[triplet(key)]++;
		}
	}

	if(c_counts.size() > 256) { //If there are fewer than 256 colors then finished

		vector < pair<triplet, int> > colors(c_counts.begin(), c_counts.end()); //Copy map key/value pairs to a vector
		sort(colors.begin(), colors.end(), cmp); //Sort the vector by the value (color count) of the pair
		vector <triplet> reduced_colors(256);

		int red, green, blue;
		for(i=0; i<256; ++i) {
			tie (red, green, blue) = colors[i].first;
			reduced_colors[i] = make_tuple (red, green, blue);
			//cout << i << " " << red << " " << green << " " << blue << endl;
		}
		
		int r0, g0, b0, r1, g1, b1;
		double min_d, d;
		triplet closest;
		map <triplet, triplet> q_map;

		for (i = 0; i < width*height*4; i+=4) { //for each pixel
			key = make_tuple(data[i], data[i+1], data[i+2]);
			tie (r0, g0, b0) = key;
			if( q_map.find(key) == q_map.end() ) { //Search for correct target color

				//Not in map, find closest of the 256
				min_d = INT_MAX; //Reset minimum distance for each source color
				for (j = 0; j < 256; ++j) { //for each reduced color
					tie (r1, g1, b1) = reduced_colors[j]; //unpack the tuple
					d = sqrt( (double) (r1-r0)*(r1-r0) + (g1-g0)*(g1-g0) + (b1-b0)*(b1-b0) ); //Calculate euclidean distance
					if(d < min_d) {
						min_d = d;
						closest = reduced_colors[j];
					}
				}				
				q_map[triplet(key)] = closest; //Store closest color to the quantization map
			}
			tie (data[i], data[i+1], data[i+2]) = q_map[triplet(key)]; 	//Overwrite the pixel color with the quantized color
		}//for each pixel
		

	}//If fewer than 256


	delete rgb;
    return true;
}// Quant_Populosity
Exemple #11
0
/**
 * Put out requests to have FFT operations done, and measure how
 * long it takes to get the results back.
 */
int
main(int argc, char *argv[])
{
	struct tuple *s, *t, *u;
	int i, j, iters = 0;
	int r1[PARALLEL], r2[PARALLEL], r3[PARALLEL];
	double x[N], y[N], mult;
	struct timeval T0, T1;
	int rndsock;
	struct context ctx;

	if (get_server_portnumber(&ctx)) {
		if (argc < 3) {
			/* help message */
			fprintf(stderr,
				"Usage: %s <server> <portnumber>\n", argv[0]);
			exit(1);
		}
		strcpy(ctx.peername, argv[1]);
		ctx.portnumber = atoi(argv[2]);
	}

	rndsock = open("/dev/urandom", O_RDONLY);
	mult = 1.0 / pow(2.0, 31);
	for (i = 0; i < N; i++) {
		x[i] = mult * random_int();
		y[i] = mult * random_int();
	}

	s = make_tuple("siiis#s#", "fft",
		       0, 0, 0, x, N * sizeof(double), y, N * sizeof(double));

	t = make_tuple("siii??", "fft done", 0, 0, 0);

	gettimeofday(&T0, NULL);

	while (1) {

		for (j = 0; j < PARALLEL; j++) {
			r1[j] = random_int();
			r2[j] = random_int();
			r3[j] = random_int();
			s->elements[1].data.i = r1[j];
			s->elements[2].data.i = r2[j];
			s->elements[3].data.i = r3[j];
			if (put_tuple(s, &ctx)) {
				perror("put_tuple failed");
				exit(1);
			}
		}

		for (j = 0; j < PARALLEL; j++) {
			t->elements[1].data.i = r1[j];
			t->elements[2].data.i = r2[j];
			t->elements[3].data.i = r3[j];
			u = get_tuple(t, &ctx);
			if (u == NULL) {
				perror("get_tuple failed");
				exit(1);
			}
		}

		gettimeofday(&T1, NULL);
		iters += PARALLEL;
		printf("%f\n", TIMEVAL_DIFF(T0, T1) / iters);
	}

	close(rndsock);
	destroy_tuple(s);
	destroy_tuple(t);
	destroy_tuple(u);

	return 0;
}
///////////////////////////////////////////////////////////////////////////////
//
//      Run simplified version of Hertzmann's painterly image filter.
//      You probably will want to use the Draw_Stroke funciton and the
//      Stroke class to help.
//      Return success of operation.
//
///////////////////////////////////////////////////////////////////////////////
bool TargaImage::NPR_Paint()
{
	TargaImage ref_image = TargaImage(*this);
	TargaImage canvas = TargaImage(width, height); //Constant color that will get painted on
	
	int x, y, i, j, g, m;
	int r0, g0, b0, r1, g1, b1;
	typedef tuple<int, int, double> error_point;

	double d, area_error;
	int T = 25; //Error threshold
	static const unsigned int arr[] = {7, 3, 1}; //Brush sizes
	vector<int> rs (arr, arr + sizeof(arr) / sizeof(arr[0]) );

	int rad;
	//*****
	//Paint
	//*****
	for(size_t b=0; b < rs.size(); b++) { //For each brush size
		ref_image.Filter_Gaussian_N(2*rs[b]+1); //referenceImage
		g = 2*rs[b]+1; //Could also be 2*rs[b]+1
		rad = 3*rs[b];
		//***Paint a layer***
		vector <Stroke> strokes;
		//Find all of the grid block centers
		vector < tuple<int, int> > centers;
		centers.reserve( (width/g + 1)* (height/g + 1) );
		for( x = g/2; x < g*(width/g); x+=g ) {
			for( y = g/2; y < g*(height/g); y+=g ) {
				centers.push_back( make_tuple(x, y) );
			}
		}
		//Construct overlapping blocks around right and bottom edge, Possibly Unnecessary
		if (width % g) { //Hold x constant equal to width-g/2-1, loop down the column increasing y			
			x = width - g/2 - 1;
			for( y = g/2; y < g*(height/g); y+=g ) {
				centers.push_back( make_tuple(x, y) );
			}
		}
		if (height % g) {
			y = height - g/2 - 1; //Hold y constant equal to height-g/2-1, loop across the row increasing x
			for( x = g/2; x < g*(width/g); x+=g ) {
				centers.push_back( make_tuple(x, y) );
			}
		}
		//if both x_extra and y_extra then need final bottom right corner block, probably unnecessary		
		

		for( size_t c=0; c < centers.size(); c++ ) { //For each grid center
			tie (x, y) = centers[c];
				//Find the error in the area/region/block
				vector < error_point > errors;
				errors.reserve(g*g);
				area_error = 0;
				for (j = -g/2; j<=g/2; ++j) {
					for (i = -g/2; i<=g/2; ++i) {
						m = (width*(y+j) + (x+i))*4; //Location of current pixel
						tie (r0, g0, b0) = make_tuple(ref_image.data[m], ref_image.data[m+1], ref_image.data[m+2]);
						tie (r1, g1, b1) = make_tuple(canvas.data[m], canvas.data[m+1], canvas.data[m+2]);
						//Calculate euclidean distance
						d = sqrt( (double) (r1-r0)*(r1-r0) + (g1-g0)*(g1-g0) + (b1-b0)*(b1-b0) ); 
						//Save the coordinate and its error
						errors.push_back(error_point(x+i, y+j, d));
						area_error += d;
					}//i
				}//j
				if( area_error > T ) {
					//Find largest error point
					sort(errors.begin(), errors.end(), cmp_errors);					
					tie (i, j, d) = errors.front();
					//Build a Stroke at loacation x,y with radius and value of r,g,b,alpha
					m = (width*j + i)*4; //Location of current pixel
					Stroke s = Stroke(rad, i, j, ref_image.data[m], ref_image.data[m+1], ref_image.data[m+2], ref_image.data[m+3]);
					strokes.push_back(s);
				}

		}//c

		//*******
		//Paint all strokes in random order onto canvas
		//*******
		random_shuffle ( strokes.begin(), strokes.end() );
		vector <Stroke>::iterator it;
		for (it = strokes.begin(); it != strokes.end(); it++) {
			canvas.Paint_Stroke(*it);
		}

		//Reset the reference image to the unchanged image data for the next smaller pass
		memcpy(ref_image.data, data, sizeof(unsigned char) * width * height * 4); 
	}//b	

	//Write the canvas data to the displayed image
	memcpy(data, canvas.data, sizeof(unsigned char) * width * height * 4);
	//Overwrite alpha values
	for(i=3; i<width*height*4; i+=4) {
		data[i] = 255;
	}

    return true;
}
////////////////////////////////////////////////////////////////////////////////
// AccessPoint::connect                                                       //
//                                                                            //
// creates connection to another terminal                                     //
////////////////////////////////////////////////////////////////////////////////
void AccessPoint::connect(Terminal* t, adapt_struct ad, traffic_struct ts, accCat AC) {

  Traffic* tr = new Traffic(ptr2sch, randgen, mylog, this, t, ts);
  connection[t] = make_tuple(link_adapt(this,t,ad, mylog), tr, AC);
}
bool CICreateScoreForMove(SCIInputOption& Option, CPatGrpWrapper& PatGrp,
							uchar * pBoardConsiderd, string& Sample,
							int xSampleZero, int ySampleZero,
							int depth, float ScoreAbove)
{
	float Score = 0.0f;
	string sGrpSeed = PatGrp.getGrpParam(0);
	size_t ScorePos = sGrpSeed.find('*');
	if (ScorePos == string::npos) {
		cerr << "Error: Badly formed CI dep found with no score: " << sGrpSeed << endl;
		return false;
	}
	Score = (float)stoi(sGrpSeed.substr(ScorePos + 1));
	bool bWhiteTurnFromSample = Sample[cTurnPosInSample] == 'w';
	bool bWhiteTurnFromDep = sGrpSeed[cTurnPosInDep] == 'w';
	if (bWhiteTurnFromDep != bWhiteTurnFromSample) {
		cerr << "Error: Turn choice in dep does not match sample chosen";
		return false;
	}
	vector<tuple<int, int, int, int> > Deltas;
	bool bNextTurnWhite = (sGrpSeed[cNextTurnInDep] == 'w');
	string sDeltas = sGrpSeed.substr(cNextTurnInDep + 1, ScorePos - cNextTurnInDep - 1);
	int iFrom = -1;
	int iTo = -1; 
	//float ScoreChart[] = { 0.0f, 1.0f, 2.0f, -1.0f, -2.0f };
	
	for (int iDelta = 0; sDeltas.length() >= 6; iDelta++) { // 6 for +/-|digit|+/-|digit|prevchar|nowchar
		string sOneDelta = sDeltas.substr(0, 6);
		sDeltas = sDeltas.substr(6);
		int x = stoi(sOneDelta.substr(0, 2)) + xSampleZero;
		int y = stoi(sOneDelta.substr(2, 2)) + ySampleZero;
		int PrevChar = sOneDelta[4] - 'a';
		int NewChar = sOneDelta[5] - 'a';
		if ((PrevChar == 1 || PrevChar == 3) && bWhiteTurnFromDep) {
			iFrom = iDelta;
		}
		else if ((PrevChar == 2 || PrevChar == 4) && !bWhiteTurnFromDep) {
			iFrom = iDelta;
		}
		else if ((NewChar == 1 || NewChar == 3) && bWhiteTurnFromDep) {
			iTo = iDelta;
		}
		else if ((NewChar == 2 || NewChar == 4) && !bWhiteTurnFromDep) {
			iTo = iDelta;
		}
		//Score -= ScoreChart[PrevChar];
		//Score += ScoreChart[NewChar];
		Deltas.push_back(make_tuple(x, y, PrevChar, NewChar));
		
	}
	ScoreAbove += Score;
	depth--;
	if (depth > 0) {
		Score = CIEvalResultOfMove(Deltas, pBoardConsiderd, bNextTurnWhite, depth, ScoreAbove);
	}
	else {
		Score = ScoreAbove; 
	}
	if (iFrom == -1 || iTo == -1) {
		cerr << "Error: dep string seems invalid: " << sGrpSeed << endl;
		return false;
	}
	Option.MoveData = get<1>(Deltas[iTo]) | ((get<0>(Deltas[iTo])) << cInputShift)
					| ((get<1>(Deltas[iFrom])) << (2 * cInputShift)) 
					| ((get<0>(Deltas[iFrom])) << (3 * cInputShift))
					| 0x1 << (4 * cInputShift);
	//Option.Score = (bWhiteTurnFromDep ? Score : -Score);
	Option.Score = Score;

	return true;

}
Exemple #15
0
					Dwarf_Attribute attr = (*lastCreatedDie)->_attribute;
					while (NULL != attr->_nextAttr) {
						attr = attr->_nextAttr;
					}
					attr->_nextAttr = newAttr;
				}
			} else {
				delete newAttr;
			}
		}
	}
	return ret;
}

static const tuple<const char *, Dwarf_Half, Dwarf_Half> attrStrings[] = {
	make_tuple("byte_size", DW_AT_byte_size, DW_FORM_udata),
	make_tuple("bit_size", DW_AT_bit_size, DW_FORM_udata),
	make_tuple("comp_dir", DW_AT_comp_dir, DW_FORM_string),
	make_tuple("const_value", DW_AT_const_value, DW_FORM_sdata),
	make_tuple("data_member_location", DW_AT_data_member_location, DW_FORM_udata),
	make_tuple("decl_file", DW_AT_decl_file, DW_FORM_udata),
	make_tuple("decl_line", DW_AT_decl_line, DW_FORM_udata),
	make_tuple("external", DW_AT_external, DW_FORM_flag),
	make_tuple("linkage_name", DW_AT_linkage_name, DW_FORM_string),
	make_tuple("name", DW_AT_name, DW_FORM_string),
	make_tuple("specification", DW_AT_specification, DW_FORM_ref1),
	make_tuple("type", DW_AT_type, DW_FORM_ref1),
	make_tuple("upper_bound", DW_AT_upper_bound, DW_FORM_udata),
};

static void
Exemple #16
0
vector<tuple<int, int, int>> VariantCaller::report_variants(const vector<int>& depths, const string& vcf_file) {
  vector<tuple<int, int, int>> positions;

  //TODO: deal with possible exception
  ofstream vcf_stream(vcf_file, ios::out | ios::app);

  for (int i = 0; i < (int)piles.size(); i++) {

    // No variants to consider
    if (!piles[i])
      continue;

    const map<string, alt_counts>& pile = *(piles[i].get());
    string ref = full_ref.substr(i + start_position, 1);

    vector<string> final_alts;
    vector<string> final_refs;

    if (depths[i] >= depth_min) {
      int var_depth_min = depths[i] * var_depth_frac_min;
      int max_altlen = 0;
      int max_reflen = 0;
      string ref_del;

      for (auto& alt : pile) {
        // TODO: check if this is necessary
        if (alt.first.size() == 0)
          continue;

        int count = alt.second.nf + alt.second.nr;

        if (min((double)alt.second.nf / count, (double)alt.second.nr / count) > strand_bias_min && 
            (count > max(var_depth_min, var_depth_hard_min)) &&
            (alt.first.compare(ref) != 0)) {

          // Look for a deletion
          if (alt.first.substr(0,1).compare("-") == 0) {
            if (i + alt.first.size() + 1 < piles.size()) {
              final_alts.push_back(ref);
              string ref_str = full_ref.substr(start_position + i, alt.first.size() + 1);
              final_refs.push_back(ref_str);
              max_reflen = max(max_reflen, (int)alt.first.size() + 1);
              max_altlen = max(max_altlen, 1);
              vcf_stream << contig << '\t' << i + start_position + 1 << "\t.\t" 
                << ref_str << '\t' << ref << "\t20\tPASS\t.\tGT\t1/1" << endl;
            }
          } else {
            final_alts.push_back(alt.first);
            final_refs.push_back(ref);
            max_reflen = max(max_reflen, 1);
            max_altlen = max(max_altlen, (int)alt.first.size());
            vcf_stream << contig << '\t' << i + start_position + 1 << "\t.\t" 
              << ref << '\t' << alt.first << "\t20\tPASS\t.\tGT\t1/1" << endl;
          }
        }
      }

      if (final_alts.size() == 0)
        continue;

      // Send call back to cagar where there is only one variant
      if (final_alts.size() == 1)
        positions.push_back(make_tuple(i + start_position, max_reflen, max_altlen));
    }
  }
  vcf_stream.close();
  return positions;
}
Exemple #17
0
int main(int argc, char* argv[])
{
    if (!ltfat_int_is_compatible(sizeof(int)))
    {
        std::cout << "Incompatible size of int. libltfat was probably"
                     " compiled with -DLTFAT_LARGEARRAYS" << std::endl;
        exit(1);
    }

    string inFile, outFile, resFile;
    double targetsnrdb = 40;
    double atprodreltoldb = -80;
    size_t maxit = 0, maxat = 0;
    double seglen = 0.0;
    double kernthr = 1e-4;
    vector<tuple<string,int,int,int,int>> dicts;
    size_t numSamples = 0;
    int numChannels = 0;
    int sampRate = 0;
    bool do_pedanticsearch = false;
    bool do_verbose = false;
    int alg = ltfat_dgtmp_alg_mp;

    try
    {
        string examplestr{"Usage:\n" + 
            string(argv[0]) + " -d win,a,M input.wav"
            + "\nExample:\n" +
            string(argv[0]) + " -d HANN,512,2048 input.wav"
        };
        cxxopts::Options options(argv[0], "\nMatching Pursuit Decomposition with Multi-Gabor dictionaties");
        options
        .positional_help("-s snr -d win,a,M[:win2,a2,M2:...] input.wav"
                         "\n\nPerforms matching pursuit decomposition of input.wav using"
                         " given (multi-)Gabor dictionary such that the target SNR is"
                         " snr.")
        .show_positional_help();

        options.add_options()
        ("i,input", "Input *.wav filei (REQUIRED)", cxxopts::value<string>())
        ("o,output","Output *.wav file", cxxopts::value<string>())
        ("r,residual","Residual *.wav file", cxxopts::value<string>() )
        ("d,dict","Dictionary specification (REQUIRED). Format: win1,hop1,channels1:win2,hop2,channels2. "
                  "Example: blackman,512,2048:blackman,256,1024:... Supported windows are: blackman, hann",
                  cxxopts::value<string>() )
        ("s,snr", "Target signal-to-noise ratio in dB",
         cxxopts::value<double>()->default_value(to_string(targetsnrdb)))
        ("atprodtol", "Relative inner product tolerance in dB",
         cxxopts::value<double>()->default_value(to_string(atprodreltoldb)))
        ("maxit", "Maximum number of iterations", cxxopts::value<size_t>() )
        ("maxat", "Maximum number of atoms", cxxopts::value<size_t>() )
        ("alg", "MP algorithm. Available: mp(default),cyclicmp,selfprojmp", cxxopts::value<string>() )
        ("kernthr", "Kernel truncation threshold",
         cxxopts::value<double>()->default_value(to_string(kernthr)))
        ("seglen", "Segment length in seconds. 0 disables the segmentation.",
         cxxopts::value<double>()->default_value(to_string(seglen)) )
        ("pedanticsearch", "Enables pedantic search. Pedantic search is always enabled for cyclic MP.",
         cxxopts::value<bool>(do_pedanticsearch) )
        ("verbose", "Print additional information.",
         cxxopts::value<bool>(do_verbose) )
        ("help", "Print help");

        options.parse_positional({"input"});

        auto result = options.parse(argc, argv);

        if (result.count("help"))
        {
            cout << options.help({""}) << endl;
            exit(0);
        }

        if (result.count("input"))
        {
            inFile = result["input"].as<string>();
            try
            {
                WavReader<LTFAT_REAL> wrtmp{inFile};
                numSamples = wrtmp.getNumSamples();
                numChannels = wrtmp.getNumChannels();
                sampRate = wrtmp.getSampleRate();
                if( numSamples == 0)
                {
                    cout << "Empty input wav file" << endl;
                    exit(1);
                }
            }
            catch(...)
            {
                cout << "Cannot open " << inFile << endl;
                exit(1);
            }
        }
        else
        {
            cout << "No input file specified." << endl;
            cout << examplestr << endl;
            exit(1);
        }

        if (result.count("output"))
            outFile = result["output"].as<string>();

        if (result.count("residual"))
            resFile = result["residual"].as<string>();

        if(result.count("seglen"))
        {
            seglen =  result["seglen"].as<double>();
            if(seglen < 0.0)
            {
                cout << "seglen must be greater than 0." << endl;
                exit(1);
            }
        }

        if (result.count("atprodtol"))
        {
            atprodreltoldb = result["atprodtol"].as<double>();
            if(atprodreltoldb > 0)
            {
                cout << "The relative inner product tolernace must be less than 0 dB." << endl;
                exit(1);
            }
        }

        if (result.count("snr"))
        {
            targetsnrdb = result["snr"].as<double>();
            if(targetsnrdb < 0)
            {
                cout << "Target SNR must be greater than 0 dB." << endl;
                exit(1);
            }
        }

        if (result.count("alg"))
        {
            string algstr = result["alg"].as<string>();
            if( algstr.compare("mp") == 0 ) alg = ltfat_dgtmp_alg_mp;
            else if( algstr.compare("cyclicmp") == 0 ) alg = ltfat_dgtmp_alg_loccyclicmp;
            else if( algstr.compare("selfprojmp") == 0 ) alg = ltfat_dgtmp_alg_locselfprojmp;
            else
            {
                cout << "Unrecognized algorithm." << endl;
                exit(1);
            }
        }

        if (result.count("dict"))
        {
             string toparse = result["dict"].as<string>() + ":";

             int pos;
             while ((pos = toparse.find(":")) != -1)
             {
                 string dictstr = toparse.substr(0,pos);
                 toparse = toparse.substr(pos+1,toparse.size()-pos);
                 if( !dictstr.empty() )
                 {
                    dictstr += ",";
                    vector<string> dictvec;
                    int pos2;
                    while ((pos2 = dictstr.find(",")) != -1)
                    {
                        string itemstr = dictstr.substr(0,pos2);
                        dictstr = dictstr.substr(pos2+1,dictstr.size()-pos2);
                        if( !itemstr.empty())
                            dictvec.push_back(itemstr);
                    }
                    if(dictvec.size() != 3 && dictvec.size() != 4)
                    {
                        cout << "Parse error: Dictionary should consist of 3 or 4 items: win,a,M or win,a,M,gl" << endl;
                        exit(1);
                    }
                    transform(dictvec[0].begin(), dictvec[0].end(), dictvec[0].begin(), ::tolower);
                    int winenum = -1;
                    if( 0 > (winenum = ltfat_str2firwin(dictvec[0].c_str())) )
                    {
                       cout << "Parse error: Window " << dictvec[0]
                            << " not recognized." << endl;
                       exit(1);
                    }
                    if(dictvec.size() == 3)
                        dicts.push_back(make_tuple(dictvec[0],winenum,stoi(dictvec[1]),stoi(dictvec[2]),stoi(dictvec[2])));
                    else if(dictvec.size() == 4)
                        dicts.push_back(make_tuple(dictvec[0],winenum,stoi(dictvec[1]),stoi(dictvec[2]),stoi(dictvec[3])));
                 }
             }
        }
        else
        {
            cout << "No dictionary specified." << endl;
            cout << examplestr << endl;
            exit(1);
        }

        if (!result.count("maxat"))
            maxat = (size_t) (numSamples);
        else
            maxat = result["maxat"].as<size_t>();

        if (!result.count("maxit"))
            maxit = (size_t) numSamples;
        else
            maxit =  result["maxit"].as<size_t>();

        if(maxat == 0) maxat = maxit;
        if(maxit == 0) maxit = 2*maxat;

        if (result.count("kernthr"))
        {
            kernthr = result["kernthr"].as<double>();
        }
    }
    catch (const cxxopts::OptionException& e)
    {
        std::cout << "error parsing options: " << e.what() << std::endl;
        exit(1);
    }


    LTFAT_NAME(dgtrealmp_parbuf)* pbuf = NULL;
    LTFAT_NAME(dgtrealmp_parbuf_init)(&pbuf);
    auto unipb = uni_ptrdel<LTFAT_NAME(dgtrealmp_parbuf)>(
    pbuf,[](auto* p){ LTFAT_NAME(dgtrealmp_parbuf_done)(&p); });

    // LTFAT_NAME(dgtrealmp_setparbuf_alg)(pbuf, ltfat_dgtmp_alg_LocOMP);

    for(auto dict:dicts)
    {
       if( 0 > LTFAT_NAME(dgtrealmp_parbuf_add_firwin)(pbuf, (LTFAT_FIRWIN)(get<1>(dict)), get<4>(dict), get<2>(dict), get<3>(dict)))
       {
          cout << "Bad dictionary: " << get<0>(dict) << "," << get<2>(dict)  << "," << get<3>(dict) << endl;
          exit(1);
       }
    }

    if( seglen == 0.0 || numSamples <= seglen*sampRate )
    {
        ltfat_int L = LTFAT_NAME(dgtrealmp_getparbuf_siglen)(pbuf, numSamples);

        LTFAT_NAME(dgtrealmp_setparbuf_phaseconv)(pbuf, LTFAT_TIMEINV);
        LTFAT_NAME(dgtrealmp_setparbuf_pedanticsearch)(pbuf, do_pedanticsearch);
        LTFAT_NAME(dgtrealmp_setparbuf_atprodreltoldb)(pbuf, atprodreltoldb);
        LTFAT_NAME(dgtrealmp_setparbuf_snrdb)(pbuf, targetsnrdb);
        LTFAT_NAME(dgtrealmp_setparbuf_kernrelthr)(pbuf, kernthr);
        LTFAT_NAME(dgtrealmp_setparbuf_maxatoms)(pbuf, maxat);
        LTFAT_NAME(dgtrealmp_setparbuf_maxit)(pbuf, maxit);
        LTFAT_NAME(dgtrealmp_setparbuf_iterstep)(pbuf, L);
        LTFAT_NAME(dgtrealmp_setparbuf_alg)(pbuf, static_cast<ltfat_dgtmp_alg>(alg));

        vector<vector<LTFAT_REAL>> f(numChannels);
        for(auto& fEl:f) fEl = vector<LTFAT_REAL>(L,0.0);

        vector<vector<LTFAT_REAL>> fout(numChannels);
        for(auto& fEl:fout) fEl = vector<LTFAT_REAL>(L);

        vector<unique_ptr<LTFAT_COMPLEX[]>> coef;

        for (int pidx = 0; pidx < LTFAT_NAME(dgtrealmp_getparbuf_dictno)(pbuf); pidx++ )
        {
            ltfat_int clen = LTFAT_NAME(dgtrealmp_getparbuf_coeflen)(pbuf, numSamples, pidx);
            coef.push_back( unique_ptr<LTFAT_COMPLEX[]>(new LTFAT_COMPLEX[clen]) );
        }

        {
            WavReader<LTFAT_REAL> wr{inFile};
            wr.readSamples(f);
        }

        LTFAT_NAME(dgtrealmp_state)*  plan = NULL;
        auto t1 = Clock::now();
        if( 0 != LTFAT_NAME(dgtrealmp_init)( pbuf, L, &plan)) return -1;
        auto t2 = Clock::now();
        int dur = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
        cout << "INIT DURATION: " << dur << " ms" << std::endl;
        auto uniplan = uni_ptrdel<LTFAT_NAME(dgtrealmp_state)>(
        plan,[](auto* p){ LTFAT_NAME(dgtrealmp_done)(&p); });

        for (int nCh=0;nCh<numChannels;nCh++)
        {
            t1 = Clock::now();
            int status = LTFAT_NAME(dgtrealmp_execute_decompose)(plan, f[nCh].data(), (LTFAT_COMPLEX**) coef.data());
            t2 = Clock::now();
            dur = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
            cout << "DURATION: " << dur << " ms" << std::endl;

            t1 = Clock::now();
            LTFAT_NAME(dgtrealmp_execute_synthesize)(plan, (const LTFAT_COMPLEX**) coef.data(), NULL, fout[nCh].data());
            t2 = Clock::now();
            int dur2 = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
            cout << "SYN DURATION: " << dur2 << " ms" << std::endl;

            size_t atoms; LTFAT_NAME(dgtrealmp_get_numatoms)(plan, &atoms);
            size_t iters; LTFAT_NAME(dgtrealmp_get_numiters)(plan, &iters);
            LTFAT_REAL snr; LTFAT_NAME(snr)(f[nCh].data(), fout[nCh].data(), L, &snr);

            cout << "atoms=" << atoms << ", iters=" << iters << ", SNR=" << snr << " dB"
                 << ", perit=" << 1000.0 * dur / ((double)iters) << "us, exit code=" << status <<endl;

        }

        if(!outFile.empty())
        {
            WavWriter<LTFAT_REAL> ww{outFile,sampRate,(int)fout.size()};
            ww.writeSamples(fout);
        }

        if(!resFile.empty())
        {
            for(size_t nCh=0;nCh<fout.size();nCh++)
                for(size_t l=0;l<fout[nCh].size();l++)
                    fout[nCh][l] =  f[nCh][l] - fout[nCh][l];

            WavWriter<LTFAT_REAL> ww{resFile,sampRate,(int)fout.size()};
            ww.writeSamples(fout);
        }
    }
    else
    {
        cout << "Segment-wise processing is not supported yet." << endl;
    }
    return 0;
}
 tuple<int *, char ***> CommandLineArgs::getProgramArgs()
 {
     //if(_argc == nullptr || _argv == nullptr)
     //throw std::runtime_error("CommandLineArgs: cannot access program arguments, they wasn't set.");
     return make_tuple(_argc, _argv);
 }
Exemple #19
0
/*
 *  Copy a structure to a heap.
 */
Eterm
copy_struct(Eterm obj, Uint sz, Eterm** hpp, ErlOffHeap* off_heap)
{
    char* hstart;
    Uint hsize;
    Eterm* htop;
    Eterm* hbot;
    Eterm* hp;
    Eterm* objp;
    Eterm* tp;
    Eterm  res;
    Eterm  elem;
    Eterm* tailp;
    Eterm* argp;
    Eterm* const_tuple;
    Eterm hdr;
    int i;
#ifdef DEBUG
    Eterm org_obj = obj;
    Uint org_sz = sz;
#endif

    if (IS_CONST(obj))
	return obj;

    hp = htop = *hpp;
    hbot   = htop + sz;
    hstart = (char *)htop;
    hsize = (char*) hbot - hstart;
    const_tuple = 0;

    /* Copy the object onto the heap */
    switch (primary_tag(obj)) {
    case TAG_PRIMARY_LIST: argp = &res; goto L_copy_list;
    case TAG_PRIMARY_BOXED: argp = &res; goto L_copy_boxed;
    default:
	erl_exit(ERTS_ABORT_EXIT,
		 "%s, line %d: Internal error in copy_struct: 0x%08x\n",
		 __FILE__, __LINE__,obj);
    }

 L_copy:
    while (hp != htop) {
	obj = *hp;

	switch (primary_tag(obj)) {
	case TAG_PRIMARY_IMMED1:
	    hp++;
	    break;
	case TAG_PRIMARY_LIST:
	    objp = list_val(obj);
	    if (in_area(objp,hstart,hsize)) {
		hp++;
		break;
	    }
	    argp = hp++;
	    /* Fall through */

	L_copy_list:
	    tailp = argp;
	    while (is_list(obj)) {
		objp = list_val(obj);
		tp = tailp;
		elem = *objp;
		if (IS_CONST(elem)) {
		    *(hbot-2) = elem;
		    tailp = hbot-1;
		    hbot -= 2;
		}
		else {
		    *htop = elem;
		    tailp = htop+1;
		    htop += 2;
		}
		*tp = make_list(tailp - 1);
		obj = *(objp+1);
	    }
	    switch (primary_tag(obj)) {
	    case TAG_PRIMARY_IMMED1: *tailp = obj; goto L_copy;
	    case TAG_PRIMARY_BOXED: argp = tailp; goto L_copy_boxed;
	    default:
		erl_exit(ERTS_ABORT_EXIT,
			 "%s, line %d: Internal error in copy_struct: 0x%08x\n",
			 __FILE__, __LINE__,obj);
	    }
	    
	case TAG_PRIMARY_BOXED:
	    if (in_area(boxed_val(obj),hstart,hsize)) {
		hp++;
		break;
	    }
	    argp = hp++;

	L_copy_boxed:
	    objp = boxed_val(obj);
	    hdr = *objp;
	    switch (hdr & _TAG_HEADER_MASK) {
	    case ARITYVAL_SUBTAG:
		{
		    int const_flag = 1; /* assume constant tuple */
		    i = arityval(hdr);
		    *argp = make_tuple(htop);
		    tp = htop;	/* tp is pointer to new arity value */
		    *htop++ = *objp++; /* copy arity value */
		    while (i--) {
			elem = *objp++;
			if (!IS_CONST(elem)) {
			    const_flag = 0;
			}
			*htop++ = elem;
		    }
		    if (const_flag) {
			const_tuple = tp; /* this is the latest const_tuple */
		    }
		}
		break;
	    case REFC_BINARY_SUBTAG:
		{
		    ProcBin* pb;

		    pb = (ProcBin *) objp;
		    if (pb->flags) {
			erts_emasculate_writable_binary(pb);
		    }
		    i = thing_arityval(*objp) + 1;
		    hbot -= i;
		    tp = hbot;
		    while (i--)  {
			*tp++ = *objp++;
		    }
		    *argp = make_binary(hbot);
		    pb = (ProcBin*) hbot;
		    erts_refc_inc(&pb->val->refc, 2);
		    pb->next = off_heap->mso;
		    pb->flags = 0;
		    off_heap->mso = pb;
		    off_heap->overhead += pb->size / sizeof(Eterm);
		}
		break;
	    case SUB_BINARY_SUBTAG:
		{
		    ErlSubBin* sb = (ErlSubBin *) objp;
		    Eterm real_bin = sb->orig;
		    Uint bit_offset = sb->bitoffs;
		    Uint bit_size = sb -> bitsize;
		    Uint offset = sb->offs;
		    size_t size = sb->size;
		    Uint extra_bytes;
		    Uint real_size;
		    if ((bit_size + bit_offset) > 8) {
			extra_bytes = 2;
		    } else if ((bit_size + bit_offset) > 0) {
			extra_bytes = 1;
		    } else {
			extra_bytes = 0;
		    } 
		    real_size = size+extra_bytes;
		    objp = binary_val(real_bin);
		    if (thing_subtag(*objp) == HEAP_BINARY_SUBTAG) {
			ErlHeapBin* from = (ErlHeapBin *) objp;
			ErlHeapBin* to;
			i = heap_bin_size(real_size);
			hbot -= i;
			to = (ErlHeapBin *) hbot;
			to->thing_word = header_heap_bin(real_size);
			to->size = real_size;
			sys_memcpy(to->data, ((byte *)from->data)+offset, real_size);
		    } else {
			ProcBin* from = (ProcBin *) objp;
			ProcBin* to;
			
			ASSERT(thing_subtag(*objp) == REFC_BINARY_SUBTAG);
			if (from->flags) {
			    erts_emasculate_writable_binary(from);
			}
			hbot -= PROC_BIN_SIZE;
			to = (ProcBin *) hbot;
			to->thing_word = HEADER_PROC_BIN;
			to->size = real_size;
			to->val = from->val;
			erts_refc_inc(&to->val->refc, 2);
			to->bytes = from->bytes + offset;
			to->next = off_heap->mso;
			to->flags = 0;
			off_heap->mso = to;
			off_heap->overhead += to->size / sizeof(Eterm);
		    }
		    *argp = make_binary(hbot);
		    if (extra_bytes != 0) {
			ErlSubBin* res;
			hbot -= ERL_SUB_BIN_SIZE;
			res = (ErlSubBin *) hbot;
			res->thing_word = HEADER_SUB_BIN;
			res->size = size;
			res->bitsize = bit_size;
			res->bitoffs = bit_offset;
			res->offs = 0;
			res->is_writable = 0;
			res->orig = *argp;
			*argp = make_binary(hbot);
		    }
		    break;
		}
		break;
	    case FUN_SUBTAG:
		{
		    ErlFunThing* funp = (ErlFunThing *) objp;

		    i =  thing_arityval(hdr) + 2 + funp->num_free;
		    tp = htop;
		    while (i--)  {
			*htop++ = *objp++;
		    }
#ifndef HYBRID /* FIND ME! */
		    funp = (ErlFunThing *) tp;
		    funp->next = off_heap->funs;
		    off_heap->funs = funp;
		    erts_refc_inc(&funp->fe->refc, 2);
#endif
		    *argp = make_fun(tp);
		}
		break;
	    case EXTERNAL_PID_SUBTAG:
	    case EXTERNAL_PORT_SUBTAG:
	    case EXTERNAL_REF_SUBTAG:
		{
		  ExternalThing *etp = (ExternalThing *) htop;

		  i =  thing_arityval(hdr) + 1;
		  tp = htop;

		  while (i--)  {
		    *htop++ = *objp++;
		  }

		  etp->next = off_heap->externals;
		  off_heap->externals = etp;
		  erts_refc_inc(&etp->node->refc, 2);

		  *argp = make_external(tp);
		}
		break;
	    case BIN_MATCHSTATE_SUBTAG:
		erl_exit(ERTS_ABORT_EXIT,
			 "copy_struct: matchstate term not allowed");
	    default:
		i = thing_arityval(hdr)+1;
		hbot -= i;
		tp = hbot;
		*argp = make_boxed(hbot);
		while (i--) {
		    *tp++ = *objp++;
		}
	    }
	    break;
	case TAG_PRIMARY_HEADER:
	    if (header_is_thing(obj) || hp == const_tuple) {
		hp += header_arity(obj) + 1;
	    } else {
		hp++;
	    }
	    break;
	}
    }

#ifdef DEBUG
    if (htop != hbot)
	erl_exit(ERTS_ABORT_EXIT,
		 "Internal error in copy_struct() when copying %T:"
		 " htop=%p != hbot=%p (sz=%bpu)\n",
		 org_obj, htop, hbot, org_sz); 
#else
    if (htop > hbot) {
	erl_exit(ERTS_ABORT_EXIT,
		 "Internal error in copy_struct(): htop, hbot overrun\n");
    }
#endif
    *hpp = (Eterm *) (hstart+hsize);
    return res;
}
Status ATCConfigParserPlugin::update(const std::string& source,
                                     const ParserConfig& config) {
  auto cv = config.find(kParserKey);
  if (cv == config.end()) {
    return Status(1, "No configuration for ATC (Auto Table Construction)");
  }
  auto obj = data_.getObject();

  data_.copyFrom(cv->second.doc(), obj);
  data_.add(kParserKey, obj);

  const auto& ac_tables = data_.doc()[kParserKey];
  auto tables = RegistryFactory::get().registry("table");
  auto registered = registeredATCTables();

  for (const auto& ac_table : ac_tables.GetObject()) {
    std::string table_name{ac_table.name.GetString()};
    auto params = ac_table.value.GetObject();

    std::string query{params.HasMember("query") && params["query"].IsString()
                          ? params["query"].GetString()
                          : ""};
    std::string path{params.HasMember("path") && params["path"].IsString()
                         ? params["path"].GetString()
                         : ""};
    std::string platform{params.HasMember("platform") &&
                                 params["platform"].IsString()
                             ? params["platform"].GetString()
                             : ""};

    if (query.empty() || path.empty()) {
      LOG(WARNING) << "ATC Table: " << table_name << " is misconfigured";
      continue;
    }

    if (!checkPlatform(platform)) {
      VLOG(1) << "Skipping ATC table: " << table_name
              << " because platform doesn't match";
      continue;
    }

    TableColumns columns;
    std::string columns_value;
    columns_value.reserve(256);

    for (const auto& column : params["columns"].GetArray()) {
      columns.push_back(make_tuple(
          std::string(column.GetString()), TEXT_TYPE, ColumnOptions::DEFAULT));
      columns_value += std::string(column.GetString()) + ",";
    }

    registered.erase(table_name);
    std::string table_settings{table_name + query + columns_value + path};
    std::string old_setting;
    auto s = getDatabaseValue(
        kPersistentSettings, kDatabaseKeyPrefix + table_name, old_setting);

    // The ATC table hasn't changed so we skip ahead
    if (table_settings == old_setting) {
      continue;
    }

    // Remove the old table to replace with the new one
    s = removeATCTables({table_name});
    if (!s.ok()) {
      LOG(WARNING) << "ATC table overrides core table; Refusing registration";
      continue;
    }

    s = setDatabaseValue(
        kPersistentSettings, kDatabaseKeyPrefix + table_name, table_settings);

    if (!s.ok()) {
      LOG(WARNING) << "Could not write to database";
      continue;
    }

    s = tables->add(
        table_name, std::make_shared<ATCPlugin>(path, columns, query), true);

    if (!s.ok()) {
      LOG(WARNING) << s.getMessage();
      deleteDatabaseValue(kPersistentSettings, kDatabaseKeyPrefix + table_name);
      continue;
    }

    PluginResponse resp;
    Registry::call(
        "sql", "sql", {{"action", "attach"}, {"table", table_name}}, resp);
    LOG(INFO) << "Registered ATC table: " << table_name;
  }

  if (registered.size() > 0) {
    VLOG(1)
        << "Removing any ATC tables that were removed in this configuration "
           "change";
    removeATCTables(registered);
  }
  return Status();
}
Exemple #21
0
/*
 * Copy a term that is guaranteed to be contained in a single
 * heap block. The heap block is copied word by word, and any
 * pointers are offsetted to point correctly in the new location.
 *
 * Typically used to copy a term from an ets table.
 *
 * NOTE: Assumes that term is a tuple (ptr is an untagged tuple ptr).
 */
Eterm
copy_shallow(Eterm* ptr, Uint sz, Eterm** hpp, ErlOffHeap* off_heap)
{
    Eterm* tp = ptr;
    Eterm* hp = *hpp;
    Sint offs = hp - tp;

    while (sz--) {
	Eterm val = *tp++;

	switch (primary_tag(val)) {
	case TAG_PRIMARY_IMMED1:
	    *hp++ = val;
	    break;
	case TAG_PRIMARY_LIST:
	case TAG_PRIMARY_BOXED:
	    *hp++ = offset_ptr(val, offs);
	    break;
	case TAG_PRIMARY_HEADER:
	    *hp++ = val;
	    switch (val & _HEADER_SUBTAG_MASK) {
	    case ARITYVAL_SUBTAG:
		break;
	    case REFC_BINARY_SUBTAG:
		{
		    ProcBin* pb = (ProcBin *) (hp-1);
		    int tari = thing_arityval(val);

		    sz -= tari;
		    while (tari--) {
			*hp++ = *tp++;
		    }
		    erts_refc_inc(&pb->val->refc, 2);
		    pb->next = off_heap->mso;
		    off_heap->mso = pb;
		    off_heap->overhead += pb->size / sizeof(Eterm);
		}
		break;
	    case FUN_SUBTAG:
		{
#ifndef HYBRID /* FIND ME! */
		    ErlFunThing* funp = (ErlFunThing *) (hp-1);
#endif
		    int tari = thing_arityval(val);

		    sz -= tari;
		    while (tari--) {
			*hp++ = *tp++;
		    }
#ifndef HYBRID /* FIND ME! */
		    funp->next = off_heap->funs;
		    off_heap->funs = funp;
		    erts_refc_inc(&funp->fe->refc, 2);
#endif
		}
		break;
	    case EXTERNAL_PID_SUBTAG:
	    case EXTERNAL_PORT_SUBTAG:
	    case EXTERNAL_REF_SUBTAG:
		{
		    ExternalThing* etp = (ExternalThing *) (hp-1);
		    int tari = thing_arityval(val);

		    sz -= tari;
		    while (tari--) {
			*hp++ = *tp++;
		    }
		    etp->next = off_heap->externals;
		    off_heap->externals = etp;
		    erts_refc_inc(&etp->node->refc, 2);
		}
		break;
	    default:
		{
		    int tari = header_arity(val);

		    sz -= tari;
		    while (tari--) {
			*hp++ = *tp++;
		    }
		}
		break;
	    }
	    break;
	}
    }
    *hpp = hp;
    return make_tuple(ptr + offs); 
}
  bool is_straight_line_drawing(const Graph& g, 
                                GridPositionMap drawing, 
                                VertexIndexMap vm
                                )
  {

    typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
    typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator_t;
    typedef typename graph_traits<Graph>::edge_descriptor edge_t;
    typedef typename graph_traits<Graph>::edge_iterator edge_iterator_t;
    typedef typename graph_traits<Graph>::edges_size_type e_size_t;
    typedef typename graph_traits<Graph>::vertices_size_type v_size_t;

    typedef std::size_t x_coord_t;
    typedef std::size_t y_coord_t;
    typedef boost::tuple<edge_t, x_coord_t, y_coord_t> edge_event_t;
    typedef typename std::vector< edge_event_t > edge_event_queue_t;

    typedef tuple<y_coord_t, y_coord_t, x_coord_t, x_coord_t> active_map_key_t;
    typedef edge_t active_map_value_t;
    typedef std::map< active_map_key_t, active_map_value_t > active_map_t;
    typedef typename active_map_t::iterator active_map_iterator_t;


    edge_event_queue_t edge_event_queue;
    active_map_t active_edges;

    edge_iterator_t ei, ei_end;
    for(tie(ei,ei_end) = edges(g); ei != ei_end; ++ei)
      {
        edge_t e(*ei);
        vertex_t s(source(e,g));
        vertex_t t(target(e,g));
        edge_event_queue.push_back
          (make_tuple(e, 
                      static_cast<std::size_t>(drawing[s].x),
                      static_cast<std::size_t>(drawing[s].y)
                      )
           );
        edge_event_queue.push_back
          (make_tuple(e,
                      static_cast<std::size_t>(drawing[t].x),
                      static_cast<std::size_t>(drawing[t].y)
                      )
           );
      }

    // Order by edge_event_queue by first, then second coordinate 
    // (bucket_sort is a stable sort.)
    bucket_sort(edge_event_queue.begin(), edge_event_queue.end(),
                property_map_tuple_adaptor<edge_event_t, 2>()
                );
    
    bucket_sort(edge_event_queue.begin(), edge_event_queue.end(),
                property_map_tuple_adaptor<edge_event_t, 1>()
                );

    typedef typename edge_event_queue_t::iterator event_queue_iterator_t;
    event_queue_iterator_t itr_end = edge_event_queue.end();
    for(event_queue_iterator_t itr = edge_event_queue.begin(); 
        itr != itr_end; ++itr
        )
      {
        edge_t e(get<0>(*itr));
        vertex_t source_v(source(e,g));
        vertex_t target_v(target(e,g));
        if (drawing[source_v].x > drawing[target_v].x)
          std::swap(source_v, target_v);

        active_map_key_t key(get(drawing, source_v).y,
                             get(drawing, target_v).y,
                             get(drawing, source_v).x,
                             get(drawing, target_v).x
                             );

        active_map_iterator_t a_itr = active_edges.find(key);
        if (a_itr == active_edges.end())
          {
            active_edges[key] = e;
          }
        else
          {
            active_map_iterator_t before, after;
            if (a_itr == active_edges.begin())
              before = active_edges.end();
            else
              before = prior(a_itr);
            after = next(a_itr);

            if (after != active_edges.end() || before != active_edges.end())
              {
                
                edge_t f = after != active_edges.end() ? 
                  after->second : before->second;

                vertex_t e_source(source(e,g));
                vertex_t e_target(target(e,g));
                vertex_t f_source(source(f,g));
                vertex_t f_target(target(f,g));

                if (intersects(drawing[e_source].x, 
                               drawing[e_source].y,
                               drawing[e_target].x,
                               drawing[e_target].y,
                               drawing[f_source].x, 
                               drawing[f_source].y,
                               drawing[f_target].x,
                               drawing[f_target].y
                               )
                    )
                  return false;
              }

            active_edges.erase(a_itr);

          }
      }

    return true;
    
  }
bool LiveAppModuleImpl::DispatchUdpPacket(BYTE* data, int size, const InetSocketAddress& remoteSockAddr, UINT proxyType, ExtraProxyEnum extraProxy)
{
	m_Statistics.TotalFlow.Download.Record( size );
	const size_t totalDataSize = size;
	APP_EVENT( "DispatchUdpPacket " << make_buffer_pair(data, size) << " from " << remoteSockAddr );//<< " " << HexEncoding::Encode(string((const char*)data, size)) );
	//BYTE* rawData = data;
	//size_t rawSize = size;
	this->RecordDownload(size);
	SimpleSocketAddress sockAddr(remoteSockAddr);

	if ((size_t) size >= sizeof( OLD_UDP_PACKET_HEAD ) + sizeof( GUID ) )
	{
#ifndef _PPL_TEMP_DISABLE_TRACKER
		PacketInputStream is( data, size );
		// 可能是老报文,尝试由tracker模块处理
		if (m_tracker->TryHandleResponse( is, remoteSockAddr, (BYTE)proxyType ))
		{
			m_trackerFlow.Download.Record(size);
			return true;
		}
#endif
	}
	// 解混淆
	int len = PacketObfuscator::UnObfuscate( reinterpret_cast<BYTE*>( data ), size );
	if ( len <= 0 )
	{
		// 解混淆失败,可能是vod的报文
		if ( VODProxy::HandlePacket( data, size, remoteSockAddr, m_BootModule.get(), m_StunModule.get() ) )
		{
			return true;
		}
		m_Statistics.TotalInvalidPackets++;
		if ( size >= 3 )
		{
			VIEW_ERROR( "LiveAppModuleImpl::DispatchUdpPacket unshuffle failed 1 " << make_tuple( size, len ) << sockAddr << " action=" << strings::format( "0x%02X%02X 0x%02X", data[0], data[1], data[2] ) );
		}
		else
		{
			VIEW_ERROR( "LiveAppModuleImpl::DispatchUdpPacket unshuffle failed 1 " << make_tuple( size, len ) << sockAddr );
		}
		//LIVE_ASSERT( false );
		return false;
	}
	if ( size < len )
	{
		m_Statistics.TotalInvalidPackets++;
		VIEW_ERROR( "LiveAppModuleImpl::DispatchUdpPacket unshuffle failed 2 " << make_tuple( size, len ) << sockAddr );
		return false;
	}
	// 截去混淆部分的头,取出真正的报文部分
	data += len;
	size -= len;

	PacketInputStream is( data, size );
	NEW_UDP_PACKET_HEAD head;
	is >> head;
	if ( !is )
	{
		m_Statistics.TotalInvalidPackets++;
		VIEW_ERROR( "LiveAppModuleImpl::DispatchUdpPacket invalid 2 " << size << " " << sockAddr );
		return false;
	}
	if ( head.ProtocolVersion < SYNACAST_VERSION_3 )
	{
		m_Statistics.TotalInvalidPackets++;
		// 检查版本号与MAGIC
		APP_ERROR("Invalid protocol version " << head.ProtocolVersion << " " << sockAddr);
		return false;
	}
	UDPT_INFO("LiveAppModuleImpl::DispatchUdp "<<sockAddr << " " << (int)head.Action);

	// session 处理完
	if ( head.Action & PPL_P2P_CONNECTION_ACTION_FLAG )
	{
		UDP_SESSION_INFO sessionInfo;
		is >> sessionInfo;
		if ( !is )
		{
			m_Statistics.TotalInvalidPackets++;
			APP_ERROR("Invalid session size " << size << " " << sockAddr);
			return false;
		}
		bool res = m_Manager->HandleUDPSessionPacket(is, head, sessionInfo, sockAddr);
		if ( false == res )
		{
			m_Statistics.TotalInvalidPackets++;
		}
		else
		{
			m_Statistics.UDPConnectionFlow.Download.Record(totalDataSize);
		}
		return res;
	}
Exemple #24
0
void PeerConnector::HandleNoDegree( data_input_stream& is, const PACKET_PEER_INFO& packetPeerInfo, bool isTCP )
{
	PPRedirectInfo redirectPacket;
	is >> redirectPacket;
	if ( !is )
		return;
	VIEW_DEBUG("HandleNoDegree " << make_tuple( packetPeerInfo.Address, packetPeerInfo.OuterAddress ) << " " << redirectPacket.Peers.size() );
	m_ipPool.AddConnectFailed(packetPeerInfo.OuterAddress, isTCP);
	PEER_CORE_INFO coreInfo;
	coreInfo.Clear();
	for (UINT8 index = 0; index < redirectPacket.Peers.size(); ++index)
	{
		const REDIRECT_PEER_INFO& peer = redirectPacket.Peers[index];
		//VIEW_INFO("Redirect " << peer.Address << " " << peer.Status.UploadBWLeft << " Qos=" << peer.Status.Qos << " DL=" << (int)peer.Status.DegreeLeft << " DO=" << peer.Status.OutDegree << " DI=" << peer.Status.InDegree << " SP=" << peer.Status.SkipPercent );
		PEER_ADDRESS targetAddr ;
		if ( peer.OuterAddress.IP == m_NetInfo->GetOuterIP() )	// 外部IP相同,取内网IP连接,局域网连接
		{
			targetAddr = peer.Address;
		}
		else if ( peer.OuterAddress.IsValid() )						// 外网地址可用,取外网地址
		{
			targetAddr = peer.OuterAddress;
		}
		else														// 没有外网地址可用,只好取内网地址
		{
			targetAddr = peer.Address;
		}
		LIVE_ASSERT( targetAddr.IsValid() );
		/*
		if ( false == peer.OuterAddress.IsValid() )
		{
			// 如果OuterAddress无效(可能是还没有获取到外部ip和端口),使用内部地址代替
			targetAddr = peer.Address;
			LIVE_ASSERT( false );
		}
		else if ( peer.OuterAddress.IP == m_NetInfo->OuterAddress.IP )
		{
			// 外部IP和我的外部IP相同,此peer跟我处于同一内网,使用内部地址连接
			targetAddr = peer.Address;
		}
		else
		{
			targetAddr = peer.OuterAddress;
		}
		LIVE_ASSERT( 0 != targetAddr.UdpPort || 0 != targetAddr.TcpPort );
		*/
		m_ipPool.AddCandidate(targetAddr, coreInfo, CANDIDATE_FROM_REDIRECT);
		// 检查剩余度是否不足
		if ( m_PeerManager.GetDegreeLeft() < 0 )
			return;
		// 检查出度是否已经过大
		if ( m_PeerManager.GetStatistics().Degrees.All.Out + 3 > m_PeerManager.GetMaxLocalPeerCount() )
			return;
		/// 检查pending连接是否已经过多
		if ( IsBusy() )
			return;
		PeerItem peerItem;
		peerItem.Init();
		peerItem.CanDetect = true;
		peerItem.Info.Address = targetAddr;
		PeerConnectParam param(peerItem, false, m_PeerManager.GetConnectTimeout(), false);
		if ( targetAddr.TcpPort == 0 && targetAddr.UdpPort == 0 )	// 两个端口都没有,错误
		{
			VIEW_ERROR( "PeerConnector::HandleNoDegree - targetAddr error. inner address: " << peer.Address << " outer address: " << peer.OuterAddress );
		}
		else if ( targetAddr.TcpPort == 0 )	// 没有TCP端口
		{
			this->ConnectUDP( targetAddr, param );
		}
		else if ( targetAddr.UdpPort == 0 )	// 没有UDP端口,只能连接TCP
		{
			this->ConnectTCP( targetAddr, 0, param );
		}
		else if (peer.ConnectionType == 0)	// 两个端口都存在
		{
			// tcp
			this->ConnectTCP( targetAddr, 0, param );
		}
		else if ( peer.ConnectionType == 1 )
		{
			// udp
			this->ConnectUDP( targetAddr, param );
		}
		else
		{
			VIEW_ERROR( "PeerConnector::HandleNoDegree invalid peer connection type " << peer.ConnectionType << " " << make_tuple( peer.Address, peer.OuterAddress, targetAddr ) << " " << packetPeerInfo.OuterAddress );
		}
	}
}
PerformanceEvaluator::PerformanceEvaluator(){
  _performanceValueTuples["NORXE"] = make_tuple(make_shared<vector<double>>(),
                                                PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                  return CalculateNORXE(xEst,P,xReal);
                                                  }),
                                                FinishFunction([=](VecPtr vec) {
                                                  return CalculateAverage(vec);
                                                }));
  _performanceValueTuples["FPOS"] = make_tuple(make_shared<vector<double>>(),
                                                PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                  return CalculateFPOS(xEst,P,xReal);
                                                }),
                                                FinishFunction([=](VecPtr vec) {
                                                  return CalculateRM(vec);
                                                }));
  _performanceValueTuples["FVEL"] = make_tuple(make_shared<vector<double>>(),
                                                PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                  return CalculateFVEL(xEst,P,xReal);
                                                }),
                                                FinishFunction([=](VecPtr vec) {
                                                  return CalculateRM(vec);
                                                }));
  _performanceValueTuples["RMSPOS"] = make_tuple(make_shared<vector<double>>(),
                                                PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                  return Square(CalculatePOS(xEst,P,xReal));
                                                }),
                                                FinishFunction([=](VecPtr vec) {
                                                  return CalculateRM(vec);
                                                }));
  _performanceValueTuples["RMSVEL"] = make_tuple(make_shared<vector<double>>(),
                                                 PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                   return Square(CalculateVEL(xEst,P,xReal));
                                                 }),
                                                 FinishFunction([=](VecPtr vec) {
                                                   return CalculateRM(vec);
                                                 }));
  _performanceValueTuples["RMSSPD"] = make_tuple(make_shared<vector<double>>(),
                                                 PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                   return Square(CalculateSPD(xEst,P,xReal));
                                                 }),
                                                 FinishFunction([=](VecPtr vec) {
                                                   return CalculateRM(vec);
                                                 }));
  _performanceValueTuples["RMSCRS"] = make_tuple(make_shared<vector<double>>(),
                                                 PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                   return Square(CalculateCRS(xEst,P,xReal));
                                                 }),
                                                 FinishFunction([=](VecPtr vec) {
                                                   return CalculateRM(vec);
                                                 }));
  _performanceValueTuples["NEES"] = make_tuple(make_shared<vector<double>>(),
                                                 PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                   return CalculateNEES(xEst,P,xReal);
                                                 }),
                                                 FinishFunction([=](VecPtr vec) {
                                                   return CalculateAverage(vec);
                                                 }));
  _performanceValueTuples["MOD2PR"] = make_tuple(make_shared<vector<double>>(),
                                               PerformanceFunction([](SVref xEst,SCMref P,SVref xReal) {
                                                 return -99999.99999;//this will never happen
                                               }),
                                               FinishFunction([=](VecPtr vec) {
                                                 return CalculateAverage(vec);
                                               }));
  _performanceValueTuples["RAWRMSPOS"] = make_tuple(make_shared<vector<double>>(),
                                                 PerformanceFunction([=](SVref xEst,SCMref P,SVref xReal) {
                                                   return Square(CalculatePOS(xEst,P,xReal));
                                                 }),
                                                 FinishFunction([=](VecPtr vec) {
                                                   return CalculateRM(vec);
                                                 }));

}
//Return a status of the elevator which contains
//Current floor, number of people inside, direction it is going and queue
//of destination floors.
tuple<int, int, int, vector<int>> Elevator::status() {
	return make_tuple(currFloor, capacity, direction, destFloors);
}
Exemple #27
0
namespace rmath_rv
{
	template<typename... Args>
	struct _rmathrv
	{
		function<double(Args...)> rf; 
		function<double(double, Args..., int)> df; 
		function<double(double, Args..., int, int)> pf;
		function<double(double, Args..., int, int)> qf;
	};

	using rv2p = _rmathrv<double, double>;
	using rv3p = _rmathrv<double, double, double>;
	
	unordered_map<string, rv2p> rm2prv = 
	{{ "norm"  , { rnorm,  dnorm,  pnorm,  qnorm  }},
	 { "gamma" , { rgamma, dgamma, pgamma, qgamma }}, 
	 { "lnorm" , { rlnorm, dlnorm, plnorm, qlnorm }}};
 
	unordered_map<string, rv3p> rm3prv = 
	{{ "hyper" , { rhyper,  dhyper,  phyper,  qhyper }}};

	auto rv_maps = make_tuple(rm2prv, rm3prv);

	template<typename... Args>
	class RMathRV
	{
	public:
		RMathRV(const string family,
				Args... args)
			{
				_family = family;
				_para = tuple<Args...>(args...);
				const auto _pn = tuple_size<decltype(_para)>::value - 2;
				// Note: potential run time error here: the "family" may not be
				// in the map object's keys 
				_rv = get<_pn>(rv_maps)[family];
				_np = _pn;
			}
		~RMathRV()
			{				
			}

		inline vector<double> rvs(const unsigned int _n)
			{
				vector<double> x_s(_n, 0.0);
				for (auto &itr : x_s)
					itr = stackoverflow::call(_rv.rf, _para);
				return x_s; 
			}
		
	private:		
		string _family; 
		tuple<Args...> _para;
		int _np;
		_rmathrv<Args...> _rv;		
	};

	using RMathRV2p = RMathRV<double, double>;
	using RMathRV3p = RMathRV<double, double, double>;
};
Exemple #28
0
 inline type make_empty_tuple()
 {
   // TODO: return a static instance
   nd::array field_types = nd::empty(0, make_type());
   return make_tuple(field_types);
 }
Exemple #29
0
// LinearSizeConstraint::getCoeffs() 
tuple getCoeffs(LinearSizeConstraint& c)
{
  int a, b;
  c.getCoeffs(a,b);
  return make_tuple(a,b);
}
Exemple #30
0
        else
        {
            ADD_FAILURE() << "Found " << circles.size() << " on frame " << i ;
        }
    }
    {
        Mat frame;
        cap >> frame;
        EXPECT_TRUE(frame.empty());
    }
    cap.release();
    ASSERT_FALSE(cap.isOpened());
}

Param test_data[] = {
    make_tuple("video/x-raw, format=BGR"  , Size(640, 480), Size(640, 480), COLOR_BGR2RGB),
    make_tuple("video/x-raw, format=GRAY8", Size(640, 480), Size(640, 480), COLOR_GRAY2RGB),
    make_tuple("video/x-raw, format=UYVY" , Size(640, 480), Size(640, 480), COLOR_YUV2RGB_UYVY),
    make_tuple("video/x-raw, format=YUY2" , Size(640, 480), Size(640, 480), COLOR_YUV2RGB_YUY2),
    make_tuple("video/x-raw, format=YVYU" , Size(640, 480), Size(640, 480), COLOR_YUV2RGB_YVYU),
    make_tuple("video/x-raw, format=NV12" , Size(640, 480), Size(640, 720), COLOR_YUV2RGB_NV12),
    make_tuple("video/x-raw, format=NV21" , Size(640, 480), Size(640, 720), COLOR_YUV2RGB_NV21),
    make_tuple("video/x-raw, format=YV12" , Size(640, 480), Size(640, 720), COLOR_YUV2RGB_YV12),
    make_tuple("video/x-raw, format=I420" , Size(640, 480), Size(640, 720), COLOR_YUV2RGB_I420),
    make_tuple("video/x-bayer"            , Size(640, 480), Size(640, 480), COLOR_BayerBG2RGB),
    make_tuple("jpegenc ! image/jpeg"     , Size(640, 480), Size(640, 480), COLOR_BGR2RGB)
};

INSTANTIATE_TEST_CASE_P(videoio, Videoio_Gstreamer_Test, testing::ValuesIn(test_data));

} // namespace