8#ifndef INCLUDED_SDSL_STRUCTURE_TREE
9#define INCLUDED_SDSL_STRUCTURE_TREE
15#include <unordered_map>
26 for (
size_t i = 0; i < level; i++) out <<
"\t";
32 using map_type = std::unordered_map<std::string, std::unique_ptr<structure_tree_node>>;
49 auto child_itr = m_children.find(hash);
50 if (child_itr == m_children.end())
54 m_children[hash] = std::unique_ptr<structure_tree_node>(new_node);
60 return (*child_itr).second.get();
80template <format_type F>
89 out <<
"{" << std::endl;
91 out <<
"\"class_name\":"
92 <<
"\"" << v->
type <<
"\"," << std::endl;
95 <<
"\"" << v->
name <<
"\"," << std::endl;
98 <<
"\"" << v->
size <<
"\"";
102 out <<
"," << std::endl;
104 out <<
"\"children\":[" << std::endl;
105 size_t written_child_elements = 0;
106 for (
const auto & child : v->
children)
108 if (written_child_elements++ > 0) { out <<
"," << std::endl; }
113 out <<
"]" << std::endl;
126 std::stringstream jsonheader;
127 jsonheader <<
"<html>\n"
129 <<
" <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n"
130 <<
" <title>" << file_name <<
"</title>\n"
131 <<
" <script src=\"file:///builddir/build/BUILD/sdsl-lite-3.0.1/external/d3/d3.min.js\"></script>"
132 <<
" <script src=\"http://d3js.org/d3.v2.js\"></script>\n"
133 <<
" <style type=\"text/css\">\n"
134 <<
" path { stroke: #000; stroke-width: 0.8; cursor: pointer; }\n"
135 <<
" text { font: 11px sans-serif; cursor: pointer; }\n"
136 <<
" body { width: 900; margin: 0 auto; }\n"
137 <<
" h1 { text-align: center; margin: .5em 0; }\n"
138 <<
" #breadcrumbs { display: none; }\n"
139 <<
" svg { font: 10px sans-serif; }\n"
142 <<
"<body marginwidth=\"0\" marginheight=\"0\">\n"
143 <<
"<button><a id=\"download\">Save as SVG</a></button>\n"
144 <<
" <div id=\"chart\"></div>" << std::endl;
145 return jsonheader.str();
150 std::stringstream jsonbody;
151 jsonbody <<
"<script type=\"text/javascript\">" << std::endl
156 " x = d3.scale.linear().range([0, 2 * Math.PI]),\n"
157 " y = d3.scale.pow().exponent(1.3).domain([0, 1]).range([0, r]),\n"
159 " color = d3.scale.category20c(),\n"
160 " duration = 1000;\n"
162 "var vis = d3.select(\"#chart\").append(\"svg:svg\")\n"
163 " .attr(\"width\", w + p * 2)\n"
164 " .attr(\"height\", h + p * 2)\n"
166 " .attr(\"transform\", \"translate(\" + (r + p) + \",\" + (r + p) + \")\");\n"
168 "vis.append(\"p\")\n"
169 " .attr(\"id\", \"intro\")\n"
170 " .text(\"Click to zoom!\");\n"
172 "var partition = d3.layout.partition()\n"
174 " .size([2 * Math.PI, r * r])\n"
175 " .value(function(d) { return d.size; });\n"
177 "var arc = d3.svg.arc()\n"
178 " .startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })\n"
179 " .endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })\n"
180 " .innerRadius(function(d) { return Math.max(0, d.y ? y(d.y) : d.y); })\n"
181 " .outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });\n"
185 <<
"var spaceJSON = " << jsonsize <<
";" << std::endl
189 " var nodes = partition.nodes(spaceJSON);\n"
191 " var path = vis.selectAll(\"path\").data(nodes);\n"
192 " path.enter().append(\"path\")\n"
193 " .attr(\"id\", function(d, i) { return \"path-\" + i; })\n"
194 " .attr(\"d\", arc)\n"
195 " .attr(\"fill-rule\", \"evenodd\")\n"
196 " .style(\"fill\", colour)\n"
197 " .on(\"click\", click);\n"
199 " path.append(\"title\").text(function(d) { return 'class name: ' + d.class_name + "
200 "'\\nmember_name: ' + d.name + '\\n size: ' + sizeMB(d) });\n"
202 " var text = vis.selectAll(\"text\").data(nodes);\n"
203 " var textEnter = text.enter().append(\"text\")\n"
204 " .style(\"opacity\", 1)\n"
205 " .style(\"fill\", function(d) {\n"
206 " return brightness(d3.rgb(colour(d))) < 125 ? \"#eee\" : \"#000\";\n"
208 " .attr(\"text-anchor\", function(d) {\n"
209 " return x(d.x + d.dx / 2) > Math.PI ? \"end\" : \"start\";\n"
211 " .attr(\"dy\", \".2em\")\n"
212 " .attr(\"transform\", function(d) {\n"
213 " var multiline = (d.name || \"\").split(\" \").length > 1,\n"
214 " angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,\n"
215 " rotate = angle + (multiline ? -.5 : 0);\n"
216 " return \"rotate(\" + rotate + \")translate(\" + (y(d.y) + p) + \")rotate(\" + (angle > "
217 "90 ? -180 : 0) + \")\";\n"
219 " .on(\"click\", click);\n"
221 " textEnter.append(\"title\").text(function(d) { return 'class name: ' + d.class_name + "
222 "'\\nmember_name: ' + d.name + '\\n size: ' + sizeMB(d) });\n"
224 " textEnter.append(\"tspan\")\n"
226 " .text(function(d) { return d.dx < 0.05 ? \"\" : d.depth ? d.name.split(\" \")[0] : "
228 " textEnter.append(\"tspan\")\n"
230 " .attr(\"dy\", \"1em\")\n"
231 " .text(function(d) { return d.dx < 0.05 ? \"\" : d.depth ? d.name.split(\" \")[1] || "
234 " function click(d) {\n"
235 " path.transition()\n"
236 " .duration(duration)\n"
237 " .attrTween(\"d\", arcTween(d));\n"
239 " // Somewhat of a hack as we rely on arcTween updating the scales.\n"
241 " .style(\"visibility\", function(e) {\n"
242 " return isParentOf(d, e) ? null : d3.select(this).style(\"visibility\");\n"
244 " .transition().duration(duration)\n"
245 " .attrTween(\"text-anchor\", function(d) {\n"
246 " return function() {\n"
247 " return x(d.x + d.dx / 2) > Math.PI ? \"end\" : \"start\";\n"
250 " .attrTween(\"transform\", function(d) {\n"
251 " var multiline = (d.name || \"\").split(\" \").length > 1;\n"
252 " return function() {\n"
253 " var angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,\n"
254 " rotate = angle + (multiline ? -.5 : 0);\n"
255 " return \"rotate(\" + rotate + \")translate(\" + (y(d.y) + p) + \")rotate(\" + (angle "
256 "> 90 ? -180 : 0) + \")\";\n"
259 " .style(\"opacity\", function(e) { return isParentOf(d, e) ? 1 : 1e-6; })\n"
260 " .each(\"end\", function(e) {\n"
261 " d3.select(this).style(\"visibility\", isParentOf(d, e) ? null : \"hidden\");\n"
266 "function sizeMB(d) {\n"
267 "// if (d.children) {\n"
268 "// var sum = calcSum(d);\n"
269 "// return (sum / (1024*1024)).toFixed(2) + 'MB';\n"
271 " return (d.size / (1024*1024)).toFixed(2) + 'MB';\n"
275 "function calcSum(d) {\n"
276 " if(d.children) {\n"
278 " function recurse(d) {\n"
279 " if(d.children) d.children.forEach( function(child) { recurse(child); } );\n"
280 " else sum += d.size;\n"
283 " console.log(sum);\n"
284 " console.log(d.children);\n"
287 " console.log(d.size);\n"
292 "function isParentOf(p, c) {\n"
293 " if (p === c) return true;\n"
294 " if (p.children) {\n"
295 " return p.children.some(function(d) {\n"
296 " return isParentOf(d, c);\n"
302 "function colour(d) {\n"
303 " return color(d.name);\n"
306 "// Interpolate the scales!\n"
307 "function arcTween(d) {\n"
308 " var my = maxY(d),\n"
309 " xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),\n"
310 " yd = d3.interpolate(y.domain(), [d.y, my]),\n"
311 " yr = d3.interpolate(y.range(), [d.y ? 20 : 0, r]);\n"
312 " return function(d) {\n"
313 " return function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };\n"
317 "// Interpolate the scales!\n"
318 "function arcTween2(d) {\n"
319 " var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),\n"
320 " yd = d3.interpolate(y.domain(), [d.y, 1]),\n"
321 " yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);\n"
322 " return function(d, i) {\n"
324 " ? function(t) { return arc(d); }\n"
325 " : function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };\n"
329 "function maxY(d) {\n"
330 " return d.children ? Math.max.apply(Math, d.children.map(maxY)) : d.y + d.dy;\n"
333 "// http://www.w3.org/WAI/ER/WD-AERT/#color-contrast\n"
334 "function brightness(rgb) {\n"
335 " return rgb.r * .299 + rgb.g * .587 + rgb.b * .114;\n"
337 "d3.select(\"#download\").on(\"click\", function () {\n"
338 "d3.select(this).attr(\"href\", 'data:application/octet-stream;base64,' + "
339 "btoa(d3.select(\"#chart\").html())).attr(\"download\", \"memorysun.svg\")})\n\n"
343 <<
"</script>" << std::endl
344 <<
"</body>" << std::endl
345 <<
"</html>" << std::endl;
346 return jsonbody.str();
354 std::stringstream json_data;
const map_type & children
structure_tree_node(const std::string &n, const std::string &t)
structure_tree_node * add_child(const std::string &n, const std::string &t)
static structure_tree_node * add_child(structure_tree_node *v, const std::string &name, const std::string &type)
static void add_size(structure_tree_node *v, uint64_t value)
Namespace for the succinct data structure library.
void write_structure_tree< HTML_FORMAT >(const structure_tree_node *v, std::ostream &out, SDSL_UNUSED size_t level)
void write_structure_tree(const structure_tree_node *v, std::ostream &out, size_t level=0)
void output_tab(std::ostream &out, size_t level)
std::string create_js_body(const std::string &jsonsize)
void write_structure_tree< JSON_FORMAT >(const structure_tree_node *v, std::ostream &out, size_t level)
std::string create_html_header(const char *file_name)