From c82251732a0a9fc869753b1a41b251ec51498012 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Wed, 25 Aug 2021 15:59:24 +0200 Subject: [PATCH] Module 15: Get Strings from the Grid --- a.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/a.c b/a.c index 9aabf97..90e6a63 100644 --- a/a.c +++ b/a.c @@ -37,7 +37,14 @@ ostream& operator<<(ostream& os, const Point& p) { // for printing //-Span struct Span { Span(Point p, int l, bool v) : point(p), len(l), vert(v) {} - + Point GetPoint(int i) const { + assert(i >= 0 && i < len); + if (vert) { + return Point(point.row + i, point.col); + } else { + return Point(point.row, point.col + i); + } + } friend ostream& operator<<(ostream& os, const Span& s); Point point; @@ -112,7 +119,6 @@ public: } 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; i= 0 && p.row < rows() && p.col >= 0 && p.col < cols()); } + // Fills in attributes of the string + string GetString(const Span& s) const { + int len = s.len; + string temp; + temp.resize(len); + for (int i=0; i= rows()) { + p.row = 0; + p.col++; + wrap = true; + } + } else { + p.col++; + if (p.col >= cols()) { + p.col = 0; + p.row++; + wrap = true; + } + } + return !wrap; + } void FillSpans(bool vert) { Point p; // check all spans @@ -224,10 +265,11 @@ struct Grid { // cout << "SPAN START: " << p << "\n"; int len = 0; + bool keep_going = false; do { - Next(p, vert); + keep_going = NextStopAtWrap(p, vert); len++; - } while (in_bounds(p) && !is_block(p)); + } while (keep_going && !is_block(p)); //cout << "END OF SPAN!!! len=" << len << "\n"; spans.push_back(Span(startp, len, vert)); } @@ -258,7 +300,8 @@ struct Grid { void Print() const { cout << "Grid: " << name << " (rows=" << rows() - << ", cols=" << cols() << ")\n"; + << ", cols=" << cols() + << ", max_size=" << max_size() << ")\n"; for (string s : lines) { cout << " " << s << "\n"; } @@ -266,7 +309,7 @@ struct Grid { void PrintSpans() const { cout << "Spans:\n"; for (const Span& s : spans) { - cout << " " << s << "\n"; + cout << " " << s << " " << GetString(s) << "\n"; } } string name; // strings are initialized empty @@ -275,13 +318,13 @@ struct Grid { }; int main() { - Library lib; - lib.ReadFromFile("top_12000.txt"); - Grid grid("MY GRID"); grid.LoadFromFile("test"); grid.Check(); grid.Print(); grid.FillSpans(); grid.PrintSpans(); + + Library lib; + lib.ReadFromFile("top_12000.txt", grid.max_size()); } \ No newline at end of file