/****************************** MAIN *****************************/ int main (int argc, char **argv) { int c; while (1) { static struct option long_options[] = { //These options don’t set a flag. //We distinguish them by their indices. {"help", no_argument, 0, 'h'}, {"threads", required_argument, 0, 't'}, {"name", required_argument, 0, 'n'}, {0, 0, 0, 0} }; //getopt_long stores the option index here. int option_index = 0; c = getopt_long (argc, argv, "h:t:o:p:r:",long_options, &option_index); //Detect the end of the options. if (c == -1) { break; } switch (c) { case 'h': print_help(); return 0; case 't': num_threads = atoi(optarg); break; case 'n': name = optarg; break; } } init_system(); string aux_model; string id; string idrelated; string sample; int threshold; string func = "";; string line = ""; int func_id; unordered_map<string,unsigned long>::const_iterator it_func_id; unordered_map<string,unsigned long>::const_iterator it_func_value_id; while (getline(std::cin, line)) { if(!line.empty()){ istringstream iss(line); iss >> func >> id; it_func_id = APIID.find(func); if (it_func_id != APIID.end()){ switch(it_func_id->second) { case 0: if(exist_sample(id)) { cout << "[\"Err\",\"duplicated sample\"]" << endl; }else{ iss >> sample; string ok = add_sample(id,&sample); save_sample(id,&sample); cout << ok << endl; } break; case 1: it_func_value_id = APIVALUEID.find(id); if(it_func_value_id == APIVALUEID.end()){ cout << "[\"Err\",\"missmatch function\"]" << endl; }else{ iss >> id; if(!exist_sample(id)) { cout << "[\"Err\",\"missing sample\"]" << endl; }else{ iss >> threshold; switch(it_func_value_id->second) { case 0: get_value_snps(id,threshold); break; case 1: get_value_ides(id,threshold); break; } } } break; case 3: it_func_value_id = APIVALUEID.find(id); if(it_func_value_id == APIVALUEID.end()){ cout << "[\"Err\",\"missmatch function\"]" << endl; }else{ iss >> threshold; switch(it_func_value_id->second) { case 0: get_all_values_snps(threshold); break; case 1: get_all_values_ides(threshold); break; } } break; case 2: save_system(); cout << "[\"OK\"]" << endl; break; case 4: iss >> idrelated; if(!exist_sample(id) || !exist_sample(idrelated)) { cout << "[\"Err\",\"missing sample\"]" << endl; }else{ get_values_neighbour(id,idrelated); } break; case 5: iss >> idrelated; if(!processed_sample(id)) { cout << "[\"Err\",\"missing sample\"]" << endl; }else{ cout << "[\"OK\"]" << endl; } break; case 6: write_log("{action:exit_system, date:"+current_date_time()+"}"); cout << "Bye !!" << endl; return 0; break; case 7: aux_model = get_model(); cout << "[\"OK\",\"" << aux_model << "\"]" << endl; break; default: cout << "[\"Err\",\"missing function\"]" << endl; break; } }else{ cout << "[\"Err\",\"missmatch function\"]" << endl; } }
unordered_map<int,int>::iterator begin (int s) {return g.find(s)->second.begin();}
void flush_creatures (){ creatures_map.clear(); }
/****************************************************************************** Description: function for sending back the result Input Value.: Return Value: ******************************************************************************/ void server_result (int sock, string userID) { if (debug) printf("result thread\n\n"); int n, fd; char response[] = "ok"; sem_t *sem_match = new sem_t(); // create a new semaphore in heap queue<string> *imgQueue = 0; // queue storing the file names // Init semaphore and put the address of semaphore into map if (sem_init(sem_match, 0, 0) != 0) { errorSocket("ERROR semaphore init failed", sock); } // grap the lock pthread_mutex_lock(&sem_map_lock); sem_map[userID] = sem_match; pthread_mutex_unlock(&sem_map_lock); // reponse to the client if (!orbit) { n = write(sock, response, sizeof(response)); if (n < 0) { error("ERROR writting to socket"); } } else { MsgD.send(sock, response, sizeof(response)); } struct sockaddr_in myaddr; int ret; char buf[1024]; int serverPort = 9879; if (storm) { if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) printf("socket create failed\n"); if (debug) printf("socket created\n"); /* bind it to all local addresses and pick any port number */ memset((char *)&myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); myaddr.sin_port = htons(serverPort); if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { perror("bind failed"); goto stop; } if (debug) printf("socket binded\n"); } while(!global_stop) { sem_wait(sem_match); // get the address of image queue if (imgQueue == 0) { imgQueue = queue_map[userID]; } // check if the queue is empty if (imgQueue->empty()) { sem_map.erase(userID); queue_map.erase(userID); user_map.erase(userID); delete(sem_match); delete(imgQueue); sem_destroy(sem_match); // if (orbit) // { // MsgD.close(sock, 0); // } printf("[server] client disconnected --- result\n"); // pthread_exit(NULL); //terminate calling thread! return; } if (!storm) { if (debug) printf("\n----------- start matching -------------\n"); string file_name = imgQueue->front(); if (debug) printf("file name: [%s]\n", file_name.c_str()); imgQueue->pop(); // create a new thread to do the image processing pthread_t thread_id; struct arg_result trans_info; trans_info.sock = sock; strcpy(trans_info.file_name, file_name.c_str()); /* create thread and pass socket and file name to send file */ if (pthread_create(&thread_id, 0, result_child, (void *)&(trans_info)) == -1) { fprintf(stderr,"pthread_create error!\n"); break; //break while loop } pthread_detach(thread_id); } else { // receive part bzero(buf, sizeof(buf)); printf("wait for the result...\n"); ret = recv(fd, buf, sizeof(buf), 0); if (ret < 0) { printf("receive error\n"); } else { int matchedIndex = atoi(buf); printf("received result: %d\n\n", matchedIndex); char defMsg[] = "none"; char sendInfo[200]; if (matchedIndex == 0) { // write none to client if (!orbit) { if (write(sock, defMsg, sizeof(defMsg)) < 0) { errorSocket("ERROR writting to socket", sock); } } else { MsgD.send(sock, defMsg, sizeof(defMsg)); } if (debug) printf("not match\n"); } else { // send result to client string info = ImgMatch::getInfo(matchedIndex); sprintf(sendInfo, "%s,0,0,0,0,0,0,0,0,", info.c_str()); if (debug) printf("sendInfo: %s\n", sendInfo); if (!orbit) { if (write(sock, sendInfo, sizeof(sendInfo)) < 0) { errorSocket("ERROR writting to socket", sock); } } else { MsgD.send(sock, sendInfo, sizeof(sendInfo)); } if (debug) printf("matched image index: %d\n", matchedIndex); } } } // end } stop: if (!orbit) { close(sock); } if (storm) { close(fd); } printf("[server] Connection closed. --- result\n\n"); delete(sem_match); // pthread_exit(NULL); //terminate calling thread! return; }
/****************************************************************************** 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; }
int main(int argc, char ** argv) { ofstream log; log.open("/tmp/silben-master.log"); int sbuf = pvm_initsend(PvmDataDefault); log << "read frequency list" << endl; read_word_frequency_list(WORD_FREQ_LIST); log << "finished reading" << endl; log << "spawning threads" << endl; int host_count; pvm_config(&host_count,NULL,NULL); int task_count=host_count*PER_HOST_TASK_COUNT; // Task-IDs int tids[task_count]; char **args = NULL; int r=pvm_spawn((char *)"silben_slave",args,PvmTaskDefault /*PvmTaskDebug*/,NULL,task_count,tids); if (r<task_count) { for(int ct=0;ct<task_count;ct++) printf("Spawn error %d\n",tids[ct]); } log << "finished spawning" << endl; time_t begin_time, end_time; time(&begin_time); char *word = (char *) malloc(48); char *tree = (char *) malloc(1024*1024); // log << "looping" << endl; while (word_list.size()) { // See if DATA is pending while (pvm_probe(-1,DATA)) { int recbuf=pvm_recv(-1,DATA); log << "got DATA with " << recbuf << endl; int block_size; pvm_upkint(&block_size,1,1); log << "Recieving data block of size " << block_size << endl; for (int ct = 0; ct < block_size; ct++) { pvm_upkstr(word); pvm_upkstr(tree); string sword = string(word); string stree = string(tree); // log << "Unpacked " << sword << " \t" << sword.size() << " and " << stree << " \t" << stree.size() << endl; tree_list[sword] = stree; } } // Wait for a NEXT request // log << "Waiting for NEXT" << endl; int recbuf=pvm_recv(-1,NEXT); log << "Received NEXT with " << recbuf << endl; int tid; pvm_bufinfo(recbuf,NULL,NULL,&tid); pvm_initsend(PvmDataDefault); int block_size = BLOCK_SIZE; if (word_list.size() < BLOCK_SIZE) block_size = word_list.size(); log << "Sending data block of size " << block_size << endl; pvm_pkint(&block_size,1,1); for (int ct = 0; ct < block_size; ct++) { string sword = word_list.back(); // log << "Pack " << sword << endl; pvm_pkstr((char *) sword.c_str() ); word_list.pop_back(); } int sbuf = pvm_send(tid,DATA); log << "send DATA with " << sbuf << endl; } // See if DATA is pending while (pvm_probe(-1,DATA)) { int recbuf=pvm_recv(-1,DATA); log << "got DATA with " << recbuf << endl; int block_size; pvm_upkint(&block_size,1,1); log << "Recieving data block of size " << block_size << endl; for (int ct = 0; ct < block_size; ct++) { pvm_upkstr(word); pvm_upkstr(tree); string sword = string(word); string stree = string(tree); // log << "Unpacked " << sword << " \t" << sword.size() << " and " << stree << " \t" << stree.size() << endl; tree_list[sword] = stree; } } // Syncing up after end of data for (int ct = 0; ct < task_count; ct++) { // Sending empty data twice to be sure to be received and unblock receive pvm_initsend(PvmDataDefault); int null = 0; pvm_pkint(&null,1,1); pvm_send(tids[ct],DATA); pvm_initsend(PvmDataDefault); pvm_send(tids[ct],DATA); log << "Send empty DATA to " << tids[ct] << endl; // Waiting for last message int recbuf=pvm_recv(tids[ct],FINAL); log << "got FINAL with " << recbuf << endl; int block_size; pvm_upkint(&block_size,1,1); log << "Recieving data block of size " << block_size << endl; for (int ct2 = 0; ct2 < block_size; ct2++) { pvm_upkstr(word); pvm_upkstr(tree); string sword = string(word); string stree = string(tree); log << "Unpacked " << sword << " \t" << sword.size() << " and " << stree << " \t" << stree.size() << endl; tree_list[sword] = stree; } int tid; pvm_bufinfo(recbuf,NULL,NULL,&tid); pvm_initsend(PvmDataDefault); int sbuf = pvm_send(tids[ct],DIE); log << "send DIE with " << sbuf << endl; } time(&end_time); log << "Matching took " << difftime(end_time,begin_time) << " seconds" << endl; log << "Start writing output" << endl; ofstream ofile; int wordcount = 0; ofile.open(OUTPUT_FILE); for (unordered_map<string,int>::iterator it = frequency_list.begin(); it != frequency_list.end(); it++) { // log << it->first << "\t" << tree_list[it->first] << "\t" << it->second << endl; ofile << it->first << "\t" << tree_list[it->first] << "\t" << it->second << endl; } ofile.close(); log << "Finished writing output" << endl; log.close(); free(word); free(tree); log << "Exiting" << endl; pvm_exit(); }
void rmscache(const char *path) { pthread_mutex_lock(&slock); scache.erase(path); pthread_mutex_unlock(&slock); }
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, command, file; getline(cin, line); //get command and the files in the command if (line.find(" ") != string::npos) { command = line.substr(0, line.find(" ")); file = line.substr(line.find(" ") + 1); } else { command = line; //if first space is the end } if (cin.eof()) throw cix_exit(); if (SIGINT_throw_cix_exit) throw cix_exit(); log << "command " << command << endl; log << "file " << file << endl; const auto& itor = command_map.find(command); cix_command cmd = itor == command_map.end() ? CIX_ERROR : itor->second; switch (cmd) { case CIX_EXIT: throw cix_exit(); break; case CIX_GET: cix_get(server, file); break; case CIX_HELP: cix_help(); break; case CIX_LS: cix_ls(server); break; case CIX_PUT: cix_put(server, file); break; case CIX_RM: cix_rm(server, file); 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; }
uint8_t* mbl_mw_metawearboard_serialize(const MblMwMetaWearBoard* board, uint32_t* size) { vector<uint8_t> serialized_state; serialized_state.push_back(SIGNAL_COMPONENT_SERIALIZATION_FORMAT); board->firmware_revision.serialize(serialized_state); serialized_state.push_back((uint8_t)board->module_number.size()); serialized_state.insert(serialized_state.end(), board->module_number.begin(), board->module_number.end()); { vector<uint8_t> sorted_keys; for (auto it : board->module_info) { sorted_keys.push_back(it.first); } sort(sorted_keys.begin(), sorted_keys.end()); serialized_state.push_back((uint8_t)board->module_info.size()); for (auto it : sorted_keys) { board->module_info.at(it).serialize(serialized_state); } } { uint8_t n_events= 0; vector<uint8_t> event_states; vector<ResponseHeader> sorted_keys; for (auto it : board->module_events) { sorted_keys.emplace_back(it.first.module_id, it.first.register_id, it.first.data_id); } sort(sorted_keys.begin(), sorted_keys.end()); for (auto it : sorted_keys) { board->module_events.at(it)->serialize(event_states); n_events++; // serialize component signals after the main signal // rely on this ordering to restore the components vector if (MblMwDataSignal* signal= dynamic_cast<MblMwDataSignal*>(board->module_events.at(it))) { for(auto component: signal->components) { component->serialize(event_states); n_events++; } } } serialized_state.push_back(n_events); serialized_state.insert(serialized_state.end(), event_states.begin(), event_states.end()); } { vector<uint8_t> sorted_keys; for (auto it : board->module_config) { sorted_keys.push_back(it.first); } sort(sorted_keys.begin(), sorted_keys.end()); serialized_state.push_back((uint8_t)board->module_config.size()); for (auto it : sorted_keys) { serialized_state.push_back(it); CONFIG_SERIALIZATION.at(it)(board, serialized_state); } } serialize_logging(board, serialized_state); *size = (uint32_t) serialized_state.size(); uint8_t* state_bytes = (uint8_t*)malloc(sizeof(uint8_t) * (*size)); memcpy(state_bytes, serialized_state.data(), *size); return state_bytes; }
void command_parse(SSL *ssl, char *command_line) { char command[MAXLINE], arg[MAXLINE]; char message[MAXLINE + 1]; int n = sscanf(command_line, "%s%s", command, arg); if(strcmp(command, "signup") == 0) // signup a new user. { if(n != 2) { printf("wrong command.\n"); return; } char password[MAXLINE]; char retype[MAXLINE]; //close the echo struct termios init_settings,new_settings; tcgetattr(fileno(stdin), &init_settings); new_settings = init_settings; new_settings.c_lflag &= ~ECHO; if(tcsetattr(fileno(stdin), TCSAFLUSH, &new_settings) != 0) { printf("could not set termios attributes.\n"); return; } while(true) { printf("New password:"******"\nRetry new password:"******"\nSorry, passwords do not match.\n"); } printf("\n"); tcsetattr(fileno(stdin), TCSANOW, &init_settings); int len = strlen(password); if(password[len - 1] == '\n') password[len - 1] = '\0'; snprintf(message, MAXLINE,"signup %s %s", arg, password); int k = strlen(message); if(SSL_write(ssl, message, k) != k) { printf("command_parse: ssl_writen error"); return; } k = SSL_read(ssl, message, MAXLINE); if(k < 0) { printf("command_parse: SSL_read error"); return; } message[k] = '\0'; printf("%s\n", message); } else if(strcmp(command, "signin") == 0) // user login { if(n != 2) { printf("wrong command.\n"); return; } char password[MAXLINE]; //cancel echo struct termios init_settings,new_settings; tcgetattr(fileno(stdin), &init_settings); new_settings = init_settings; new_settings.c_lflag &= ~ECHO; if(tcsetattr(fileno(stdin), TCSAFLUSH, &new_settings) != 0) { printf("could not set termios attributes.\n"); return; } printf("Password: "******"\n"); tcsetattr(fileno(stdin), TCSANOW, &init_settings); int len = strlen(password); if(password[len - 1] == '\n') password[len - 1] = '\0'; snprintf(message, MAXLINE, "signin %s %s", arg, password); SSL_write(ssl, message, strlen(message)); int k = SSL_read(ssl, message, MAXLINE); if(k < 0) { printf("SSL_read error"); return; } message[k] = '\0'; printf("%s", message); char temp[MAXLINE]; snprintf(temp, MAXLINE, "user %s signin successfully.\n", arg); if(strcmp(temp, message) ==0) snprintf(cmd_line, MAXLINE, "%s@%s> ", arg, server_address); } else if(strcmp(command, "signout") == 0) // signout { if(n != 1) { printf("wrong command.\n"); return; } SSL_write(ssl, "signout", strlen("signout")); int k = SSL_read(ssl, message, MAXLINE); if(k < 0) { printf("SSL_read error"); return; } message[k] = '\0'; printf("%s", message); snprintf(cmd_line, MAXLINE, "%s:%s> ", server_address, master_port); } else if(strcmp(command, "exit") == 0) // exit { if(n != 1) { printf("wrong command.\n"); return; } SSL_write(ssl, "exit", strlen("exit")); stop = true; } else if(strcmp(command, "delete") == 0) // delete a file { if(n != 2) { printf("wrong command.\n"); return; } int k = strlen(command_line); if(ssl_writen(ssl, command_line, k) != k) { printf("ssl_write error.\n"); return; } k = SSL_read(ssl, message, MAXLINE); if(k < 0) { printf("SSL_read error.\n"); return; } message[k] = '\0'; printf("%s", message); } else if(strcmp(command, "ls") == 0) // list all the files of the user. { if(n != 1) { printf("wrong command.\n"); return; } SSL_write(ssl, "ls", strlen("ls")); int len; int k = SSL_read(ssl, message, MAXLINE); message[k] = '\0'; if(strcmp(message,"not signed in.\n")== 0) printf("not signed in.\n"); else { sscanf(message, "%d\n", &len); char *p_list = (char *)malloc(len + 1); char *p = strchr(message, '\n') + 1; strcpy(p_list, p); int len_temp = strlen(p_list); if(len_temp < len) { if(ssl_readn(ssl, p_list + len_temp, len - len_temp) < 0) { printf("ssl_readn error.\n"); return; } } p_list[len] = '\0'; printf("%s", p_list); } } else if(strcmp(command, "upload") == 0) // upload a file to the slave server { // commandline format: upload ip_address file_name file_size if(n != 2) { printf("wrong command.\n"); return; } // add the file to be uploaded to the upload array. struct stat64 buf; memset(&buf, 0, sizeof(buf)); int file = open(arg,FILE_MODE); fstat64(file, &buf); if(file < 0) { printf("file %s not exist.\n", arg); return; } snprintf(message, MAXLINE, "upload %s %s %lld",ip_address, arg, (long long)buf.st_size); int len = strlen(message); if(ssl_writen(ssl, message, len) != len) { printf("ssl_writen error\n"); return; } int k = SSL_read(ssl, message, MAXLINE); if(k < 0) { printf("SSL_read error\n"); return; } message[k] = '\0'; char temp[MAXLINE]; snprintf(temp, MAXLINE, "file %s already exists.\n", arg); if(strcmp(temp, message) != 0) { pthread_mutex_lock(&upload_mutex); upload_array.insert(make_pair(arg, record(buf.st_size))); pthread_mutex_unlock(&upload_mutex); } close(file); printf("%s", message); } else if(strcmp(command, "download") == 0) // download a file from the slave server. { if(n != 2) { printf("wrong command.\n"); return; } snprintf(message, MAXLINE, "download %s %s", ip_address, arg); int len = strlen(message); if(ssl_writen(ssl, message, len) != len) { printf("ssl_writen error\n"); return; } int k = SSL_read(ssl, message, MAXLINE); if(k < 0) { printf("SSL_read error\n"); return; } message[k] = '\0'; if(strcmp(message, "file not exist.\n") != 0 && strcmp(message, "storage server not connected.\n") != 0) { // add the file to be downloaded to the download array. pthread_mutex_lock(&download_mutex); download_array.insert(make_pair(arg, record())); pthread_mutex_unlock(&download_mutex); } printf("%s", message); } else if(strcmp(command, "status") == 0) { if(n != 1) { printf("wrong command.\n"); return; } status_query = true; pthread_create(&thread, NULL, status_thread, NULL); struct termios newt, oldt; // save terminal settings tcgetattr(0, &oldt); // init new settings newt = oldt; //change settings newt.c_lflag &= ~(ICANON | ECHO); //apply settings tcsetattr(0, TCSANOW, &newt); getchar(); tcsetattr(0, TCSANOW, &oldt); status_query = false; } else if(command[0] == 'l') { char *p = command_line; while(*p == ' ') ++p; system(p + 1); if(strncmp(p + 1,"cd", 2) == 0) chdir(arg); } else printf("command not found.\n"); }
void parseVecReport(string fileName,unordered_map<string,string>& htabVecMessages,unordered_map<string,set<string>>& htabLines,bool isVec6){ string line; bool isMsg = false; ifstream infile(fileName); if(infile.is_open()){ while(getline(infile,line)){ if(line.find("error") != string::npos){ cout<<endl<<line; infile.close(); exit(0); } else if(line.find("warning") != string::npos) continue; else{ vector<string> vecTemp = splitStringByDelimiter(line,':'); if(vecTemp.size() < 3){ if(!isMsg){ cerr<<endl<<endl<<"The following line(s) from the vec report have not been processed:"<<endl; isMsg = true; } cerr<<endl<<line; } else{ string strFileName = vecTemp[0].substr(0,vecTemp[0].find_first_of("(")); string strLineNb = vecTemp[0].substr(vecTemp[0].find("(")+1,vecTemp[0].find(")")-vecTemp[0].find("(")-1); string strKey = strFileName + ";" + strLineNb; string strReason; for(int j=2;j<vecTemp.size();j++){ if(isVec6){ strReason += trimWhiteSpaces(vecTemp[j]); } else{ string trimmedString = trimWhiteSpaces(vecTemp[j]); vector<string> vecInner = splitStringByDelimiter(trimmedString,' '); if(vecInner.size() > 0){ if(htabVecMessages.find(vecInner[0]) != htabVecMessages.end()){ strReason += htabVecMessages.at(vecInner[0]); if(vecInner.size() == 2) strReason += vecInner[1]; } else{ strReason += trimmedString; } } } if(j==vecTemp.size()-1) strReason+=";"; else strReason+=" - "; } if(htabLines.find(strKey) != htabLines.end()){ set<string> reasons = htabLines.at(strKey); if(reasons.find(strReason) == reasons.end()){ reasons.insert(strReason); } htabLines.at(strKey) = reasons; } else{ set<string> reasons; reasons.insert(strReason); htabLines.insert(make_pair<string,set<string>>(strKey,reasons)); } } } } infile.close(); } }
static void manageEquipmentEvent(color_ostream& out) { if (!df::global::world) return; multimap<Plugin*,EventHandler> copy(handlers[EventType::INVENTORY_CHANGE].begin(), handlers[EventType::INVENTORY_CHANGE].end()); unordered_map<int32_t, InventoryItem> itemIdToInventoryItem; unordered_set<int32_t> currentlyEquipped; for ( auto a = df::global::world->units.all.begin(); a != df::global::world->units.all.end(); a++ ) { itemIdToInventoryItem.clear(); currentlyEquipped.clear(); df::unit* unit = *a; /*if ( unit->flags1.bits.dead ) continue; */ auto oldEquipment = equipmentLog.find(unit->id); bool hadEquipment = oldEquipment != equipmentLog.end(); vector<InventoryItem>* temp; if ( hadEquipment ) { temp = &((*oldEquipment).second); } else { temp = new vector<InventoryItem>; } //vector<InventoryItem>& v = (*oldEquipment).second; vector<InventoryItem>& v = *temp; for ( auto b = v.begin(); b != v.end(); b++ ) { InventoryItem& i = *b; itemIdToInventoryItem[i.itemId] = i; } for ( size_t b = 0; b < unit->inventory.size(); b++ ) { df::unit_inventory_item* dfitem_new = unit->inventory[b]; currentlyEquipped.insert(dfitem_new->item->id); InventoryItem item_new(dfitem_new->item->id, *dfitem_new); auto c = itemIdToInventoryItem.find(dfitem_new->item->id); if ( c == itemIdToInventoryItem.end() ) { //new item equipped (probably just picked up) InventoryChangeData data(unit->id, NULL, &item_new); for ( auto h = copy.begin(); h != copy.end(); h++ ) { EventHandler handle = (*h).second; handle.eventHandler(out, (void*)&data); } continue; } InventoryItem item_old = (*c).second; df::unit_inventory_item& item0 = item_old.item; df::unit_inventory_item& item1 = item_new.item; if ( item0.mode == item1.mode && item0.body_part_id == item1.body_part_id && item0.wound_id == item1.wound_id ) continue; //some sort of change in how it's equipped InventoryChangeData data(unit->id, &item_old, &item_new); for ( auto h = copy.begin(); h != copy.end(); h++ ) { EventHandler handle = (*h).second; handle.eventHandler(out, (void*)&data); } } //check for dropped items for ( auto b = v.begin(); b != v.end(); b++ ) { InventoryItem i = *b; if ( currentlyEquipped.find(i.itemId) != currentlyEquipped.end() ) continue; //TODO: delete ptr if invalid InventoryChangeData data(unit->id, &i, NULL); for ( auto h = copy.begin(); h != copy.end(); h++ ) { EventHandler handle = (*h).second; handle.eventHandler(out, (void*)&data); } } if ( !hadEquipment ) delete temp; //update equipment vector<InventoryItem>& equipment = equipmentLog[unit->id]; equipment.clear(); for ( size_t b = 0; b < unit->inventory.size(); b++ ) { df::unit_inventory_item* dfitem = unit->inventory[b]; InventoryItem item(dfitem->item->id, *dfitem); equipment.push_back(item); } } }
/* TODO: consider checking item creation / experience gain just in case */ static void manageJobCompletedEvent(color_ostream& out) { if (!df::global::world) return; int32_t tick0 = eventLastTick[EventType::JOB_COMPLETED]; int32_t tick1 = df::global::world->frame_counter; multimap<Plugin*,EventHandler> copy(handlers[EventType::JOB_COMPLETED].begin(), handlers[EventType::JOB_COMPLETED].end()); map<int32_t, df::job*> nowJobs; for ( df::job_list_link* link = &df::global::world->job_list; link != NULL; link = link->next ) { if ( link->item == NULL ) continue; nowJobs[link->item->id] = link->item; } #if 0 //testing info on job initiation/completion //newly allocated jobs for ( auto j = nowJobs.begin(); j != nowJobs.end(); j++ ) { if ( prevJobs.find((*j).first) != prevJobs.end() ) continue; df::job& job1 = *(*j).second; out.print("new job\n" " location : 0x%X\n" " id : %d\n" " type : %d %s\n" " working : %d\n" " completion_timer : %d\n" " workerID : %d\n" " time : %d -> %d\n" "\n", job1.list_link->item, job1.id, job1.job_type, ENUM_ATTR(job_type, caption, job1.job_type), job1.flags.bits.working, job1.completion_timer, getWorkerID(&job1), tick0, tick1); } for ( auto i = prevJobs.begin(); i != prevJobs.end(); i++ ) { df::job& job0 = *(*i).second; auto j = nowJobs.find((*i).first); if ( j == nowJobs.end() ) { out.print("job deallocated\n" " location : 0x%X\n" " id : %d\n" " type : %d %s\n" " working : %d\n" " completion_timer : %d\n" " workerID : %d\n" " time : %d -> %d\n" ,job0.list_link == NULL ? 0 : job0.list_link->item, job0.id, job0.job_type, ENUM_ATTR(job_type, caption, job0.job_type), job0.flags.bits.working, job0.completion_timer, getWorkerID(&job0), tick0, tick1); continue; } df::job& job1 = *(*j).second; if ( job0.flags.bits.working == job1.flags.bits.working && (job0.completion_timer == job1.completion_timer || (job1.completion_timer > 0 && job0.completion_timer-1 == job1.completion_timer)) && getWorkerID(&job0) == getWorkerID(&job1) ) continue; out.print("job change\n" " location : 0x%X -> 0x%X\n" " id : %d -> %d\n" " type : %d -> %d\n" " type : %s -> %s\n" " working : %d -> %d\n" " completion timer : %d -> %d\n" " workerID : %d -> %d\n" " time : %d -> %d\n" "\n", job0.list_link->item, job1.list_link->item, job0.id, job1.id, job0.job_type, job1.job_type, ENUM_ATTR(job_type, caption, job0.job_type), ENUM_ATTR(job_type, caption, job1.job_type), job0.flags.bits.working, job1.flags.bits.working, job0.completion_timer, job1.completion_timer, getWorkerID(&job0), getWorkerID(&job1), tick0, tick1 ); } #endif for ( auto i = prevJobs.begin(); i != prevJobs.end(); i++ ) { //if it happened within a tick, must have been cancelled by the user or a plugin: not completed if ( tick1 <= tick0 ) continue; if ( nowJobs.find((*i).first) != nowJobs.end() ) { //could have just finished if it's a repeat job df::job& job0 = *(*i).second; if ( !job0.flags.bits.repeat ) continue; df::job& job1 = *nowJobs[(*i).first]; if ( job0.completion_timer != 0 ) continue; if ( job1.completion_timer != -1 ) continue; //still false positive if cancelled at EXACTLY the right time, but experiments show this doesn't happen for ( auto j = copy.begin(); j != copy.end(); j++ ) { (*j).second.eventHandler(out, (void*)&job0); } continue; } //recently finished or cancelled job df::job& job0 = *(*i).second; if ( job0.flags.bits.repeat || job0.completion_timer != 0 ) continue; for ( auto j = copy.begin(); j != copy.end(); j++ ) { (*j).second.eventHandler(out, (void*)&job0); } } //erase old jobs, copy over possibly altered jobs for ( auto i = prevJobs.begin(); i != prevJobs.end(); i++ ) { Job::deleteJobStruct((*i).second, true); } prevJobs.clear(); //create new jobs for ( auto j = nowJobs.begin(); j != nowJobs.end(); j++ ) { /*map<int32_t, df::job*>::iterator i = prevJobs.find((*j).first); if ( i != prevJobs.end() ) { continue; }*/ df::job* newJob = Job::cloneJobStruct((*j).second, true); prevJobs[newJob->id] = newJob; } }
void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) { static bool doOnce = false; // const string eventNames[] = {"world loaded", "world unloaded", "map loaded", "map unloaded", "viewscreen changed", "core initialized", "begin unload", "paused", "unpaused"}; // out.print("%s,%d: onStateChange %d: \"%s\"\n", __FILE__, __LINE__, (int32_t)event, eventNames[event].c_str()); if ( !doOnce ) { //TODO: put this somewhere else doOnce = true; EventHandler buildingHandler(Buildings::updateBuildings, 100); DFHack::EventManager::registerListener(EventType::BUILDING, buildingHandler, NULL); //out.print("Registered listeners.\n %d", __LINE__); } if ( event == DFHack::SC_MAP_UNLOADED ) { lastJobId = -1; for ( auto i = prevJobs.begin(); i != prevJobs.end(); i++ ) { Job::deleteJobStruct((*i).second, true); } prevJobs.clear(); tickQueue.clear(); livingUnits.clear(); buildings.clear(); constructions.clear(); equipmentLog.clear(); Buildings::clearBuildings(out); lastReport = -1; lastReportUnitAttack = -1; gameLoaded = false; multimap<Plugin*,EventHandler> copy(handlers[EventType::UNLOAD].begin(), handlers[EventType::UNLOAD].end()); for (auto a = copy.begin(); a != copy.end(); a++ ) { (*a).second.eventHandler(out, NULL); } } else if ( event == DFHack::SC_MAP_LOADED ) { /* int32_t tick = df::global::world->frame_counter; multimap<int32_t,EventHandler> newTickQueue; for ( auto i = tickQueue.begin(); i != tickQueue.end(); i++ ) newTickQueue.insert(pair<int32_t,EventHandler>(tick+(*i).first, (*i).second)); tickQueue.clear(); tickQueue.insert(newTickQueue.begin(), newTickQueue.end()); //out.print("%s,%d: on load, frame_counter = %d\n", __FILE__, __LINE__, tick); */ //tickQueue.clear(); if (!df::global::item_next_id) return; if (!df::global::building_next_id) return; if (!df::global::job_next_id) return; if (!df::global::ui) return; if (!df::global::world) return; nextItem = *df::global::item_next_id; nextBuilding = *df::global::building_next_id; nextInvasion = df::global::ui->invasions.next_id; lastJobId = -1 + *df::global::job_next_id; constructions.clear(); for ( auto i = df::global::world->constructions.begin(); i != df::global::world->constructions.end(); i++ ) { df::construction* constr = *i; if ( !constr ) { if ( Once::doOnce("EventManager.onLoad null constr") ) { out.print("EventManager.onLoad: null construction.\n"); } continue; } if ( constr->pos == df::coord() ) { if ( Once::doOnce("EventManager.onLoad null position of construction.\n") ) out.print("EventManager.onLoad null position of construction.\n"); continue; } constructions[constr->pos] = *constr; } for ( size_t a = 0; a < df::global::world->buildings.all.size(); a++ ) { df::building* b = df::global::world->buildings.all[a]; Buildings::updateBuildings(out, (void*)b); buildings.insert(b->id); } lastSyndromeTime = -1; for ( size_t a = 0; a < df::global::world->units.all.size(); a++ ) { df::unit* unit = df::global::world->units.all[a]; for ( size_t b = 0; b < unit->syndromes.active.size(); b++ ) { df::unit_syndrome* syndrome = unit->syndromes.active[b]; int32_t startTime = syndrome->year*ticksPerYear + syndrome->year_time; if ( startTime > lastSyndromeTime ) lastSyndromeTime = startTime; } } lastReport = -1; if ( df::global::world->status.reports.size() > 0 ) { lastReport = df::global::world->status.reports[df::global::world->status.reports.size()-1]->id; } lastReportUnitAttack = -1; lastReportInteraction = -1; reportToRelevantUnitsTime = -1; reportToRelevantUnits.clear(); for ( size_t a = 0; a < EventType::EVENT_MAX; a++ ) { eventLastTick[a] = -1;//-1000000; } gameLoaded = true; } }
int get(int key) { if (m_map.find(key) == m_map.end()) return -1; MoveToHead(key); return m_map[key]->value; }
int32_t mbl_mw_metawearboard_deserialize(MblMwMetaWearBoard* board, uint8_t* state, uint32_t size) { uint8_t *current_addr = state, format = *current_addr; if (format != INIT_SERIALIZATION_FORMAT && format != SIGNAL_COMPONENT_SERIALIZATION_FORMAT) { return MBL_MW_STATUS_ERROR_SERIALIZATION_FORMAT; } board->firmware_revision.deserialize(&(++current_addr)); uint8_t module_str_size = *current_addr; current_addr++; board->module_number.assign(current_addr, current_addr + module_str_size); current_addr += module_str_size; board->module_info.clear(); uint8_t module_info_size = *current_addr; current_addr++; for (uint8_t i = 0; i < module_info_size; i++) { board->module_info.emplace(*current_addr, ¤t_addr); } for (auto it : board->module_events) { delete it.second; } board->module_events.clear(); uint8_t n_events = *current_addr; current_addr++; for (uint8_t i = 0; i < n_events; i++) { MblMwEvent* saved_event; switch (*current_addr) { case MBL_MW_MODULE_GPIO: switch(static_cast<GpioRegister>(CLEAR_READ_MODIFIERS(*(current_addr + 1)))) { case GpioRegister::READ_AI_ABS_REF: case GpioRegister::READ_AI_ADC: saved_event= new MblMwGpioAnalogSignal(¤t_addr, board); break; case GpioRegister::PIN_CHANGE_NOTIFY: saved_event= new MblMwGpioPinNotifySignal(¤t_addr, board); break; default: saved_event = new MblMwDataSignal(¤t_addr, board); break; } break; case MBL_MW_MODULE_DATA_PROCESSOR: saved_event = static_cast<DataProcessorRegister>(CLEAR_READ_MODIFIERS(*(current_addr + 1))) == DataProcessorRegister::STATE ? new MblMwDataSignal(¤t_addr, board) : new MblMwDataProcessor(¤t_addr, board); break; case MBL_MW_MODULE_TIMER: saved_event = new MblMwTimer(¤t_addr, board); break; case MBL_MW_MODULE_I2C: switch(static_cast<SerialPassthroughRegister>(CLEAR_READ_MODIFIERS(*(current_addr + 1)))) { case SerialPassthroughRegister::I2C_READ_WRITE: saved_event= new MblMwI2cSignal(¤t_addr, board); break; case SerialPassthroughRegister::SPI_READ_WRITE: saved_event= new MblMwSpiSignal(¤t_addr, board); break; default: saved_event= new MblMwDataSignal(¤t_addr, board); break; } break; case MBL_MW_MODULE_SETTINGS: saved_event = static_cast<SettingsRegister>(*(current_addr + 1)) == SettingsRegister::DISCONNECT_EVENT ? new MblMwEvent(¤t_addr, board) : new MblMwDataSignal(¤t_addr, board); break; default: saved_event = new MblMwDataSignal(¤t_addr, board); break; } ResponseHeader header_copy(saved_event->header); header_copy.disable_silent(); if (board->module_events.count(header_copy)) { auto signal = dynamic_cast<MblMwDataSignal*>(board->module_events[saved_event->header]); signal->components.push_back(dynamic_cast<MblMwDataSignal*>(saved_event)); } else { board->module_events.emplace(header_copy, saved_event); } } uint8_t module_config_size= *current_addr; current_addr++; for (uint8_t i= 0; i < module_config_size; i++) { auto fn= CONFIG_DESERIALIZATION.at(*current_addr); current_addr++; fn(board, ¤t_addr); } deserialize_logging(board, (format == SIGNAL_COMPONENT_SERIALIZATION_FORMAT), ¤t_addr); return MBL_MW_STATUS_OK; }
void asferknnclustering::kNNClustering(string distancemetric) { bool converged=false; asferstringdistance asd; int max_no_of_points=0; int max_cluster_id=0; //training labelled dataset ifstream input1; char line1[256]; char line2[256]; string encstrpath1(asferroot); encstrpath1.append("/asfer.enterprise.encstr.kNN"); input1.open(encstrpath1.c_str(), ifstream::in); labelled_points.clear(); int i=0; while (!input1.eof()) { input1.getline(line1,256); string encstrtr(line1); if(encstrtr != "") { cout<<"training dataset : encstrtr = "<<encstrtr<<endl; labelled_points[encstrtr] = i % MAX_NO_CLUSTERS; i++; } } cout<<"Before kNN clustering"<<endl; printClusters(); //kNN clustering classification ifstream input2; string encstrpath2(asferroot); encstrpath2.append("/asfer.enterprise.encstr"); input2.open(encstrpath2.c_str(), ifstream::in); while (!input2.eof()) //for each unlabelled encoded string { input2.getline(line2,256); string encstrtr(line2); if(encstrtr != "") { int editdistance_radius=8; unordered_map<string,int> kNearestNeighbours; // <encoded_string,cluster_id> printClusters(); for(unordered_map<string,int>::iterator it1 = labelled_points.begin(); it1 != labelled_points.end(); it1++) // for each labelled encoded string { //compute distance to all labelled points from this unlabelled point //and create a map of k Nearest neighbours that are with in a //radius edit distance int ed; cout<<"kNNClustering iteration: encstrtr = "<<encstrtr<<endl; if(it1->first != "") { ed = asd.editDistanceWagnerFischer(encstrtr, it1->first); if(ed < editdistance_radius) { kNearestNeighbours[it1->first] = it1->second; } } } //find the cluster id label that is //in majority in above k Nearest Neighbours unordered_map<int,int> labelMajority; // <cluster_id, no_of_points_in_cluster> for(unordered_map<string,int>::iterator it2 = kNearestNeighbours.begin(); it2 != kNearestNeighbours.end(); it2++) { //if(it2->first != "") labelMajority[it2->second]++; } for(unordered_map<int,int>::iterator it3 = labelMajority.begin(); it3 != labelMajority.end(); it3++) { if(it3->second > max_no_of_points) { max_no_of_points = it3->second; max_cluster_id = it3->first; } } //label with max_cluster_id labelled_points[encstrtr]=max_cluster_id; max_no_of_points=0; max_cluster_id=0; } } printClusters(); }
vector<vector<int>> createTupleTable(unordered_map<int, vector<Pair>> clauseAnswers, vector<Relationship> relationships, unordered_map<string, int> *synIndexMap) { //// cout << "Begin creating tuple table" << endl; vector<vector<int>> tupleTable; vector<int> emptyTuple; tupleTable.push_back(emptyTuple); for(int i=0; i<int(relationships.size()); i++) { if (AbstractWrapper::GlobalStop) { // cout<< "Timeout detected! Stopping QueryProcessor Tuple!" << endl; return tupleTable; } //// cout << "Adding relationship " << i << " to tuple table" << endl; vector<Pair> clauseAns = clauseAnswers.at(i); //Initialize variables Relationship r = relationships.at(i); Relationship::RelType rt = r.getRelType(); //// cout << "Relation Type being added to tuple table: " << rt << endl; string token1; string token2; Relationship::TokenType type1; Relationship::TokenType type2; if(rt == Relationship::PATTERN) { type1 = r.getToken1Type(); if(type1 == Relationship::SYNONYM) { token1 = r.getPatternSyn(); token2 = r.getToken1(); type2 = r.getToken1Type(); } else { token1 = r.getPatternSyn(); token2 = r.getPatternSyn(); type1 = Relationship::SYNONYM; type2 = Relationship::INVALIDTOKEN; } } else { token1 = r.getToken1(); token2 = r.getToken2(); type1 = r.getToken1Type(); type2 = r.getToken2Type(); } bool ignore1 = false; bool ignore2 = false; if(type1 != Relationship::SYNONYM) ignore1 = true; if(type2 != Relationship::SYNONYM || (token1==token2)) ignore2 = true; //Case 1: both are Synonyms if(!ignore1 && !ignore2) { //// cout << "Case 1" << endl; bool tk1InMap = false; bool tk2InMap = false; if(synIndexMap->find(token1) != synIndexMap->end()) tk1InMap = true; else { //// cout << "Adding " << token1 << " to synMap with index " << synIndexMap->size() << endl; synIndexMap->insert(make_pair(token1, synIndexMap->size())); } if(synIndexMap->find(token2) != synIndexMap->end()) tk2InMap = true; else { //// cout << "Adding " << token2 << " to synMap with index " << synIndexMap->size() << endl; synIndexMap->insert(make_pair(token2, synIndexMap->size())); } // Check if clause has no answer, return empty tuple table if(clauseAns.size()==0) { //// cout << "empty clause detected, returning empty table" << endl; tupleTable.clear(); break; } vector<vector<int>> newTable; //Case 1.1: Both Synonyms are new if(!tk1InMap && !tk2InMap) { //// cout << "Case 1.1" << endl; for(int x=0; x<int(tupleTable.size()); x++) { for(int y=0; y<int(clauseAns.size()); y++) { vector<int> tuple; vector<int> tuple1 = tupleTable.at(x); Pair tuple2 = clauseAns.at(y); tuple.insert(tuple.begin(), tuple1.begin(), tuple1.end()); //// cout << tuple2.ans1 << " " << tuple2.ans2 << endl; tuple.push_back(tuple2.ans1); tuple.push_back(tuple2.ans2); newTable.push_back(tuple); } } } //Case 1.2: First Synonym is new else if(!tk1InMap) { //// cout << "Case 1.2" << endl; for(int x=0; x<int(tupleTable.size()); x++) { for(int y=0; y<int(clauseAns.size()); y++) { vector<int> tuple; vector<int> tuple1 = tupleTable.at(x); Pair tuple2 = clauseAns.at(y); int index = synIndexMap->at(token2); if(tuple1.at(index) == tuple2.ans2) { tuple.insert(tuple.begin(), tuple1.begin(), tuple1.end()); tuple.push_back(tuple2.ans1); newTable.push_back(tuple); } } } } //Case 1.3: Second Synonym is new else if(!tk2InMap) { //// cout << "Case 1.3" << endl; for(int x=0; x<int(tupleTable.size()); x++) { for(int y=0; y<int(clauseAns.size()); y++) { vector<int> tuple; vector<int> tuple1 = tupleTable.at(x); Pair tuple2 = clauseAns.at(y); int index = synIndexMap->at(token1); if(tuple1.at(index) == tuple2.ans1) { tuple.insert(tuple.begin(), tuple1.begin(), tuple1.end()); tuple.push_back(tuple2.ans2); newTable.push_back(tuple); } } } } //Case 1.4: Both Synonyms have been seen before else { //// cout << "Case 1.4" << endl; for(int x=0; x<int(tupleTable.size()); x++) { for(int y=0; y<int(clauseAns.size()); y++) { vector<int> tuple; vector<int> tuple1 = tupleTable.at(x); Pair tuple2 = clauseAns.at(y); int index1 = synIndexMap->at(token1); int index2 = synIndexMap->at(token2); if(tuple1.at(index1) == tuple2.ans1 && tuple1.at(index2) == tuple2.ans2) { tuple.insert(tuple.begin(), tuple1.begin(), tuple1.end()); newTable.push_back(tuple); } } } } tupleTable = newTable; } //Case 2: only first arg is Synonyms else if(!ignore1) { //// cout << "Case 2" << endl; bool tkInMap = false; if(synIndexMap->find(token1) != synIndexMap->end()) tkInMap = true; else { //// cout << "Adding " << token1 << " to synMap with index " << synIndexMap->size() << endl; synIndexMap->insert(make_pair(token1, synIndexMap->size())); } // Check if clause has no answer, return empty tuple table if(clauseAns.size()==0) { //// cout << "empty clause detected, returning empty table" << endl; tupleTable.clear(); break; } vector<vector<int>> newTable; //Case 2.1: have seen Synonym if(tkInMap) { //// cout << "Case 2.1" << endl; for(int x=0; x<int(tupleTable.size()); x++) { for(int y=0; y<int(clauseAns.size()); y++) { vector<int> tuple; vector<int> tuple1 = tupleTable.at(x); Pair tuple2 = clauseAns.at(y); int index = synIndexMap->at(token1); if(tuple1.at(index) == tuple2.ans1) { tuple.insert(tuple.begin(), tuple1.begin(), tuple1.end()); newTable.push_back(tuple); } } } } //Case 2.2: haven't seen Synonym else { //// cout << "Case 2.2" << endl; for(int x=0; x<int(tupleTable.size()); x++) { for(int y=0; y<int(clauseAns.size()); y++) { vector<int> tuple; vector<int> tuple1 = tupleTable.at(x); Pair tuple2 = clauseAns.at(y); tuple.insert(tuple.begin(), tuple1.begin(), tuple1.end()); tuple.push_back(tuple2.ans1); newTable.push_back(tuple); } } } tupleTable = newTable; } //Case 3: only second arg is Synonyms else if(!ignore2) { //// cout << "Case 3" << endl; bool tkInMap = false; if(synIndexMap->find(token2) != synIndexMap->end()) tkInMap = true; else { //// cout << "Adding " << token2 << " to synMap with index " << synIndexMap->size() << endl; synIndexMap->insert(make_pair(token2, synIndexMap->size())); } // Check if clause has no answer, return empty tuple table if(clauseAns.size()==0) { //// cout << "empty clause detected, returning empty table" << endl; tupleTable.clear(); break; } vector<vector<int>> newTable; //Case 3.1: have seen Synonym if(tkInMap) { //// cout << "Case 3.1" << endl; for(int x=0; x<int(tupleTable.size()); x++) { for(int y=0; y<int(clauseAns.size()); y++) { vector<int> tuple; vector<int> tuple1 = tupleTable.at(x); Pair tuple2 = clauseAns.at(y); int index = synIndexMap->at(token2); if(tuple1.at(index) == tuple2.ans2) { tuple.insert(tuple.begin(), tuple1.begin(), tuple1.end()); newTable.push_back(tuple); } } } } //Case 3.2: haven't seen Synonym else { //// cout << "Case 3.2" << endl; for(int x=0; x<int(tupleTable.size()); x++) { for(int y=0; y<int(clauseAns.size()); y++) { vector<int> tuple; vector<int> tuple1 = tupleTable.at(x); Pair tuple2 = clauseAns.at(y); tuple.insert(tuple.begin(), tuple1.begin(), tuple1.end()); tuple.push_back(tuple2.ans2); newTable.push_back(tuple); } } } tupleTable = newTable; } //Case 4: neither are Synonyms(answer is boolean) else { //// cout << "Case 4" << endl; if(clauseAns.size() == 0) tupleTable.clear(); } //If tuple table is ever empty, return empty tuple table if(tupleTable.size() == 0) return tupleTable; //// cout <<"Tuple Table Size: " << tupleTable.size() << endl; //// cout << "Current Tuple Length: " << tupleTable.at(0).size() << endl; } //// cout << "End creating tuple table" << endl; return tupleTable; }
bool haveSeenState(State* state) { assert(state != nullptr); return stateActions.count(state) > 0; }
void deallocate_memory(){ sample_path_list.clear(); list_samples.clear(); indexes.clear(); ides_index_name.clear(); }
void clearscache() { pthread_mutex_lock(&slock); scache.clear(); pthread_mutex_unlock(&slock); }
//////////////////////////////////////////////////////////////////////////////// // OTHERS void update_predefined_parameters( unordered_map<wstring, pair<wstring, int> > const& params) { unordered_map<wstring, pair<wstring, int> >::const_iterator i; //////////////////////////////////////////////////////////////////////////////// // SIZE if ((i=params.find(pre_edge_width))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_draw::set_edge_width(v); } catch (...) {} } else { sociarium_project_draw::set_edge_width( sociarium_project_draw::get_default_edge_width()); } if ((i=params.find(pre_node_size))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_draw::set_node_size(v); } catch (...) {} } else { sociarium_project_draw::set_node_size( sociarium_project_draw::get_default_node_size()); } if ((i=params.find(pre_coordinates_size))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_draw::set_coordinates_size(v); } catch (...) {} } if ((i=params.find(pre_grid_interval))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_layout::set_grid_interval(v); } catch (...) {} } //////////////////////////////////////////////////////////////////////////////// // FORCE DIRECTION if ((i=params.find(pre_force_scale))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_force_direction::set_force_scale(v); } catch (...) {} } if ((i=params.find(pre_kk_force_NN))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_force_direction::set_kk_force_CC(v); } catch (...) {} } if ((i=params.find(pre_kk_force_CC))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_force_direction::set_kk_force_CC(v); } catch (...) {} } if ((i=params.find(pre_spring_force_CN))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_force_direction::set_spring_force_CN(v); } catch (...) {} } if ((i=params.find(pre_spring_force_NN))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_force_direction::set_spring_force_NN(v); } catch (...) {} } if ((i=params.find(pre_spring_length_CN))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_force_direction::set_spring_length_CN(v); } catch (...) {} } if ((i=params.find(pre_spring_length_NN))!=params.end()) { wstring const& s = i->second.first; try { float const v = boost::lexical_cast<float>(s); if (v>0.0f) sociarium_project_force_direction::set_spring_length_NN(v); } catch (...) {} } sociarium_project_force_direction::should_be_updated(); }
/****************************************************************************** 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; } // 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; } write_length = fwrite(buffer, sizeof(char), length, fp); if (write_length < length) { printf("File:\t Write Failed!\n"); break; } bzero(buffer, sizeof(buffer)); received_size += length; if (received_size >= file_size) { if (debug) printf("file size full\n"); break; } } // 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 char img[file_size]; int offset = 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; } // copy the content into img for (int i = 0; i < length; ++i) { img[i + offset] = buffer[i]; } bzero(buffer, sizeof(buffer)); offset += length; if (offset >= file_size) { if (debug) printf("file size full\n"); break; } } // 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, sizeof(img)); 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); } // 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[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, sizeof(img)); 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); } // 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; }
//////////////////////////////////////////////////////////////////////////////// // TEXTURE void update_texture_parameters( unordered_map<wstring, pair<wstring, int> > const& params) { unordered_map<wstring, pair<wstring, int> >::const_iterator i; if ((i=params.find(pre_texture_folder))!=params.end()) { wstring path = i->second.first; if (!path.empty() && path[path.size()-1]!=L'\\') path += L'\\'; sociarium_project_texture::set_texture_folder_path(path); } else sociarium_project_texture::set_texture_folder_path(L""); GLint wrap_s = GL_CLAMP_TO_EDGE_EXT; GLint wrap_t = GL_CLAMP_TO_EDGE_EXT; /* GL_REPEAT * GL_CLAMP * GL_CLAMP_TO_BORDER_EXT (unavailable??) * GL_CLAMP_TO_EDGE_EXT */ if ((i=params.find(pre_texture_wrap_s))!=params.end()) { wstring const s = i->second.first; if (s==L"GL_CLAMP") wrap_s = GL_CLAMP; else if (s==L"GL_REPEAT") wrap_s = GL_REPEAT; else if (s==L"GL_CLAMP_TO_EDGE_EXT") wrap_s = GL_CLAMP_TO_EDGE_EXT; sociarium_project_texture::set_texture_parameter_wrap_s(wrap_s); } if ((i=params.find(pre_texture_wrap_t))!=params.end()) { wstring const s = i->second.first; if (s==L"GL_CLAMP") wrap_t = GL_CLAMP; else if (s==L"GL_REPEAT") wrap_t = GL_REPEAT; else if (s==L"GL_CLAMP_TO_EDGE_EXT") wrap_t = GL_CLAMP_TO_EDGE_EXT; sociarium_project_texture::set_texture_parameter_wrap_t(wrap_t); } if ((i=params.find(pre_node_texture))!=params.end()) { // Change default node texture. wstring const filename = i->second.first; sociarium_project_texture::set_default_node_texture_tmp(filename); } else { wstring const path_tmp = sociarium_project_texture::get_texture_folder_path(); sociarium_project_texture::set_texture_folder_path(L""); sociarium_project_texture::set_default_node_texture_tmp( DEFAULT_NODE_TEXTURE_FILE); sociarium_project_texture::set_texture_folder_path(path_tmp); } if ((i=params.find(pre_edge_texture))!=params.end()) { // Change default edge texture. wstring const filename = i->second.first; sociarium_project_texture::set_default_edge_texture_tmp(filename); } else { wstring const path_tmp = sociarium_project_texture::get_texture_folder_path(); sociarium_project_texture::set_texture_folder_path(L""); sociarium_project_texture::set_default_edge_texture_tmp( DEFAULT_EDGE_TEXTURE_FILE); sociarium_project_texture::set_texture_folder_path(path_tmp); } if ((i=params.find(pre_community_texture))!=params.end()) { // Change default community texture. wstring const filename = i->second.first; sociarium_project_texture::set_default_community_texture_tmp(filename); } else { wstring const path_tmp = sociarium_project_texture::get_texture_folder_path(); sociarium_project_texture::set_texture_folder_path(L""); sociarium_project_texture::set_default_community_texture_tmp( DEFAULT_COMMUNITY_TEXTURE_FILE); sociarium_project_texture::set_texture_folder_path(path_tmp); } if ((i=params.find(pre_community_edge_texture))!=params.end()) { // Change default community edge texture. wstring const filename = i->second.first; sociarium_project_texture::set_default_community_edge_texture_tmp(filename); } else { wstring const path_tmp = sociarium_project_texture::get_texture_folder_path(); sociarium_project_texture::set_texture_folder_path(L""); sociarium_project_texture::set_default_community_edge_texture_tmp( DEFAULT_COMMUNITY_EDGE_TEXTURE_FILE); sociarium_project_texture::set_texture_folder_path(path_tmp); } }
unordered_map<int,int>::iterator find(int s, int d){ return g.find(s)->second.find(d);}
int main(int argc, char *argv[]) { int D, K; if (argc != 7 || sscanf(argv[3], "%d", &D) != 1 || sscanf(argv[4], "%d", &K) != 1) { printf("[usage] <vector.bin> <length*.csv folder path> <D> <K> <unigram2phrase-nn> <word2word-nn>\n"); return 0; } loadVector(argv[1]); loadPatterns(argv[2]); cerr << unigrams.size() << endl; cerr << phrases.size() << endl; vector< vector<double> > axis(D, vector<double>(dimension, 0)); double sqrt3 = sqrt(3.0); for (int i = 0; i < D; ++ i) { for (int j = 0; j < dimension; ++ j) { axis[i][j] = sample_normal(); /*double roll = next_double2(); if (roll < 1.0 / 6) { axis[i][j] = sqrt3; } else if (roll < 1.0 / 3) { axis[i][j] = -sqrt3; }*/ } } for (int i = 0; i < D; ++ i) { double sum = 0; for (int j = 0; j < dimension; ++ j) { sum += sqr(axis[i][j]); } sum = sqrt(sum); for (int j = 0; j < dimension; ++ j) { axis[i][j] /= sum; } } vector<Point> unigramPoints, phrasesPoints, wordsPoints; FOR (unigram, unigrams) { if (word2vec.count(unigram->first)) { Point cur(unigram->first, word2vec[unigram->first]); unigramPoints.push_back(cur); wordsPoints.push_back(cur); } } FOR (phrase, phrases) { if (word2vec.count(phrase->first)) { Point cur(phrase->first, word2vec[phrase->first]); phrasesPoints.push_back(cur); wordsPoints.push_back(cur); } } normalize(unigramPoints); normalize(phrasesPoints); normalize(wordsPoints); vector<Point> projUnigramPoints, projPhrasesPoints, projWordsPoints; projUnigramPoints = project(unigramPoints, axis); projPhrasesPoints = project(phrasesPoints, axis); projWordsPoints = project(wordsPoints, axis); /* normalize(projUnigramPoints); normalize(projPhrasesPoints); normalize(projWordsPoints);*/ for (int i = 0; i < wordsPoints.size(); ++ i) { word2vec[wordsPoints[i].name] = wordsPoints[i].x; } cerr << "vectors projected" << endl; KDTree phraseTree(projPhrasesPoints); vector<vector<pair<string, double>>> u2p(unigramPoints.size(), vector<pair<string,double>>(K, make_pair("", 0.0))); #pragma omp parallel for schedule(dynamic, 1000) for (int i = 0; i < unigramPoints.size(); ++ i) { // if (i % 1000 == 0) { // cerr << i << " " << unigramPoints[i].name<< endl; // } vector<string> sim = phraseTree.query(projUnigramPoints[i], 100); vector< pair<double, string> > order; const vector<double> &u = unigramPoints[i].x; for (int j = 0; j < sim.size(); ++ j) { double dot = 0; const vector<double> &v = word2vec[sim[j]]; myAssert(u.size() == v.size(), "dimension mismatch!"); for (int d = 0; d < u.size(); ++ d) { dot += u[d] * v[d]; } order.push_back(make_pair(dot, sim[j])); } sort(order.rbegin(), order.rend()); if (order.size() > K) { order.resize(K); } myAssert(order.size() == K, "not enough neighbors!"); double maxi = order[0].first; for (int j = 0; j < order.size(); ++ j) { u2p[i][j].first = order[j].second; u2p[i][j].second = order[j].first / maxi; } } cerr << "u2p nn done" << endl; FILE* out = tryOpen(argv[5], "w"); for (int i = 0; i < unigramPoints.size(); ++ i) { fprintf(out, "%s", unigramPoints[i].name.c_str()); for (int j = 0; j < K; ++ j) { fprintf(out, "\t%s\t%.10f", u2p[i][j].first.c_str(), u2p[i][j].second); } fprintf(out, "\n"); } fclose(out); cerr << "u2p output done" << endl; KDTree wordTree(projWordsPoints); vector<vector<pair<string, double>>> w2w(wordsPoints.size(), vector<pair<string,double>>(K, make_pair("", 0.0))); #pragma omp parallel for schedule(dynamic, 1000) for (int i = 0; i < wordsPoints.size(); ++ i) { vector<string> sim = wordTree.query(projWordsPoints[i], 100); vector< pair<double, string> > order; const vector<double> &u = wordsPoints[i].x; for (int j = 0; j < sim.size(); ++ j) { double dot = 0; const vector<double> &v = word2vec[sim[j]]; myAssert(u.size() == v.size(), "dimension mismatch!"); for (int d = 0; d < u.size(); ++ d) { dot += u[d] * v[d]; } order.push_back(make_pair(dot, sim[j])); } sort(order.rbegin(), order.rend()); if (order.size() > K) { order.resize(K); } myAssert(order.size() == K, "not enough neighbors!"); double maxi = order[0].first; for (int j = 0; j < order.size(); ++ j) { w2w[i][j].first = order[j].second; w2w[i][j].second = order[j].first / maxi; } } cerr << "w2w nn done" << endl; out = tryOpen(argv[6], "w"); for (int i = 0; i < wordsPoints.size(); ++ i) { fprintf(out, "%s", wordsPoints[i].name.c_str()); for (int j = 0; j < K; ++ j) { fprintf(out, "\t%s\t%.10f", w2w[i][j].first.c_str(), w2w[i][j].second); } fprintf(out, "\n"); } fclose(out); cerr << "w2w output done" << endl; }
unordered_map<int,int>::iterator end (int s) {return g.find(s)->second.end();}
void Buildings::clearBuildings(color_ostream& out) { corner1.clear(); corner2.clear(); locationToBuilding.clear(); }
void inline CCreature::unmap() {creatures_map.erase(x + (y << 8));};
void handleUpdatePacket(uint8* data, int len) { UpdatePacketSelf update = *(UpdatePacketSelf*)&data[1]; //if( player_x != update.positionX || player_y != update.positionY ) { //printf( "X/Y: %i, %i\n", update.positionX, update.positionY ); //} player_x = update.positionX; player_y = update.positionY; player_health = update.currentHPPct; if( player_health <= 25 ) { printf( "[%s] Health (%i) < 25. Exiting!\n", currentDateTime().c_str(), player_health ); logout( player_id ); SLEEP( 100 ); exit_bot = true; finished_quest = true; missing_quest = true; } /*for( int j = 0; j < len; j++ ) { fprintf( out, "%02X ", data[j] ); } fprintf( out, "\n" ); fprintf( out, "Update positionX: %i\n", update.positionX ); fprintf( out, "Update positionY: %i\n", update.positionY ); fprintf( out, "Update spellflags: %i\n", update.spellflags ); fprintf( out, "Update colorbits: %i\n", update.colorbits ); fprintf( out, "Update numInRangeDynamicObjects: %i\n", update.numInRangeDynamicObjects ); fprintf( out, "Update numInRangeUnits: %i\n", update.numInRangeUnits ); fprintf( out, "Update unklol: %i\n", update.unklol ); fprintf( out, "Update flags: %i\n", update.flags ); fprintf( out, "Update rotation: %i\n", update.rotation ); fprintf( out, "Update animation: %i\n", update.animation ); fprintf( out, "Update spellEffect: %i\n", update.spellEffect ); fprintf( out, "Update numInRangeSpellEffects: %i\n", update.numInRangeSpellEffects ); fprintf( out, "Update unklol2: %i\n", update.unklol2 ); fprintf( out, "Update unklol3: %i\n", update.unklol3 ); fprintf( out, "Update currentHPPct: %i\n", update.currentHPPct ); fprintf( out, "Update currentMPPct: %i\n", update.currentMPPct ); fprintf( out, "Update currentExpPct: %i\n", update.currentExpPct ); fprintf( out, "\n\n" );*/ int offset = 23; if( update.numInRangeDynamicObjects > 0 ) { for( int i = 0; i < update.numInRangeDynamicObjects; i++ ) { if( data[offset] == 0x01 ) offset += 8; else if( data[offset] == 0x02 ) offset += 12; else if( data[offset] == 0x03 ) offset += 6; } } if( update.numInRangeSpellEffects > 0 ) { for( int x = 0; x < update.numInRangeSpellEffects; x++ ) { if( data[offset] >= 240 ) offset += 12; else offset += 8; } } setAllOoR(); if( update.numInRangeUnits > 0 ) { for( int i = 0; i < update.numInRangeUnits; i++ ) { uint16 id = ( data[offset+1] << 8 ) + data[offset]; uint16 trueid = id & 0x7FFF; // Attempt to find unit in our unitmap setInRange( trueid ); offset += 2; if( id & 0x8000 ) continue; else { uint8 updateflag = data[offset]; offset += 1; if(updateflag & 0x01) { UpdatePacketUnitMovement movement = *(UpdatePacketUnitMovement*)&data[offset]; um_units.at( trueid ).movement = movement; //fprintf( out, "Player: %i | Movement | X/Y: %i, %i\n", trueid, movement.positionX, movement.positionY ); //UnitMap::UpdateUnitsMovement(trueid, *(UpdatePacketUnitMovement*)&data[offset]); offset += 5; } if(updateflag & 0x02) { UpdatePacketUnitAuras auras = *(UpdatePacketUnitAuras*)&data[offset]; um_units.at( trueid ).auras = auras; //UnitMap::UpdateUnitsAuras(trueid, *(UpdatePacketUnitAuras*)&data[offset]); offset += 3; } if(updateflag & 0x04) { UpdatePacketUnitModels models = *(UpdatePacketUnitModels*)&data[offset]; um_units.at( trueid ).models = models; //fprintf( out, "Player: %i | ModelInfo | Model: %i, Weapon: %i, Shield: %i, Helmet: %i\n", trueid, models.model, models.weapon, models.shield, models.helmet ); //UnitMap::UpdateUnitsModel(trueid, *(UpdatePacketUnitModels*)&data[offset]); offset += 8; } if(updateflag & 0x08) { UpdatePacketUnitAnim anim = *(UpdatePacketUnitAnim*)&data[offset]; //UnitMap::UpdateUnitsAnim(trueid, *(UpdatePacketUnitAnim*)&data[offset]); um_units.at( trueid ).anim = anim; if( anim.anim == 21 || anim.anim == 45 ) { um_units.at( trueid ).dead = true; } else { um_units.at( trueid ).dead = false; } offset += 1; } if(updateflag & 0x10) { UpdatePacketUnitSpellEffect spellfx = *(UpdatePacketUnitSpellEffect*)&data[offset]; //UnitMap::UpdateUnitsSpellEffect(trueid, *(UpdatePacketUnitSpellEffect*)&data[offset]); um_units.at( trueid ).spelleffect = spellfx; offset += 1; } if(updateflag & 0x20) { offset += 1 + data[offset]; } } } } clearOoR(); if( update.flags & 0x01 ) { if( data[offset] == 0x31 && data[offset+1] == 0x30 && data[offset+2] == 0x30 ) { finished_quest = true; } } //fprintf( out, "\n\n" ); }