From e39354cb15cc5875b0eaff953d9b9de3aed107a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Doblas=20Jim=C3=A9nez?= Date: Mon, 19 Jul 2021 22:15:11 +0200 Subject: [PATCH] Module 9: Search the Library (C++ code only) --- a.c | 174 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 125 insertions(+), 49 deletions(-) diff --git a/a.c b/a.c index 8a20779..486509f 100644 --- a/a.c +++ b/a.c @@ -8,53 +8,124 @@ using namespace std; // For compiling C++ code // g++ a.c -o a -class Library { - public: - void ComputeStats() { - assert(counts.empty()); - counts.resize(18); - for (string s : words) { - int len = s.length(); - if (len < 18) { - counts[len]++; - } - } - } - void PrintStats() const { - cout << "Here are the counts of each word length:\n"; - for (int i=1; i= 0 && i < words.size()); - return words[i]; - } - void ReadFromFile(string filename) { - ifstream f; - f.open(filename); - while (!f.eof()) - { - string line; - getline(f, line); - // cout << line << "\n"; - if (!line.empty()) - { - int len = line.length(); - if (line[len-1] == '\r') { - line = line.substr(0, len-1); - } - words.push_back(line); - } - } - cout << "Read " << words.size() << " words from file '" - << filename << "'\n"; - } - private: - vector words; - vector counts; -}; +string ToUpper(string s) +{ + string s2; + for (char c : s) + { + s2.push_back(toupper(c)); + } + return s2; +} +struct Word +{ + Word(string s) + { + word = s; + } + string word; +}; +typedef vector Words; + +const int num_buckets = 1001; + +// hash function +int bucket(string s) +{ + assert(!s.empty()); + int i = 0; + for (char c : s) + { + i = (i * 217) + c; + } + i = abs(i); + int b = i % num_buckets; + assert(b >= 0 && b < num_buckets); + return b; +} + +class Library +{ +public: + Library() + { + shelves.resize(num_buckets); + } + bool IsWord(string s) const + { + for (Word w : shelves[bucket(s)]) + { + if (s == w.word) + { + return true; + } + } + return false; + } + void ComputeStats() + { + assert(counts.empty()); + counts.resize(18); + for (Word w : words) + { + int len = w.word.length(); + if (len < 18) + { + counts[len]++; + } + } + } + void PrintStats() const + { + cout << "Here are the counts of each word length:\n"; + for (int i = 1; i < counts.size(); i++) + { + cout << "[" << i << "] " << counts[i] << "\n"; + } + } + string GetWord(int i) const + { + assert(i >= 0 && i < words.size()); + return words[i].word; + } + void ReadFromFile(string filename) + { + ifstream f; + f.open(filename); + while (!f.eof()) + { + string line; + getline(f, line); + // cout << line << "\n"; + if (!line.empty()) + { + line = ToUpper(line); + int len = line.length(); + if (line[len - 1] == '\r') + { + line = line.substr(0, len - 1); + } + words.push_back(Word(line)); + shelves[bucket(line)].push_back(Word(line)); + } + } + cout << "Read " << words.size() << " words from file '" + << filename << "'\n"; + } + void DebugBuckets() const + { + for (int i = 0; i < shelves.size(); i++) + { + cout << "[" << i << "] " << shelves[i].size() << "\n"; + } + } + +private: + Words words; + vector shelves; + vector counts; +}; struct Grid { @@ -106,7 +177,7 @@ struct Grid cout << " " << s << "\n"; } } - string name; // string are initialized empty + string name; // strings are initialized empty vector lines; }; @@ -114,8 +185,13 @@ int main() { Library lib; lib.ReadFromFile("top_12000.txt"); - lib.ComputeStats(); - lib.PrintStats(); + //lib.ComputeStats(); + //lib.PrintStats(); + + cout << lib.IsWord("DOG") << "\n"; + cout << lib.IsWord("CAT") << "\n"; + cout << lib.IsWord("THANKS") << "\n"; + lib.DebugBuckets(); //Grid grid("MY GRID"); //grid.LoadFromFile("test");