void tc_swap() { std::cout << "-----\t swap" << '\n'; MySTL::vector<int> foo(3, 100); // three ints with a value of 100 MySTL::vector<int> bar(5, 200); // five ints with a value of 200 foo.swap(bar); std::cout << "foo contains:"; printvector(foo); std::cout << "bar contains:"; printvector(bar); }
void tc_clear() { std::cout << "-----\t clear" << '\n'; MySTL::vector<int> myvector; myvector.push_back(100); myvector.push_back(200); myvector.push_back(300); std::cout << "myvector contains:"; printvector(myvector); myvector.clear(); myvector.push_back(1101); myvector.push_back(2202); std::cout << "myvector contains:"; printvector(myvector); }
int main(int argc, char **argv) { int *sndbuffer, *recvbuffer; int rank, size, i; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); sndbuffer = (int *)malloc(size*sizeof(int)); recvbuffer = (int *)malloc(size*sizeof(int)); for(i=0; i<size; i++) sndbuffer[i] = i*i+rank; printvector(rank, sndbuffer); MPI_Alltoall(sndbuffer, 1, MPI_INT, recvbuffer, 1, MPI_INT, MPI_COMM_WORLD); printvector(rank, recvbuffer); MPI_Finalize(); return 0; }
void tc_at() { std::cout << "-----\t at" << '\n'; MySTL::vector<int> myvector(10); // 10 zero-initialized ints // assign some values: for (unsigned i = 0; i < myvector.size(); i++) myvector.at(i) = i; std::cout << "myvector contains:"; printvector(myvector); }
void tc_push_back() { std::cout << "-----\t push_back" << '\n'; MySTL::vector<int> myvector; for (auto i = 0; i < 10; ++i) { srand(static_cast<unsigned>(time(nullptr))); myvector.push_back(rand()); } printvector(myvector); }
int main(int argc, char** argv) { int x[DIM]; /* Vector x element R^n */ initialize(x, DIM); eratosthenes(x, DIM); printf("Sorted vector x[%d]:\n", DIM); printvector(x, DIM); return EXIT_SUCCESS; }
int main(int argc, char** argv) { double x[DIM]; /* Vector x element R^n */ printf("Input vector x[%d]:\n", DIM); scanvector(x, DIM); bubblesort(x, DIM); printf("Sorted vector x[%d]:\n", DIM); printvector(x, DIM); return EXIT_SUCCESS; }
// iterator void tc_iterators() { std::cout << "-----\t iterators" << '\n'; MySTL::vector<int> myvector(5); // 5 default-constructed ints auto i = 0; //MySTL::vector<int>::reverse_iterator rit = myvector.rbegin(); //for (; rit != myvector.rend(); ++rit) // *rit = ++i; std::cout << "myvector contains:"; printvector(myvector); }
void tc_data() { std::cout << "-----\t data" << '\n'; MySTL::vector<int> myvector(5); int* p = myvector.data(); *p = 10; ++p; *p = 20; p[2] = 100; std::cout << "myvector contains:"; printvector(myvector); }
void tc_resize() { std::cout << "-----\t resize" << '\n'; MySTL::vector<int> myvector; // set some initial content: for (int i = 1; i < 10; i++) myvector.push_back(i); myvector.resize(5); myvector.resize(8, 100); myvector.resize(12); std::cout << "myvector contains:"; printvector(myvector); }
void tc_erase() { std::cout << "-----\t erase" << '\n'; MySTL::vector<int> myvector; // set some values (from 1 to 10) for (int i = 1; i <= 10; i++) myvector.push_back(i); // erase the 6th element myvector.erase(myvector.begin() + 5); // erase the first 3 elements: myvector.erase(myvector.begin(), myvector.begin() + 3); std::cout << "myvector contains:"; printvector(myvector); }
int main(int argc, char** argv) { int b[DIM]; double x=0; int e=0; printf("Input x: "); scanf("%lf", &x); dec2float(x, DIM, b, &e); if(x <= 0) { printf("x has to be greater than zero!\n"); return EXIT_SUCCESS; } printf("x with base 2: "); printvector(b, DIM); printf("The exponent e is: %d\n", e); return EXIT_SUCCESS; }
int main(int argc, char** argv) { /* * Matrix a element R^mn where A_jk = a_j+k*m of Matrix A * element R^mxn */ double a[DIM_M*DIM_N]; double b[DIM_N*DIM_O]; double c[DIM_M*DIM_O]; printf("Input matrix A[%d][%d]:\n", DIM_M, DIM_N); scanvector(a, DIM_M*DIM_N); printf("Input matrix B[%d][%d]:\n", DIM_N, DIM_O); scanvector(b, DIM_N*DIM_O); matmult(a, b, c, DIM_M, DIM_N, DIM_O); printf("Solved matrix C[%d][%d]:\n", DIM_M, DIM_O); printvector(c, DIM_M*DIM_O); return EXIT_SUCCESS; }
// element access void tc_elementAccess() { std::cout << "-----\t element access" << '\n'; MySTL::vector<int> myvector(10); // 10 zero-initialized elements MySTL::vector<int>::size_type sz = myvector.size(); // assign some values: for (unsigned i = 0; i < sz; i++) myvector[i] = i; // reverse vector using operator[]: for (unsigned i = 0; i < sz / 2; i++) { int temp; temp = myvector[sz - 1 - i]; myvector[sz - 1 - i] = myvector[i]; myvector[i] = temp; } std::cout << "myvector contains:"; printvector(myvector); }
void tc_insert() { std::cout << "-----\t insert" << '\n'; MySTL::vector<int> myvector(3, 100); MySTL::vector<int>::iterator it; it = myvector.begin(); it = myvector.insert(it, 200); myvector.insert(it, 2, 300); // "it" no longer valid, get a new one: it = myvector.begin(); MySTL::vector<int> anothervector(2, 400); myvector.insert(it + 2, anothervector.begin(), anothervector.end()); int myarray[] = { 501, 502, 503 }; myvector.insert(myvector.begin(), myarray, myarray + 3); std::cout << "myvector contains:"; printvector(myvector); }
void tc_constructor() { std::cout << "-----\t constructor" << '\n'; vector<int> v1; // constructor: default v1 = { 1, 2, 3, 4, 5 }; // assign content: initializer list printvector(v1); vector<int> v2(5, 10); // constructor: fill printvector(v2); vector<int> v3({ 4, 5, 6, 7, 8 }); // constructor: initializer list printvector(v3); vector<int> v4(v1); // copy constructor printvector(v4); vector<int> v5 = v1; // assign content: copy printvector(v5); // the iterator constructor can also be used to construct from arrays: int myints[] = { 16, 2, 77, 29 }; MySTL::vector<int> v6(myints, myints + sizeof(myints) / sizeof(int)); printvector(v6); }
double SearchLambda(int *nn,double *eta,int len) { int i,j,k,ind[2]; int *preindex=malloc(sizeof(int)*len),*index=malloc(sizeof(int)*len); double step,*val=malloc(sizeof(double)*len),pre1,pre2,*val_c=malloc(sizeof(double)*len),del=0; double *preval=malloc(sizeof(double)*(1+len)); memcpy(preval,eta,sizeof(double)*(len+1)); for(i=0; i<len; i++) { val[i]=psi(nn[i]+eta[i+1])-psi(eta[i+1]); val_c[i]=val[i]; index[i]=i; del-=val[i]; } quicksort(val_c,index,0,len-1); //coarse to fine for(k=0;k<8;k++){ step=S_STEP*(int)pow(10,7-k); //for(k=0;k<7;k++){ //step=S_STEP*(int)pow(10,6-k); memset(preindex,0,len*sizeof(int)); while(samevector(preindex,index,len)==0) { memcpy(preindex,index,sizeof(int)*len); for(i=0; i<(int)len/2; i++) { ind[0]=index[i]; ind[1]=index[len-1-i]; index[i]=i; index[len-1-i]=len-1-i; /*for all pairs */ //printf("aaa %d,%d,%f,%f,%f,%f\n ",ind[0],ind[1],eta[1+ind[0]],eta[1+ind[1]],val[ind[0]],val[ind[1]]); // //if((eta[1+ind[0]]<0 || eta[1+ind[1]]<0)){ //printf("input err: %d,%f,%f,%f\n", k,eta[1+ind[0]], eta[1+ind[1]],step); //} if(val[ind[0]]>val[ind[1]]){ printf("sort err: %d,%d,%f,%f,%f,%f,%f\n", k,i,val_c[i],val_c[len-i-1],val[ind[0]], val[ind[1]],step); printv1(len,index); //printv1(len,nn); printvector(len,val_c); printvector(len,val); } while(val[ind[0]]<val[ind[1]] && eta[1+ind[0]]-step>0) { pre1=val[ind[0]]; pre2=val[ind[1]]; eta[1+ind[0]]-= step; eta[1+ind[1]]+= step; val[ind[0]]=psi(eta[1+ind[0]]+nn[ind[0]])-psi(eta[1+ind[0]]); val[ind[1]]=psi(eta[1+ind[1]]+nn[ind[1]])-psi(eta[1+ind[1]]); //if(eta[1+ind[0]]>0){ //printf("gg %d,%d,%f,%f,%f,%f\n ",ind[0],ind[1],eta[1+ind[0]],eta[1+ind[1]],val[ind[0]],val[ind[1]]); //} // if( eta[1+ind[0]]<0 || eta[1+ind[1]]<0){ // printf("iteration err: %d,%f,%f,%f,%f,%f\n", k,val[ind[0]],val[ind[1]],eta[1+ind[0]], eta[1+ind[1]],step); // } } //be careful about the equal case... if(val[ind[0]]>val[ind[1]]) { /*one step back*/ eta[1+ind[0]]+= step; eta[1+ind[1]]-= step; //if( eta[1+ind[0]]<0 || eta[1+ind[1]]<0){ //printf("add back err: %d,%f,%f,%f,%f,%f,%f,%f\n", k,val[ind[0]],val[ind[1]],pre1,pre2,eta[1+ind[0]], eta[1+ind[1]],step); //} val[ind[0]]=pre1; val[ind[1]]=pre2; //printf("fff %d,%d,%f,%f,%f,%f\n ",ind[0],ind[1],eta[1+ind[0]],eta[1+ind[1]],val[ind[0]],val[ind[1]]); //if(val[ind[0]]>val[ind[1]]) { // printf("pre_wrong...%d,%f,%f\n",k,val[ind[0]],val[ind[1]]); //} } } if (len%2==1){ index[(len-1)/2]=(len-1)/2; } memcpy(val_c,val,sizeof(double)*len); quicksort(val_c,index,0,len-1); //printv1(len,index); //printv1(len,nn); //printvector(len+1,eta); } } //printvector(len,val); //printvector(len+1,eta); for(i=0; i<len; i++) { del+=val[i]; } free(preindex); free(index); free(val); free(val_c); return del; }
void DumpVector(Expression_vector& list) { std::ostringstream strstr; printvector(strstr, list); message(strstr.str()); }
pid_t ProcessBuf::open(const std::vector<std::string>& av) { LOG(DBG,"ProcessBuf::open" << printvector(av)); #ifdef WIN32 SECURITY_ATTRIBUTES saAttr; ZeroMemory(&saAttr, sizeof(saAttr)); // Set the bInheritHandle flag so pipe handles are inherited. saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL; // Create a pipe for the child process's STDOUT. if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)) return 0; // Ensure the read handle to the pipe for STDOUT is not inherited. if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)) return 0; // Create a pipe for the child process's STDIN. if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)) return 0; // Ensure the write handle to the pipe for STDIN is not inherited. if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0)) return 0; STARTUPINFO startupInfo; ZeroMemory(&processInformation, sizeof(processInformation)); ZeroMemory(&startupInfo, sizeof(startupInfo)); startupInfo.cb = sizeof(startupInfo); startupInfo.hStdOutput = g_hChildStd_OUT_Wr; startupInfo.hStdError = g_hChildStd_OUT_Wr; startupInfo.hStdInput = g_hChildStd_IN_Rd; startupInfo.dwFlags |= STARTF_USESTDHANDLES; std::stringstream args; for (uint32_t i = 1; i < av.size(); i++) { args << " " << av[i]; } std::string argstr = args.str(); // get PATH variable DWORD bufferSize = 65535; //Limit according to http://msdn.microsoft.com/en-us/library/ms683188.aspx std::string buff; buff.resize(bufferSize); std::string paths = std::string(::getenv("PATH")) + ";;"; // search for dlv in all directories listed in PATH bool result = false; while (!result && paths.find(";") != std::wstring::npos) { std::string path = buff.substr(0, paths.find(";")); path += (path.size() > 0 ? "\\" : "") + av[0]; paths = paths.substr(buff.find(";") + 1); std::string cmdstr = path; result = (CreateProcess((LPCSTR)cmdstr.c_str(), (LPSTR)argstr.c_str(), NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo, &processInformation) == TRUE); } if (!result) { std::cout << GetLastError() << std::endl; } return processInformation.dwProcessId; #elif defined(POSIX) // close before re-open it if (process != -1) { int ret = close(); if (ret != 0) { return ret < 0 ? ret : -ret; } } outpipes[0] = 0; outpipes[1] = 0; inpipes[0] = 0; inpipes[1] = 0; // we want a full-duplex stream -> create two pairs of pipes if (::pipe(outpipes) < 0) { ::perror("pipes"); return -1; } if (::pipe(inpipes) < 0) { ::perror("pipes"); return -1; } // create a new process process = ::fork(); switch (process) { case -1: // error ::perror("fork"); ::exit(process); break; case 0: // child { // setup argv char* argv[av.size() + 1]; int i = 0; for (std::vector<std::string>::const_iterator it = av.begin(); it != av.end(); it++) { std::string::size_type size = it->size(); argv[i] = new char[size + 1]; it->copy(argv[i], size); argv[i][size] = '\0'; i++; } argv[i] = NULL; // redirect stdin and stdout and stderr if (::dup2(outpipes[1], STDOUT_FILENO) < 0) { ::perror("dup2"); ::exit(1); } if (::dup2(outpipes[1], STDERR_FILENO) < 0) { ::perror("dup2"); ::exit(1); } if (::dup2(inpipes[0], STDIN_FILENO) < 0) { ::perror("dup2"); ::exit(1); } // stdout and stdin is redirected, close unneeded filedescr. ::close(outpipes[0]); ::close(outpipes[1]); ::close(inpipes[0]); ::close(inpipes[1]); // execute command, should not return WARNING("TODO handle signals to parent process (pass on to children s.t. child process is not reparented to init)") ::execvp(*argv, argv); // just in case we couldn't execute the command ::exit(127); } break; default: // parent // close writing end of the output pipe ::close(outpipes[1]); outpipes[1] = -1; // close reading end of the input pipe ::close(inpipes[0]); inpipes[0] = -1; break; } return process; #else #error Either POSIX or WIN32 must be defined #endif }