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++)
33 using map_type = std::unordered_map<std::string, std::unique_ptr<structure_tree_node>>;
48 auto child_itr = m_children.find(hash);
49 if (child_itr == m_children.end())
53 m_children[hash] = std::unique_ptr<structure_tree_node>(new_node);
59 return (*child_itr).second.get();
84template <format_type F>
93 out <<
"{" << std::endl;
95 out <<
"\"class_name\":"
96 <<
"\"" << v->
type <<
"\"," << std::endl;
99 <<
"\"" << v->
name <<
"\"," << std::endl;
102 <<
"\"" << v->
size <<
"\"";
106 out <<
"," << std::endl;
108 out <<
"\"children\":[" << std::endl;
109 size_t written_child_elements = 0;
110 for (
auto const & child : v->
children)
112 if (written_child_elements++ > 0)
114 out <<
"," << std::endl;
120 out <<
"]" << std::endl;
133 std::stringstream jsonheader;
134 jsonheader <<
"<html>\n"
136 <<
" <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n"
137 <<
" <title>" << file_name <<
"</title>\n"
138 <<
" <script src=\"https://github.com/xxsds/sdsl-lite/blob/master/external/d3/d3.min.js\"></script>\n"
139 <<
" <script src=\"https://d3js.org/d3.v2.js\"></script>\n"
140 <<
" <style type=\"text/css\">\n"
141 <<
" path { stroke: #000; stroke-width: 0.8; cursor: pointer; }\n"
142 <<
" text { font: 11px sans-serif; cursor: pointer; }\n"
143 <<
" body { width: 900; margin: 0 auto; }\n"
144 <<
" h1 { text-align: center; margin: .5em 0; }\n"
145 <<
" #breadcrumbs { display: none; }\n"
146 <<
" svg { font: 10px sans-serif; }\n"
149 <<
"<body marginwidth=\"0\" marginheight=\"0\">\n"
150 <<
"<button><a id=\"download\">Save as SVG</a></button>\n"
151 <<
" <div id=\"chart\"></div>" << std::endl;
152 return jsonheader.str();
157 std::stringstream jsonbody;
158 jsonbody <<
"<script type=\"text/javascript\">" << std::endl
163 " x = d3.scale.linear().range([0, 2 * Math.PI]),\n"
164 " y = d3.scale.pow().exponent(1.3).domain([0, 1]).range([0, r]),\n"
166 " color = d3.scale.category20c(),\n"
167 " duration = 1000;\n"
169 "var vis = d3.select(\"#chart\").append(\"svg:svg\")\n"
170 " .attr(\"width\", w + p * 2)\n"
171 " .attr(\"height\", h + p * 2)\n"
173 " .attr(\"transform\", \"translate(\" + (r + p) + \",\" + (r + p) + \")\");\n"
175 "vis.append(\"p\")\n"
176 " .attr(\"id\", \"intro\")\n"
177 " .text(\"Click to zoom!\");\n"
179 "var partition = d3.layout.partition()\n"
181 " .size([2 * Math.PI, r * r])\n"
182 " .value(function(d) { return d.size; });\n"
184 "var arc = d3.svg.arc()\n"
185 " .startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })\n"
186 " .endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })\n"
187 " .innerRadius(function(d) { return Math.max(0, d.y ? y(d.y) : d.y); })\n"
188 " .outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });\n"
192 <<
"var spaceJSON = " << jsonsize <<
";" << std::endl
196 " var nodes = partition.nodes(spaceJSON);\n"
198 " var path = vis.selectAll(\"path\").data(nodes);\n"
199 " path.enter().append(\"path\")\n"
200 " .attr(\"id\", function(d, i) { return \"path-\" + i; })\n"
201 " .attr(\"d\", arc)\n"
202 " .attr(\"fill-rule\", \"evenodd\")\n"
203 " .style(\"fill\", colour)\n"
204 " .on(\"click\", click);\n"
206 " path.append(\"title\").text(function(d) { return 'class name: ' + d.class_name + "
207 "'\\nmember_name: ' + d.name + '\\n size: ' + sizeMB(d) });\n"
209 " var text = vis.selectAll(\"text\").data(nodes);\n"
210 " var textEnter = text.enter().append(\"text\")\n"
211 " .style(\"opacity\", 1)\n"
212 " .style(\"fill\", function(d) {\n"
213 " return brightness(d3.rgb(colour(d))) < 125 ? \"#eee\" : \"#000\";\n"
215 " .attr(\"text-anchor\", function(d) {\n"
216 " return x(d.x + d.dx / 2) > Math.PI ? \"end\" : \"start\";\n"
218 " .attr(\"dy\", \".2em\")\n"
219 " .attr(\"transform\", function(d) {\n"
220 " var multiline = (d.name || \"\").split(\" \").length > 1,\n"
221 " angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,\n"
222 " rotate = angle + (multiline ? -.5 : 0);\n"
223 " return \"rotate(\" + rotate + \")translate(\" + (y(d.y) + p) + \")rotate(\" + (angle > "
224 "90 ? -180 : 0) + \")\";\n"
226 " .on(\"click\", click);\n"
228 " textEnter.append(\"title\").text(function(d) { return 'class name: ' + d.class_name + "
229 "'\\nmember_name: ' + d.name + '\\n size: ' + sizeMB(d) });\n"
231 " textEnter.append(\"tspan\")\n"
233 " .text(function(d) { return d.dx < 0.05 ? \"\" : d.depth ? d.name.split(\" \")[0] : "
235 " textEnter.append(\"tspan\")\n"
237 " .attr(\"dy\", \"1em\")\n"
238 " .text(function(d) { return d.dx < 0.05 ? \"\" : d.depth ? d.name.split(\" \")[1] || "
241 " function click(d) {\n"
242 " path.transition()\n"
243 " .duration(duration)\n"
244 " .attrTween(\"d\", arcTween(d));\n"
246 " // Somewhat of a hack as we rely on arcTween updating the scales.\n"
248 " .style(\"visibility\", function(e) {\n"
249 " return isParentOf(d, e) ? null : d3.select(this).style(\"visibility\");\n"
251 " .transition().duration(duration)\n"
252 " .attrTween(\"text-anchor\", function(d) {\n"
253 " return function() {\n"
254 " return x(d.x + d.dx / 2) > Math.PI ? \"end\" : \"start\";\n"
257 " .attrTween(\"transform\", function(d) {\n"
258 " var multiline = (d.name || \"\").split(\" \").length > 1;\n"
259 " return function() {\n"
260 " var angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,\n"
261 " rotate = angle + (multiline ? -.5 : 0);\n"
262 " return \"rotate(\" + rotate + \")translate(\" + (y(d.y) + p) + \")rotate(\" + (angle "
263 "> 90 ? -180 : 0) + \")\";\n"
266 " .style(\"opacity\", function(e) { return isParentOf(d, e) ? 1 : 1e-6; })\n"
267 " .each(\"end\", function(e) {\n"
268 " d3.select(this).style(\"visibility\", isParentOf(d, e) ? null : \"hidden\");\n"
273 "function sizeMB(d) {\n"
274 "// if (d.children) {\n"
275 "// var sum = calcSum(d);\n"
276 "// return (sum / (1024*1024)).toFixed(2) + 'MB';\n"
278 " return (d.size / (1024*1024)).toFixed(2) + 'MB';\n"
282 "function calcSum(d) {\n"
283 " if(d.children) {\n"
285 " function recurse(d) {\n"
286 " if(d.children) d.children.forEach( function(child) { recurse(child); } );\n"
287 " else sum += d.size;\n"
290 " console.log(sum);\n"
291 " console.log(d.children);\n"
294 " console.log(d.size);\n"
299 "function isParentOf(p, c) {\n"
300 " if (p === c) return true;\n"
301 " if (p.children) {\n"
302 " return p.children.some(function(d) {\n"
303 " return isParentOf(d, c);\n"
309 "function colour(d) {\n"
310 " return color(d.name);\n"
313 "// Interpolate the scales!\n"
314 "function arcTween(d) {\n"
315 " var my = maxY(d),\n"
316 " xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),\n"
317 " yd = d3.interpolate(y.domain(), [d.y, my]),\n"
318 " yr = d3.interpolate(y.range(), [d.y ? 20 : 0, r]);\n"
319 " return function(d) {\n"
320 " return function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };\n"
324 "// Interpolate the scales!\n"
325 "function arcTween2(d) {\n"
326 " var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),\n"
327 " yd = d3.interpolate(y.domain(), [d.y, 1]),\n"
328 " yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);\n"
329 " return function(d, i) {\n"
331 " ? function(t) { return arc(d); }\n"
332 " : function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };\n"
336 "function maxY(d) {\n"
337 " return d.children ? Math.max.apply(Math, d.children.map(maxY)) : d.y + d.dy;\n"
340 "// http://www.w3.org/WAI/ER/WD-AERT/#color-contrast\n"
341 "function brightness(rgb) {\n"
342 " return rgb.r * .299 + rgb.g * .587 + rgb.b * .114;\n"
344 "d3.select(\"#download\").on(\"click\", function () {\n"
345 "d3.select(this).attr(\"href\", 'data:application/octet-stream;base64,' + "
346 "btoa(d3.select(\"#chart\").html())).attr(\"download\", \"memorysun.svg\")})\n\n"
350 <<
"</script>" << std::endl
351 <<
"</body>" << std::endl
352 <<
"</html>" << std::endl;
353 return jsonbody.str();
360 std::stringstream json_data;
structure_tree_node(std::string const &n, std::string const &t)
map_type const & children
structure_tree_node * add_child(std::string const &n, std::string const &t)
static structure_tree_node * add_child(structure_tree_node *v, std::string const &name, std::string const &type)
static void add_size(structure_tree_node *v, uint64_t value)
Namespace for the succinct data structure library.
std::string create_js_body(std::string const &jsonsize)
void write_structure_tree< HTML_FORMAT >(structure_tree_node const *v, std::ostream &out, SDSL_UNUSED size_t level)
void write_structure_tree(structure_tree_node const *v, std::ostream &out, size_t level=0)
std::string create_html_header(char const *file_name)
void write_structure_tree< JSON_FORMAT >(structure_tree_node const *v, std::ostream &out, size_t level)
void output_tab(std::ostream &out, size_t level)