-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsysyDATA.cpp
More file actions
140 lines (138 loc) · 5.13 KB
/
Copy pathsysyDATA.cpp
File metadata and controls
140 lines (138 loc) · 5.13 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
#ifndef defs_hpp
#include "defs.hpp"
#define defs_hpp
#endif
using namespace std;
/* =============================================== */
/* condese */
/* - merge nodes of the same depth */
/* - at most n nodes be merged once, n:length */
/* =============================================== */
void dataAggr::condense(vector<int> &shape) {
assert(!aggr.empty());
int depth = aggr.back()->depth;
assert(depth > 0);
dataAggr *cds = new dataAggr(depth - 1);
for (int k = 0; k < shape[depth - 1]; k++)
{
if (aggr.empty()) continue;
dataCell* back = aggr.back();
if (back->depth != depth) continue;
cds->aggr.push_back(back);
aggr.pop_back();
}
reverse(cds->aggr.begin(), cds->aggr.end());
aggr.push_back(cds);
}
/* =============================================== */
/* merge */
/* - new elements are going to be appended */
/* - merge the end */
/* =============================================== */
void dataAggr::merge(vector<int> &shape) {
while (!aggr.empty()) {
int depth = aggr.back()->depth;
assert(depth > 0);
int sz = aggr.size(), k = shape[depth - 1];
if (sz < k || aggr[sz-k]->depth != depth) return ;
condense(shape);
}
}
/* =============================================== */
/* liftup */
/* - merge immediately after condense */
/* - merge until back().depth <= depth */
/* =============================================== */
void dataAggr::liftup(vector<int> &shape, int depth) {
while (!aggr.empty() && aggr.back()->depth > depth)
{ condense(shape); merge(shape); }
merge(shape);
}
/* =============================================== */
/* prune */
/* - array -> {array} */
/* =============================================== */
dataAggr* dataAggr::prune(vector<int> &shape) {
assert(!aggr.empty());
while (true) {
bool condense_flag = false;
int depth = aggr.back()->depth;
assert(depth > 0);
if (aggr.front()->depth != depth) /* elements' depth is not consistant */
condense_flag = true;
if (aggr.size() > shape[depth -1]) /* elements' quantity exceeds bound */
condense_flag = true;
if (!condense_flag) break;
condense(shape);
}
depth = aggr[0]->depth - 1;
return this;
}
/* =============================================== */
/* instantialize */
/* - substitue expr with its integer value */
/* =============================================== */
void dataAggr::instantialize() {
for (auto child: aggr)
child->instantialize();
}
/* =============================================== */
/* initialize */
/* - set initial value */
/* =============================================== */
void dataAggr::initialize(string &eeyore, vector<int> &shape, int addr, bool var) {
addr *= shape[depth];
for (int i = 0; i < aggr.size(); i++)
aggr[i]->initialize(eeyore, shape, addr + i, var);
}
void dataLeaf::initialize(string &eeyore, vector<int> &shape, int addr, bool var) {
if (expr == NULL) return ;
if (var) {
eeyoreAST *lval = new _eVAR(eeyore);
eeyoreAST *rval = expr->atomize(lval);
if (lval != rval) /* optimization here */
eeyoreStmt(new _eDIRECT(lval, rval));
}
else {
eeyoreAST *rval = expr->atomize(NULL);
eeyoreStmt(new _eSAVE(eeyore, new _eNUM(addr * 4), rval));
}
}
/* =============================================== */
/* debug */
/* - visualization */
/* =============================================== */
void dataAggr::debug() {
printf("{ ");
bool colon = false;
for (auto x: aggr) {
if (colon) printf(", ");
x->debug();
colon = true;
}
printf(" }");
}
/* =============================================== */
/* zero-padding */
/* - explicitly set 0 for unassigned vars */
/* =============================================== */
void zero_padding(dataCell* &root, vector<int> &shape, int depth) {
if (depth == shape.size()) {
/* leaf node */
if (!root)
root = (dataCell *)(new dataLeaf(depth, new _INTEGER(0)));
return ;
}
if (!root)
root = (dataCell *)(new dataAggr(depth));
dataAggr *x = (dataAggr *) root;
assert(x->aggr.size() <= shape[depth]);
for (auto &child: x->aggr)
zero_padding(child, shape, depth + 1);
for (int i = x->aggr.size(); i < shape[depth]; i++) {
dataCell *child = NULL;
zero_padding(child, shape, depth + 1);
x->aggr.push_back(child);
}
assert(x->aggr.size() == shape[depth]);
}