-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquadtree.cpp
More file actions
61 lines (51 loc) · 1.75 KB
/
quadtree.cpp
File metadata and controls
61 lines (51 loc) · 1.75 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
#include <cassert>
#include "quadtree.h"
Quadtree::Quadtree(Node* head, size_t height) : head(head), height(height) {}
Quadtree::Quadtree(const Quadtree &other)
: head(other.head->Clone(height - 1).release()),
height(other.height) {}
bool Quadtree::at(size_t i, size_t j) const {
if (head.get() == nullptr)
return false;
return head->at(i, j, height - 1);
}
std::ostream& operator<<(std::ostream& os,
const Quadtree& quadtree) {
// size_t size = (1 << quadtree.height) * MIN_SIZE;
size_t size = 1 << (quadtree.height + 2);
os << "[";
for (size_t i = 0; i < size; ++i) {
for (size_t j = 0; j < size; ++j) {
bool val = quadtree.at(i, j);
os << val;
}
if (i != size - 1) os << ", ";
}
os << "]";
return os;
}
Quadtree Quadtree::operator+(const Quadtree& other) {
return Quadtree(Node::Union(head.get(),
other.head.get(),
height - 1)
.release(), height);
}
Quadtree Quadtree::operator*(const Quadtree& other) {
return Quadtree(Node::Product(head.get(),
other.head.get(),
height - 1)
.release(), height);
}
Quadtree Quadtree::Construct(std::string text) {
MatrixProcessor processor;
// CHECK THAT THE SIZE EVEN MAKES SENSE
// FOR A QUADTREE
// size 2 -> height 1
// size 4 -> height 2
std::string s = processor.strip(text);
size_t size = (size_t) sqrt(s.length());
assert(size * size == s.length());
size_t height = (size_t) (log(size) / log(2)) - 2;
// make sure this is right
return Quadtree(Node::Construct(s).release(), height);
}