#include #include // for ifstream #include // for exit #include "CPstring.h" #include "uvmap.h" #include "hmap.h" #include "bstmap.h" #include "ctimer.h" #include "iterator.h" // the mapstats class tracks how many // different words there are, how many total words // and what the average word length is class MapStats : public MapBase { public: MapStats(); virtual void function(string & key, int & value); virtual void report(); int getTotal() const { return myTotal; } private: int myUnique; // # different words int myTotal; // total # words int myLengths; // sum of all word lengths }; MapStats::MapStats() : myUnique(0), myTotal(0), myLengths(0) { // all done in initializer list } void MapStats::function(string & key, int & value) { myUnique++; myTotal += value; myLengths += key.length() * value; } void MapStats::report() { cout << "number of unique words = " << myUnique << endl; cout << "number of total words = " << myTotal << endl; cout << "average word length = " << static_cast(myLengths)/myTotal << endl; } void Print(Pair & p) { cout << p.second << "\t" << p.first << endl; } void ProcessWords(istream & input,Map & map) // postcondition: all words from input read, stored in map with count // of occurrences { string word; input.seekg(0); input.clear(); // clear for reading while (input >> word) { word = word.Downcase(); if (! map.includes(word)) // word not stored in map { map.insert(word,1); // insert with one occurrence } else { map.getValue(word) += 1; // bump # occurrneces by 1 } } // uncomment line below to print all entries in table map.apply(Print); return; MapStats stats; map.apply(stats); stats.report(); cout << "frequent words (greater than 2%)" << endl; int total = stats.getTotal(); Iterator > * iter = map.makeIterator(); for(iter->first(); ! iter->isDone(); iter->next()) { if (iter->current().second > 0.02 *total) { cout << iter->current().second << "\t" << iter->current().first << endl; } } } unsigned int Hash(const string & s) // postcondition: returns hash value for s (from Weiss) { unsigned int hval = 0; int k; int len = s.length(); for(k=0; k < len; k++) { hval = (hval << 5) ^ hval ^ s[k]; } return hval; } int main() { HMap hmap(Hash,7001); BSTMap bstmap; UVMap uvmap(1000); string filename; ifstream input; cout << "file name: "; cin >> filename; input.open(filename.c_str()); if (input.fail()) { cerr << "could not open file " << filename << endl; exit(1); } CTimer timer; timer.Start(); ProcessWords(input,hmap); timer.Stop(); cout << timer.ElapsedTime() << " seconds hash" << endl; input.close(); input.open(filename.c_str()); timer.Start(); ProcessWords(input,bstmap); timer.Stop(); cout << timer.ElapsedTime() << " seconds bst" << endl; input.close(); input.open(filename.c_str()); timer.Start(); ProcessWords(input,uvmap); timer.Stop(); cout << timer.ElapsedTime() << " seconds uvmap" << endl; }