-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.cpp
More file actions
208 lines (175 loc) · 5.42 KB
/
cell.cpp
File metadata and controls
208 lines (175 loc) · 5.42 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "cell.h"
#include <cassert>
#include <iostream>
#include <string>
#include <optional>
using namespace std::literals;
std::ostream &operator<<(std::ostream &output, const CellInterface::Value &value) {
std::visit([&](const auto &x) { output << x; }, value);
return output;
}
class Cell::Impl {
public:
Impl() = default;
virtual ~Impl() = default;
virtual Value GetValue() const = 0;
virtual std::string GetText() const = 0;
virtual std::vector<Position> GetReferencedCells() const {
return {};
}
virtual void InvalidateCache() {};
};
class Cell::EmptyImpl : public Impl {
public:
Value GetValue() const override {
return ""s;
}
std::string GetText() const override {
return ""s;
}
};
class Cell::TextImpl : public Impl {
public:
explicit TextImpl(std::string str) : text_(std::move(str)) {
if (text_.empty()) {
throw std::invalid_argument("Empty string for TextCell");
}
}
Value GetValue() const override {
if (text_[0] == ESCAPE_SIGN) {
return text_.substr(1);
}
return text_;
}
std::string GetText() const override {
return text_;
}
private:
std::string text_;
};
class Cell::FormulaImpl : public Impl {
public:
explicit FormulaImpl(std::string str, const SheetInterface &sheet)
: formula_(ParseFormula(std::move(str))), sheet_(sheet) {}
Value GetValue() const override {
if (!cache_.has_value()) {
cache_ = formula_->Evaluate(sheet_);
}
return std::visit(
[](const auto &formula_interface_value) { return Value(formula_interface_value); }, cache_.value());
}
std::string GetText() const override {
return FORMULA_SIGN + formula_->GetExpression();
}
std::vector<Position> GetReferencedCells() const override {
return formula_->GetReferencedCells();
}
void InvalidateCache() override {
cache_.reset();
}
private:
std::unique_ptr<FormulaInterface> formula_;
const SheetInterface &sheet_;
mutable std::optional<FormulaInterface::Value> cache_;
};
Cell::Cell(SheetInterface &sheet) : impl_(std::make_unique<EmptyImpl>()), sheet_(sheet) {}
Cell::~Cell() {}
void Cell::Set(const std::string &text) {
if (text.empty()) {
impl_ = std::make_unique<EmptyImpl>();
return;
}
//Текст, который содержит только знак "=", формулой не считается, а считается текстом
if (text[0] == FORMULA_SIGN && text.size() > 1) {
auto impl = std::make_unique<FormulaImpl>(text.substr(1), sheet_);
//check circular dependency
if (CircularDependency(*impl)) {
throw CircularDependencyException("Circular dependency");
}
//CircularDependency ok - store value
impl_ = std::move(impl);
//
auto referenced_cells = impl_->GetReferencedCells();
//create outref cells if not exist
for (auto &cell : referenced_cells) {
if (!sheet_.GetCell(cell)) {
sheet_.SetCell(cell, {});
}
}
//update references
FillCellsRefs();
//
InvalidateCache();
//
return;
}
impl_ = std::make_unique<TextImpl>(text);
}
void Cell::Clear() {
Set(""); //impl_ = std::make_unique<EmptyImpl>();
}
Cell::Value Cell::GetValue() const {
return impl_->GetValue();
}
std::string Cell::GetText() const {
return impl_->GetText();
}
std::vector<Position> Cell::GetReferencedCells() const {
return impl_->GetReferencedCells();
}
bool Cell::IsReferenced() const {
return !inRefCells_.empty();
}
bool Cell::CircularDependency(const Cell::Impl &impl_) const {
auto get_referenced_cells = impl_.GetReferencedCells();
if (get_referenced_cells.empty()) {
//no dependencies
return false;
}
//make set of all cells in formula
std::unordered_set<const CellInterface *> refs;
for (const auto &pos : get_referenced_cells) {
refs.insert(sheet_.GetCell(pos));
}
//visit all cells and check circular references
std::unordered_set<const CellInterface *> visited;
std::stack<const CellInterface *> to_visit;
to_visit.push(this);
while (!to_visit.empty()) {
const CellInterface *current = to_visit.top();
to_visit.pop();
visited.insert(current);
if (refs.find(current) != refs.end()) {
return true;
}
auto &ref_cells = static_cast<const Cell *>(current)->inRefCells_;
for (auto &incoming : ref_cells) {
if (visited.find(incoming) == visited.end()) {
to_visit.push(incoming);
}
}
}
return false;
}
void Cell::FillCellsRefs() {
//first clear inref in all outgoing ref cells
for (auto &outgoing : outRefCells_) {
outgoing->inRefCells_.erase(this);
}
outRefCells_.clear();
for (const auto &pos : impl_->GetReferencedCells()) {
auto outgoing = reinterpret_cast<Cell *>(sheet_.GetCell(pos));
//update cross links
outRefCells_.insert(outgoing);
outgoing->inRefCells_.insert(this);
}
}
void Cell::InvalidateCache() {
impl_->InvalidateCache();
for (auto in_ref : inRefCells_) {
in_ref->InvalidateCache();
}
}
std::unique_ptr<Cell> CreateCell(SheetInterface &sheet) {
return std::make_unique<Cell>(sheet);
}