void testAt(IntVector& test) { cout << "First position" << endl; cout << test.at(0) << endl << endl; cout << "Last position" << endl; cout << test.at(test.size() - 1) << endl << endl; cout << "Front position" << endl; cout << test.front() << endl << endl; cout << "Back position" << endl; cout << test.back() << endl << endl; cout << "First pos changed to last pos" << endl; test.at(0) = test.back(); testValues(test); }
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QScriptEngine eng; // register our custom types qScriptRegisterMetaType<IntVector>(&eng, toScriptValue, fromScriptValue); qScriptRegisterMetaType<StringVector>(&eng, toScriptValue, fromScriptValue); QScriptValue val = eng.evaluate("[1, 4, 7, 11, 50, 3, 19, 60]"); fprintf(stdout, "Script array: %s\n", qPrintable(val.toString())); IntVector iv = qscriptvalue_cast<IntVector>(val); fprintf(stdout, "qscriptvalue_cast to QVector<int>: "); for (int i = 0; i < iv.size(); ++i) fprintf(stdout, "%s%d", (i > 0) ? "," : "", iv.at(i)); fprintf(stdout, "\n"); val = eng.evaluate("[9, 'foo', 46.5, 'bar', 'Qt', 555, 'hello']"); fprintf(stdout, "Script array: %s\n", qPrintable(val.toString())); StringVector sv = qscriptvalue_cast<StringVector>(val); fprintf(stdout, "qscriptvalue_cast to QVector<QString>: "); for (int i = 0; i < sv.size(); ++i) fprintf(stdout, "%s%s", (i > 0) ? "," : "", qPrintable(sv.at(i))); fprintf(stdout, "\n"); return 0; }
void generateBacktrack(IntVector& current_set, unsigned n) { unsigned csum = std::accumulate(current_set.begin(), current_set.end(), 0); if (csum > n) { //discard this solution return; } if (csum == n) //if solution, print it { processSolution(current_set); } else { IntVector candidates = getCandidates(current_set, n, csum); for (unsigned i =0; i < candidates.size(); ++i) { current_set.push_back(candidates.at(i)); generateBacktrack(current_set, n); current_set.pop_back(); } } }
void DataLogger::assignMatrixGroup(int groupCode, const MatrixXF & value) { #ifdef DATA_LOG_DEBUG if (curr == NULL) { cout << "ASSERTION FAILED: No current record! " << endl; exit(-1); } const unsigned int groupSize = groups[groupCode].size(); if (groupSize != value.size()) { cout << "ASSERTION FAILED: Matrix Group Size does not match Matrix Data Size!" <<endl; exit(-1); } #endif //cout << "Group " << groupCode << " (" << groupNames[groupCode] << ") Size " << groupSize << " vs. " << value.size() << endl; IntVector * g = &(groups[groupCode]); int k=0; for (unsigned int j=0; j<value.cols(); j++) { for (unsigned int i=0; i<value.rows(); i++) { //cout << "Assigning item " << g->at(k) << " '" << itemNames[g->at(k)] << "' = " << value(i,j) << endl; curr->at(g->at(k)) = value(i,j); k++; } } }
void baseReqTests(IntVector& intVector) { assert(intVector.empty()); assert(intVector.size() == 0); // Insert 20 things into the vector for (int i = 0; i < 20; i++) { intVector.push_back(i); } // Make sure vector is no longer empty assert(!intVector.empty()); // Make sure there is exactly 20 things in vector assert(intVector.size() == 20); // Make sure things are in the right place assert(intVector.at(0) == 0); assert(intVector.at(5) == 5); assert(intVector.at(15) == 15); assert(intVector.at(19) == 19); // Let's pop something assert(intVector.pop_back() == 19); assert(intVector.size() == 19); try { intVector.at(19); } catch (int e) { assert(true); } // Let's pop everything int old = 19; while (!intVector.empty()) { int temp = intVector.pop_back(); assert(temp == old - 1); old = temp; } }
void processSolution(IntVector const& subset) { std::cout<<"{"; for (unsigned i =0; i < subset.size(); ++i) { if (subset.at(i)) { std::cout<<i<<" "; } } std::cout<<"}\n"; }
void DataLogger::assignGroup(int groupCode, const VectorXF & value) { #ifdef DATA_LOG_DEBUG if (curr == NULL) { cout << "ASSERTION FAILED: No current record! " << endl; exit(-1); } #endif const unsigned int groupSize = groups[groupCode].size(); //cout << "Group " << groupCode << " (" << groupNames[groupCode] << ") Size " << groupSize << " vs. " << value.size() << endl; IntVector * g = &(groups[groupCode]); for (unsigned int i=0; i<groupSize; i++) { curr->at(g->at(i)) = value(i); } }
unsigned getPreviousMin(IntVector const& s, unsigned i) { //we have to compute min number of coins for i, knowing solution for 0..i-1 //subtract each coin from i (x = i - coin(j)) and get minimum from s[x] unsigned diff = i - coins.at(0); if (diff > s.size()) { std::cerr<<"diff: "<<diff<<"\n"; throw std::logic_error("Invalid diff"); } unsigned min = s.at(diff); //start with this unsigned found_coin = 0; unsigned prev_idx = diff; for (unsigned j = 1; j < coins.size(); ++j) { //std::cout<<"Coin at j :"<<coins.at(j)<<"\n"; if (i > coins.at(j)) { diff = i - coins.at(j); if (diff > s.size()) { std::cerr<<"diff2: "<<diff<<"\n"; throw std::logic_error("Invalid diff"); } if (min > s.at(diff)) { min = s.at(diff); found_coin = j; prev_idx = diff; } } } IntVector tmp = dyn_solution.at(prev_idx); tmp.push_back(coins.at(found_coin)); dyn_solution.push_back(tmp); //std::cout<<"For i: "<<i<<" found min: "<<min<<" at :"<<diff<<"\n"; //std::cout<<"Found coin: "<<coins.at(found_coin)<<"\n"; //printVector(tmp); return min; }
int FileFlattener::getFlattenedIndex(int folderIndex, int fileIndex) { if (folderIndex >= 0 && folderIndex < folders.size()) { IntVector *files = &folders.at(folderIndex); if (fileIndex >= 0 && fileIndex < files->size()) { return files->at(fileIndex); } else { return NO_FILE; } } else { return NO_FILE; } }
void generateBacktrack(IntVector& current_set, unsigned n) { if (current_set.size() == n) //if solution, print it { processSolution(current_set); } else { IntVector candidates; candidates.push_back(0); candidates.push_back(1); for (unsigned i =0; i < candidates.size(); ++i) { current_set.push_back(candidates.at(i)); generateBacktrack(current_set, n); current_set.pop_back(); } } }
void testValues(IntVector& test) { cout << "Size: " << test.size() << endl; cout << "Capacity: " << test.capacity() << endl; cout << "Array: " << endl; //display array if vector is NOT empty if (!test.empty()) { for (int i = 0; i < test.size(); i++) { cout << "#" << i << ": " << test.at(i); cout << endl; } } else { cout << "Vector is empty!" << endl; } cout << endl; }
void generateSubsets(unsigned number) { IntVector set; for (unsigned i = 0; i <= number; ++i) { set.push_back(i); } printVector(set); IntVector current_set; unsigned limit = (1 << (number+1)) - 1; for (unsigned i = 0; i <= limit; ++i) { current_set.clear(); unsigned cnum = i; unsigned count = 0; //std::cout<<"Process: "<<i<<"\n"; while (cnum) { if (cnum & 0x1) //last bit is set { current_set.push_back(set.at(count)); } cnum >>= 1; //right shift one bit ++count; } printVector(current_set); } }