void ListExample::ExampleConstructor(void) { // constructors used in the same order as described above: list<int> first; // empty list of ints // khởi tạo một danh sách trống cout << "The contents of first are: "; PrintListInt(first); list<int> second (4,100); // four ints with value 100 // khởi tạo 4 số int với giá trị 100 cout << "The contents of second are: "; PrintListInt(second); list<int> third (second.begin(),second.end()); // iterating through second // khởi tạo từ một danh sách khác cout << "The contents of third are: "; PrintListInt(third); list<int> fourth (third); // a copy of third // sao chép danh sách thứ 3 cout << "The contents of fourth are: "; PrintListInt(fourth); // the iterator constructor can also be used to construct from arrays: // sử dụng con trỏ khởi tạo từ một mảng có sẵn int myints[] = {16,2,77,29}; list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); cout << "The contents of fifth are: "; PrintListInt(fifth); }
int main () { //using the connstructors in order //iterating through the lists std::list<int> first; // empty list of ints std::list<int> second (4,100); // (4,50) 4, four ints with value 50 std::list<int> third (second.begin(),second.end()); // iterating through second std::list<int> fourth (third); // fourth int is a copy of third // the iterator constructors: int myints[] = {16,20,40,19}; int *it;// declaring algorithm std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); std::cout << "The contents of fifth are: "; for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++) std::cout << *it << ' '; std::cout << '\n'; //using algorithm find it = std::find (myints, myints+6, 20); if (it != myints+6) std::cout << "Element found in myints: " << *it << '\n'; else std::cout << "Element not found in myints\n"; return 0; }
int main () { std::set<int> first; // empty set of ints int myints[]= {10,20,30,40,50}; std::set<int> second (myints,myints+5); // range //find and delete std::set<int>::iterator it; it = second.find (50); second.erase (it); //insert second.insert(1); std::set<int> third (second); // a copy of second std::set<int> fourth (second.begin(), second.end()); // iterator ctor. bool(*fn_pt)(int,int) = fncomp; std::set<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare std::set<int,classcomp> fifth(myints,myints+5); // class as Compare std::set<int,classcomp>::iterator iter = fifth.begin(); while(iter != fifth.end()) { std::cout<<*iter<<std::endl; iter++; } return 0; }
TEST(AffineTransform, ScaleFloatSize) { WebCore::AffineTransform test(6.0, 5.0, 4.0, 3.0, 2.0, 1.0); testValueConstruction(test); WebCore::FloatSize first(1.0f, 2.0f); test.scale(first); EXPECT_DOUBLE_EQ(6.0, test.a()); EXPECT_DOUBLE_EQ(5.0, test.b()); EXPECT_DOUBLE_EQ(8.0, test.c()); EXPECT_DOUBLE_EQ(6.0, test.d()); EXPECT_DOUBLE_EQ(2.0, test.e()); EXPECT_DOUBLE_EQ(1.0, test.f()); WebCore::FloatSize second(1.0f, 0.5f); test.scale(second); testValueConstruction(test); WebCore::FloatSize third(2.0f, 1.0f); test.scale(third); EXPECT_DOUBLE_EQ(12.0, test.a()); EXPECT_DOUBLE_EQ(10.0, test.b()); EXPECT_DOUBLE_EQ(4.0, test.c()); EXPECT_DOUBLE_EQ(3.0, test.d()); EXPECT_DOUBLE_EQ(2.0, test.e()); EXPECT_DOUBLE_EQ(1.0, test.f()); WebCore::FloatSize fourth(0.5f, 1.0f); test.scale(fourth); testValueConstruction(test); WebCore::FloatSize fifth(0.5f, 2.0f); test.scale(fifth); EXPECT_DOUBLE_EQ(3.0, test.a()); EXPECT_DOUBLE_EQ(2.5, test.b()); EXPECT_DOUBLE_EQ(8.0, test.c()); EXPECT_DOUBLE_EQ(6.0, test.d()); EXPECT_DOUBLE_EQ(2.0, test.e()); EXPECT_DOUBLE_EQ(1.0, test.f()); }
//Menu algoritmos de repetiçao void load_alg_loop_while (void) { do { printf("\n\t../Loop Algorithms - While \n"); printf("\nPlease choose one of the following options:\n"); printf("_____________________________________________________\n\n"); printf("1......................................................\n"); printf("2......................................................\n"); printf("3......................................................\n"); printf("4......................................................\n"); printf("5......................................................\n"); printf("6......................................................\n"); printf("7......................................................\n"); printf("8.Return\n"); printf("9.Exit\n"); printf("_____________________________________________________\n\n"); printf("\t\tEnter Choice: "); scanf("%u", &choice); system("clear"); switch (choice) { case 1: first(); break; case 2: second(); break; case 3: third(); break; case 4: fourth(); break; case 5: fifth(); break; case 6: sixth(); break; case 7: seventh(); break; case 8:load_alg_loop(); break; case 9: printf("\nQuitting program!\n"); exit(FLAG); break; default: printf("\nInvalid choice!\n"); break; } } while (choice != 9); }
int main () { int myints[]= {10,60,50,20}; std::priority_queue<int> first; std::priority_queue<int> second (myints,myints+4); std::priority_queue<int, std::vector<int>, std::greater<int> > third (myints,myints+4); // using mycomparison: typedef std::priority_queue<int,std::vector<int>,mycomparison> mypq_type; mypq_type fourth; // less-than comparison mypq_type fifth (mycomparison(true)); // greater-than comparison return 0; }
//Menu algoritmos sequenciais void load_vectors (void) { do { printf("\n\t../Vectors \n"); printf("\nPlease choose one of the following options:\n"); printf("_____________________________________________________\n\n"); printf("1.-----------------------------------------------------\n"); printf("2.-----------------------------------------------------\n"); printf("3.-----------------------------------------------------\n"); printf("4.-----------------------------------------------------\n"); printf("5.-----------------------------------------------------\n"); printf("6. Return\n"); printf("7. Exit\n"); printf("_____________________________________________________\n\n"); printf("\t\tEnter Choice: "); scanf("%u", &choice); system("clear"); switch(choice) { case 1: first(); break; case 2: second(); break; case 3: third(); break; case 4: fourth(); break; case 5: fifth(); break; case 6: load_menu(); break; case 7: printf("\nQuitting program!\n"); exit(FLAG); break; default: printf("\nInvalid choice!\n"); break; } } while (choice != 11); }
int main () { // constructors used in the same order as described above: std::vector<int> first; // empty vector of ints std::vector<int> second (4,100); // four ints with value 100 std::vector<int> third (second.begin(),second.end()); // iterating through second std::vector<int> fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); std::cout << "The contents of fifth are:"; for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
int main () { // constructors used in the same order as described above: forward_list<int> first; // default: empty forward_list<int> second (3,77); // fill: 3 seventy-sevens forward_list<int> third (second.begin(), second.end()); // range initialization forward_list<int> fourth (third); // copy constructor forward_list<int> fifth (std::move(fourth)); // move ctor. (fourth wasted) forward_list<int> sixth = {3, 52, 25, 90}; // initializer_list constructor std::cout << "first:" ; for (int& x: first) std::cout << " " << x; std::cout << '\n'; std::cout << "second:"; for (int& x: second) std::cout << " " << x; std::cout << '\n'; std::cout << "third:"; for (int& x: third) std::cout << " " << x; std::cout << '\n'; std::cout << "fourth:"; for (int& x: fourth) std::cout << " " << x; std::cout << '\n'; std::cout << "fifth:"; for (int& x: fifth) std::cout << " " << x; std::cout << '\n'; std::cout << "sixth:"; for (int& x: sixth) std::cout << " " << x; std::cout << '\n'; return 0; }
int main () { std::map<char,int> first; first['a']=10; first['b']=30; first['c']=50; first['d']=70; std::map<char,int> second (first.begin(),first.end()); std::map<char,int> third (second); std::map<char,int,classcomp> fourth; // class as Compare bool(*fn_pt)(char,char) = fncomp; std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare return 0; }
void AuxCalphadEnergy::computeBarrier() { Real first(0); Real second(0); Real third(0); Real fourth(0); Real fifth(0); Real sixth; if(_n_OP_vars == 1) sixth = 0; else sixth = 1; for (unsigned int i=0; i<_n_OP_vars; i++) { first += std::pow((*_OP[i])[_qp], 2); second += std::pow((*_OP[i])[_qp], 3); third += std::pow((*_OP[i])[_qp], 4); Real square_sum(0); Real quad_sum(0); for (unsigned int j=0; j < _n_OP_vars; j++) { if (j > i) square_sum += std::pow((*_OP[j])[_qp], 2); if (j != i) quad_sum += std::pow((*_OP[j])[_qp], 4); } fourth += ( std::pow((*_OP[i])[_qp], 2) )*square_sum; fifth += ( std::pow((*_OP[i])[_qp], 2) )*quad_sum; sixth *= std::pow((*_OP[i])[_qp], 2); } _g = first - 2*second + third + fourth + fifth + sixth; }
/* * * Summary: 1. string can be consider a descendant of vectors 1.1 every single oper you can do with vectors you can do with strings 1.2 important string extras, e.g. "+" , c_str, find, substr, compare * * */ int main() { // constructors used in the same order as described above: std::vector<int> first; // empty vector of ints std::vector<int> second (4,100); // four ints with value 100 std::vector<int> third (second.begin(),second.end()); // iterating through second std::vector<int> fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); vector<int> vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // C++11 vec.erase(vec.begin() + 3); // erase 4th element vec.erase(vec.begin(), vec.begin() + 3); // erase from begin() to begin() + 2 but not begin()+3 // easier to understand thinking of vec.erase(vec.begin(), vec.end()); for(int i = 0; i < vec.size(); i++) cout << vec[i] << " "; cout << endl; return 0; }
void eval(BOOLEAN do_gc) { static unsigned int count = 0; OBJECT_PTR exp = car(reg_next_expression); OBJECT_PTR opcode = car(exp); pin_globals(); if(do_gc) { count++; if(count == GC_FREQUENCY) { gc(false, true); count = 0; } } if(opcode == APPLY && profiling_in_progress) { last_operator = reg_accumulator; if(prev_operator != NIL) { OBJECT_PTR operator_to_be_used; hashtable_entry_t *e; unsigned int count; unsigned int mem_alloc; double elapsed_wall_time; double elapsed_cpu_time; double temp1 = get_wall_time(); clock_t temp2 = clock(); unsigned int temp3 = memory_allocated(); profiling_datum_t *pd = (profiling_datum_t *)malloc(sizeof(profiling_datum_t)); if(IS_SYMBOL_OBJECT(prev_operator)) operator_to_be_used = prev_operator; else { OBJECT_PTR res = get_symbol_from_value(prev_operator, reg_current_env); if(car(res) != NIL) operator_to_be_used = cdr(res); else operator_to_be_used = cons(LAMBDA, cons(get_params_object(prev_operator), cons(car(get_source_object(prev_operator)), NIL))); } e = hashtable_get(profiling_tab, (void *)operator_to_be_used); if(e) { profiling_datum_t *pd = (profiling_datum_t *)e->value; count = pd->count + 1; elapsed_wall_time = pd->elapsed_wall_time + temp1 - wall_time_var; elapsed_cpu_time = pd->elapsed_cpu_time + (temp2 - cpu_time_var) * 1.0 / CLOCKS_PER_SEC; mem_alloc = pd->mem_allocated + temp3 - mem_alloc_var; hashtable_remove(profiling_tab, (void *)operator_to_be_used); free(pd); } else { count = 1; elapsed_wall_time = temp1 - wall_time_var; elapsed_cpu_time = (temp2 - cpu_time_var) * 1.0 / CLOCKS_PER_SEC; mem_alloc = temp3 - mem_alloc_var; } pd->count = count; pd->elapsed_wall_time = elapsed_wall_time; pd->elapsed_cpu_time = elapsed_cpu_time; pd->mem_allocated = mem_alloc; hashtable_put(profiling_tab, (void *)operator_to_be_used, (void *)pd); } wall_time_var = get_wall_time(); cpu_time_var = clock(); mem_alloc_var = memory_allocated(); prev_operator = reg_accumulator; } if(opcode == HALT) { halt_op(); } else if(opcode == REFER) { if(refer(CADR(exp))) return; reg_next_expression = CADDR(exp); } else if(opcode == CONSTANT) { if(constant(CADR(exp))) return; reg_next_expression = CADDR(exp); } else if(opcode == CLOSE) { if(closure(exp)) return; reg_next_expression = fifth(exp); } else if(opcode == MACRO) { if(macro(exp)) return; reg_next_expression = CADDDDR(exp); } else if(opcode == TEST) { if(reg_accumulator != NIL) reg_next_expression = CADR(exp); else reg_next_expression = CADDR(exp); } //Not using this WHILE; reverting //to macro definition, as this //version doesn't handle (BREAK) else if(opcode == WHILE) { OBJECT_PTR cond = CADR(exp); OBJECT_PTR body = CADDR(exp); OBJECT_PTR ret = NIL; while(1) { OBJECT_PTR temp = reg_current_stack; reg_next_expression = cond; while(car(reg_next_expression) != NIL) { eval(false); if(in_error) return; } if(reg_accumulator == NIL) break; reg_next_expression = body; while(car(reg_next_expression) != NIL) { eval(false); if(in_error) return; } //to handle premature exits //via RETURN-FROM if(reg_current_stack != temp) return; ret = reg_accumulator; } reg_accumulator = ret; reg_next_expression = CADDDR(exp); } else if(opcode == ASSIGN) { if(assign(CADR(exp))) return; reg_next_expression = CADDR(exp); } else if(opcode == DEFINE) { if(define(CADR(exp))) return; reg_next_expression = CADDR(exp); } else if(opcode == CONTI) { if(conti()) return; reg_next_expression = CADR(exp); } else if(opcode == NUATE) //this never gets called { reg_current_stack = CADR(exp); reg_accumulator = CADDR(exp); reg_current_value_rib = NIL; reg_next_expression = cons(CONS_RETURN_NIL, cdr(reg_next_expression)); } else if(opcode == FRAME) { if(frame(exp)) return; reg_next_expression = CADDR(exp); } else if(opcode == ARGUMENT) { if(argument()) return; reg_next_expression = CADR(exp); } else if(opcode == APPLY) { apply_compiled(); } else if(opcode == RETURN) { return_op(); } }
//Menu algorithmos condicionais void load_alg_conditional(void) { do { printf("\n\t../Conditional Algorithms \n"); printf("\nPlease choose one of the following options:\n"); printf("______________________________________________________\n\n"); printf("1.------------------------------------------------------\n"); printf("2.------------------------------------------------------\n"); printf("3.------------------------------------------------------\n"); printf("4.------------------------------------------------------\n"); printf("5.------------------------------------------------------\n"); printf("6.------------------------------------------------------\n"); printf("7.------------------------------------------------------\n"); printf("8.------------------------------------------------------\n"); printf("9.------------------------------------------------------\n"); printf("10.-----------------------------------------------------\n"); printf("11.-----------------------------------------------------\n"); printf("12.Return...\n"); printf("13.Exit\n"); printf("_____________________________________________________\n\n"); printf("\t\tEnter Choice: "); scanf("%u", &choice); system("clear"); switch(choice) { case 1: first(); break; case 2: second(); break; case 3: third(); break; case 4: fourth(); break; case 5: fifth(); break; case 6: sixth(); break; case 7: seventh(); break; case 8: eighth(); break; case 9: ninth(); break; case 10: tenth(); break; case 11: eleventh(); break; case 12: load_menu(); break; case 13: printf("\n Quitting program!\n"); exit(FLAG); break; default: printf("\n Invalid choice!\n"); break; } } while (choice != 8); }
extern "C" void * haloThread(void * id){ char * HaloID = (char *)id; cout << "Analyzing halo with ID " << HaloID << endl; int numberOfParticles, nCols; double ** positionsArray; Particle ** particlesArray; FILE * output; double radius = 0; double u; FILE * positionFile = getPositionFile(HaloID); //make the file path if (positionFile == NULL) printf("POSITION FILE NULL\n"); numberOfParticles = getNumberOfRows(positionFile); //count number of particles in file nCols = getNumberOfColumns(positionFile); //get number of columns in file (3) particlesArray = new ParticleArray[numberOfParticles]; //make an array of particles for (int i = 0; i < numberOfParticles; i++){ particlesArray[i] = new Particle(); //each location in the array is a particle (with positions) } positionsArray = assembleArray(positionFile,numberOfParticles,nCols); for (int i = 0; i<numberOfParticles;i++){ particlesArray[i]->position.x = positionsArray[i][0]; particlesArray[i]->position.y = positionsArray[i][1]; particlesArray[i]->position.z = positionsArray[i][2]; free(positionsArray[i]); } delete positionsArray; for(int i = 0; i < numberOfParticles; i++){ for(int j = 0; j < numberOfParticles; j++){ radius = sqrt(sq((particlesArray[i]->position.x)-(particlesArray[j]->position.x))+sq((particlesArray[i]->position.y)-(particlesArray[j]->position.y))+sq((particlesArray[i]->position.z)-(particlesArray[j]->position.z))); u = radius/EPSILON; if(u<0.5){ particlesArray[i]->directPotential += GRAVITATIONAL_CONSTANT*PARTICLE_MASS*MASS_MULTIPLIER/EPSILON*(16.0/3.0*sq(u)-48.0/5.0*fourth(u)+32.0/5.0*fifth(u)-14.0/5.0); } else if(u<1){ particlesArray[i]->directPotential += GRAVITATIONAL_CONSTANT*PARTICLE_MASS*MASS_MULTIPLIER/EPSILON*(1.0/(15.0*u)+32.0/3.0*sq(u)-16.0*third(u)+48.0/5.0*fourth(u)-32.0/15.0*fifth(u)-16.0/5.0); } else{ particlesArray[i]->directPotential += GRAVITATIONAL_CONSTANT*PARTICLE_MASS*MASS_MULTIPLIER/EPSILON*(-1.0/u); } } if(i%100000==0){cout <<HaloID <<" - " <<i <<"th particle's potential calculated" <<endl;} } output = makeOutputFileName(HaloID); //make output file //print output for(int i = 0; i<numberOfParticles; i++){ fprintf(output,"%f%c\n",particlesArray[i]->directPotential,delimeter); } //free memory for (int i = 0; i < numberOfParticles; i++){ delete particlesArray[i]; } delete particlesArray; //close files fclose(positionFile); fclose(output); // Reduce our active threads count. pthread_mutex_lock(numberOfThreadsMutex); numberOfThreads--; pthread_mutex_unlock(numberOfThreadsMutex); pthread_cond_signal(threadAvailable); // Signal to wake up main. cout <<"Halo " << HaloID <<" done." <<endl; }
void WCModeSketchConicTwoPointCreate::OnMouseDown(const WCMouseButton &button) { //Restart if right click if (button == WCMouseButton::Right()) { if(this->_stage == 0) return; //Delete first and second points, and first line delete this->_p1; delete this->_p2; delete this->_line1; //Post second click if (this->_stage > 1) { //Delete third point delete this->_p3; //Post third click if (this->_stage > 2) { //Delete fourth points, and second line delete this->_p4; delete this->_line2; //Post fourth click if (this->_stage > 3) { //Delete fifth point and conic delete this->_p5; if (this->_conic != NULL) { delete this->_conic; this->_conic = NULL; } } } } //Reset the stage this->_stage = 0; return; } else if (button != WCMouseButton::Left()) return; if (this->_stage == 0) { //Record location this->_first = WCVector4(this->_xSuggest, this->_ySuggest, 0.0, 1.0); WCVector4 pos( this->_workbench->Sketch()->ReferencePlane()->TransformMatrix() * this->_first ); //Create first and second point this->_p1 = new WCGeometricPoint(pos); this->_p1->Color(WCSketchFeature::InprocessColor); this->_p1->Size(WCSketchFeature::PointSize); this->_p2 = new WCGeometricPoint(pos); this->_p2->Color(WCSketchFeature::InprocessColor); this->_p2->Size(WCSketchFeature::PointSize); //Create the first line this->_line1 = new WCGeometricLine(pos, pos); this->_line1->Color(WCSketchFeature::InprocessColor); this->_line1->Thickness(WCSketchFeature::LineThickness); //Increment the stage this->_stage++; } else if (this->_stage == 1) { //Record location this->_second = WCVector4(this->_xSuggest, this->_ySuggest, 0.0, 1.0); WCVector4 pos( this->_workbench->Sketch()->ReferencePlane()->TransformMatrix() * this->_second ); //Create third point this->_p3 = new WCGeometricPoint(pos); this->_p3->Color(WCSketchFeature::InprocessColor); this->_p3->Size(WCSketchFeature::PointSize); //Increment the stage this->_stage++; } else if (this->_stage == 2) { //Record location this->_third = WCVector4(this->_xSuggest, this->_ySuggest, 0.0, 1.0); WCVector4 pos( this->_workbench->Sketch()->ReferencePlane()->TransformMatrix() * this->_third ); //Create fourth point and second line this->_p4 = new WCGeometricPoint(pos); this->_p4->Color(WCSketchFeature::InprocessColor); this->_p4->Size(WCSketchFeature::PointSize); this->_line2 = new WCGeometricLine(pos, pos); this->_line2->Color(WCSketchFeature::InprocessColor); this->_line2->Thickness(WCSketchFeature::LineThickness); //Increment the stage this->_stage++; } else if (this->_stage == 3) { //Record location this->_fourth = WCVector4(this->_xSuggest, this->_ySuggest, 0.0, 1.0); WCVector4 pos( this->_workbench->Sketch()->ReferencePlane()->TransformMatrix() * this->_fourth ); //Create fifth point this->_p5 = new WCGeometricPoint(pos); this->_p5->Color(WCSketchFeature::InprocessColor); this->_p5->Size(WCSketchFeature::PointSize); //Create the visualization conic WCVector4 firstTangent = (this->_p2->Data() - this->_p1->Data()).Normalize(true); WCVector4 secondTangent = (this->_p4->Data() - this->_p3->Data()).Normalize(true); this->_conic = WCNurbsCurve::Conic(this->_creator->Document()->Scene()->GeometryContext(), this->_p1->Data(), firstTangent, this->_p3->Data(), secondTangent, this->_p5->Data()); //Check to make sure non-null return if (this->_conic != NULL) { this->_conic->Color(WCSketchFeature::InprocessColor); this->_conic->Thickness(WCSketchFeature::LineThickness); } //Increment the stage this->_stage++; } else if (this->_stage == 4) { //Only execute if there is a valid conic currently if (this->_conic != NULL) { //Get the last click WCVector4 fifth(this->_xSuggest, this->_ySuggest, 0.0, 1.0); //Find the tangents WCVector4 firstTangent = (this->_second - this->_first).Normalize(true); WCVector4 secondTangent = (this->_fourth - this->_third).Normalize(true); //Create and execute action WCAction *action = WCSketchConicTwoPoint::ActionCreate(this->_workbench->Sketch(), "", this->_first, firstTangent, this->_third, secondTangent, fifth); this->_workbench->Sketch()->Document()->ExecuteAction(action); //Destroy the visualization conic delete this->_conic; this->_conic = NULL; } //Destroy all objects delete this->_p1; delete this->_p2; delete this->_line1; delete this->_p3; delete this->_p4; delete this->_line2; delete this->_p5; //Reset the stage this->_stage = 0; } }