-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheet.cpp
More file actions
157 lines (131 loc) · 3.4 KB
/
sheet.cpp
File metadata and controls
157 lines (131 loc) · 3.4 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
#include "sheet.h"
using namespace std::literals;
void Sheet::SetCell(Position pos, std::string text)
{
if (!pos.IsValid()) {
throw InvalidPositionException("");
}
if (pos.row >= size_.rows) {
AddRow(pos.row - size_.rows + 1);
}
if (pos.col >= size_.cols) {
AddColumn(pos.col - size_.cols + 1);
}
auto& curr_cell = cells_[pos.row][pos.col];
// óâåëè÷èâàåì èíäåêñàöèþ ñòðîêè è ñòîëáöà íà îäíó çàïèñü
if (!curr_cell) {
++rows_fit_[pos.row];
++cols_fit_[pos.col];
curr_cell = std::make_unique<Cell>(*this);
}
curr_cell->Set(std::move(text));
}
const CellInterface* Sheet::GetCell(Position pos) const
{
if (!pos.IsValid()) {
throw InvalidPositionException("");
}
if (pos.row >= size_.rows || pos.col >= size_.cols) {
return nullptr;
}
return cells_[pos.row][pos.col].get();
}
CellInterface* Sheet::GetCell(Position pos)
{
if (!pos.IsValid()) {
throw InvalidPositionException("");
}
if (pos.row >= size_.rows || pos.col >= size_.cols) {
return nullptr;
}
return cells_[pos.row][pos.col].get();
}
void Sheet::ClearCell(Position pos)
{
if (!pos.IsValid()) {
throw InvalidPositionException("");
}
Cell* curr_cell = static_cast<Cell*>(GetCell(pos));
// óìåíüøàåì èíäåêñàöèþ ñòðîêè è ñòîëáöà íà îäíó çàïèñü
if (curr_cell != nullptr) {
--rows_fit_[pos.row];
--cols_fit_[pos.col];
curr_cell->Clear();
}
// íàäî óðåçàòü îáëàñòü ïî ìèíèìàëüíûì äàííûì
ShrinkToFit();
}
Size Sheet::GetPrintableSize() const
{
return { size_.rows, size_.cols };
}
void Sheet::PrintValues(std::ostream& output) const
{
Print(output, [&output](const Cell* cell) {
std::visit(
[&output](const auto& x) {
output << x;
},
cell->GetValue());
});
}
void Sheet::PrintTexts(std::ostream& output) const
{
Print(output, [&output](const Cell* cell) {
output << cell->GetText();
});
}
CellInterface* Sheet::ObtainCell(Position pos)
{
CellInterface* cell = GetCell(pos);
if (cell == nullptr) {
SetCell(pos, ""s);
cell = GetCell(pos);
}
return static_cast<Cell*>(cell);
}
void Sheet::AddColumn(int count)
{
int new_size = size_.cols + count;
for (auto& row : cells_) {
row.resize(new_size);
}
size_.cols = new_size;
cols_fit_.resize(new_size);
}
void Sheet::AddRow(int count)
{
int new_size = size_.rows + count;
cells_.resize(new_size);
for (int i = size_.rows; i < new_size; ++i) {
cells_[i].resize(size_.cols);
}
size_.rows = new_size;
rows_fit_.resize(new_size);
}
void Sheet::ShrinkToFit()
{
size_t row_count = size_.rows;
if (row_count > 0) {
while (row_count > 0 && rows_fit_[row_count - 1] == 0) {
--row_count;
}
cells_.resize(row_count);
rows_fit_.resize(row_count);
size_.rows = row_count;
}
size_t col_count = size_.cols;
if (col_count > 0) {
while (col_count > 0 && cols_fit_[col_count - 1] == 0) {
--col_count;
}
for (auto& row : cells_) {
row.resize(col_count);
}
cols_fit_.resize(col_count);
size_.cols = col_count;
}
}
std::unique_ptr<SheetInterface> CreateSheet() {
return std::make_unique<Sheet>();
}