-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.cpp
More file actions
121 lines (105 loc) · 3.72 KB
/
debug.cpp
File metadata and controls
121 lines (105 loc) · 3.72 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
#include "debug.h"
#include <stdio.h>
#include <string>
#include <fstream>
void dbg::msg (std::string msg)
{
printf("%s\n", msg.c_str());
fflush(stdout);
}
// Trees
void dbg::print_tree(Node *root)
{
if (root->is_leaf()) {
Leaf* leaf = (Leaf*) root;
printf("Leaf %d: %c\n", root->get_frequency(), leaf->get_element());
}
else {
Group* node = (Group*) root;
printf("Node %d: %d %d\n", root->get_frequency(), node->get_left_child()->get_frequency(), node->get_right_child()->get_frequency());
dbg::print_tree(node->get_left_child());
dbg::print_tree(node->get_right_child());
}
}
void dbg::print_tree_map(Node *root, std::map<void*,int> &index_map)
{
if (root->is_leaf()) {
Leaf* leaf = (Leaf*) root;
printf("Leaf %d (%p): %c\n", index_map[root], root, leaf->get_element());
}
else {
Group* node = (Group*) root;
printf("Node %d (%p): %d %d\n", index_map[root], root, index_map[node->get_left_child()], index_map[node->get_right_child()]);
dbg::print_tree_map(node->get_left_child(), index_map);
dbg::print_tree_map(node->get_right_child(), index_map);
}
}
std::string print_graph (Node *root, std::ofstream &output)
{
static int idx = 0;
int curr_idx = idx;
idx++;
if (root->is_leaf()) {
Leaf *leaf = (Leaf*) root;
std::string leaf_id = "\"" + std::to_string(curr_idx)
+ ":" + std::to_string(root->get_frequency())
+ ":" + leaf->get_element() + "\"";
return leaf_id;
}
else {
Group *node = (Group*) root;
std::string node_id = "\"" + std::to_string(curr_idx) + ":" +
std::to_string(root->get_frequency()) + "\"";
output << node_id << " -> " << print_graph(node->get_left_child(), output) << ";\n";
output << node_id << " -> " << print_graph(node->get_right_child(), output) << ";\n";
return node_id;
}
}
void dbg::graph_to_img (Node* root)
{
static int img = 0;
std::ofstream output;
output.open("graph_" + std::to_string(img) + ".gv");
img++;
output << "digraph G {\n";
print_graph(root, output);
output << "}\n";
}
std::string print_graph_with_code (Node *root, std::ofstream &output, std::map<char, std::pair<long long,char>> code)
{
static int idx = 0;
int curr_idx = idx;
idx++;
if (root->is_leaf()) {
Leaf *leaf = (Leaf*) root;
printf("%c: %lld\n", leaf->get_element(), code[leaf->get_element()].first);
std::string leaf_id = "\"";
leaf_id.push_back(leaf->get_element());
leaf_id += ":" + std::to_string((int)code[leaf->get_element()].first) + "\"";
return leaf_id;
}
else {
Group *node = (Group*) root;
std::string node_id = "\"" + std::to_string(curr_idx) + "\"";
output << node_id << " -> " << print_graph_with_code(node->get_left_child(), output, code) << ";\n";
output << node_id << " -> " << print_graph_with_code(node->get_right_child(), output, code) << ";\n";
return node_id;
}
}
void dbg::graph_to_img_with_code (Node* root, std::map<char,std::pair<long long,char>> code)
{
static int img = 0;
std::ofstream output;
output.open("graph_" + std::to_string(img) + ".gv");
img++;
output << "digraph G {\n";
print_graph_with_code(root, output, code);
output << "}\n";
}
// Program specific
void print_encoding (char element, std::map<char,std::pair<long long,char>> &code) {
std::bitset<64> bits (code[element].first);
std::string str = bits.to_string();
std::cout << "Encode " << element << " as ";
std::cout << str.substr(str.size()-code[element].second, str.size()) << std::endl;
}