void PrintEqualProduct(int num) { typedef unordered_multimap<int, pair<int, int> > MultiMap; typedef unordered_map<int, int> ProductMap; MultiMap resultMap; ProductMap productMap; for (int i = 1; i <= num; ++i) { for (int j = 1; j <= num; ++j) { resultMap.insert( MultiMap::value_type(i*j, pair<int,int>(i, j)) ); productMap.insert( pair<int,int>(i*j,i*j) ); } } pair<MultiMap::const_iterator, MultiMap::const_iterator> eqRange; MultiMap::const_iterator it1; MultiMap::const_iterator it2; ProductMap::const_iterator it; for (it = productMap.begin(); it != productMap.end(); ++it) { eqRange = resultMap.equal_range(it->second); //search value i*j in resultMap first element, return a range pairs for (it1 = eqRange.first; it1 != eqRange.second; ++it1) { for (it2 = eqRange.first; it2 != eqRange.second; ++it2) { cout<< it1->second.first <<" * "<<it1->second.second<<" = "<< it2->second.first <<" * "<<it2->second.second <<endl; } } } }
bool Hdf5Dataset::getMultimap(MultiMap &mMap) { hsize_t dims_out[2], count[2], offset[2]; hid_t dataspace = H5Dget_space(this->poses_dataset_); /* dataspace handle */ int rank = H5Sget_simple_extent_ndims(dataspace); herr_t status_n = H5Sget_simple_extent_dims(dataspace, dims_out, NULL); herr_t status; int chunk_size, chunk_itr; if (dims_out[0] % 10) { chunk_itr = 11; } else { chunk_itr = 10; } chunk_size = (dims_out[0] / 10); offset[0] = 0; for (int it = 0; it < chunk_itr; it++) { offset[1] = 0; if ((dims_out[0] - (chunk_size * it)) / chunk_size != 0) { count[0] = chunk_size; offset[0] = chunk_size * it; } else { count[0] = (dims_out[0] - (chunk_size * it)); offset[0] = count[0]; } count[1] = 10; double data_out[count[0]][count[1]]; status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL); hsize_t dimsm[2]; dimsm[0] = count[0]; dimsm[1] = count[1]; hid_t memspace; memspace = H5Screate_simple(RANK_OUT, dimsm, NULL); status = H5Dread(this->poses_dataset_, H5T_NATIVE_DOUBLE, memspace, dataspace, H5P_DEFAULT, data_out); for(int i=0; i<count[0]; i++) { std::vector<double> sphere_center(3); std::vector<double> Poses(7); for(int j=0; j<3;j++) { sphere_center[j] = data_out[i][j]; } for(int k=3;k<10; k++) { Poses[k-3] = data_out[i][k]; } mMap.insert(std::make_pair(sphere_center, Poses)); } } return 0; }
int main() { MultiMap<int, int> m; m[1] = 10; m[1] += 20; m[1] += 20; m[2] = 30; m[2] += 30; m[3] = 40; m[2] = 40; m.erase(3); m.erase(1, 20); m.erase(2, 123); cout << m[1].getValueCount() << endl; cout << m[2].getValueCount() << endl; cout << m[3].getValueCount() << endl; return 0; for (int i = 0; i < 100; ++i) { int n = rand() % 1000000; if (rand() % 2 == 1) { cout << "Insert " << n << endl; m.insert(n, i % 100); } else { cout << "Erase Value" << endl; m[rand() % 1000000].eraseValue(); } } // sleep(100); return 0; for (int i = 0; i < 20; ++i) { int n = rand() % 10; cout << "inserted: " << n << ", " << i << endl; m.insert(n, i); } for (int i = 0; i < 20; ++i) { cout << "inserted: " << i << ", " << i << endl; m.insert(i, i); } cout << "insertion completed" << endl; ConstCursor<int, int> cx = m[2]; cout << cx.keyOk() << endl; cout << cx.valueOk() << endl; cout << cx.getKey() << endl; cout << cx.getKeyCount() << endl; cout << cx.getValue() << endl; cout << cx.getValueCount() << endl; cout << m[2].getKey() << " (" << m[2].getKeyCount() << ")" << endl; cout << m[2].getKey() << " (" << m[2].getKeyCount() << ")" << endl; cout << m[2].getKey() << " (" << m[2].getKeyCount() << ")" << endl; cout << "array access completed" << endl; m[2].insertValue(8); m[2].insertValue(7); m[2].insertValue(8); m[2][7].eraseValue(); m[7].eraseKey(); MultiMap<int, int> n = m; Cursor<int, int> csr(&n); while (csr.keyOk()) { cout << csr.getKey() << " (" << csr.getKeyCount() << "): "; while (csr.valueOk()) { cout << csr.getValue() << " [" << csr.getValueCount() << "] "; csr.nextValue(); } cout << endl; csr.nextKey(); } }
int main() { /*Database db; db.loadFromURL("http://cs.ucla.edu/classes/winter14/cs32/Projects/4/Data/census.csv"); */ MultiMap m; m.insert("D", 8); m.insert("B", 4); m.insert("F", 12); m.insert("A", 1); m.insert("C", 5); m.insert("E", 9); m.insert("G", 13); m.insert("A", 2); m.insert("C", 6); m.insert("E", 10); m.insert("G", 14); m.insert("A", 3); m.insert("C", 7); m.insert("E", 11); m.insert("G", 15); MultiMap::Iterator c(m.findEqual("D")); assert(c.valid()); MultiMap::Iterator d; for (; c.valid(); c.prev()) d = c; for (; d.valid(); d.next()) cout << d.getKey() << " " << d.getValue() << endl; cout << endl; MultiMap::Iterator a(m.findEqual("D")); assert(a.valid()); MultiMap::Iterator b; for (; a.valid(); a.next()) b = a; for (; b.valid(); b.prev()) cout << b.getKey() << " " << b.getValue() << endl; MultiMap::Iterator t; t = m.findEqual("A"); if (t.valid()) { cout << endl; cout << t.getKey() << " " << t.getValue() << endl; t.next(); cout << t.getKey() << " " << t.getValue() << endl; t.next(); cout << t.getKey() << " " << t.getValue() << endl; } t = m.findEqualOrSuccessor("Fz"); if (t.valid()) { cout << endl; cout << t.getKey() << " " << t.getValue() << endl; t.next(); cout << t.getKey() << " " << t.getValue() << endl; t.next(); cout << t.getKey() << " " << t.getValue() << endl; } t = m.findEqualOrPredecessor("E"); t.next(); MultiMap::Iterator after = t; t.next(); MultiMap::Iterator afterafter = t; t.prev(); t.prev(); if (t.valid() && after.valid() && afterafter.valid()) { cout << endl; cout << t.getKey() << " " << t.getValue() << endl; t.next(); cout << t.getKey() << " " << t.getValue() << endl; t.next(); cout << t.getKey() << " " << t.getValue() << endl; } Database database; Database::FieldDescriptor fd1, fd2, fd3; fd1.name = "username"; fd1.index = Database::it_indexed; // username is an indexed field fd2.name = "phonenum"; fd2.index = Database::it_indexed; // phone # is an indexed field fd3.name = "age"; fd3.index = Database::it_none; // age is NOT an indexed field std::vector<Database::FieldDescriptor> schema; schema.push_back(fd1); schema.push_back(fd2); schema.push_back(fd3); database.specifySchema(schema); vector<string> row; string username = "******"; string phonenum = "6265375521"; string age = "17"; row.push_back(username); row.push_back(phonenum); row.push_back(age); database.addRow(row); vector<string> row2; username = "******"; phonenum = "5403317654"; age = "20"; row2.push_back(username); row2.push_back(phonenum); row2.push_back(age); database.addRow(row2); vector<string> row3; username = "******"; phonenum = "6265375521"; age = "25"; row3.push_back(username); row3.push_back(phonenum); row3.push_back(age); database.addRow(row3); vector<int> results; vector<Database::SortCriterion> sortCrit; vector<Database::SearchCriterion> searchCrit; Database::SearchCriterion s1; s1.fieldName = "username"; s1.minValue = "A"; s1.maxValue = "C"; Database::SearchCriterion s2; s2.fieldName = "phonenum"; s2.minValue = "6000000000"; s2.maxValue = ""; searchCrit.push_back(s1); searchCrit.push_back(s2); database.search(searchCrit, sortCrit, results); cout << endl << "Passed all tests" << endl; }