bool saveMyMap(string filename, MyMap<KeyType, ValueType>& m) { // save contents of m to a file ofstream stream("filename"); // Create an ofstream object to create the file if (! stream) return false; // Return false if we can't create the file writeItem(stream, m.size()); // Save the number of associations in m to stream KeyType x; ValueType* y = m.getFirst(x); for (int i = 0; i < m.size(); i++) { writeItem(stream, x); writeItem(stream, *y); y = m.getNext(x); } return true; }
void tst_ExceptionSafety::exceptionMap() { { MyMap map; MyMap map2; MyMap map3; throwType = ThrowNot; for (int i = 0; i<10; i++) map[ FlexibleThrowerSmall(i) ] = FlexibleThrowerSmall(i); return; // further test are deactivated until Map is fixed. for( int i = ThrowAtCopy; i<=ThrowAtComparison; i++ ) { try { throwType = (ThrowType)i; map[ FlexibleThrowerSmall(10) ] = FlexibleThrowerSmall(10); } catch(...) { } QCOMPARE( map.size(), 10 ); QCOMPARE( map[ FlexibleThrowerSmall(1) ], FlexibleThrowerSmall(1) ); } map2 = map; try { throwType = ThrowLater; map2[ FlexibleThrowerSmall(10) ] = FlexibleThrowerSmall(10); } catch(...) { } /* qDebug("%d %d", map.size(), map2.size() ); for( int i=0; i<map.size(); i++ ) qDebug( "Value at %d: %d",i, map.value(FlexibleThrowerSmall(i), FlexibleThrowerSmall()).value() ); QCOMPARE( map.value(FlexibleThrowerSmall(1), FlexibleThrowerSmall()), FlexibleThrowerSmall(1) ); qDebug( "Value at %d: %d",1, map[FlexibleThrowerSmall(1)].value() ); qDebug("%d %d", map.size(), map2.size() ); */ QCOMPARE( map[ FlexibleThrowerSmall(1) ], FlexibleThrowerSmall(1) ); QCOMPARE( map.size(), 10 ); QCOMPARE( map2[ FlexibleThrowerSmall(1) ], FlexibleThrowerSmall(1) ); QCOMPARE( map2.size(), 10 ); } QCOMPARE(objCounter, 0 ); // check that every object has been freed }
bool IndexerImpl::save(string filenameBase) { ofstream outfile(filenameBase+".txt"); if ( ! outfile ) { cerr << "Error: Cannot create data.txt!" << endl; return false; } //Map to store locations to grab from array MyMap<int, int>* h2pull = new MyMap<int, int>; //Save size of urlToCount to outfile outfile << urlToCount->size() << endl; string word; vector<point>* value = urlToCount->getFirst(word); while (value!=NULL) { //save word and size of vector outfile << word << " " << value->size() << endl; vector<point>::iterator it = value->begin(); while (it!=value->end()) { int hash = it->m_hash; outfile << hash << " " << it->m_count << endl; h2pull->associate(hash%MAX_MAP_SIZE, hash%MAX_MAP_SIZE); it++; } value = urlToCount->getNext(word); } //Save size of h2pull (number of elements in array to visit) outfile << h2pull->size() << endl; int temp; int* location = h2pull->getFirst(temp); while (location!=NULL) { int size = idToUrl[temp]->size(); outfile << temp << " " << size <<endl; int hash; string* url = idToUrl[temp]->getFirst(hash); while(url!=NULL) { outfile << hash << " "<< *url << endl; url = idToUrl[temp]->getNext(hash); } location = h2pull->getNext(temp); } delete h2pull; return true; }
bool loadMyMap(string filename, MyMap<KeyType, ValueType>& m) { m.clear(); ifstream stream("filename"); if (!stream) return false; int size = m.size(); if (!readItem(stream, size)) return false; // Read the number of associations in m from stream, returning false if we can't KeyType x; ValueType y; for (int i = 0; i < size; i++) { if (!readItem(stream, x)) return false; stream.ignore(10000, '\n'); if (!readItem(stream, y)) return false; stream.ignore(10000, '\n'); m.associate(x, y); } return true; }
int main() { MyMap<string, int> months; months["january"] = 31; months["february"] = 28; months["march"] = 31; months["april"] = 30; months["may"] = 31; months["june"] = 30; months["july"] = 31; months["august"] = 31; months["september"] = 30; months["october"] = 31; months["november"] = 30; months["december"] = 31; cout << "Map size: " << months.size() << endl; if (months.empty()) cout << "Map is empty!" << endl; else cout << "Map is not empty!" << endl; cout << "june -> " << months["june"] << endl; cout << "blabla -> " << months["blabla"] << endl; cout << endl; display(months); MyMap<string, int>::iterator it; it = months.find("blabla"); months.erase(it); display(months); return 0; }