#include // library for printing #include // support for strings #include // support for vectors #include // support for hash-tables #include #include // support for reading files using namespace std; // For compiling C++ code // g++ a.c -o a string ToUpper(string s) { string s2; for (char c : s) { s2.push_back(toupper(c)); } return s2; } // -------------------------------------------------------------------------------- //-Point struct Point { Point() {} Point(int r, int c) : row(r), col(c) {} friend ostream& operator<<(ostream& os, const Point& p); // overloading the "<<" op int row = 0; // defaults value for the constructor int col = 0; }; ostream& operator<<(ostream& os, const Point& p) { // for printing os << "(" << p.row << "," << p.col << ")"; return os; } // -------------------------------------------------------------------------------- //-Span struct Span { Span(Point p, int l, bool v) : point(p), len(l), vert(v) {} friend ostream& operator<<(ostream& os, const Span& s); Point point; int len; bool vert; }; ostream& operator<<(ostream& os, const Span& s) { os << "[" << s.point << " len=" << s.len << " vert=" << s.vert << "]"; return os; } // -------------------------------------------------------------------------------- //-Word struct Word { Word() {} // default empty constructor Word(string s) : word(s) {} // standard way of initialization int len() const { return word.length(); } string word; }; typedef vector Words; // will store pointers to Words (will not delete by itself) typedef unordered_map WordMap; // hash-table from stl // -------------------------------------------------------------------------------- //-Library class Library { public: Library() {} // hash-tables are automatically initialized ~Library() { // destructor of the pointers (instead of the unique pointer) for (Word* w : words_) { delete w; } } void FindWord(const string& s) const { // references are prefered instead of copies auto it = word_map_.find(s); if (it != word_map_.end()) { for (const Word* w : it->second) { cout << " " << w->word; } cout << "\n"; } } bool IsWord(string s) const { auto it = word_map_.find(s); // use iterator if (it == word_map_.end()) { return false; // if word is not found on hast-table } else { return true; } //return word_map_.count(s) > 0; // True if word exists } void ComputeStats() { assert(counts_.empty()); counts_.resize(18); for (const Word* w : words_) { // Word is a pointer! int len = w->word.length(); // w is not an actual object (it's a pointer) 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 CreatePatternHash(Word* w) { int len = w->len(); if (len > 7) return; // avoid load of long words int num_patterns = 1 << len; // create 2^len patterns // cout << "PATTERN HASH on " << w->word << "\n"; for (int i=0; iword; for (int j=0; j> j) & 1) { // get every bit and check if it's 1 temp[j] = '-'; } } // cout << " " << temp << "\n"; word_map_[temp].push_back(w); } } void ReadFromFile(string filename) { ifstream f; f.open(filename); while (f.is_open() && !f.eof()) { // check for the file! 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); } Word* w = new Word(line); words_.push_back(w); // Word would be allocated on the heap CreatePatternHash(w); } } cout << "Read " << words_.size() << " words from file '" << filename << "'\n"; } void DebugBuckets() const { for (int i = 0; i < word_map_.bucket_count(); i++) { cout << "[" << i << "] " << word_map_.bucket_size(i) << "\n"; } } private: // _ is used to indicate privacy Words words_; // master vector of words WordMap word_map_; // pattern hash vector counts_; }; struct Grid { Grid(string n) { name = n; } int rows() const { return lines.size(); } int cols() const { if (lines.empty()) { return 0; } else { return lines[0].size(); } } void LoadFromFile(string filename) { ifstream f; f.open("test"); while (f.is_open() && !f.eof()) { // check for the file string line; getline(f, line); // cout << line << "\n"; if (!line.empty() && line[0] != '#') { lines.push_back(line); } } } void Check() const { for (string s : lines) { assert(s.size() == cols()); } } void Print() const { cout << "Grid: " << name << " (rows=" << rows() << ", cols=" << cols() << ")\n"; for (string s : lines) { cout << " " << s << "\n"; } } string name; // strings are initialized empty vector lines; }; int main() { Library lib; lib.ReadFromFile("top_12000.txt"); Grid grid("MY GRID"); grid.LoadFromFile("test"); grid.Check(); grid.Print(); Point p1; Point p2(2,1); cout << "Point1 is " << p1 << "\n"; cout << "Point2 is " << p2 << "\n"; Span s1(p1, 3, true); Span s2(p2, 5, false); cout << "Span1 is " << s1 << "\n"; cout << "Span2 is " << s2 << "\n"; }