-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsparsemap.cpp
More file actions
112 lines (87 loc) · 1.94 KB
/
sparsemap.cpp
File metadata and controls
112 lines (87 loc) · 1.94 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
#include "cell.h"
#include "document.h"
#include "sparsemap.h"
SparseMap::SparseMap(Document *parent)
: document_(parent)
{
}
SparseMap::SparseMap(const SparseMap &other)
{
document_ = other.document_;
const CellMap &cells = other.cells();
for (CellMap::ConstIterator it = cells.begin(); it != cells.end(); ++it) {
cells_[it.key()] = new Cell(*it.value());
}
}
SparseMap::~SparseMap()
{
clear();
}
bool SparseMap::contains(const QPoint &pos) const
{
return cells_.contains(pos);
}
Cell* SparseMap::cellAt(const QPoint &pos)
{
if (!document_->boundingRect().contains(pos))
return NULL;
CellMap::iterator it = cells_.find(pos);
if (it == cells_.end()) {
Cell *c = new Cell(pos, document_);
cells_[pos] = c;
return c;
}
return it.value();
}
Cell* SparseMap::overwrite(const Cell &c)
{
if (!document_->boundingRect().contains(c.pos()))
return NULL;
if (cells_.contains(c.pos())) {
delete cells_[c.pos()];
cells_.remove(c.pos());
}
if (!c.isEmpty()) {
Cell *newCell = new Cell(c);
cells_.insert(c.pos(), newCell);
return newCell;
}
return NULL;
}
Cell* SparseMap::merge(const Cell &c)
{
if (!document_->boundingRect().contains(c.pos()))
return NULL;
if (cells_.contains(c.pos())) {
Cell *oc = cells_[c.pos()];
oc->merge(c);
return oc;
} else {
Cell *newCell = new Cell(c);
cells_.insert(c.pos(), newCell);
return newCell;
}
}
void SparseMap::remove(const QPoint &pos)
{
if (cells_.contains(pos)) {
Cell *c = cells_[pos];
delete c;
cells_.remove(pos);
}
}
void SparseMap::clear()
{
for (QMap<QPoint, Cell *>::iterator it = cells_.begin();
it != cells_.end();
++it) {
delete it.value();
}
cells_.clear();
}
bool operator<(const QPoint &a, const QPoint &b)
{
long long al = (long long)a.y() << 32 | (long long)a.x();
long long bl = (long long)b.y() << 32 | (long long)b.x();
return al < bl;
}