Module 15: Get Strings from the Grid

This commit is contained in:
David Doblas Jiménez 2021-08-25 15:59:24 +02:00
parent d9795f1ea1
commit c82251732a
1 changed files with 56 additions and 13 deletions

69
a.c
View File

@ -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<num_patterns; i++) {
@ -127,7 +133,7 @@ public:
word_map_[temp].push_back(w);
}
}
void ReadFromFile(string filename) {
void ReadFromFile(string filename, int max_size) {
ifstream f;
f.open(filename);
while (f.is_open() && !f.eof()) { // check for the file!
@ -140,9 +146,11 @@ public:
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);
if (len <= max_size) {
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 '"
@ -172,6 +180,7 @@ struct Grid {
return lines[0].size();
}
}
int max_size() const { return max(rows(), cols()); }
// Returns character value of the box at point 'p'
// 'p' must be in bounds
char box(const Point& p) const {
@ -193,6 +202,17 @@ struct Grid {
bool in_bounds(const Point& p) const {
return (p.row >= 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<len; i++) {
Point p = s.GetPoint(i);
temp[i] = box(p);
}
return temp;
}
// Next increments the point across the grid, one box at a time
// Returns true if point is still in bounds
bool Next(Point& p, bool vert) {
@ -211,6 +231,27 @@ struct Grid {
}
return in_bounds(p);
}
// NextStopAtWrap is like "Next" except it returns false at every wrap
// Returns true if we stay on the same line
bool NextStopAtWrap(Point& p, bool vert) {
bool wrap = false;
if (vert) {
p.row++;
if (p.row >= 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());
}