Repository navigation
Expand file tree
/
Copy pathsheet.cpp
More file actions
118 lines (96 loc) · 3.04 KB
/
Copy pathsheet.cpp
File metadata and controls
118 lines (96 loc) · 3.04 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
#include "sheet.h"
#include "cell.h"
#include "common.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <optional>
using namespace std::literals;
Sheet::~Sheet() {}
void Sheet::SetCell(Position pos, std::string text) {
if (!pos.IsValid()) {
throw InvalidPositionException("invalid position");
}
const auto& cell = cells_.find(pos);
//auto& cell = cells_.at(pos);
if (cell == cells_.end()) {
cells_.emplace(pos, std::make_unique<Cell>(*this));
}
cells_.at(pos)->Set(std::move(text));
}
const CellInterface* Sheet::GetCell(Position pos) const {
return GetCellPtr(pos);
}
CellInterface* Sheet::GetCell(Position pos) {
return GetCellPtr(pos);
}
void Sheet::ClearCell(Position pos) {
if (!pos.IsValid()) {
throw InvalidPositionException("invalid position");
}
const auto& cell = cells_.find(pos);
if (cell != cells_.end() && cell->second != nullptr) {
cell->second->Clear();
cell->second.reset();
}
}
Size Sheet::GetPrintableSize() const {
//if (cells_.begin() == cells_.end()) return { 0, 0 };
Size result{ 0, 0 };
for (auto it = cells_.begin(); it != cells_.end(); ++it) {
if (it->second != nullptr){// && !it->second->GetText().empty()) {
const int c = it->first.col;
const int r = it->first.row;
result.rows = std::max(result.rows, r + 1);
result.cols = std::max(result.cols, c + 1);
}
}
return { result.rows, result.cols};
}
void Sheet::PrintValues(std::ostream& output) const {
Size size = GetPrintableSize();
for (int r = 0; r < size.rows; ++r) {
for (int c = 0; c < size.cols; ++c) {
if (c > 0) {
output << "\t";
}
const auto& it = cells_.find({ r, c });
if (it != cells_.end() && it->second != nullptr && !it->second->GetText().empty()) {
std::visit([&](const auto value) {output << value; }, it->second->GetValue());
}
}
output << "\n";
}
}
void Sheet::PrintTexts(std::ostream& output) const {
Size size = GetPrintableSize();
for (int r = 0; r < size.rows; ++r) {
for (int c = 0; c < size.cols; ++c) {
if (c > 0) {
output << "\t";
}
const auto& it = cells_.find({ r, c });
if (it != cells_.end() && it->second != nullptr && !it->second->GetText().empty()) {
output << it->second->GetText();
}
}
output << "\n";
}
}
const Cell* Sheet::GetCellPtr(Position pos) const {
if (!pos.IsValid()) {
throw InvalidPositionException("Invalid position");
}
const auto cell = cells_.find(pos);
if (cell == cells_.end()) {
return nullptr;
}
return cells_.at(pos).get();
}
Cell* Sheet::GetCellPtr(Position pos) {
return const_cast<Cell*>(
static_cast<const Sheet&>(*this).GetCellPtr(pos));
}
std::unique_ptr<SheetInterface> CreateSheet() {
return std::make_unique<Sheet>();
}