-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsheet.cpp
More file actions
192 lines (168 loc) · 4.73 KB
/
sheet.cpp
File metadata and controls
192 lines (168 loc) · 4.73 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "sheet.h"
#include <algorithm>
#include <iostream>
using namespace std::literals;
namespace {
std::ostream& operator<<(std::ostream& output, const CellInterface::Value &val) {
std::visit([&output](auto &v) {
output << v;
}, val);
return output;
}
} // namespace
void Sheet::SetCell(Position pos, std::string text) {
TestPosition(pos);
rows_[pos.row].SetCell(pos.col, text, *this);
if (rows_[pos.row].Size() > 0 && !text.empty()) {
non_empty_rows_.insert(pos.row);
}
}
const CellInterface* Sheet::GetCell(Position pos) const {
TestPosition(pos);
auto row = rows_.find(pos.row);
if (row == rows_.end()) {
return nullptr;
}
return row->second[pos.col];
}
CellInterface* Sheet::GetCell(Position pos) {
TestPosition(pos);
auto row = rows_.find(pos.row);
if (row == rows_.end()) {
return nullptr;
}
return row->second[pos.col];
}
void Sheet::ClearCell(Position pos) {
TestPosition(pos);
auto row = rows_.find(pos.row);
if (row != rows_.end()) {
row->second.Clear(pos.col);
if (row->second.Size() == 0) {
non_empty_rows_.erase(pos.row);
}
}
}
Size Sheet::GetPrintableSize() const {
if (non_empty_rows_.empty()) {
return {0, 0};
}
Size result;
result.rows = *non_empty_rows_.rbegin() + 1;
for (auto &row : rows_) {
result.cols = std::max(result.cols, static_cast<int>(row.second.Size()));
}
return result;
}
void Sheet::PrintValues(std::ostream& output) const {
Size size = GetPrintableSize();
auto end = rows_.end();
for (auto i = 0; i < size.rows; ++i) {
auto row = rows_.find(i);
if (row != end) {
row->second.PrintValues(output, size.cols);
} else {
Row::PrintEmptyRow(output, size.cols);
}
}
}
void Sheet::PrintTexts(std::ostream& output) const {
Size size = GetPrintableSize();
auto end = rows_.end();
for (auto i = 0; i < size.rows; ++i) {
auto row = rows_.find(i);
if (row != end) {
row->second.PrintTexts(output, size.cols);
} else {
Row::PrintEmptyRow(output, size.cols);
}
}
}
Cell* Sheet::GetCellOrCreate(Position pos) {
TestPosition(pos);
if (GetCell(pos) == nullptr) {
SetCell(pos, ""s);
}
return dynamic_cast<Cell*>(GetCell(pos));
}
void Sheet::TestPosition(Position pos) {
if (!pos.IsValid()) {
throw InvalidPositionException{"Invalid position"s};
}
}
std::unique_ptr<SheetInterface> CreateSheet() {
return std::make_unique<Sheet>();
}
void Row::SetCell(size_t col, std::string text, Sheet &sheet) {
// если в текущей позиции еще не было ячейки - создаём
if (auto cell = data_.find(col); cell == data_.end()) {
data_[col] = std::make_unique<Cell>(sheet);
}
data_[col]->Set(text);
if (!text.empty()) {
non_empty_cells_.insert(col);
}
}
CellInterface* Row::operator[](size_t col) {
auto cell = data_.find(col);
if (cell == data_.end()) {
return nullptr;
}
return cell->second.get();
}
const CellInterface* Row::operator[](size_t col) const {
auto cell = data_.find(col);
if (cell == data_.end()) {
return nullptr;
}
return cell->second.get();
}
void Row::Clear(size_t col) {
auto cell = data_.find(col);
if (cell != data_.end()) {
cell->second->Clear();
non_empty_cells_.erase(col);
// если у ячейки нет детей - удаляем её полностью
// иначе нет, чтобы сохранить список детей
if (!cell->second->IsReferenced()) {
data_.erase(cell);
}
}
}
void Row::PrintValues(std::ostream &output, size_t total_cells) const {
for (size_t i = 0; i < total_cells; ++i) {
auto cell = data_.find(i);
if (cell != data_.end()) {
output << cell->second->GetValue();
}
if (i < total_cells - 1) {
output << '\t';
}
}
output << '\n';
}
void Row::PrintTexts(std::ostream &output, size_t total_cells) const {
for (size_t i = 0; i < total_cells; ++i) {
auto cell = data_.find(i);
if (cell != data_.end()) {
output << cell->second->GetText();
}
if (i < total_cells - 1) {
output << '\t';
}
}
output << '\n';
}
size_t Row::Size() const {
if (non_empty_cells_.empty()) {
return 0;
} else {
return *non_empty_cells_.rbegin() + 1;
}
}
void Row::PrintEmptyRow(std::ostream &output, size_t total_cells) {
for (size_t i = 0; i < total_cells - 1; ++i) {
output << '\t';
}
output << '\n';
}