int main (int argc, char** argv) { log.execname (basename (argv[0])); log << "starting" << endl; vector<string> args (&argv[1], &argv[argc]); //Print a usage error if the there are too many input commands if (args.size() > 2) usage(); //Otherwise get the host and port from daemon string host = get_cix_server_host (args, 0); in_port_t port = get_cix_server_port (args, 1); //Begin to parse the input lines if (args.size() == 1) {//// string prog_name = args[0]; size_t rc_find = prog_name.find("."); //Case unable to find server name if(rc_find == string::npos) { host = get_cix_server_host (args,1); port = get_cix_server_port (args,0); } //Case: found server name else { host = get_cix_server_host (args,0); port = get_cix_server_port (args,1); } } //Case: Default, run with args 0, args 1 else { host = get_cix_server_host (args, 0); port = get_cix_server_port (args, 1); } log << to_string (hostinfo()) << endl; //Starter code: try to connect try { log << "connecting to " << host << " port " << port << endl; client_socket server (host, port); log << "connected to " << to_string (server) << endl; //Infinite loop, prog_name input from cin and parse it for (;;) { string line; getline (cin, line); if (cin.eof()) throw cix_exit(); if (SIGINT_throw_cix_exit) throw cix_exit(); //Begin to make //Minor loop, cleans input from cin size_t end = 0; vector<string> cleaned_line; for (;;) { size_t start = line.find_first_not_of (" \t", end); if (start == string::npos) break; end = line.find_first_of (" \t", start); cleaned_line.push_back (line.substr (start, end - start)); } //If there is nothing there, then continue through if(cleaned_line.size() == 0) { continue; } //Log the command and find it in the map log << "command " << cleaned_line[0] << endl; const auto& itor = command_map.find (cleaned_line[0]); cix_command cmd = itor == command_map.end() ? CIX_ERROR : itor->second; //Switch depending on the command type switch (cmd) { case CIX_EXIT: throw cix_exit(); break; case CIX_HELP: cix_help(); break; case CIX_LS: cix_ls (server); break; case CIX_GET: //Check for invalid get command if(cleaned_line.size() < 2) { cout << "Error: Invalid arguments to: get" << endl; break; } cix_get (server, cleaned_line[1]); break; case CIX_PUT: //Check for invalid put command if(cleaned_line.size() < 2) { cout << "Error: Invalid arguments to: put" << endl; break; } cix_put (server, cleaned_line[1]); break; case CIX_RM: //Check for invalid rm command if(cleaned_line.size() < 2) { cout << "Error: Invalid arguments to: rm" << endl; break; } cix_rm (server, cleaned_line[1]); break; default: log << line << ": invalid command" << endl; break; } } }catch (socket_error& error) { log << error.what() << endl; }catch (cix_exit& error) { log << "caught cix_exit" << endl; } log << "finishing" << endl; return 0; }
// from train_cat_item_item to find better match one void gao_test(char * test_file_txt, char * output_file_txt){ fstream item_online_test_file(test_file_txt); ofstream item_output_file(output_file_txt); double st = clock(); int now = 0, SHOW = 1; LL test_item; while(item_online_test_file >> test_item){ vector<PDL> ans; unordered_map<LL, bool> has; LL bias = 100000; double sstt = clock(); if(now++ % SHOW == 0) cerr<<"now:\t"<<now<<endl; // add most buyers for first choice vector<PLL> vec_buyer;// (buyertimes, item) unordered_map<LL, int> item_bought_times; vector<LL> & buyers = item_buyers[test_item]; cerr<<"check buyer number = "<<buyers.size()<<endl; for(int i = 0; i < buyers.size(); i++){ LL buyer = buyers[i]; vector<LL> & bought_items = buyer_items[buyer]; for(int j = 0; j < bought_items.size(); j++){ LL another_item = bought_items[j]; if (another_item == test_item) continue; LL aic = item_cat[another_item], tic = item_cat[test_item]; //if(cat_cat_map[haxi(aic, tic)]) // if logical reasonable, cat-cat can be matched if(cat_cat_map.find(haxi(aic, tic)) != cat_cat_map.end()) item_bought_times[another_item]++; } } for(unordered_map<LL, int>::iterator it = item_bought_times.begin(); it != item_bought_times.end(); it++){ vec_buyer.push_back(PLL(it->second, it->first)); } sort(vec_buyer.begin(), vec_buyer.end()); reverse(vec_buyer.begin(), vec_buyer.end()); int sz = vec_buyer.size(), max_bought = 0; if (sz > 0){ cerr<<"max buyer number:\t"<<vec_buyer[0].first<<"\tmaxitem:\t"<<vec_buyer[0].second<<"\tmin buyer number:\t"<<vec_buyer[sz - 1].first<<"\tminitem:\t"<<vec_buyer[sz - 1].second<<endl; max_bought = vec_buyer[0].first; } for(int i = 0; i < vec_buyer.size(); i++){ if((int)ans.size() >= OUTNUM) break; LL item = vec_buyer[i].second; int times = vec_buyer[i].first; if(max_bought > 1 && times == 1) continue; ans.push_back(PLL(bias * times, item)); has[item] = true; } cerr<<"total item bought number = "<<ans.size()<<endl; // prob similarity search LL test_item_cat = item_cat[test_item]; vector<PLL> &vp = train_cat_item_item[test_item_cat]; vector<double> prob_left(vp.size()); vector<vector<PDL> > vvp(vp.size()); for (int i = 0; i < vp.size(); i++){ vvp[i] = same[vp[i].second]; prob_left[i] = similarity(test_item, vp[i].first); } vector<int> pt(vvp.size(), 0); for (int c = 0; ; c++){ if((int)ans.size() >= OUTNUM) break; int w = -1; double max_prob = -1111111; LL choose_item = -1; bool check = false; for(int i = 0; i < vvp.size(); i++){ int p = pt[i]; vector<PDL>& vpdl = vvp[i]; if (p >= (int) vpdl.size()) continue; check = true; double prob = prob_left[i] * vpdl[p].first; if (prob > max_prob){ max_prob = prob; w = i; choose_item = vpdl[p].second; } } if(check == false) break; pt[w]++; if(has.find(choose_item) == has.end()){ ans.push_back(PDL(max_prob, choose_item)); has[choose_item] = true; } } // sometime no need to sort //sort(ans.begin(), ans.end()); //reverse(ans.begin(), ans.end()); // write into file if((now - 1)% SHOW == 0){ for(int i = 0; i < 20; i++) cerr<<ans[i].first<<"\t"<<ans[i].second<<"\tin train: "<<train_item_map[ans[i].second]<<endl; int last = ans.size(); cerr<<"last\t" <<ans[last - 1].first<<" "<<ans[last - 1].second<<endl; } cerr<<"time use = "<<(clock() - sstt) / CLOCKS_PER_SEC<<endl; item_output_file<<test_item<<" "; for(int i = 0; i < OUTNUM - 1; i++){ item_output_file<<ans[i].second<<","; } item_output_file<<ans[OUTNUM - 1].second<<"\n"; } cout<<"get online test over, time use:\t"<<(clock() - st) / CLOCKS_PER_SEC<<"\n"; }
unsigned long DataKmerCount(char* combinefileName, char* outputFileDir, char* shortName, int k, unsigned long power, bool doubleStrand, bool zeroCountOut) { // Initialize total_DataNum. total_DataNum records the total number of kmer in rth dataset unsigned long total = 0; ifstream fin(combinefileName); //EDIT fin(argv[1]) // unsigned long copynumber; // cout << "begin scan" << endl; // Begin to scan lines in dataset from top to the bottom // countKmer ONLY for single strand while(fin.getline(seq,seqlength)) { //cout << seq << endl; if (doubleStrand == false) { // sinlge strand total += SeqKmerCountSingle(seq, k, power); // cout << "seq" << endl; }else{ // double strand total += SeqKmerCount(seq, k, power); } } cout << "total characters: " << total << endl; fin.close(); // Output kmer count-pw files char kstr[5]; sprintf(kstr, "%d", k); char kmerCountfile[1000]; if (outputFileDir == NULL) { strcpy(kmerCountfile,shortName); strcat(kmerCountfile,"_k"); strcat(kmerCountfile,kstr); }else{ strcpy(kmerCountfile,outputFileDir); strcat(kmerCountfile,shortName); strcat(kmerCountfile,"_k"); strcat(kmerCountfile,kstr); } if (doubleStrand == false) { // single strand strcat(kmerCountfile,"_ss_wc"); }else{ strcat(kmerCountfile,"_ds_wc"); } cout << "kmerCountFileName: " << kmerCountfile << endl; ofstream foutKmerCount(kmerCountfile); if (zeroCountOut == false) { // only output those occurring // Sort the kmer vector<unsigned long> temp; for( unordered_map<unsigned long,unsigned long>::iterator i = HashTable.begin(); i!= HashTable.end(); i++) temp.push_back(i->first); sort(temp.begin(), temp.end()); // print for(vector<unsigned long>::iterator j = temp.begin(); j!=temp.end(); j++) { unsigned long key = *j; foutKmerCount << key << "," << HashTable[key] << endl; } }else { // output all kmers for(unsigned long key = 0; key < pow(4,k); key++) { foutKmerCount << key << "," << HashTable[key] << endl; } } foutKmerCount.close(); return total; }
void ProcessDLLData(const wchar_t *filename, const wstring &mod_dir) { const IniFile *const dlldata = new IniFile(filename); const IniGroup *group; wstring dllname = dlldata->getWString("", "name"); HMODULE dllhandle; if (dllhandles.find(dllname) != dllhandles.cend()) dllhandle = dllhandles[dllname]; else { dllhandle = GetModuleHandle(dllname.c_str()); dllhandles[dllname] = dllhandle; } if (dllexports.find(dllname) == dllexports.end()) { group = dlldata->getGroup("Exports"); dllexportcontainer exp; for (auto iter = group->cbegin(); iter != group->cend(); ++iter) { dllexportinfo inf; inf.address = GetProcAddress(dllhandle, iter->first.c_str()); inf.type = iter->second; exp.exports[iter->first] = inf; } dllexports[dllname] = exp; } const auto exports = &dllexports[dllname].exports; dlltexlists.clear(); if (dlldata->hasGroup("TexLists")) { group = dlldata->getGroup("TexLists"); for (auto iter = group->cbegin(); iter != group->cend(); ++iter) { NJS_TEXLIST *key = (NJS_TEXLIST*)std::stoul(iter->first, nullptr, 16); vector<string> valstr = split(iter->second, ','); NJS_TEXLIST *value; if (valstr.size() > 1) value = ((NJS_TEXLIST**)(*exports)[valstr[0]].address)[std::stoul(valstr[1])]; else value = (NJS_TEXLIST*)(*exports)[valstr[0]].address; dlltexlists[key] = value; } } dlllabels.clear(); group = dlldata->getGroup("Files"); for (auto iter = group->cbegin(); iter != group->cend(); ++iter) { auto type = dllfilefuncmap.find(split(iter->second, '|')[0]); if (type != dllfilefuncmap.end()) type->second(mod_dir + L'\\' + MBStoUTF16(iter->first, CP_UTF8)); } char buf[16]; for (unsigned int k = 0; k < 9999; k++) { snprintf(buf, sizeof(buf), "Item%u", k); if (dlldata->hasGroup(buf)) { group = dlldata->getGroup(buf); const dllexportinfo &exp = (*exports)[group->getString("Export")]; auto type = dlldatafuncmap.find(exp.type); if (type != dlldatafuncmap.end()) type->second(group, exp.address); } } delete dlldata; }
bool isPowerOfThree01(int n) { static unordered_map<int, bool> power; if (power.size()==0) init(power); return power.find(n) != power.end(); }
bool isUnique(string word) { string key = word.length()<3 ? word : word[0]+to_string(word.length()-2)+word[word.length()-1]; if(abbr.find(key)==abbr.end() || (abbr[key]==1 && dict[word])) return true; else return false; }
static int enregistrerLRUinterne(unsigned int *w,int tempo) { static int vcardinal=0; static int atempo=-1; if (atempo!= tempo) { vcardinal=cardinal; fprintf(stderr,"LRU cache taille :%d sentence:%d card:%d h:%d old:%d new%d \n",lru_count,atempo,cardinal,hit,older,newer); lru_count=0; hit=0; atempo=tempo; } hit++; ram[0].w=w; unordered_map <Pgram,int,hash_X ,hash_X>::iterator j =cache.find(ram); // dump(cache,"in passe1"); if (j!= cache.end()) { int p=j->second; if (ram[p].tempo>=tempo) return -1; ram[p].tempo=tempo; if (p==newer) return -1; if (older!=p) { ram[ram[p].older].newer = ram[p].newer; ram[ram[p].newer].older=ram[p].older;} else { ram[ram[p].newer].older=-1; older=ram[p].newer; } assert (older>0); //assert (older< vcardinal || vcardinal <1000); ram[p].newer=-1; ram[p].older=newer; ram[newer].newer=p; newer=p; return -1; } lru_count++; if (cardinal <TAILLEMAX) { memcpy( ram[cardinal].w,w,ordreMax*sizeof(int)); ram[cardinal].older=newer; ram[newer].newer=cardinal; newer=cardinal; ram[cardinal].tempo=tempo; ram[cardinal].newer=-1; cache[ram+cardinal]=cardinal; cardinal ++; ram[cardinal-1].val= 3*cardinal; return cardinal-1; } dump(cache,"in pass1 av er"); assert(ram[older].tempo<tempo); cache.erase(ram+older); dump(cache,"in passe1 apr er"); memcpy( ram[older].w,w,ordreMax*sizeof(int)); ram[older].tempo=tempo; ram[older].older=newer; ram[newer].newer=older; newer=older; cache[ram+older]=older; int tempOlder=older; older=ram[older].newer; assert(older>0); //assert (older< vcardinal || vcardinal <1000); ram[older].older=-1; dump(cache,"in passe1 apr ins"); return tempOlder; }
int dfs(TreeNode* node){ if(!node) return INT_MIN; if(mps.find(node) != mps.end()) return mps[node]; int ret = node->val+max(0,max(dfs(node->left),dfs(node->right))); mps[node] = ret; return ret; }
void do_command(char *command_c, int client) { string command = command_c; string output; vector<string> tokens; try { tokens = tokenize(command); } catch (...) { server_send(client, "404 COMMAND NOT FOUND\n"); return; } if (tokens[0] == "SHORTEST_PATH_A2A") { if ((tokens.size() < 3) || !bitcoin_address_quick_valid(tokens[1]) || !bitcoin_address_quick_valid(tokens[2])) { server_send(client, "500 Arguments error.\n"); return; } output = find_path(tokens[1], tokens[2]); if (!output.empty()) { server_send(client, "BEGIN\n"); server_send(client, output); server_send(client, "END\n"); } else { server_send(client, "500 No path.\n"); } return; } else if (tokens[0] == "SUCCESSORS") { if (tokens.size() < 2) { server_send(client, "500 Arguments error.\n"); return; } server_send(client, "BEGIN\n"); unordered_set<string> successors = find_successors(tokens[1]); if (!successors.empty()) { for (auto& it : successors) { output += it + ","; } /* Remove last character (,) */ try { output.pop_back(); } catch (...) { // Do nothing for now. } server_send(client, output + "\n"); server_send(client, "END\n"); } else server_send(client, "500 No successors.\n"); return; } else if (tokens[0] == "PREDECESSORS") { if ((tokens.size() < 2) || !bitcoin_address_quick_valid(tokens[1])) { server_send(client, "500 Arguments error.\n"); return; } server_send(client, "BEGIN\n"); unordered_set<string> predecessors = find_predecessors(tokens[1]); if (!predecessors.empty()) { for (auto& it : predecessors) { output += it + ","; } /* Remove last character (,) */ try { output.pop_back(); } catch (...) { // Do nothing for now. } server_send(client, output + "\n"); server_send(client, "END\n"); } else server_send(client, "500 No predecessors.\n"); return; } else if (tokens[0] == "A2A") { if ((tokens.size() < 3) || !bitcoin_address_quick_valid(tokens[1]) || !bitcoin_address_quick_valid(tokens[2])) { server_send(client, "500 Arguments error.\n"); return; } server_send(client, "BEGIN\n"); unordered_set<string> tx_hashes = a2a(tokens[1], tokens[2]); if (!tx_hashes.empty()) { for (auto& it : tx_hashes) { server_send(client, it + "\n"); } server_send(client, "END\n"); } else server_send(client, "500 No transactions.\n"); return; } else if (tokens[0] == "A2C") { if ((tokens.size() < 3) || !bitcoin_address_quick_valid(tokens[1])) { server_send(client, "500 Arguments error.\n"); return; } server_send(client, "BEGIN\n"); unordered_set<string> tx_hashes = a2c(tokens[1], (unsigned int)stoi(tokens[2])); if (!tx_hashes.empty()) { for (auto& it : tx_hashes) { server_send(client, it + "\n"); } } else { server_send(client, "500 No transactions.\n"); } server_send(client, "END\n"); return; } else if (tokens[0] == "C2A") { if ((tokens.size() < 3) || !bitcoin_address_quick_valid(tokens[2])) { server_send(client, "500 Arguments error.\n"); return; } server_send(client, "BEGIN\n"); unordered_set<string> tx_hashes = c2a((unsigned int)stoi(tokens[1]), tokens[2]); if (!tx_hashes.empty()) { for (auto& it : tx_hashes) { server_send(client, it + "\n"); } } else { server_send(client, "500 No transactions.\n"); } server_send(client, "END\n"); return; } else if (tokens[0] == "C2C") { if (tokens.size() < 3) { server_send(client, "500 Arguments error.\n"); return; } server_send(client, "BEGIN\n"); unordered_set<string> tx_hashes = c2c((unsigned int)stoi(tokens[1]), (unsigned int)stoi(tokens[2])); if (!tx_hashes.empty()) { for (auto& it : tx_hashes) { server_send(client, it + "\n"); } } else { server_send(client, "500 No transactions.\n"); } server_send(client, "END\n"); return; } else if (tokens[0] == "PRINT_CLUSTER") { unsigned int cluster; if (tokens.size() < 2) { server_send(client, "500 Arguments error.\n"); return; } try { print_cluster(client, stoi(tokens[1])); } catch (std::invalid_argument e) { server_send(client, "500 Arguments error.\n"); } return; } else if (tokens[0] == "PRINT_CLUSTER_ID") { if (tokens.size() < 2) { server_send(client, "500 Arguments error.\n"); return; } try { print_cluster_id(client, tokens[1]); } catch (std::invalid_argument e) { server_send(client, "500 Arguments error.\n"); } return; } else if (tokens[0] == "PRINT_CLUSTER_LABEL") { unsigned int cluster; if (tokens.size() < 2) { server_send(client, "500 Arguments error.\n"); return; } try { print_cluster_label(client, stoi(tokens[1])); } catch (std::invalid_argument e) { server_send(client, "500 Arguments error.\n"); } return; } else if (tokens[0] == "PRINT_NEIGHBORS") { unsigned int cluster; if ((tokens.size() < 2) || !bitcoin_address_quick_valid(tokens[1])) { server_send(client, "500 Arguments error.\n"); return; } try { auto got = clusters.find(addresses.at(tokens[1])); if (got == clusters.end()) { server_send(client, "500 Address not present in any cluster.\n"); return; } else { cluster = got->second; } print_cluster(client, cluster); } catch (std::out_of_range& e) { server_send(client, "500 Address not present in database.\n"); } return; } else if (tokens[0] == "STATS") { server_send(client, "BEGIN\n"); server_send(client, to_string(addresses.size())); server_send(client, "\n"); server_send(client, to_string(transactions.size())); server_send(client, "\nEND\n"); return; } else if (tokens[0] == "QUIT") { exit(0); } else { server_send(client, "404 COMMAND NOT FOUND\n"); } }
dat DP( int len , int s0 , int s1 , int s2 , int k0 , int k1 , int k2 , bool& nok ){ int ee = enc( len , s0 , s1 , s2 , k0 , k1 , k2 ); nok = true; if( len <= 0 ) return make_tuple( "" , "" , "" ); { int vl0 = msk[ 0 ][ s0 + len - 1 ] ^ msk[ 0 ][ s0 - 1 ]; int vl1 = msk[ 1 ][ s1 + len - 1 ] ^ msk[ 1 ][ s1 - 1 ]; int vl2 = msk[ 2 ][ s2 + len - 1 ] ^ msk[ 2 ][ s2 - 1 ]; if( vl0 != vl1 or vl0 != vl2 ){ nok = false; return make_tuple( "" , "" , "" ); } } { auto it = memo.find( ee ); if( it != memo.end() ){ nok = it->second.second; return it->second.first; } } if( len == 1 ){ string cur = ""; cur += c[ 0 ][ s0 ]; memo[ ee ] = { make_tuple( cur , cur , cur ) , true }; return make_tuple( cur , cur , cur ); } bool found = false; dat ret = make_tuple("", "", ""); for( int root = 0 ; root < 26 ; root ++ ){ int at0 = at[ 0 ][ root ]; int at1 = at[ 1 ][ root ]; int at2 = at[ 2 ][ root ]; int s , t; { int llen = -1; if( not check( s0 , len , k0 , at0 , llen ) ) continue; if( not check( s1 , len , k1 , at1 , llen ) ) continue; if( not check( s2 , len , k2 , at2 , llen ) ) continue; s = (llen == -1 ? 0 : llen ); t = (llen == -1 ? len - 1 : llen ); } for( int llen = s ; llen <= t ; llen ++ ){ int ssl[ 3 ] , kdl[ 3 ]; int ssr[ 3 ] , kdr[ 3 ]; upd( s0 , k0 , llen , ssl[ 0 ] , kdl[ 0 ] , ssr[ 0 ] , kdr[ 0 ] ); upd( s1 , k1 , llen , ssl[ 1 ] , kdl[ 1 ] , ssr[ 1 ] , kdr[ 1 ] ); upd( s2 , k2 , llen , ssl[ 2 ] , kdl[ 2 ] , ssr[ 2 ] , kdr[ 2 ] ); bool c*k; dat lret = DP( llen , ssl[ 0 ] , ssl[ 1 ] , ssl[ 2 ] , kdl[ 0 ] , kdl[ 1 ] , kdl[ 2 ] , c*k ); if( not c*k ) continue; dat rret = DP( len - llen - 1 , ssr[ 0 ] , ssr[ 1 ] , ssr[ 2 ] , kdr[ 0 ] , kdr[ 1 ] , kdr[ 2 ] , c*k ); if( not c*k ) continue; dat got; Pre( got ) = (char)(root + 'A') + Pre( lret ) + Pre( rret ); In( got ) = In( lret ) + (char)(root + 'A') + In( rret ); Post( got ) = Post( lret ) + Post( rret ) + (char)(root + 'A'); if( not found or got < ret ){ found = true; ret = got; } } } memo[ ee ] = { ret , found }; nok = found; return ret; }
int main() { sae::io::vid_t gvid = 0, geid = 0; // GraphBuilder is not efficient for this kind of large data. cerr << "graph builder..." << endl; sae::io::GraphBuilder<uint64_t> builder; builder.AddVertexDataType("Author"); builder.AddVertexDataType("Publication"); builder.AddVertexDataType("JConf"); builder.AddEdgeDataType("Publish"); builder.AddEdgeDataType("Appear"); builder.AddEdgeDataType("Influence"); // build Authors cerr << "building authors..." << endl; { ifstream authors_csv("authors.csv"); CSVReader authors_reader(authors_csv); Author a; string names; vector<string> row; authors_reader.readrow(row); // header while (authors_reader.readrow(row)) { a.id = stoi(row[0]); a.names = split(row[1], ','); a.position = row[2]; a.affiliation = row[3]; a.imgurl = row[4]; a.topics = split(row[5], ','); a.h_index = stoi(row[6]); a.publication_number = stoi(row[7]); a.citation_number = stoi(row[8]); aid_map[a.id] = gvid++; builder.AddVertex(a.id, a, "Author"); } } // build Conferences cerr << "building conferences..." << endl; { ifstream jconfs("jconf_name.txt"); JConf j; while (jconfs >> j.id) { getline(jconfs >> std::ws, j.name); jconf_map[j.id] = gvid++; builder.AddVertex(JCONF_BASE + j.id, j, "JConf"); } } // build Publications cerr << "building publications and appear relations..." << endl; { ifstream pubs("publication.txt"); ifstream pub_ext("publication_ext.txt"); ifstream pub_topics("terms_given_publication.txt"); int pid, tmp_id, tmp_pubtopic_id; string tmp_abstract, tmp_topics; tmp_id = tmp_pubtopic_id = -1; while (pubs >> pid) { Publication p; p.id = pid; if (p.id % 10000 == 0) { cerr << "publication" << p.id << std::endl; } getline(pubs.ignore(), p.title, '\t'); pubs >> p.jconf; pubs >> p.year; string authors; getline(pubs.ignore(), authors, '\t'); // unused string citation_number; getline(pubs, citation_number); if (citation_number.size() > 0) { //cerr << "!!!" << citation_number << endl; p.citation_number = stoi(citation_number); } else { p.citation_number = -1; } if (p.id > tmp_id) { while (pub_ext >> tmp_id) { getline(pub_ext.ignore(), tmp_abstract); if (tmp_id >= p.id) break; } } if (p.id > tmp_pubtopic_id) { while (pub_topics >> tmp_pubtopic_id) { getline(pub_topics.ignore(), tmp_topics); if (tmp_id >= p.id) break; } } if (p.id == tmp_id) { p.abstract = tmp_abstract; } if (p.id == tmp_pubtopic_id) { p.topics = split(tmp_topics, '\t'); } pid_map[p.id] = gvid++; builder.AddVertex(PUBLICATION_BASE + p.id, p, "Publication"); auto jit = jconf_map.find(p.jconf); if (jit == jconf_map.end()) { if (p.jconf > 0) cerr << "jconf not found: " << p.jconf << endl; } else { builder.AddEdge(PUBLICATION_BASE + p.id, JCONF_BASE + p.jconf, PublicationJConf(), "Appear"); } if (pubs.fail()) { cerr << "Read failed!" << p.id << endl; pubs.clear(ios::failbit); } //cerr << "pub id: " << p.id << " % " << gvid << " % " << p.title << " % " << authors << " % " << p.jconf << " % " << p.year << " % "<< p.citation_number << endl; } } // build relations cerr << "building publish relations..." << endl; { ifstream a2p("a2p_a.txt"); AuthorPublication ap; int aid, pid; int ncount = 0; while (a2p >> aid >> pid) { auto ait = aid_map.find(aid); auto pit = pid_map.find(pid); if (ait == aid_map.end()) { cerr << "Author not found: " << aid << endl; } if (pit == pid_map.end()) { cerr << "Publication not found: " << pid << ", not found count: " << (++ncount) << endl; } if (ait == aid_map.end() || pit == pid_map.end()) { continue; } builder.AddEdge(AUTHOR_BASE + aid, PUBLICATION_BASE + pid, ap, "Publish"); } } cerr << "building influence graph..." << endl; { ifstream influence("influence_all.txt"); AuthorInfluence ai; int source, target; while (influence >> source >> target >> ai.topic >> ai.score) { if (aid_map.find(source) == aid_map.end() || aid_map.find(target) == aid_map.end()) { cerr << "Author not found: " << source << ", " << target << endl; continue; } builder.AddEdge(AUTHOR_BASE + source, AUTHOR_BASE + target, ai, "Influence"); } } cerr << "saving graph..." << endl; // save graph builder.Save("aminer"); cerr << "saved graph" << endl; return 0; }
ustring ColorSpaceManager::detect_known_colorspace(ustring colorspace, const char *file_format, bool is_float) { if (colorspace == u_colorspace_auto) { /* Auto detect sRGB or raw if none specified. */ if (is_float) { bool srgb = (colorspace == "sRGB" || colorspace == "GammaCorrected" || (colorspace.empty() && (strcmp(file_format, "png") == 0 || strcmp(file_format, "tiff") == 0 || strcmp(file_format, "dpx") == 0 || strcmp(file_format, "jpeg2000") == 0))); return srgb ? u_colorspace_srgb : u_colorspace_raw; } else { return u_colorspace_srgb; } } else if (colorspace == u_colorspace_srgb || colorspace == u_colorspace_raw) { /* Builtin colorspaces. */ return colorspace; } else { /* Use OpenColorIO. */ #ifdef WITH_OCIO { thread_scoped_lock cache_lock(cache_colorspaces_mutex); /* Cached lookup. */ if (cached_colorspaces.find(colorspace) != cached_colorspaces.end()) { return cached_colorspaces[colorspace]; } } /* Detect if it matches a simple builtin colorspace. */ bool is_scene_linear, is_srgb; is_builtin_colorspace(colorspace, is_scene_linear, is_srgb); thread_scoped_lock cache_lock(cache_colorspaces_mutex); if (is_scene_linear) { VLOG(1) << "Colorspace " << colorspace.string() << " is no-op"; cached_colorspaces[colorspace] = u_colorspace_raw; return u_colorspace_raw; } else if (is_srgb) { VLOG(1) << "Colorspace " << colorspace.string() << " is sRGB"; cached_colorspaces[colorspace] = u_colorspace_srgb; return u_colorspace_srgb; } /* Verify if we can convert from the requested color space. */ if (!get_processor(colorspace)) { OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig(); if (!config || !config->getColorSpace(colorspace.c_str())) { VLOG(1) << "Colorspace " << colorspace.c_str() << " not found, using raw instead"; } else { VLOG(1) << "Colorspace " << colorspace.c_str() << " can't be converted to scene_linear, using raw instead"; } cached_colorspaces[colorspace] = u_colorspace_raw; return u_colorspace_raw; } /* Convert to/from colorspace with OpenColorIO. */ VLOG(1) << "Colorspace " << colorspace.string() << " handled through OpenColorIO"; cached_colorspaces[colorspace] = colorspace; return colorspace; #else VLOG(1) << "Colorspace " << colorspace.c_str() << " not available, built without OpenColorIO"; return u_colorspace_raw; #endif } }
/****************************************************************************** Description.: There is a separate instance of this function for each connection. It handles all communication once a connnection has been established. Input Value.: Return Value: - ******************************************************************************/ void *serverThread (void * inputsock) { int sock = *((int *)inputsock); int n; char buffer[100]; string userID; char *threadType; char fail[] = "failed"; // Receive the header bzero(buffer, sizeof(buffer)); if (!orbit) { n = read(sock, buffer, sizeof(buffer)); if (n < 0) { errorSocket("ERROR reading from socket", sock); } } // below is orbit mode, using MFAPI else { MsgD.recv(sock, buffer, sizeof(buffer)); } printf("[server] header content: %s\n\n",buffer); threadType = strtok(buffer, ","); userID = strtok(NULL, ","); // grap the lock pthread_mutex_lock(&user_map_lock); // confirm that this user does not log in if (user_map.find(userID) == user_map.end()) { // put the new user into user map user_map[userID] = 1; } else { if (user_map[userID] == 1) { // increase user thread count user_map[userID] = 2; } else { // remember to unlock! pthread_mutex_unlock(&user_map_lock); // reponse to the client if (!orbit) { if (write(sock, "failed", sizeof("failed")) < 0) { errorSocket("ERROR writting to socket", sock); } close(sock); } else { MsgD.send(sock, fail, sizeof(fail)); } printf("[server] User exist. Connection closed.\n\n"); return 0; } } pthread_mutex_unlock(&user_map_lock); if (strcmp(threadType, "transmit") == 0) { server_transmit(sock, userID); } else if (strcmp(threadType, "result") == 0) { server_result(sock, userID); } else { if (!orbit) { close(sock); } printf("[server] Command Unknown. Connection closed.\n\n"); } return 0; }
/****************************************************************************** Description: function for transmitting the frames Input Value.: Return Value: ******************************************************************************/ void server_transmit (int sock, string userID) { // printf("transmitting part\n"); int n; char response[] = "ok"; char file_name_temp[60]; char *file_name; int write_length = 0; int length = 0; queue<string> *imgQueue = new queue<string>(); // queue storing the file names // grap the lock pthread_mutex_lock(&queue_map_lock); queue_map[userID] = imgQueue; // put the address of queue into map pthread_mutex_unlock(&queue_map_lock); pthread_mutex_t queueLock; // mutex lock for queue operation sem_t *sem_match = 0; // init the mutex lock if (pthread_mutex_init(&queueLock, NULL) != 0) { errorSocket("ERROR mutex init failed", sock); } if (!orbit) { char buffer[BUFFER_SIZE]; char *file_size_char; int file_size; int received_size = 0; // reponse to the client n = write(sock, response, sizeof(response)); if (n < 0) { pthread_mutex_destroy(&queueLock); errorSocket("ERROR writting to socket", sock); } while (!global_stop) { received_size = 0; // receive the file info bzero(buffer, sizeof(buffer)); n = read(sock,buffer, sizeof(buffer)); if (n <= 0) { pthread_mutex_destroy(&queueLock); // signal the result thread to terminate sem_post(sem_match); errorSocket("ERROR reading from socket", sock); } // store the file name and the block count file_name = strtok(buffer, ","); strcpy(file_name_temp, file_name); if (debug) printf("\n[server] file name: [%s]\n", file_name); file_size_char = strtok(NULL, ","); file_size = strtol(file_size_char, NULL, 10); if (debug) printf("file size: %d\n", file_size); // calculate the time consumption here struct timeval tpstart,tpend; double timeuse; gettimeofday(&tpstart,NULL); // reponse to the client n = write(sock, response, sizeof(response)); if (n <= 0) { pthread_mutex_destroy(&queueLock); // signal the result thread to terminate sem_post(sem_match); errorSocket("ERROR writting to socket", sock); } if (!storm) { FILE *fp = fopen(file_name, "w"); if (fp == NULL) { printf("File:\t%s Can Not Open To Write!\n", file_name); break; } int done = 0; // receive the data from client and store them into buffer bzero(buffer, sizeof(buffer)); while((length = recv(sock, buffer, sizeof(buffer), 0))) { if (length < 0) { printf("Recieve Data From Client Failed!\n"); break; } int remain = file_size - received_size; if (remain < BUFFER_SIZE) { length = remain; done = 1; } write_length = fwrite(buffer, sizeof(char), length, fp); if (write_length < length) { printf("File:\t Write Failed!\n"); break; } bzero(buffer, sizeof(buffer)); if (done) { if (debug) printf("file size full\n"); break; } received_size += length; } // print out time comsumption gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;// notice, should include both s and us // printf("used time:%fus\n",timeuse); printf("receive used time:%fms\n",timeuse / 1000); if (debug) printf("[server] Recieve Finished!\n\n"); // finished fclose(fp); } // below is storm mode else { // in storm mode, don't save img into disk int offset = 0; char* img = new char[file_size]; int done = 0; // receive the data from server and store them into buffer bzero(buffer, sizeof(buffer)); while((length = recv(sock, buffer, sizeof(buffer), 0))) { if (length < 0) { printf("Recieve Data From Client Failed!\n"); break; } int remain = file_size - offset; if (remain < BUFFER_SIZE) { length = remain; done = 1; } // copy the content into img for (int i = 0; i < length; ++i) { img[i + offset] = buffer[i]; } bzero(buffer, sizeof(buffer)); if (done) { if (debug) printf("offset: %d\n", offset + remain); if (debug) printf("file size full\n"); break; } offset += length; // if (debug) printf("offset: %d\n", offset); } // print out time comsumption gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;// notice, should include both s and us // printf("used time:%fus\n",timeuse); printf("receive used time:%fms\n",timeuse / 1000); if (debug) printf("[server] Recieve Finished!\n\n"); // send request to spout if (debug) printf("Now try to connect the spout\n"); int sockfd, ret; int spoutPort = 9878; struct sockaddr_in spout_addr; struct hostent *spout; struct in_addr ipv4addr; char buf_spout[100]; char* spout_IP; const int len = spoutIP.length(); spout_IP = new char[len+1]; strcpy(spout_IP, spoutIP.c_str()); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { printf("ERROR opening socket\n"); return; } inet_pton(AF_INET, spout_IP, &ipv4addr); spout = gethostbyaddr(&ipv4addr, sizeof(ipv4addr), AF_INET); if (debug) printf("\n[server] Spout address: %s\n", spout_IP); if (spout == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &spout_addr, sizeof(spout_addr)); spout_addr.sin_family = AF_INET; bcopy((char *)spout->h_addr, (char *)&spout_addr.sin_addr.s_addr, spout->h_length); spout_addr.sin_port = htons(spoutPort); while (connect(sockfd,(struct sockaddr *) &spout_addr, sizeof(spout_addr)) < 0) { printf("The spout is not available now, wait a while and reconnect\n\n"); usleep(100000); // sleep 100ms } printf("[server] Get connection to spout\n"); bzero(buf_spout, sizeof(buf_spout)); sprintf(buf_spout, "%d", file_size); if (debug) printf("[server] send the file size\n"); ret = write(sockfd, buf_spout, sizeof(buf_spout)); if (ret < 0) { printf("error sending\n"); return; } // get the response bzero(buf_spout, sizeof(buf_spout)); if (debug) printf("[server] now wait for response\n"); ret = read(sockfd, buf_spout, sizeof(buf_spout)); if (ret < 0) { printf("error reading\n"); return; } if (debug) printf("got response: %s\n", buf_spout); if (debug) printf("[server] send the img\n"); ret = write(sockfd, img, file_size); if (ret < 0) { printf("error sending\n"); return; } if (debug) printf("ret: %d\n", ret); printf("[server] Finished transmitting image to spout\n\n"); close(sockfd); delete[] img; } // lock the queue, ensure there is only one thread modifying the queue pthread_mutex_lock(&queueLock); // store the file name to the waiting queue string file_name_string(file_name_temp); imgQueue->push(file_name_string); pthread_mutex_unlock(&queueLock); // get the address of sem_match if (sem_match == 0) { while (sem_map.find(userID) == sem_map.end()); sem_match = sem_map[userID]; } // signal the result thread to do image processing sem_post(sem_match); // pause(); } close(sock); } // below is orbit mode else { // get the id length int id_length = 1; int divisor = 10; while (sock / divisor > 0) { ++id_length; divisor *= 10; } int recv_length = BUFFER_SIZE * 4; // 4096 bytes per time char buffer[recv_length]; char *file_size_char; int file_size; int received_size = 0; if (debug) printf("\nstart receiving file\n"); // reponse to the client MsgD.send(sock, response, sizeof(response)); while (!global_stop) { received_size = 0; bzero(buffer, sizeof(buffer)); // get the file info from client n = MsgD.recv(sock, buffer, 100); if (n < 0) { pthread_mutex_destroy(&queueLock); // signal the result thread to terminate sem_post(sem_match); errorSocket("ERROR reading from socket", sock); return; } file_name = strtok(buffer, ","); strcpy(file_name_temp, file_name); printf("\n[server] file name: [%s]\n", file_name); file_size_char = strtok(NULL, ","); file_size = strtol(file_size_char, NULL, 10); printf("[server] file size: %d\n", file_size); // calculate the time consumption here struct timeval tpstart,tpend; double timeuse; gettimeofday(&tpstart,NULL); // reponse to the client MsgD.send(sock, response, sizeof(response)); // local mode if (!storm) { FILE *fp = fopen(file_name, "w"); if (fp == NULL) { printf("File:\t[%s] Can Not Open To Write!\n", file_name); } // receive the data from server and store them into buffer while(1) { bzero(buffer, sizeof(buffer)); n = MsgD.recv(sock, buffer, sizeof(buffer)); if (n <= 0) { pthread_mutex_destroy(&queueLock); // signal the result thread to terminate sem_post(sem_match); errorSocket("ERROR reading from socket", sock); } if (file_size - received_size <= recv_length) { int remain = file_size - received_size; write_length = fwrite(buffer, sizeof(char), remain, fp); if (write_length < remain) { printf("File:\t Write Failed!\n"); break; } break; } write_length = fwrite(buffer, sizeof(char), recv_length, fp); if (write_length < recv_length) { printf("File:\t Write Failed!\n"); break; } received_size += recv_length; } // print out time comsumption gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;// notice, should include both s and us // printf("used time:%fus\n",timeuse); printf("receive used time:%fms\n",timeuse / 1000); printf("[server] Recieve Finished!\n\n"); // finished fclose(fp); } // below is storm mode else { // in storm mode, don't save img into disk char* img = new char[file_size]; int offset = 0; // receive the data from client and store them into buffer while(1) { bzero(buffer, sizeof(buffer)); n = MsgD.recv(sock, buffer, sizeof(buffer)); if (n <= 0) { pthread_mutex_destroy(&queueLock); // signal the result thread to terminate sem_post(sem_match); errorSocket("ERROR reading from socket", sock); } if (file_size - received_size <= recv_length) { int remain = file_size - received_size; // copy the content into img for (int i = 0; i < remain; ++i) { img[i + offset] = buffer[i]; } break; } // copy the content into img for (int i = 0; i < recv_length; ++i) { img[i + offset] = buffer[i]; } offset += recv_length; received_size += recv_length; } // print out time comsumption gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;// notice, should include both s and us // printf("used time:%fus\n",timeuse); printf("receive used time:%fms\n",timeuse / 1000); if (debug) printf("[server] Recieve Finished!\n\n"); // send request to spout if (debug) printf("Now try to connect the spout\n"); int sockfd, ret; int spoutPort = 9878; struct sockaddr_in spout_addr; struct hostent *spout; struct in_addr ipv4addr; char buf_spout[100]; char* spout_IP; const int len = spoutIP.length(); spout_IP = new char[len+1]; strcpy(spout_IP, spoutIP.c_str()); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { printf("ERROR opening socket\n"); return; } inet_pton(AF_INET, spout_IP, &ipv4addr); spout = gethostbyaddr(&ipv4addr, sizeof(ipv4addr), AF_INET); if (debug) printf("\n[server] Spout address: %s\n", spout_IP); if (spout == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &spout_addr, sizeof(spout_addr)); spout_addr.sin_family = AF_INET; bcopy((char *)spout->h_addr, (char *)&spout_addr.sin_addr.s_addr, spout->h_length); spout_addr.sin_port = htons(spoutPort); while (connect(sockfd,(struct sockaddr *) &spout_addr, sizeof(spout_addr)) < 0) { printf("The spout is not available now, wait a while and reconnect\n\n"); usleep(100000); // sleep 100ms } printf("[server] Get connection to spout\n"); bzero(buf_spout, sizeof(buf_spout)); sprintf(buf_spout, "%d", file_size); if (debug) printf("[server] send the file size\n"); ret = write(sockfd, buf_spout, sizeof(buf_spout)); if (ret < 0) { printf("error sending\n"); return; } // get the response bzero(buf_spout, sizeof(buf_spout)); if (debug) printf("[server] now wait for response\n"); ret = read(sockfd, buf_spout, sizeof(buf_spout)); if (ret < 0) { printf("error reading\n"); return; } if (debug) printf("got response: %s\n", buf_spout); if (debug) printf("[server] send the img\n"); ret = write(sockfd, img, file_size); if (ret < 0) { printf("error sending\n"); return; } if (debug) printf("ret: %d\n", ret); printf("[server] Finished transmitting image to spout\n\n"); close(sockfd); delete[] img; } // lock the queue, ensure there is only one thread modifying the queue pthread_mutex_lock(&queueLock); // store the file name to the waiting queue string file_name_string(file_name_temp); imgQueue->push(file_name_string); pthread_mutex_unlock(&queueLock); // get the address of sem_match if (sem_match == 0) { while (sem_map.find(userID) == sem_map.end()); sem_match = sem_map[userID]; } // signal the result thread to do image processing sem_post(sem_match); } } delete(imgQueue); printf("[server] Connection closed. --- transmit\n\n"); // pthread_exit(NULL); //terminate calling thread! return; }
void getOutput( char* input, char* output, unordered_map<string, MSVS> & mp ){ unordered_map<string, vector<int> > mp_arr; // key is seqname(accession number), map is block indices set<string> setKeys; set<string> set_gene_details; map<string, double > mp_gene_count; // key is geneID + transciptID , value is count ifstream in; ofstream out; int i, index_num_mapping, num_mapping, index_mapping_list, index_host_read ; string titles, cigar, len_M1, len_N, len_M2, genome_seqname, readLine, gene_count_output, gene_details_output; // 11M101N40M , len_M1 = 11, len_N = 101, len_M2 = 40 vector<string> vecColumns, vecSamColumns; int start, end, found, pos, pos_M1, index_read_length, read_length; stringstream ss; in.open( input ); out.open( output ); for( auto it = mp.begin() ; it != mp.end() ; ++it ){ vector<int> vec_empty; mp_arr.insert( make_pair( it->first, vec_empty ) ); fillArr( mp_arr[ it->first ], it->second ); //cout << "seqname is :" << it->first << endl; //cout << "arr size is :" << mp_arr[ it->first ].size() << endl; } // read titles getline( in, titles ); vecColumns = split( titles, "\t"); index_host_read = getIndexOfColumn( vecColumns, "host_read" ); // MG00HS08:584:C5YUBACXX:6:1309:7776:84081_1:N:0:CAGATC index_num_mapping = getIndexOfColumn( vecColumns, "#Tophat_Mapping" ); index_read_length = getIndexOfColumn( vecColumns, "host_read_length" ); index_mapping_list = index_num_mapping + 1; out << titles << "\t#Gene_match\t#host_gene\thost_genes_details" << endl; int count = 0; while( getline( in, readLine ) ) { out << readLine ; vecColumns = split( readLine, "\t" ); num_mapping = atoi ( vecColumns[ index_num_mapping ].c_str() ); if( num_mapping != 1 ){ // not unique and thus annotation not needed out << "\t\\\t\\\t\\" << endl; continue; } setKeys.clear(); mp_gene_count.clear(); set_gene_details.clear(); gene_count_output = "\\"; gene_details_output = "\\"; // vecColumns[ index_mapping_list ] : 73,gi|568815578|ref|NC_000020.11|,8174332,50,101M# vecSamColumns = split( vecColumns[ index_mapping_list ] , "," ) ; int size = vecSamColumns.size(); cigar = vecSamColumns[ size - 1 ]; size = cigar.length(); cigar = cigar.substr( 0, size - 1 ); // remove # from 101M found = cigar.find_first_not_of("0123456789MN"); if( found == string::npos ){ pos = cigar.find_first_of ( 'M' ); len_M1 = cigar.substr( 0, pos ); pos_M1 = pos; pos = cigar.find_first_of ( 'N', pos_M1 + 1 ); if( pos == string::npos ) len_N = ""; else{ len_N = cigar.substr( pos_M1 + 1, pos - pos_M1 ); if ( cigar.find_first_of( 'N', pos + 1 ) != string::npos ) { // more than one N in CIGAR out << "\t\\\t\\\t\\" << endl; continue; } else len_M2 = cigar.substr( pos + 1, cigar.size() - 1 - pos ); } } else { // not annotate out << "\t\\\t\\\t\\" << endl; continue; } start = atoi( &vecSamColumns[2][0] ); end = start + atoi( &len_M1[0] ) - 1; genome_seqname = getSeqname( vecSamColumns[ 1 ] ); read_length = atoi( vecColumns[ index_read_length ].c_str() ) ; getKeys ( start, end, mp_arr[ genome_seqname ], setKeys ); getGeneCount( start, end, setKeys, mp[ genome_seqname ], mp_gene_count, set_gene_details, vecColumns[index_host_read] ); if ( len_N != "" ){ start = end + atoi( &len_N[0] ) + 1; end = start + atoi( &len_M2[0] ) -1; setKeys.clear(); getKeys ( start, end, mp_arr[ genome_seqname ], setKeys ); getGeneCount( start, end, setKeys, mp[ genome_seqname ], mp_gene_count, set_gene_details, vecColumns[index_host_read] ); } if( mp_gene_count.size() ){ vector<PAIR> vec_gene_count( mp_gene_count.begin(), mp_gene_count.end() ); sort( vec_gene_count.begin(), vec_gene_count.end(), GeneCountLess() ); //print gene names and count ss.str(""); for( vector<PAIR>::iterator p = vec_gene_count.begin(); p != vec_gene_count.end(); ++p ) ss << p->first << "(" << p->second << ")$" ; gene_count_output = ss.str(); ss.str(""); for( set<string>::iterator p = set_gene_details.begin(); p != set_gene_details.end(); ++p ) ss << *p ; gene_details_output = ss.str(); } out << "\t" << mp_gene_count.size() << "\t" + gene_count_output + "\t" + gene_details_output << endl; } in.close(); out.close(); }
int main() { sqlite3 *db; sqlite3_stmt *max_tx_id_stmt, *address_stmt, *details_stmt, *out_stmt; TX *tx_struct; ADDRESS *address_struct; ADDRESS *out_address_struct; unsigned int max_tx_id; if (sqlite3_open("../blockchain/blockchain.sqlite", &db) == SQLITE_OK) { // Prepare queries sqlite3_prepare_v2(db, "SELECT MAX(tx_id) FROM tx", -1, &max_tx_id_stmt, NULL); sqlite3_prepare_v2(db, "SELECT txout.address FROM txin LEFT JOIN txout ON (txout.txout_id = txin.txout_id) WHERE txin.tx_id = ?", -1, &address_stmt, NULL); sqlite3_prepare_v2(db, "SELECT blocks.time, tx.tx_hash FROM tx LEFT JOIN blocks ON (tx.block_id = blocks.block_id) WHERE tx.tx_id = ?", -1, &details_stmt, NULL); sqlite3_prepare_v2(db, "SELECT txout.address, txout.txout_value FROM txout LEFT JOIN txin ON (txin.txout_id = txout.txout_id) WHERE txout.tx_id = ?", -1, &out_stmt, NULL); if (sqlite3_step(max_tx_id_stmt) == SQLITE_ROW) { max_tx_id = (unsigned int)sqlite3_column_int64(max_tx_id_stmt, 0); cerr << "Number of transactions in the DB: " << max_tx_id << endl; } else { cerr << "Error: unable to get transactions." << endl; return 1; } sqlite3_finalize(max_tx_id_stmt); for (unsigned int tx_id = 1; tx_id <= max_tx_id; tx_id++) { if (tx_id % 1000000 == 0) { cerr << "Transaction: " << tx_id << endl; } sqlite3_reset(address_stmt); sqlite3_reset(details_stmt); sqlite3_reset(out_stmt); sqlite3_clear_bindings(address_stmt); sqlite3_clear_bindings(details_stmt); sqlite3_clear_bindings(out_stmt); // Bind parameters to compiled queries if (sqlite3_bind_int(address_stmt, 1, (int)tx_id) != SQLITE_OK) { cerr << "Error: unable to bind tx_id = " << tx_id << " to address_stmt." << endl; return 1; } if (sqlite3_bind_int(details_stmt, 1, (int)tx_id) != SQLITE_OK) { cerr << "Error: unable to bind tx_id = " << tx_id << " to details_stmt." << endl; return 1; } if (sqlite3_bind_int(out_stmt, 1, (int)tx_id) != SQLITE_OK) { cerr << "Error: unable to bind tx_id = " << tx_id << " to out_stmt." << endl; return 1; } int address_status = sqlite3_step(address_stmt); // Skip "generated-only" transactions if (address_status == SQLITE_DONE) { continue; } tx_struct = new TX(); tx_struct->tx_id = tx_id; transactions[tx_id] = tx_struct; while (address_status == SQLITE_ROW) { string address = string(reinterpret_cast<const char *>(sqlite3_column_text(address_stmt, 0))); if (addresses.find(address) == addresses.end()) { address_struct = new ADDRESS(); address_struct->address = address; addresses[address] = address_struct; } else { address_struct = addresses[address]; } tx_struct->inputs.insert(address_struct); address_struct->txs_out.insert(tx_struct); if (sqlite3_step(details_stmt) == SQLITE_ROW) { unsigned int timestamp = (unsigned int)sqlite3_column_int64( details_stmt, 0); string tx_hash = string(reinterpret_cast<const char *>(sqlite3_column_text(details_stmt, 1))); tx_struct->tx_hash = tx_hash; tx_struct->timestamp = timestamp; } while (sqlite3_step(out_stmt) == SQLITE_ROW) { string out_address = string(reinterpret_cast<const char *>(sqlite3_column_text(out_stmt, 0))); uint64_t amount = (uint64_t)sqlite3_column_int64(out_stmt, 1); if (addresses.find(out_address) == addresses.end()) { out_address_struct = new ADDRESS(); out_address_struct->address = out_address; addresses[out_address] = out_address_struct; } else { out_address_struct = addresses[out_address]; } tx_struct->recipients.push_back(out_address_struct); tx_struct->amounts.push_back(amount); out_address_struct->txs_in.insert(tx_struct); } address_status = sqlite3_step(address_stmt); } } } else { cerr << "Failed to open blockchain DB!" << endl; } sqlite3_finalize(address_stmt); sqlite3_finalize(details_stmt); sqlite3_finalize(out_stmt); sqlite3_close(db); cerr << "Loading clusters... "; io::CSVReader<2> in("../clusterizer/clusters.csv"); in.read_header(io::ignore_extra_column, "address", "cluster"); string address; unsigned int cluster; while (in.read_row(address, cluster)) { try { clusters[addresses.at(address)] = cluster; } catch (std::out_of_range& e) { // Do nothing. } } cerr << "done." << endl; int server_fd = server_start_listen(); cerr << "Server started." << endl; if (server_fd == -1) { cerr << "An error occured. Closing program." << endl; return 1; } mainloop(server_fd); return 0; }
// ==================================================== // Integral constraint // ==================================================== integral_constraint mk_integral_constraint(Enode * const e, unordered_map<string, flow> const & flow_map) { // nra_solver::inform: (integral 2 0 time_9 v_9_0 v_9_t x_9_0 x_9_t) Enode const * tmp = e->getCdr(); unsigned flow_id = tmp->getCar()->getValue(); tmp = tmp->getCdr(); Enode * const time_0 = tmp->getCar(); tmp = tmp->getCdr(); Enode * const time_t = tmp->getCar(); tmp = tmp->getCdr(); string key = string("flow_") + to_string(flow_id); auto const it = flow_map.find(key); if (it == flow_map.end()) { throw logic_error(key + " is not in flow_map. Failed to create integral constraint"); } flow const & _flow = it->second; vector<Enode *> const & flow_vars = _flow.get_vars(); vector<Enode *> const & flow_odes = _flow.get_odes(); vector<Enode *> vars_0, vars_t, pars_0, pars_t; vector<Enode *> par_lhs_names; vector<pair<Enode *, Enode *>> odes; for (unsigned i = 0; i < flow_vars.size(); i++) { Enode * const var_0 = tmp->getCar(); tmp = tmp->getCdr(); Enode * const var_t = tmp->getCar(); tmp = tmp->getCdr(); Enode * const ode_var = flow_vars[i]; Enode * const ode_rhs = flow_odes[i]; if (ode_rhs->isConstant() && ode_rhs->getValue() == 0.0) { // Parameter pars_0.push_back(var_0); pars_t.push_back(var_t); par_lhs_names.push_back(ode_var); } else { // Variable vars_0.push_back(var_0); vars_t.push_back(var_t); odes.emplace_back(ode_var, ode_rhs); } } if (!tmp->isEnil()) { DREAL_LOG_FATAL << "We found a problem in handling: " << e; DREAL_LOG_FATAL << "You provided " << flow_vars.size() << " differential equation(s) in the system:"; for (unsigned i = 0; i < flow_vars.size(); i++) { DREAL_LOG_FATAL << "\t" << "d/dt[" << flow_vars[i] << "] = " << flow_odes[i]; } DREAL_LOG_FATAL << "However, there are " << (flow_vars.size() + tmp->getSize()) << " pairs of variables shown:"; tmp = e->getCdr()->getCdr()->getCdr()->getCdr(); while (!tmp->isEnil()) { DREAL_LOG_FATAL << "\t" << tmp->getCar() << ", " << tmp->getCdr()->getCar(); tmp = tmp->getCdr()->getCdr(); } abort(); } return integral_constraint(e, flow_id, time_0, time_t, vars_0, pars_0, vars_t, pars_t, par_lhs_names, odes); }
void merge_both_alignments(vector<reference_index>& refindex, string sam_output_name, fragment_alignment& alignment_first, fragment_alignment& alignment_second, ofstream& fp_sam, ofstream& fp_detail, ofstream& fp_fastq, unordered_map<string, int>& umap) { if(alignment_first.gaps != 0 || alignment_second.gaps != 0) { if(DEBUG == 99) fp_detail << "\n****************************************GAPS FOUND****************************************" << endl << endl; return; } fragment_alignment pointer_first; fragment_alignment pointer_second; int first_start_index, second_start_index; int first_last_index, second_last_index; if(alignment_first.ref_start < alignment_second.ref_start) { first_start_index = alignment_first.ref_start; first_last_index = alignment_second.ref_start; second_start_index = alignment_first.ref_end; second_last_index = alignment_second.ref_end; pointer_first = alignment_first; pointer_second = alignment_second; } else { first_start_index = alignment_second.ref_start; first_last_index = alignment_first.ref_start; second_start_index = alignment_second.ref_end; second_last_index = alignment_first.ref_end; pointer_first = alignment_second; pointer_second = alignment_first; } if(first_last_index > second_start_index) return; if(second_last_index < second_start_index) return; /* cout << "\nFind Problems in Merging" << endl; cout << "First start index = " << first_start_index << endl; cout << "First last index = " << first_last_index << endl; cout << "Second start index = " << second_start_index << endl; cout << "Second last index = " << second_last_index << endl; */ //linking reference base int ref_start = 0, read_start = 0; consensus *head_node = new consensus; head_node->ref_ind = 0; head_node->refch = refindex[0].ref.at(0); head_node->read_ind = -1; head_node->readch = '\0'; head_node->quality = 0; head_node->next = NULL; head_node->up = NULL; head_node->down = NULL; consensus *current_node = head_node; for(int i = 1; i < refindex[0].ref.length(); i++) { consensus *next_node = new consensus; next_node->ref_ind = i; next_node->refch = refindex[0].ref.at(i); next_node->read_ind = -1; next_node->readch = '\0'; next_node->quality = 0; next_node->next = NULL; next_node->up = NULL; next_node->down = NULL; current_node->next = next_node; current_node->up = next_node; current_node = current_node->next; } fp_detail << endl << "Testing Reference Linked List" << endl; current_node = head_node; while(current_node != NULL) { fp_detail << "" << current_node->refch; current_node = current_node->next; } fp_detail << endl << "Reference Linked List Printed" << endl << endl; //first read alignment consensus *previous_node = NULL; int current_ref_ind = -1; int current_read_ind = -1; current_node = head_node; for(int i = 0; i < pointer_first.alignment.size(); i++) { if(pointer_first.alignment[i].first != '-') current_ref_ind += 1; if(pointer_first.alignment[i].second != '-') current_read_ind += 1; if(current_ref_ind == current_node->ref_ind) { current_node->read_ind = current_read_ind; current_node->readch = '-'; if(pointer_first.alignment[i].second != '-') { current_node->readch = pointer_first.alignment[i].second; current_node->quality = pointer_first.quality.at(i); } previous_node = current_node; current_node = current_node->next; } else { consensus *next_node = new consensus; next_node->ref_ind = current_ref_ind; next_node->refch = '-'; next_node->read_ind = current_read_ind; next_node->readch = pointer_first.alignment[i].second; current_node->quality = pointer_first.quality.at(i); next_node->next = NULL; next_node->up = NULL; next_node->down = NULL; next_node->next = current_node; if(previous_node != NULL) previous_node->next = next_node; previous_node = next_node; } } fp_detail << endl << "Testing Read_One Linked List" << endl; current_node = head_node; while(current_node != NULL) { fp_detail << "" << current_node->readch; current_node = current_node->next; } fp_detail << endl << "Read_One Linked List Printed" << endl << endl; //second read alignment current_node = head_node; while(current_node->ref_ind < first_last_index) { previous_node = current_node; current_node = current_node->next; } current_ref_ind = first_last_index - 1; current_read_ind = -1; for(int i = 0; i < pointer_second.alignment.size(); i++) { if(pointer_second.alignment[i].first != '-') current_ref_ind += 1; if(pointer_second.alignment[i].second != '-') current_read_ind += 1; if(current_ref_ind == current_node->ref_ind) { if(current_node->quality < pointer_second.quality.at(i)) { current_node->read_ind = current_read_ind; current_node->readch = pointer_second.alignment[i].second; current_node->quality = pointer_second.quality.at(i); } else { if(current_node->readch == '\0') { current_node->read_ind = current_read_ind; current_node->readch = '-'; } } previous_node = current_node; current_node = current_node->next; } else { consensus *next_node = new consensus; next_node->ref_ind = current_ref_ind; next_node->refch = '-'; next_node->read_ind = current_read_ind; next_node->readch = pointer_second.alignment[i].second; current_node->quality = pointer_second.quality.at(i); next_node->next = NULL; next_node->up = NULL; next_node->down = NULL; next_node->next = current_node; previous_node->up = next_node; previous_node = next_node; } } fp_detail << endl << "Testing Read_Two Linked List" << endl; current_node = head_node; while(current_node != NULL) { fp_detail << "" << current_node->readch; current_node = current_node->next; } fp_detail << endl << "Read_Two Linked List Printed" << endl << endl; /* consensus *head_node = new consensus; ref_start = head_node->ref_ind = pointer_first.ref_start; head_node->refch = pointer_first.alignment[0].first; read_start = head_node->read_ind = pointer_first.read_start; head_node->readch = pointer_first.alignment[0].second; head_node->next = NULL; head_node->up = NULL; head_node->down = NULL; consensus *current_node = head_node; for(int i = 1; i < pointer_first.alignment.size(); i++) { consensus *next_node = new consensus; if(pointer_first.alignment[i].first != '-') ref_start += 1; if(pointer_first.alignment[i].second != '-') read_start += 1; next_node->ref_ind = ref_start; next_node->refch = pointer_first.alignment[0].first; next_node->read_ind = read_start; next_node->readch = pointer_first.alignment[0].second; next_node->next = NULL; next_node->up = NULL; next_node->down = NULL; current_node->next = next_node; current_node = current_node->next; } */ vector<pair<char, char> > alignment; vector<char> quality; int read_index_first, read_index_second; int ref_index_first, ref_index_second; int i, k, l, x; int read_end, ref_end; int match = 0, gaps = 0, mismatch = 0; read_index_first = pointer_first.read_start; ref_index_first = pointer_first.ref_start; read_index_second = pointer_second.read_start; ref_index_second = pointer_second.ref_start; read_start = read_index_first; ref_start = ref_index_first; for(i = 0; ref_index_first < first_last_index && i < pointer_first.alignment.size(); i++) { alignment.push_back(pointer_first.alignment[i]); //if(pointer_first.alignment[i].second != '-') { quality.push_back(pointer_first.quality.at(read_index_first)); read_index_first += 1; } //if(pointer_first.alignment[i].first != '-') { ref_index_first += 1; } } //print_vector_alignment(alignment); for(k = i, l = 0; ref_index_first < second_start_index && k < pointer_first.alignment.size(); k++, l++) { assert(ref_index_first == ref_index_second); assert(pointer_first.alignment[k].first == pointer_second.alignment[l].first); { if(pointer_first.quality.at(read_index_first) > pointer_second.quality.at(read_index_second)) { alignment.push_back(pointer_first.alignment[k]); quality.push_back(pointer_first.quality.at(read_index_first)); } else { alignment.push_back(pointer_second.alignment[l]); quality.push_back(pointer_second.quality.at(read_index_second)); } } ref_index_first += 1; ref_index_second += 1; read_index_first += 1; read_index_second += 1; } //cout << endl << endl; //print_vector_alignment(alignment); for(x = l; ref_index_second < second_last_index && x < pointer_second.alignment.size(); x++) { alignment.push_back(pointer_second.alignment[x]); //if(pointer_first.alignment[i].second != '-') { quality.push_back(pointer_second.quality.at(read_index_second)); read_index_second += 1; } //if(pointer_first.alignment[i].first != '-') { ref_index_second += 1; } } read_end = read_index_second; ref_end = ref_index_second; string alignment_quality(quality.begin(), quality.end()); fragment_alignment final_alignment_info; vector<string> final_result; vector<char> read; for(int k = 0; k < alignment.size(); k++) { final_alignment_info.alignment.push_back(alignment[k]); if(alignment[k].first == alignment[k].second && alignment[k].first != '-') match += 1; if(alignment[k].first != alignment[k].second && alignment[k].first != '-') mismatch += 1; if(alignment[k].first == '-' || alignment[k].second == '-') gaps += 1; read.push_back(alignment[k].second); } //cout << endl << endl; string readseq(read.begin(), read.end()); //print_vector_alignment(alignment); final_alignment_info.total_len = alignment.size(); final_alignment_info.identity_match = match; final_alignment_info.ref_start = ref_start; final_alignment_info.read_start = read_start; final_alignment_info.ref_end = ref_end;//total_ref_ind final_alignment_info.read_end = read_end;//total_read_ind final_alignment_info.ref_ind = 0; final_alignment_info.read_dir = 1; final_alignment_info.gaps = gaps; final_alignment_info.mismatches = mismatch; final_alignment_info.quality = alignment_quality; sam_format(final_alignment_info, refindex, readseq, sam_output_name, final_result, fp_detail); fp_sam << final_result[0]; for(int k = 1; k < final_result.size(); k++) { fp_sam << "\t" << final_result[k]; //cout << i << ": " << output[k] << endl; } fp_sam << endl; /* fp_fastq << "> " << final_result[0] << endl; fp_fastq << final_result[9] << endl; fp_fastq << "+" << endl; fp_fastq << final_result[10] << endl; */ if(umap.find(final_result[5]) == umap.end()) { umap[final_result[5]] = 1; } else { umap[final_result[5]] += 1; return; } //fp_fastq << ">" << final_result[0] << "|CIGAR=" << final_result[5] << endl; //fp_fastq << final_result[9] << endl; fp_fastq << final_result[0] << endl; fp_fastq << final_result[5] << endl; fp_fastq << final_result[9] << endl; print_alignment(final_alignment_info.alignment, refindex[0].ref, readseq, final_alignment_info.ref_start, 0, 1, final_alignment_info, true, fp_detail); if(DEBUG == 99) { fp_detail << "reference = " << refindex[0].ref.substr(final_alignment_info.ref_start, 80) << endl; fp_detail << "for_read = " << readseq.substr(0, 80) << endl << endl; } assert(alignment_quality.length() == readseq.length()); return; }
// Realizeaza toate actiunile necesare pentru indeplinirea comenzii getfile. int getfile(char * file, char * client) { unordered_map<string, Client *>::const_iterator it = clients.find(client); // Daca nu se afla in vector clientul de la care doresc un fisier, adica // daca nu s-a dat comanda infoclient pentru el la un moment anterior, // afisez eroarea -1. if (it == clients.end()) { print_log(CONNECTION_ERR); return -1; } // Formez o comanda cu structura: getfile <nume_fisier> pentru client. char command[BUFLEN]; char buffer[BUFLEN]; strcpy(command, "getfile "); strcat(command, file); if (it->second->socket == -1) { // Creez socket pentru comunicarea cu celalalt client from_fd = socket(AF_INET, SOCK_STREAM, 0); if (from_fd < 0) { return -1; } struct sockaddr_in transf_addr; transf_addr.sin_family = AF_INET; transf_addr.sin_port = htons(it->second->port); inet_aton(it->second->ip, &transf_addr.sin_addr); // Conectez cei doi clienti. if (connect(from_fd, (struct sockaddr*) &transf_addr, sizeof(transf_addr)) < 0) { print_log(CONNECTION_ERR); close(from_fd); return -1; } it->second->socket = from_fd; } else { from_fd = it->second->socket; } // Trimit comanda si primesc raspunsul. if (send(from_fd, command, strlen(command), 0) <= 0 || recv(from_fd, buffer, sizeof(buffer), 0) <= 0) { print_log(CONNECTION_ERR); it->second->socket = -1; return -1; } // Verific ca nu am primit eroare. if (buffer[0] == '-') { print_log(buffer); return -1; } struct stat buf; path = folder; path += "/"; path += file; // Verific daca fisierul este duplicat if (stat(path.c_str(), &buf) >= 0) { printf("%s", DUPLICATE_FILE); write(logfd, DUPLICATE_FILE, strlen(DUPLICATE_FILE)); printf("Suprascriere? y/n?\n"); char raspuns[10]; fgets(raspuns, 10, stdin); if ((strcmp(raspuns, "y\n") != 0)){ write(logfd, "Comanda anulata\n\n", strlen("Comanda anulata\n\n")); return 0; } write(logfd, "Suprascris\n\n", strlen("Suprascris\n\n")); } // Transferul poate incepe. receiving_file = true; writefd = open(path.c_str(), O_CREAT|O_TRUNC|O_WRONLY); return 0; }
//=========================================================================== void Dim::endpointCancelQuery(int cancelId) { auto it = s_tasks.find(cancelId); if (it != s_tasks.end()) GetAddrInfoExCancel(&it->second.cancel); }
int main (int argc, char** argv) { log.execname (basename (argv[0])); log << "starting" << endl; vector<string> args (&argv[1], &argv[argc]); signal_action (SIGINT, signal_handler); signal_action (SIGTERM, signal_handler); if (args.size() > 2) usage(); string host = get_cix_server_host (args, 0); in_port_t port = get_cix_server_port (args, 1); log << to_string (hostinfo()) << endl; try { log << "connecting to " << host << " port " << port << endl; client_socket server (host, port); log << "connected to " << to_string (server) << endl; for (;;) { /* string line; getline (cin, line); if (cin.eof()) throw cix_exit(); if (SIGINT_throw_cix_exit) throw cix_exit(); log << "command " << line << endl; const auto& itor = command_map.find (line); cix_command cmd = itor == command_map.end() ? CIX_ERROR : itor->second; */ string line; getline (cin, line); if (cin.eof()) throw cix_exit(); if (SIGINT_throw_cix_exit) throw cix_exit(); log << "command " << line << endl; //parsing command size_t comFirst = line.find_first_not_of (" "); size_t comLast = line.find_first_of(" ", comFirst); string cmmnd = line.substr(comFirst, comLast - comFirst); string filename; bool fileStatus = false; //change to false for put get w/o args //if comLast == line.end(); if(line.size() > 2 && cmmnd != "exit" && cmmnd != "help"){ if (line.find_first_not_of(" ", comLast) != string::npos){ //if (comLast == line.size()){ size_t filenFirst = line.find_first_not_of (" ", comLast); size_t filenLast = line.find_last_not_of(" "); filename = line.substr(filenFirst, filenLast - filenFirst + 1); if (filename.size() != 0) fileStatus = true; } } const auto& itor = command_map.find (cmmnd); cix_command cmd = itor == command_map.end() ? CIX_ERROR : itor->second; switch (cmd) { case CIX_EXIT: throw cix_exit(); break; case CIX_HELP: cix_help(); break; case CIX_LS: cix_ls (server); break; case CIX_PUT: if (fileStatus) cix_put (server, filename); else log << "PUT UNSUCCESSFULL: NO FILENAME SPECIFIED" << endl; break; case CIX_GET: if (fileStatus) cix_get (server, filename); else log << "GET UNSUCCESSFUL: NO FILENAME SPECIFIED" << endl; break; case CIX_RM: cix_rm (server, filename); break; default: log << line << ": invalid command" << endl; break; } } }catch (socket_error& error) { log << error.what() << endl; }catch (cix_exit& error) { log << "caught cix_exit" << endl; } log << "finishing" << endl; return 0; }
void g(unordered_map<int, int>& mp, int i) { if (mp.find(i) == mp.end()) { mp[i] = 0; } mp[i]++; }
T* find(const Str& name) const { auto it = data.find(name); return (it != data.end() && it->second) ? it->second->value : nullptr; }
// query the status of the download/upload files. void* status_thread(void *arg) { //initialize the prev size. pthread_mutex_lock(&download_mutex); //update the prev to the current in the download array. for(unordered_map<string, record>::iterator iter = download_array.begin(); iter != download_array.end(); ++ iter) iter->second.prev = iter->second.current; pthread_mutex_unlock(&download_mutex); //update the prev to the current in the upload array. pthread_mutex_lock(&upload_mutex); for(unordered_map<string, record>::iterator iter = upload_array.begin(); iter != upload_array.end(); ++ iter) iter->second.prev = iter->second.current; pthread_mutex_unlock(&upload_mutex); sleep_us(500000); bool empty = false; int move_up = 0; while(status_query) { for(int i = 0; i< move_up; ++i) { printf("\033[1A"); printf("\033[K"); } move_up = 0; string result; char temp[MAXLINE]; pthread_mutex_lock(&download_mutex); if(download_array.empty()) empty = true; // show the progress of downloading files. for(unordered_map<string, record>::iterator iter= download_array.begin(); iter != download_array.end(); ++ iter) { get_progress("downloading", iter->first.c_str(), iter->second.prev, iter->second.current, iter->second.sum, temp); iter->second.prev = iter->second.current; result += temp; ++move_up; } pthread_mutex_unlock(&download_mutex); pthread_mutex_lock(&upload_mutex); if(empty && upload_array.empty()) empty = true; else empty = false; // show the progress of uploading files. for(unordered_map<string, record>::iterator iter = upload_array.begin(); iter != upload_array.end(); ++ iter) { get_progress("uploading", iter->first.c_str(), iter->second.prev, iter->second.current, iter->second.sum, temp); iter->second.prev = iter->second.current; result += temp; ++move_up; } pthread_mutex_unlock(&upload_mutex); if(empty) break; printf("%s", result.c_str()); fflush(stdout); sleep_us(500000); } if(empty) { printf("no download/upload files"); status_query = false; } return NULL; }
void gao_same_list(char * xxx_train_item_item){ fstream item_item_file(xxx_train_item_item); double st = clock(); LL a, b; int now = 0; vector<LL> train_items; while(item_item_file>>a>>b){ // item a, b if (now++ % SHOW == 0) cerr<<"now:\t"<<now<<"\titema:\t"<<a<<"\titemb:\t"<<b<<endl; train_items.push_back(a); train_items.push_back(b); LL ca = item_cat[a], cb = item_cat[b]; cat_cat_map[haxi(ca, cb)] = true; train_cat_item_item[ca].push_back(PLL(a, b)); train_cat_item_item[cb].push_back(PLL(b, a)); train_item_map[a] = 1; train_item_map[b] = 1; } // reduce cat's items && train_items lxunique(train_items); for (unordered_map<LL, vector<PLL> >::iterator it = train_cat_item_item.begin(); it != train_cat_item_item.end(); it++){ vector<PLL> & v = it->second; lxunique(v); } // cat_items now = 0; cerr<<"train_item num = "<<train_items.size()<<endl; for (int i = 0; i < train_items.size(); i++){ //for (int i = 0; i < 1000; i++){ LL train_item = train_items[i]; // same[train_item] LL train_item_cat = item_cat[train_item]; vector<LL> & same_cat_items = cat_items[train_item_cat]; if (now++ % 200 == 0) cerr<<"now:\t"<<now<<"\ttrain_item =\t"<<train_item<<"\tcheck items number =\t"<<same_cat_items.size()<<endl; vector<PDL> res; for(int j = 0; j < same_cat_items.size(); j++){ LL check_item = same_cat_items[j]; // train_item vs check_item // if (check_item == train_item) continue; double similar = similarity(train_item, check_item); res.push_back(PDL(similar, check_item)); } sort(res.begin(), res.end()); reverse(res.begin(), res.end()); if ((now - 1) % 200 == 0){ cerr<<"train_item = "<<train_item<<endl; for (int i = 0; i < 20; i++){ cerr<<res[i].first<<" "<<res[i].second<<endl; } } same[train_item] = res; } }
GMsg VectorBuffer::EditFeaturesFromInteraction( const unordered_map<long, feature_ptr> &features, const string &proj) { if (features.empty()) return G_NOERR; Q_ASSERT(proj == _layer->proj()); _undoStack->beginMacro(features.size() == 1 ? "Edit feature" : "Edit features"); for (unordered_map<long, feature_ptr>::const_iterator it = features.begin(); it != features.end(); it++) { long fid = it->first; feature_ptr src = it->second; Q_ASSERT(_deletedFeatures.find(fid) == _deletedFeatures.end()); src->put<value_unicode_string>( _config_app().VIEW_VECTORBUFFER_FIELD_EDITED, transcoder("utf-8").transcode("edited")); _undoStack->push(new VBCommandEditFeature(this, fid, src)); } _undoStack->endMacro(); return G_NOERR; }
int get(int key) { if (mp.find(key) == mp.end()) return -1; refresh(key); mp[key].second = --(order.end()); return mp[key].first; }
void gui_pop_up_window::draw() { if(!state.visible) return; // TODO: only redraw when necessary start_draw(); buffer.clear(); oclraster_support::get_pipeline()->start_orthographic_rendering(); // cache items for(const auto& item : items) { if(item_text_cache.find(item) == item_text_cache.end()) { const auto advance_map(fnt->compute_advance_map(item->second, 1)); const float height = (advance_map.back().x < advance_map.back().y ? (advance_map.back().y - advance_map.back().x) : (advance_map.back().x + advance_map.back().y)); item_text_cache.insert(make_pair(item, make_pair(fnt->cache_text(item->second), float2(height, 0.0f)))); } } // const float item_margin = roundf(gui_theme::point_to_pixel(3.5f)); // in pt const float2 text_margin(roundf(gui_theme::point_to_pixel(5.0f)), roundf(gui_theme::point_to_pixel(-1.5f))); // in pt const auto selected_item = pop_up_button->get_selected_item(); float total_height = margin * 2.0f; float selected_item_height = 0.0f; for(const auto& item : items) { if(item == selected_item) { selected_item_height = total_height; } total_height += ceilf(item_text_cache.at(item).second.x + item_margin * 2.0f); } // compute overlay position and size overlay_size = float2(button_size_abs.x, total_height); float2 offset(button_position_abs - float2(margin, 0.0f)); offset.y -= selected_item_height - margin; // clamp to (0, window size - overlay size) to prevent drawing outside of the window offset.clamp(float2(0.0f), float2(buffer.get_size()) - overlay_size); overlay_position = offset; // draw overlay rectangle theme->draw("pop_up_button", "overlay", overlay_position, overlay_size, false); // draw items const pnt mouse_pos(oclraster::get_event()->get_mouse_pos()); float y_offset = margin + overlay_position.y; const float4 font_color(theme->get_color_scheme().get("TEXT_INVERSE")); const float4 font_color_active(theme->get_color_scheme().get("TEXT")); for(const auto& item : items) { // auto& cached_item(item_text_cache.at(item)); const float text_height = cached_item.second.x; // text y-extent const float item_height = ceilf(text_height + item_margin * 2.0f); const float2 item_position(overlay_position.x, y_offset); const float2 item_size(button_size_abs.x, item_height); cached_item.second.y = item_height; // cache item height y_offset += item_height; if(mouse_pos.x > item_position.x && mouse_pos.x <= (item_position.x + item_size.x) && mouse_pos.y > item_position.y && mouse_pos.y <= (item_position.y + item_size.y)) { // active item theme->draw("pop_up_button", "item_active", item_position, item_size, false); fnt->draw_cached(cached_item.first.first.first, cached_item.first.first.second, (item_position + text_margin).rounded(), font_color_active); } else { fnt->draw_cached(cached_item.first.first.first, cached_item.first.first.second, (item_position + text_margin).rounded(), font_color); } } oclraster_support::get_pipeline()->stop_orthographic_rendering(); stop_draw(); }
void create_node(ID id){ if(nodes.find(id)==nodes.end()){ nodes[max_id] = new Node(max_id); max_id++; } }
T get(string key, ComputeFn computeFn) { // only allow one thread unique_lock<mutex> exclusion(this->monitor); // three possibilities if(cache.find(key) != cache.end()) { // we have the value cached, just return it } else if(started_computation.find(key) != started_computation.end()) { // another thread is computing the value, so we // must wait... while(cache.find(key) == cache.end()) result_ready.wait(exclusion); //result_ready.at(key)->notify_one(); } else { // tell other threads to wait for our computation started_computation.insert(key); // release the lock... exclusion.unlock(); // compute the value T value = computeFn(); // now we must re-lock the exclusion, update the map, and notify waiting threads exclusion.lock(); cache.insert(std::pair<string,T>(key,value)); } // update the use times long time = virtual_time++; if(id_2_last_used.find(key) != id_2_last_used.end()) { long prev_time = id_2_last_used.at(key); auto erase_at = last_used_2_id.find(prev_time); if(erase_at != last_used_2_id.end()) last_used_2_id.erase(erase_at); } id_2_last_used[key] = time; last_used_2_id[time] = key; // string remove_key; if(cache.size() > capacity) { // free the least recently used item. remove_key = last_used_2_id.begin()->second; cache.erase(remove_key); started_computation.erase(remove_key); } // release the lock T result = cache.at(key); exclusion.unlock(); result_ready.notify_all(); return result; }