-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
206 lines (170 loc) · 6.02 KB
/
node.cpp
File metadata and controls
206 lines (170 loc) · 6.02 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "node.h"
#include <iostream>
Link::Link() {
new (&node) std::unique_ptr<Node>(nullptr);
}
Link::~Link() {}
// initialising static class variables
int Node::currentUsage = 0;
int Node::maxUsage = 0;
Node::Node() {}
void* operator new(size_t size) noexcept {
// the user made a new node so pls record it
Node::currentUsage++;
// do a max check
if (Node::currentUsage > Node::maxUsage)
Node::maxUsage = Node::currentUsage;
void* p = std::malloc(size);
return p;
}
void operator delete(void* p) noexcept {
Node::currentUsage--;
std::free(p);
}
bool Node::at(size_t i, size_t j, size_t distanceFromLeaf) {
int newI = i, newJ = j;
// find out which quadrant [i,j]-th entry is in and
// adjust the i, j appropriately for the recursive call
size_t halfMatrixSize = 1 << (distanceFromLeaf + 2);
size_t quadrant = 0;
if (i >= halfMatrixSize) {
quadrant += 2;
newI -= halfMatrixSize;
}
if (j >= halfMatrixSize) {
quadrant++;
newJ -= halfMatrixSize;
}
// do it here I reckon
// may not be distance == 0, could be 1
if (distanceFromLeaf == 1) {
size_t bitPos = newI * ROW_LENGTH + newJ;
uint64_t num = children[quadrant].leafMatrix;
return (num >> (63 - bitPos)) & 1;
}
if (children[quadrant].node.get() == nullptr)
return false;
return children[quadrant].node
->at(newI, newJ, distanceFromLeaf - 1);
}
Node * Node::get(size_t i, size_t j) {
Node * result = children[BLOCK_DIM * i + j].node.get();
return result;
}
std::unique_ptr<Node> Node::Construct(std::string s) {
// if everything in the string is a zero, return nullptr
if (s.find("1") == std::string::npos)
return std::unique_ptr<Node>(nullptr);
auto result = std::make_unique<Node>();
MatrixProcessor p;
for (int i = 0; i < NUM_CHILDREN; ++i) {
std::string quadrant = p.recoverQuadrant(s, i);
// base case when the length is like that
if (s.length() == 256) {
result->children[i].leafMatrix = std::stoull(quadrant, 0, 2);
} else {
result->children[i].node = Construct(quadrant);
}
}
return result;
}
std::unique_ptr<Node> Node::Clone(size_t distanceFromLeaf) {
auto n = std::make_unique<Node>();
if (distanceFromLeaf == 1) {
for (int i = 0; i < NUM_CHILDREN; ++i)
n->children[i].leafMatrix = children[i].leafMatrix;
return n;
}
for (int i = 0; i < NUM_CHILDREN; ++i) {
n->children[i].node =
children[i].node.get() == nullptr ?
std::unique_ptr<Node>(nullptr) :
children[i].node->Clone(distanceFromLeaf - 1);
}
return n;
}
std::unique_ptr<Node> Node::Union
(Node * const A, Node * const B, size_t distanceFromLeaf) {
// if only one of them is nullptr, give back the other one
if (A == nullptr ^ B == nullptr) {
return (A == nullptr) ?
B->Clone(distanceFromLeaf) :
A->Clone(distanceFromLeaf);
}
// if both are nullptr, give back unique ptr to nullptr
if (A == nullptr && B == nullptr)
return std::unique_ptr<Node>(nullptr);
// now both are not nullptr
auto result = std::make_unique<Node>();
// deal with base case
if (distanceFromLeaf == 1) {
// directly union them
for (int i = 0; i < NUM_CHILDREN; ++i) {
result->children[i].leafMatrix =
A->children[i].leafMatrix | B->children[i].leafMatrix;
}
} else {
//deal with recursive case, easy! just construct a new node and each of its children
// nodes are the union of blahs
for (int i = 0; i < NUM_CHILDREN; ++i) {
result->children[i].node =
Union(A->children[i].node.get(),
B->children[i].node.get(),
distanceFromLeaf - 1);
}
}
return result;
}
std::unique_ptr<Node> Node::Product
(Node * const A, Node * const B, size_t distanceFromLeaf) {
/*
* In a product, if either side is 'all zeros', the result is always
* all zeros.
*/
//1. Check out whether either are nullptr, then return nullptr
if (A == nullptr || B == nullptr) {
return std::unique_ptr<Node>(nullptr);
}
// Neither are nullptr now
auto result = std::make_unique<Node>();
//2. Check for base case
if (distanceFromLeaf == 1) {
// regular matrix multiplication
// CHANGED CODE HERE
//
bool seenNonZero = false;
for (int i = 0; i < BLOCK_DIM; ++i) {
for (int j = 0; j < BLOCK_DIM; ++j) {
uint64_t entry = 0;
for (int k = 0; k < BLOCK_DIM; ++k) {
uint64_t first = A->children[BLOCK_DIM*i + k].leafMatrix;
uint64_t second = B->children[BLOCK_DIM*k + j].leafMatrix;
entry |= mult(first, second);
}
// entry has now been calculated
result->children[BLOCK_DIM*i + j].leafMatrix = entry;
if (entry != 0) seenNonZero = true;
}
// if all the entries are null, you need to return a nullptr
// not a dumb thing yeah
if (!seenNonZero) return std::unique_ptr<Node>(nullptr);
}
return result;
}
// we need to recursively compute the product here :) It should
// be easier than last time
for (int i = 0; i < BLOCK_DIM; ++i) {
for (int j = 0; j < BLOCK_DIM; ++j) {
// the quadrant, it's a node unique ptr
auto n = std::unique_ptr<Node>(nullptr);
for (int k = 0; k < BLOCK_DIM; ++k) {
Node* first = A->get(i, k);
Node* second = B->get(k, j);
auto product = Product(first, second, distanceFromLeaf - 1);
n = Union(n.get(), product.get(), distanceFromLeaf - 1);
}
result->children[BLOCK_DIM*i + j].node = std::move(n);
}
}
return result;
}