Repository navigation
Expand file tree
/
Copy pathcell.cpp
More file actions
182 lines (153 loc) · 4.59 KB
/
Copy pathcell.cpp
File metadata and controls
182 lines (153 loc) · 4.59 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
#include "cell.h"
#include <cassert>
#include <iostream>
#include <string>
#include <optional>
#include <stack>
#include "sheet.h"
class Cell::Impl {
public:
virtual ~Impl() = default;
virtual Value GetValue() const = 0;
virtual std::string GetText() const = 0;
virtual std::vector<Position> GetReferencedCells() const { return {}; }
virtual void InvalidateCache() {}
protected:
//Value value_;
//std::string text_;
};
class Cell::EmptyImpl : public Impl {
public:
Value GetValue() const override {
return "";
}
std::string GetText() const override {
return "";
}
};
class Cell::TextImpl : public Impl {
std::string text_;
public:
TextImpl(std::string text) : text_(std::move(text)) {
if (text_.empty()) {
throw std::logic_error("");
}
}
Value GetValue() const override {
if (text_[0] == ESCAPE_SIGN) {
return text_.substr(1);
}
return text_;
}
std::string GetText() const override {
return text_;
}
};
class Cell::FormulaImpl : public Impl {
std::unique_ptr<FormulaInterface> formula_ptr_;
const SheetInterface& sheet_;
mutable std::optional<FormulaInterface::Value> cache_;
public:
explicit FormulaImpl(std::string expression, const SheetInterface& sheet)
: sheet_(sheet) {
if (expression.empty() || expression[0] != FORMULA_SIGN) {
throw std::logic_error("");
}
//expression = expression.substr(1);
//value_ = std::string(expression);
formula_ptr_ = ParseFormula(expression.substr(1));
//text_ = "=" + formula_ptr_->GetExpression();
}
Value GetValue() const override {
if (!cache_) {
cache_ = formula_ptr_->Evaluate(sheet_);
}
auto value = formula_ptr_->Evaluate(sheet_);
if (std::holds_alternative<double>(value)) {
return std::get<double>(value);
}
return std::get<FormulaError>(value);
}
std::string GetText() const override {
return FORMULA_SIGN + formula_ptr_->GetExpression();
}
void InvalidateCache() override { cache_.reset(); }
std::vector<Position> GetReferencedCells() const { return formula_ptr_->GetReferencedCells(); }
};
std::vector<Position> Cell::GetReferencedCells() const { return impl_->GetReferencedCells(); }
Cell::Cell(Sheet& sheet)
:impl_(std::make_unique<EmptyImpl>())
, sheet_(sheet)
{}
Cell::~Cell() {}
void Cell::Set(std::string text) {
std::unique_ptr<Impl> impl;
if (text.empty()) {
impl = std::make_unique<EmptyImpl>();
}
else if (text.size() > 1 && text[0] == FORMULA_SIGN) {
impl = std::make_unique<FormulaImpl>(std::move(text), sheet_);
}
else {
impl = std::make_unique<TextImpl>(std::move(text));
}
if (WouldIntroduceCircularDependency(*impl)) {
throw CircularDependencyException("");
}
impl_ = std::move(impl);
for (Cell* outgoing : r_nodes_) {
outgoing->l_nodes_.erase(this);
}
r_nodes_.clear();
for (const auto& pos : impl_->GetReferencedCells()) {
Cell* outgoing = sheet_.GetCellPtr(pos);
if (!outgoing) {
sheet_.SetCell(pos, "");
outgoing = sheet_.GetCellPtr(pos);
}
r_nodes_.insert(outgoing);
outgoing->l_nodes_.insert(this);
}
InvalidateCacheRecursive(true);
}
void Cell::InvalidateCacheRecursive(bool force) {
impl_->InvalidateCache();
for (Cell* incoming : l_nodes_) {
incoming->InvalidateCacheRecursive();
}
}
bool Cell::WouldIntroduceCircularDependency(const Impl& new_impl) const {
if (new_impl.GetReferencedCells().empty()) {
return false;
}
std::unordered_set<const Cell*> referenced;
for (const auto& pos : new_impl.GetReferencedCells()) {
referenced.insert(sheet_.GetCellPtr(pos));
}
std::unordered_set<const Cell*> visited;
std::stack<const Cell*> to_visit;
to_visit.push(this);
while (!to_visit.empty()) {
const Cell* current = to_visit.top();
to_visit.pop();
visited.insert(current);
if (referenced.find(current) != referenced.end()) {
return true;
}
for (const Cell* incoming : current->l_nodes_) {
if (visited.find(incoming) == visited.end()) {
to_visit.push(incoming);
}
}
}
return false;
}
void Cell::Clear() {
impl_ = std::make_unique<EmptyImpl>();
}
Cell::Value Cell::GetValue() const {
return impl_->GetValue();
}
std::string Cell::GetText() const {
return impl_->GetText();
}