SDSL 3.0.1
Succinct Data Structure Library
structure_tree.hpp
Go to the documentation of this file.
1// Copyright (c) 2016, the SDSL Project Authors. All rights reserved.
2// Please see the AUTHORS file for details. Use of this source code is governed
3// by a BSD license that can be found in the LICENSE file.
8#ifndef INCLUDED_SDSL_STRUCTURE_TREE
9#define INCLUDED_SDSL_STRUCTURE_TREE
10
11#include <iostream>
12#include <memory>
13#include <sstream>
14#include <string>
15#include <unordered_map>
16
17#include <sdsl/config.hpp>
18#include <sdsl/uintx_t.hpp>
19
21namespace sdsl
22{
23
24inline void output_tab(std::ostream & out, size_t level)
25{
26 for (size_t i = 0; i < level; i++) out << "\t";
27}
28
30{
31 private:
32 using map_type = std::unordered_map<std::string, std::unique_ptr<structure_tree_node>>;
33 map_type m_children;
34
35 public:
36 const map_type & children = m_children;
37 size_t size = 0;
38 std::string name;
39 std::string type;
40
41 public:
42 structure_tree_node(const std::string & n, const std::string & t)
43 : name(n)
44 , type(t)
45 {}
46 structure_tree_node * add_child(const std::string & n, const std::string & t)
47 {
48 auto hash = n + t;
49 auto child_itr = m_children.find(hash);
50 if (child_itr == m_children.end())
51 {
52 // add new child as we don't have one of this type yet
53 structure_tree_node * new_node = new structure_tree_node(n, t);
54 m_children[hash] = std::unique_ptr<structure_tree_node>(new_node);
55 return new_node;
56 }
57 else
58 {
59 // child of same type and name exists
60 return (*child_itr).second.get();
61 }
62 }
63 void add_size(size_t s) { size += s; }
64};
65
67{
68 public:
69 static structure_tree_node * add_child(structure_tree_node * v, const std::string & name, const std::string & type)
70 {
71 if (v) return v->add_child(name, type);
72 return nullptr;
73 };
74 static void add_size(structure_tree_node * v, uint64_t value)
75 {
76 if (v) v->add_size(value);
77 };
78};
79
80template <format_type F>
81void write_structure_tree(const structure_tree_node * v, std::ostream & out, size_t level = 0);
82
83template <>
84inline void write_structure_tree<JSON_FORMAT>(const structure_tree_node * v, std::ostream & out, size_t level)
85{
86 if (v)
87 {
88 output_tab(out, level);
89 out << "{" << std::endl;
90 output_tab(out, level + 1);
91 out << "\"class_name\":"
92 << "\"" << v->type << "\"," << std::endl;
93 output_tab(out, level + 1);
94 out << "\"name\":"
95 << "\"" << v->name << "\"," << std::endl;
96 output_tab(out, level + 1);
97 out << "\"size\":"
98 << "\"" << v->size << "\"";
99
100 if (v->children.size())
101 {
102 out << "," << std::endl; // terminate the size tag from before if there are children
103 output_tab(out, level + 1);
104 out << "\"children\":[" << std::endl;
105 size_t written_child_elements = 0;
106 for (const auto & child : v->children)
107 {
108 if (written_child_elements++ > 0) { out << "," << std::endl; }
109 write_structure_tree<JSON_FORMAT>(child.second.get(), out, level + 2);
110 }
111 out << std::endl;
112 output_tab(out, level + 1);
113 out << "]" << std::endl;
114 }
115 else
116 {
117 out << std::endl;
118 }
119 output_tab(out, level);
120 out << "}";
121 }
122}
123
124inline std::string create_html_header(const char * file_name)
125{
126 std::stringstream jsonheader;
127 jsonheader << "<html>\n"
128 << " <head>\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"
140 << " </style>\n"
141 << " </head>\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();
146}
147
148inline std::string create_js_body(const std::string & jsonsize)
149{
150 std::stringstream jsonbody;
151 jsonbody << "<script type=\"text/javascript\">" << std::endl
152 << ""
153 "var w = 800,\n"
154 " h = w,\n"
155 " r = w / 2,\n"
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"
158 " p = 5,\n"
159 " color = d3.scale.category20c(),\n"
160 " duration = 1000;\n"
161 "\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"
165 " .append(\"g\")\n"
166 " .attr(\"transform\", \"translate(\" + (r + p) + \",\" + (r + p) + \")\");\n"
167 "\n"
168 "vis.append(\"p\")\n"
169 " .attr(\"id\", \"intro\")\n"
170 " .text(\"Click to zoom!\");\n"
171 "\n"
172 "var partition = d3.layout.partition()\n"
173 " .sort(null)\n"
174 " .size([2 * Math.PI, r * r])\n"
175 " .value(function(d) { return d.size; });\n"
176 "\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"
182 "\n"
183 " "
184 << std::endl
185 << "var spaceJSON = " << jsonsize << ";" << std::endl
186 << std::endl
187 << "\n"
188 "\n"
189 " var nodes = partition.nodes(spaceJSON);\n"
190 "\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"
198 "\n"
199 " path.append(\"title\").text(function(d) { return 'class name: ' + d.class_name + "
200 "'\\nmember_name: ' + d.name + '\\n size: ' + sizeMB(d) });\n"
201 "\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"
207 " })\n"
208 " .attr(\"text-anchor\", function(d) {\n"
209 " return x(d.x + d.dx / 2) > Math.PI ? \"end\" : \"start\";\n"
210 " })\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"
218 " })\n"
219 " .on(\"click\", click);\n"
220 "\n"
221 " textEnter.append(\"title\").text(function(d) { return 'class name: ' + d.class_name + "
222 "'\\nmember_name: ' + d.name + '\\n size: ' + sizeMB(d) });\n"
223 "\n"
224 " textEnter.append(\"tspan\")\n"
225 " .attr(\"x\", 0)\n"
226 " .text(function(d) { return d.dx < 0.05 ? \"\" : d.depth ? d.name.split(\" \")[0] : "
227 "\"\"; });\n"
228 " textEnter.append(\"tspan\")\n"
229 " .attr(\"x\", 0)\n"
230 " .attr(\"dy\", \"1em\")\n"
231 " .text(function(d) { return d.dx < 0.05 ? \"\" : d.depth ? d.name.split(\" \")[1] || "
232 "\"\" : \"\"; });\n"
233 "\n"
234 " function click(d) {\n"
235 " path.transition()\n"
236 " .duration(duration)\n"
237 " .attrTween(\"d\", arcTween(d));\n"
238 "\n"
239 " // Somewhat of a hack as we rely on arcTween updating the scales.\n"
240 " text\n"
241 " .style(\"visibility\", function(e) {\n"
242 " return isParentOf(d, e) ? null : d3.select(this).style(\"visibility\");\n"
243 " })\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"
248 " };\n"
249 " })\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"
257 " };\n"
258 " })\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"
262 " });\n"
263 " }\n"
264 "\n"
265 "\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"
270 "// } else {\n"
271 " return (d.size / (1024*1024)).toFixed(2) + 'MB';\n"
272 "// }\n"
273 "}\n"
274 "\n"
275 "function calcSum(d) {\n"
276 " if(d.children) {\n"
277 " var sum = 0;\n"
278 " function recurse(d) {\n"
279 " if(d.children) d.children.forEach( function(child) { recurse(child); } );\n"
280 " else sum += d.size;\n"
281 " }\n"
282 " recurse(d,sum);\n"
283 " console.log(sum);\n"
284 " console.log(d.children);\n"
285 " return sum;\n"
286 " } else {\n"
287 " console.log(d.size);\n"
288 " return d.size;\n"
289 " }\n"
290 "}\n"
291 "\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"
297 " });\n"
298 " }\n"
299 " return false;\n"
300 "}\n"
301 "\n"
302 "function colour(d) {\n"
303 " return color(d.name);\n"
304 "}\n"
305 "\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"
314 " };\n"
315 "}\n"
316 "\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"
323 " return 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"
326 " };\n"
327 "}\n"
328 "\n"
329 "function maxY(d) {\n"
330 " return d.children ? Math.max.apply(Math, d.children.map(maxY)) : d.y + d.dy;\n"
331 "}\n"
332 "\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"
336 "}\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"
340 "click(nodes[0]);\n"
341 " "
342 << std::endl
343 << "</script>" << std::endl
344 << "</body>" << std::endl
345 << "</html>" << std::endl;
346 return jsonbody.str();
347}
348
349template <>
351 std::ostream & out,
352 SDSL_UNUSED size_t level)
353{
354 std::stringstream json_data;
356
357 out << create_html_header("sdsl data structure visualization");
358 out << create_js_body(json_data.str());
359}
360
361} // namespace sdsl
362#endif
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)
#define SDSL_UNUSED
Definition: config.hpp:13
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)