-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheet.cpp
228 lines (198 loc) · 6.05 KB
/
sheet.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "sheet.h"
#include "cell.h"
#include "common.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <optional>
#include <vector>
#include <set>
using namespace std::literals;
Sheet::~Sheet() = default;
void Sheet::SetCell(Position pos, std::string text) {
if (!pos.IsValid()){
throw InvalidPositionException("Invalid position");
}
std::unique_ptr<CellInterface> tmp_cell = std::make_unique<Cell>(*this);
tmp_cell.get()->Set(text);
CheckCircularDependence_(tmp_cell.get(), pos);
UpdateDependencies_(tmp_cell.get(), pos);
if (!sheet_.count(pos)){
sheet_[pos] = std::move(tmp_cell);
UpdateCorners_(pos);
} else {
sheet_[pos] = std::move(tmp_cell);
}
}
const CellInterface* Sheet::GetCell(const Position pos) const {
if (!IsPositionValid_(pos)){
return nullptr;
}
return sheet_.at(pos).get();
}
CellInterface* Sheet::GetCell(Position pos) {
if (!IsPositionValid_(pos)){
return nullptr;
}
return sheet_.at(pos).get();
}
void Sheet::ClearCell(Position pos) {
if (!IsPositionValid_(pos)){
return;
}
sheet_.erase(pos);
if (!sheet_.size()){
top_left_ = bottom_right_ = Position::NONE;
return;
}
if (pos.row == bottom_right_.row || pos.col == bottom_right_.col){
bottom_right_ = FindMaxPosition_();
}
if (pos.row == top_left_.row || pos.col == top_left_.col){
top_left_ = FindMinPosition_();
}
}
Size Sheet::GetPrintableSize() const {
if (top_left_ == Position::NONE){
return {0, 0};
}
return {bottom_right_.row - top_left_.row + 1, bottom_right_.col - top_left_.col + 1};
}
void Sheet::PrintValues(std::ostream& output) const {
PrintFunc print_func = [](std::ostream& output, CellInterface *ptr){
const auto& value = ptr->GetValue();
if (std::holds_alternative<double>(value)){
output << std::get<double>(value);
} else if (std::holds_alternative<std::string>(value)){
output << std::get<std::string>(value);
} else {
output << std::get<FormulaError>(value);
}
};
Print_(output, print_func);
}
void Sheet::PrintTexts(std::ostream& output) const {
PrintFunc print_func = [](std::ostream& output, CellInterface *ptr){
output << ptr->GetText();
};
Print_(output, print_func);
}
void Sheet::UpdateDependencies_(CellInterface* tmp_cell, Position pos){
for (const auto& ref_pos : tmp_cell->GetReferencedCells()){
try{
CellInterface *referenced_cell = GetCell(ref_pos);
if (!referenced_cell){
sheet_[ref_pos] = std::move(std::make_unique<Cell>(*this));
sheet_.at(ref_pos).get()->Set("");
referenced_cell = sheet_.at(ref_pos).get();
}
referenced_cell->AddDependentCell(pos);
} catch (InvalidPositionException&){
}
}
}
void Sheet::CheckCircularDependence_(CellInterface* tmp_cell, Position pos) {
std::vector<Position> positions_to_check;
std::set<Position> checked_positions;
for (size_t i = 0; i < tmp_cell->GetReferencedCells().size(); ++i){
positions_to_check.push_back(tmp_cell->GetReferencedCells()[i]);
}
while (!positions_to_check.empty()){
Position current_pos = positions_to_check[0];
if (current_pos == pos){
throw CircularDependencyException("");
}
if (checked_positions.count(current_pos)){
positions_to_check.erase(positions_to_check.begin());
continue;
}
checked_positions.insert(current_pos);
CellInterface* cell;
try {
cell = GetCell(current_pos);
} catch (InvalidPositionException&){
cell = nullptr;
}
if (cell){
for (size_t i = 0; i < cell->GetReferencedCells().size(); ++i){
positions_to_check.push_back(cell->GetReferencedCells()[i]);
}
}
positions_to_check.erase(positions_to_check.begin());
}
}
void Sheet::Print_(std::ostream &output, PrintFunc print_func) const
{
if (top_left_ == Position::NONE){
return;
}
for (int y = top_left_.row; y <= bottom_right_.row; ++y){
for (int x = top_left_.col; x <= bottom_right_.col; ++x){
Position pos;
pos.row = y;
pos.col = x;
if (sheet_.count(pos)){
print_func(output, sheet_.at(pos).get());
}
if (x != bottom_right_.col){
output << '\t';
}
}
output << '\n';
}
}
bool Sheet::IsPositionValid_(Position pos) const {
if (!pos.IsValid()){
throw InvalidPositionException("Invalid position");
}
if (!sheet_.count(pos)){
return false;
}
return true;
}
Position Sheet::FindMaxPosition_(){
Position pos;
pos.col = pos.row = 0;
for (const auto& [position, cell] : sheet_){
if (position.col > pos.col){
pos.col = position.col;
}
if (position.row > pos.row){
pos.row = position.row;
}
}
return pos;
}
Position Sheet::FindMinPosition_(){
Position pos;
pos.col = (*sheet_.begin()).first.col;
pos.row = (*sheet_.begin()).first.row;
for (const auto& [position, cell] : sheet_){
if (position.col > pos.col){
pos.col = position.col;
}
if (position.row > pos.row){
pos.row = position.row;
}
}
return pos;
}
void Sheet::UpdateCorners_(Position pos){
if (top_left_ == Position::NONE){
top_left_ = bottom_right_ = pos;
return;
}
if (pos.row < top_left_.row){
top_left_.row = pos.row;
} else if (pos.row > bottom_right_.row){
bottom_right_.row = pos.row;
}
if (pos.col < top_left_.col){
top_left_.col = pos.col;
} else if (pos.col > bottom_right_.col){
bottom_right_.col = pos.col;
}
}
std::unique_ptr<SheetInterface> CreateSheet() {
return std::make_unique<Sheet>();
}