-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvas.cpp
More file actions
79 lines (66 loc) · 1.87 KB
/
Canvas.cpp
File metadata and controls
79 lines (66 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Canvas.h"
#include <iostream>
#include <cassert>
using namespace std;
Canvas::Canvas(int c, int r, char ch) : colsN(c), rowsM(r), grid(c, vector<char>(r, ch)) {
assert(c > 0 && r > 0);
}
const Canvas &Canvas::operator=(const Canvas &rhs) { // NOLINT
if (this == &rhs) return *this;
colsN = rhs.colsN;
rowsM = rhs.rowsM;
grid = rhs.grid;
return *this;
}
int Canvas::getRowsM() const {
return rowsM;
}
int Canvas::getColsN() const {
return colsN;
}
const std::vector<std::vector<char>> &Canvas::getGrid() const {
return grid;
}
void Canvas::clear(char ch) {
for (auto rowIt = grid.begin(), rowEnd = grid.end(); rowIt != rowEnd; ++rowIt) {
for (auto colIt = rowIt->begin(), colEnd = rowIt->end(); colIt != colEnd; ++colIt) {
*colIt = ch;
}
}
}
void Canvas::putChar(char ch, int c, int r) {
if (valid(c, r))
grid[c][r] = ch;
}
char Canvas::getChar(int c, int r) const {
if (valid(c, r))
return grid[c][r];
else {
cerr << "Error. Invalid column or row" << endl;
return '\0';
}
}
void Canvas::decorate() {// no decoration for plain canvas
}
bool Canvas::valid(int c, int r) const {
if (c >= 0 && r >= 0) // not negative and starts from [0,0]
return c < getColsN() && r < getRowsM(); // in column range and in row range.
else
return false;
}
const ostream &operator<<(ostream &os, const Canvas &rhs) {
for (auto rowIt = rhs.getGrid().begin(), rowEnd = rhs.getGrid().end(); rowIt != rowEnd; ++rowIt) {
for (auto colIt = rowIt->begin(), colEnd = rowIt->end(); colIt != colEnd; ++colIt) {
os << *colIt;
}
os << endl;
// another way:
//const char * p = rowIt->data();
//for (size_t i = 0; i < rowIt->size(); ++i)
//{
// os << *p;
// ++p;
//}
}
return os;
}