myVector myVector::Normalize() { double dist=sqrt(x*x+y*y+z*z); if(dist!=0) { return myVector(x/dist,y/dist,z/dist); } else return myVector(); }
int main() { map<const string, block*> myMap; vector<pair<const string, block*> > myVector(myMap.begin(), myMap.end()); sort(myVector.begin(), myVector.end(), vecotrCompare()); }
void Test3D::compute(DummyMsg *m) { /*char buf[2048]; //for debugging sprintf(buf, "Compute called :: thisIndex = { %d, %d }, m->section = %d", thisIndex.x, thisIndex.y, m->section); //debugging */ //cookie stuff. int section = m->section; int oldLength = cookies.length(); int newLength = section + 1; if(oldLength < newLength) { //only if the section proxy calling //this method is a new section proxy //(that hasn't already called this method //before, increase the size of the cookies array. cookies.resize(section+1); for (int i = oldLength; i < newLength; i++) { CkSectionInfo tempCookie; cookies[i] = tempCookie; } } CkGetSectionInfo(cookies[section], m); //the index of the cookie identifies, which section proxy we are refering to. /* DEBUG CkSectionInfo si = cookies[section]; sprintf(buf + strlen(buf), ", Cookie:" " type = %d" " pe = %d" " sCookie = { redNo:%d, val:%p }\n", (int)(si.type), si.pe, si.sInfo.sCookie.redNo, si.sInfo.sCookie.val ); CkPrintf(buf); */ CkVec<double> myVector(vectorSize); //creates elements with vectorSize elements for(int i = 0; i < vectorSize; i++) { myVector[i] = doubleVector[i]; } CkMulticastMgr *mCastGrp = CProxy_CkMulticastMgr(mCastGrpID).ckLocalBranch(); CkCallback cb(CkIndex_Main::reportSum(NULL), mainProxy); mCastGrp->contribute(sizeof(double)*vectorSize, myVector.getVec(), CkReduction::sum_double, cookies[section], cb); delete m; };
void TestSTL::predicate_example() { // unary predicate example int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sizeArray = sizeof(array)/sizeof(array[0]); std::vector<int> myVector(array, array + sizeArray); //fill a vector /** * std::countf_if algorithm: counts the number of elements that satisfy the * condition we supply (e.g isEven, isNegative) */ int res = std::count_if(myVector.begin(), myVector.end(), NumericUtilities::isEven); //returns the how many numbers are even TESTIFY_ASSERT(res == 6); res = std::count_if(myVector.begin(), myVector.end(), NumericUtilities::isNegative); //returns how many numbers are negative TESTIFY_ASSERT( res == 0); //binary predicates: Employee staff[] = { Employee(3, "Jonny"), Employee(2, "Bob"), Employee(20, "Filomela")}; int staffSize = sizeof(staff)/sizeof(staff[0]); std::vector<Employee> myStaff( staff, staff + staffSize); //fill a vector with these employees /** * std::sort algorithm: sorts the elements, from a range, into ascending order */ //sort by experience std::sort(myStaff.begin(), myStaff.end(), lessExperience); //supply a binary predicate for comparing employees TESTIFY_ASSERT( myStaff[0].getExperience() == 2); TESTIFY_ASSERT( myStaff[1].getExperience() == 3); TESTIFY_ASSERT( myStaff[2].getExperience() == 20); }
int main(int argc, char **argv) { std::vector<int> V; // create empty vector int type V.reserve(10); // reserve mem for 10 elements int type /* equal: */ std::vector<int> myVector(10); // allocate vector, init zeroes std::cout << "Original array: "; std::vector<int> myV(10); // create empty vector int type for (uint i = 0; i < myV.size(); ++i) { myV[i] = i; std::cout << myV[i] << ' '; } std::cout << "\nCopied array: "; std::vector<int> cpV(myV); // Init with copy for (uint i = 0; i < cpV.size(); ++i) std::cout << cpV[i] << ' '; std::cout << std::endl; return 0; }
myVector myVector::add(myVector v2) { return myVector(x+v2.x,y+v2.y,z+v2.z); }
myVector myVector::sub(myVector v2) { return myVector(x-v2.x,y-v2.y,z-v2.z); }
myVector myVector::productReal(double number) { return myVector(x*number,y*number,z*number); }
/** * Dynamically determines when to rearrange the pixel * allocations for the ray tracer step */ int step_decompose_image::execute ( const int & frame, ray_tracer & rt ) const { int current_frame = frame; // Consume // Execute - compute an initial even distribution int distributed = 0; int current_fragment = -1; std::vector<Point2D*> points[rt.number_fragments]; // No history yet, do an even distribution if(current_frame == 0) { int distribution = ((double)rt.image_width * (double)rt.image_height) / (double)rt.number_fragments; for(int i = 0; i < rt.image_width; ++i) { for(int j = 0; j < rt.image_height; ++j) { if((current_fragment + 1) < rt.number_fragments && distributed % distribution == 0) { points[++current_fragment] = std::vector<Point2D*>(); } points[current_fragment].push_back(new Point2D(i, j)); distributed++; } } } else { // Gather our previous pixel locations and times to compute each of them std::vector<Point2D*> all_points; std::vector<double> average_times; double total_time = 0; for(int fragment = 0; fragment < rt.number_fragments; fragment++) { std::vector<Point2D*> points; double time_to_trace; rt.pixel_locations.get(pair(current_frame - 1, fragment), points); rt.execution_time.get(pair(current_frame - 1, fragment), time_to_trace); double average_per_pixel = time_to_trace / (double) points.size(); std::cout << time_to_trace << ", "; for(int i = 0; i < points.size(); i++) { all_points.push_back(points[i]); average_times.push_back(average_per_pixel); } total_time += time_to_trace; } bool random_order = true; if(random_order) { std::vector<double> myVector(average_times.size()); std::vector<Point2D*> myVector2(all_points.size()); std::vector<Point2D*>::iterator it2 = all_points.begin(); int lastPos = 0; for(std::vector<double>::iterator it = average_times.begin(); it != average_times.end(); it2++, it++, lastPos++) { int insertPos = rand() % (lastPos + 1); if (insertPos < lastPos) { myVector[lastPos] = myVector[insertPos]; myVector2[lastPos] = myVector2[insertPos]; } myVector[insertPos] = *it; myVector2[insertPos] = *it2; } all_points = std::vector<Point2D*>(myVector2.begin(), myVector2.end()); average_times = std::vector<double>(myVector.begin(), myVector.end()); } std::cout << "\n"; int total_points = all_points.size(); double goal_time = total_time / 8.0; double current_time = 0; int current_fragment = 0; points[current_fragment] = std::vector<Point2D*>(); double new_total_time = 0; for(int i = 0; i < total_points; i++) { double time_for_pixel = average_times.back(); Point2D * point = all_points.back(); new_total_time += time_for_pixel; average_times.pop_back(); all_points.pop_back(); if(current_fragment + 1 < rt.number_fragments && (current_time + time_for_pixel) > goal_time) { points[++current_fragment] = std::vector<Point2D*>(); current_time = 0; } current_time += time_for_pixel; points[current_fragment].push_back(point); } } // Produce for(int fragment = 0; fragment < rt.number_fragments; fragment++) { std::cout << points[fragment].size() << ", "; rt.pixel_locations.put(pair(current_frame, fragment), points[fragment]); rt.frame_fragment.put(pair(frame, fragment)); } std::cout << "\n"; // Clean up //delete [] points; return CnC::CNC_Success; };
void STLMoblet::STL_iterators() { LOG("========================= STL iterators ========================================================================"); LOG("/**"); LOG("* Input iterators: allow only reading elements from a sequence and moving forward, one step,"); LOG("* using operator++. The elements are read with operator*."); LOG("* Provides also operator== and operator!=."); LOG("* An input iterator implemented in STL is for example the one provided by std::istream."); LOG("*/"); LOG("\n"); LOG("/**"); LOG("* Output iterators: allow only writing elements from a sequence and only moving forward, one step,"); LOG("* using operator++. They elements are written with operator*."); LOG("* Provides also operator== and operator!=."); LOG("* An input iterator implemented in STL is for example the one provided by std::ostream."); LOG("*/"); LOG("\n"); LOG("/**"); LOG("* Forward iterators: allow reading and writing elements from a sequence. Only moving forward, one step,"); LOG("* using operator++ is possible. They elements are read and written with operator*."); LOG("* Provides also operator== and operator!=."); LOG("* An input iterator implemented in STL is for example the one provided by std::ostream."); LOG("*/"); LOG("/**"); LOG("* Bidirectional iterators: allow reading and writing elements from a sequence. Allows moving forward and backward,"); LOG("* one step,using operator++/operator--. Their elements are read and written with operator*."); LOG("* Provides also operator== and operator!=."); LOG("* An input iterator implemented in STL is for example the ones provided by list, multiset, map, multimap."); LOG("*/"); LOG("\n"); LOG(" Example using iterators "); int sArray[] = { 0, 11, 22, 33, 44 }; int sArraySize = sizeof(sArray)/sizeof(sArray[0]); std::list<int> myList(sArray, sArray + sArraySize); LOG("Create a list object (myList) and fill it with: 0, 11, 22, 33, 44\n"); LOG("\n"); LOG("/**"); LOG("* create an bidirectional iterator"); LOG("*/"); LOG("std::list<int>::iterator iter;"); std::list<int>::iterator iter; iter = myList.begin(); LOG("iter = myList.begin();"); LOG("\n"); LOG("/**"); LOG("* read operation"); LOG("*/"); int r = *iter; log_to_console(r, "int r = *iter //r = "); LOG("\n"); LOG("/**"); LOG("* write operation"); LOG("*/"); *iter = 99; LOG("*iter = 99; //myList will contain now: 99, 11, 22, 33, 44"); LOG("\n"); LOG("/**"); LOG("* operator++"); LOG("*/"); iter++; log_to_console(*iter, "iter++; //*iter is now: "); LOG("\n"); LOG("/**"); LOG("* operator--"); LOG("*/"); --iter; log_to_console(*iter, "iter--; //*iter is now: "); LOG("\n"); LOG("/**"); LOG("* Random access iterators: allows all operations a normal pointer does: add and subtract integral values,"); LOG("* move forward and backward, one or more steps, use operator[] on it, subtract one iterator from another."); LOG("* It overloads operator<, operator>, operator==, operator!=."); LOG("* An input iterator implemented in STL is for example the ones provided by vector, deque, string."); LOG("*/"); LOG("\n"); LOG("Create vector object (myVector) and fill with: 0, 11, 22, 33, 44"); std::vector<int> myVector(sArray, sArray + sArraySize); //myVector contains now: 0, 11, 22, 33, 44 LOG("\n"); LOG("/**"); LOG("* create an random iterator"); LOG("*/"); std::vector<int>::iterator randomAcessIter; randomAcessIter = myVector.begin(); LOG("std::vector<int>::iterator randomAcessIter;"); LOG("randomAcessIter = myVector.begin();"); LOG("\n"); LOG("/**"); LOG("* read operation"); LOG("*/"); r = *randomAcessIter; log_to_console(r, "r = *randomAcessIter; //r = "); LOG("\n"); LOG("/**"); LOG("* write operation"); LOG("*/"); *randomAcessIter = 99; log_to_console(myVector, "*randomAcessIter = 99;//myVector will contain now: "); LOG("\n"); LOG("/**"); LOG("* operator++"); LOG("*/"); randomAcessIter++; log_to_console(*randomAcessIter, "randomAcessIter++; //*randomAcessIter = "); LOG("\n"); LOG("/**"); LOG("* operator--"); LOG("*/"); --randomAcessIter; log_to_console(*randomAcessIter, "randomAcessIter--; //*randomAcessIter = "); LOG("\n"); LOG("/**"); LOG("* move two steps forward"); LOG("*/"); LOG("\n"); randomAcessIter += 2; log_to_console(*randomAcessIter, "randomAcessIter +=2; //*randomAcessIter = "); LOG("\n"); }
void TestSTL::example_iterators() { /** * Input iterators: allow only reading elements from a sequence and moving forward, one step, * using operator++. The elements are read with operator*. * Provides also operator== and operator!=. * An input iterator implemented in STL is for example the one provided by std::istream. */ /** * Output iterators: allow only writing elements from a sequence and only moving forward, one step, * using operator++. They elements are written with operator*. * Provides also operator== and operator!=. * An input iterator implemented in STL is for example the one provided by std::ostream. */ /** * Forward iterators: allow reading and writing elements from a sequence. Only moving forward, one step, * using operator++ is possible. They elements are read and written with operator*. * Provides also operator== and operator!=. * An input iterator implemented in STL is for example the one provided by std::ostream. */ /** * Bidirectional iterators: allow reading and writing elements from a sequence. Allows moving forward and backward, * one step,using operator++/operator--. Their elements are read and written with operator*. * Provides also operator== and operator!=. * An input iterator implemented in STL is for example the ones provided by list, multiset, map, multimap. */ int sArray[] = { 0, 11, 22, 33, 44 }; int sArraySize = sizeof(sArray)/sizeof(sArray[0]); std::list<int> myList(sArray, sArray + sArraySize); //create an bidirectional iterator std::list<int>::iterator iter; iter = myList.begin(); //read TESTIFY_ASSERT( 0 == *iter ); //write *iter = 99; // // operator++ iter++; TESTIFY_ASSERT( 11 == *iter ); // operator-- --iter; TESTIFY_ASSERT( 99 == *iter ); /** * Random access iterators: allows all operations a normal pointer does: add and subtract integral values, * move forward and backward, one or more steps, use operator[] on it, subtract one iterator from another. * It overloads operator<, operator>, operator==, operator!=. * An input iterator implemented in STL is for example the ones provided by vector, deque, string. */ std::vector<int> myVector(sArray, sArray + sArraySize); //myVector contains now: 0, 11, 22, 33, 44 //create an random iterator std::vector<int>::iterator randomAcessIter; randomAcessIter = myVector.begin(); //read TESTIFY_ASSERT( 0 == *randomAcessIter ); //write *randomAcessIter = 99; //myVector contains now: 99, 11, 22, 33, 44 //operator++ randomAcessIter++; TESTIFY_ASSERT( 11 == *randomAcessIter ); //operator-- --randomAcessIter; TESTIFY_ASSERT( 99 == *randomAcessIter ); //move two steps forward randomAcessIter += 2; TESTIFY_ASSERT( 22 == *randomAcessIter ); std::vector<int>::iterator start = myVector.begin(); TESTIFY_ASSERT( start != randomAcessIter ); //operator < for( std::vector<int>::iterator it = start; it < (myVector.end() - 1); ++it) { *it = *(it + 1); } TESTIFY_ASSERT( 11 == myVector[0] && 22 == myVector[1] && 33 == myVector[2] && 44 == myVector[3]); }
myVector myClickTimer::getScaledCoordinates(float x,float y){ return myVector(x/mySystemSize*0.57,-1*y/mySystemSize*0.57,1); }
qreal ViewResizeRotateMoveUndo::vector2AngleDegrees(qreal dx, qreal dy) { Vector myVector(dx, dy); return myVector.toAngle()* 180/PI; }
/** * Function for illustrating predicates */ void STLMoblet::predicates_explained() { LOG("\n"); LOG("========================= about predicates ======================================================================"); LOG("/**"); LOG("* A predicate is a function that returns a boolean value based on some"); LOG("* comparison criterion."); LOG("* Predicates are used usually with STL algorithms."); LOG("* Binary predicates - predicates that take two arguments."); LOG("* Unary predicates - predicates taking one argument."); LOG("* Usually binary predicates are used with algorithms that need to compare two"); LOG("* elements (e.g sorting algorithms)."); LOG("*/"); LOG("\n"); LOG(" Example using predicates: "); LOG("\n Unary predicate example..."); int array[] = { 0, 1, 2, 3, 4}; int sizeArray = sizeof(array)/sizeof(array[0]); std::vector<int> myVector(array, array + sizeArray); //fill a vector log_to_console(myVector, "myVector contains: "); LOG("/**"); LOG("* std::countf_if algorithm: counts the number of elements that satisfy the"); LOG("* condition we supply (e.g isEven, isNegative)"); LOG("*/"); log_to_console("Calling: std::count_if(myVector.begin(), myVector.end(), " "isEven);"); int res = std::count_if(myVector.begin(), myVector.end(), NumericUtilities::isEven); //returns the how many numbers are even log_to_console(res, "int res = std::count_if(myVector.begin(), myVector.end(), " "isEven); //res = "); log_to_console("\n Binary predicate example..."); Employee staff[] = { Employee(3, "Jonny"), Employee(2, "Bob"), Employee(20, "Filomela")}; int staffSize = sizeof(staff)/sizeof(staff[0]); std::vector<Employee> myStaff( staff, staff + staffSize); log_to_console(myStaff, "myStaff contains: "); LOG("\n"); LOG("/**"); LOG("* std::sort algorithm: sorts the elements, from a range, into ascending"); LOG("* order"); LOG("* lessExperience - a binary predicate for comparing employees"); LOG("*/"); LOG("\n"); TRACE(std::sort(myStaff.begin(), myStaff.end(), lessExperiencePredicate));; log_to_console(myStaff, "myStaff contains now: "); LOG("\n"); }