FILE .h // include #include #include #include #include // GetModuleFileName #include // std::find_if #include // std::isspace #include // std::ptr_fun #include // std::find_if // Prototipi funzioni globali string ExePath(); vector split(string str,string sep); static std::string Trim(const std::string& str); double get_cpu_time(); FILE .cpp // trova il path del direttorio da cui si e' lanciato l'eseguibile string ExePath() { char buffer[MAX_PATH]; GetModuleFileName( NULL, buffer, MAX_PATH ); string::size_type pos = string( buffer ).find_last_of( "\\/" ); return string( buffer ).substr( 0, pos); } // trim, toglie spazi bianchi a inizio e fine stringa static std::string Trim(const std::string& str) { std::string s = str; // remove white space in front s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); // remove trailing white space s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); return s; } // split di una stringa in un array di elementi delimitati da separatori vector split(string str,string sep) { char* cstr=const_cast(str.c_str()); char* current; vector elem; current=strtok(cstr,sep.c_str()); while(current!=NULL) { elem.push_back(current); current=strtok(NULL,sep.c_str()); } return elem; } // se serve sarebbe da mettere a posto void split2(string s, string seperator) { vector output; std::string::size_type prev_pos = 0, pos = 0; while((pos = s.find(seperator, pos)) != std::string::npos) { std::string substring( s.substr(prev_pos, pos-prev_pos) ); output.push_back(substring); prev_pos = ++pos; } output.push_back(s.substr(prev_pos, pos-prev_pos)); // Last word } // trova il tempo esatto di CPU di esecuzione (non wallclock time) double get_cpu_time() { FILETIME a,b,c,d; if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0) { // Returns total user time. // Can be tweaked to include kernel times as well. return (double)(d.dwLowDateTime | ((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001; } else { // Handle error return 0; } } /// /// Quicksort degli indici di un array di interi /// /// l'array di cui ordinare gli indici /// Gli indici ordinati in ordine crescente vector quickSortInd(vector A) { int i,q,ptrLeft,ptrRight,lev; vector ind(A.size() ), pBeg(1000), pEnd(300); for(i=0;i<(int)A.size();i++) ind[i]=i; lev = 0; ptrLeft = pBeg[0] = 0; ptrRight= pEnd[0] = A.size()-1; while (lev >= 0) { ptrLeft = pBeg[lev]; ptrRight= pEnd[lev]; if(ptrLeft < ptrRight) { q=partition(A,ind,ptrLeft,ptrRight); pBeg[lev] = ptrLeft; pEnd[lev] = q; lev++; pBeg[lev] = q+1; pEnd[lev] = ptrRight; } else lev--; } return(ind); } int partition(vector A,vector ind,int p,int r) { int i,j,x,temp; x=A[ind[p]]; i=p-1; j=r+1; while(true) { do j=j-1; while(A[ind[j]]>x); do i=i+1; while(A[ind[i]]